Closed stevenengler closed 3 months ago
In schemars v0.8.17, an Option with a default of None results in "default": null.
Option
None
"default": null
use schemars::{schema_for, JsonSchema}; #[derive(JsonSchema, Default)] #[schemars(default = "Self::default")] pub struct MyStruct { pub my_int: Option<i32>, } fn main() { let schema = schema_for!(MyStruct); println!("{}", serde_json::to_string_pretty(&schema).unwrap()); }
{ "$schema": "http://json-schema.org/draft-07/schema#", "title": "MyStruct", "type": "object", "properties": { "my_int": { "default": null, "type": [ "integer", "null" ], "format": "int32" } } }
In schemars v0.8.18 (and the latest v0.8.20), the same Option has no default.
{ "$schema": "http://json-schema.org/draft-07/schema#", "title": "MyStruct", "type": "object", "properties": { "my_int": { "type": [ "integer", "null" ], "format": "int32" } } }
I'm not sure if this was an intentional change, but it seems to me like having "default": null would be more expected, especially given that the example used the #[schemars(default = "Self::default")] attribute.
#[schemars(default = "Self::default")]
Nope this wasn't intentional, thanks for reporting it! It should now be fixed in 0.8.21
Thanks for the quick fix!
In schemars v0.8.17, an
Option
with a default ofNone
results in"default": null
.In schemars v0.8.18 (and the latest v0.8.20), the same
Option
has no default.I'm not sure if this was an intentional change, but it seems to me like having
"default": null
would be more expected, especially given that the example used the#[schemars(default = "Self::default")]
attribute.