GREsau / schemars

Generate JSON Schema documents from Rust code
https://graham.cool/schemars/
MIT License
791 stars 220 forks source link

`Option` no longer has a default of null as of v0.8.18 #295

Closed stevenengler closed 3 months ago

stevenengler commented 3 months ago

In schemars v0.8.17, an Option with a default of None results in "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.

GREsau commented 3 months ago

Nope this wasn't intentional, thanks for reporting it! It should now be fixed in 0.8.21

stevenengler commented 3 months ago

Thanks for the quick fix!