Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

default member missing in schema for generic type that implements Default #249

Closed
seem-less opened this issue Sep 18, 2023 · 2 comments
Closed

Comments

@seem-less
Copy link

seem-less commented Sep 18, 2023

Hi there,

I would expect that the generated schema includes information as to which enum value is the default here

#[derive(Debug, Serialize, Deserialize, JsonSchema)]
#[serde(
    bound(
        serialize = "A: Default + Serialize",
        deserialize = "A: Default + Deserialize<'de>"
    )
)]
#[schemars(bound = "A: Default + JsonSchema")]
struct SomeStruct<A = Animals> {
	#[serde(default)]
    things: A,
    number: u32,
}

#[derive(Default, Debug, Serialize, Deserialize, JsonSchema)]
enum Animals {
    #[default]
    Cow,
    Tiger,
    Giraffe,
}

#[derive(Default, Debug, Serialize, Deserialize, JsonSchema)]
enum Cars {
    #[default]
    Honda,
    Toyota,
    Mitsubishi,
}

fn main() {
    let schema = schema_for!(SomeStruct);
    println!("{}", serde_json::to_string_pretty(&schema).unwrap());
}

which generates:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "SomeStruct_for_Animals",
  "type": "object",
  "required": [
    "number"
  ],
  "properties": {
    "number": {
      "type": "integer",
      "format": "uint32",
      "minimum": 0.0
    },
    "things": {
      "$ref": "#/definitions/Animals"
    }
  },
  "definitions": {
    "Animals": {
      "type": "string",
      "enum": [
        "Cow",
        "Tiger",
        "Giraffe"
      ]
    }
  }
}

If I change the struct to:

#[derive(Debug, Serialize, Deserialize, JsonSchema)]
struct SomeStruct {
    #[serde(default)]
    things: Animals,
    number: u32,
}

the generated output is:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "SomeStruct",
  "type": "object",
  "required": [
    "number"
  ],
  "properties": {
    "number": {
      "type": "integer",
      "format": "uint32",
      "minimum": 0.0
    },
    "things": {
      "default": "Cow",
      "allOf": [
        {
          "$ref": "#/definitions/Animals"
        }
      ]
    }
  },
  "definitions": {
    "Animals": {
      "type": "string",
      "enum": [
        "Cow",
        "Tiger",
        "Giraffe"
      ]
    }
  }
}

How can I ensure that the default enum is included in the generated schema?

@GREsau
Copy link
Owner

GREsau commented Nov 12, 2023

This is because the JsonSchema derive doesn't know whether A implements Serialize - you can fix it by adding an extra Serialize bound like this:

#[schemars(bound = "A: Default + JsonSchema + Serialize")]
struct SomeStruct<A = Animals> {
    #[serde(default)]
    things: A,
    number: u32,
}

@seem-less
Copy link
Author

awesome thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants