We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
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?
The text was updated successfully, but these errors were encountered:
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:
JsonSchema
A
Serialize
#[schemars(bound = "A: Default + JsonSchema + Serialize")] struct SomeStruct<A = Animals> { #[serde(default)] things: A, number: u32, }
Sorry, something went wrong.
awesome thanks!
No branches or pull requests
Hi there,
I would expect that the generated schema includes information as to which enum value is the default here
which generates:
If I change the struct to:
the generated output is:
How can I ensure that the default enum is included in the generated schema?
The text was updated successfully, but these errors were encountered: