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

Set "default" on generated schemas #6

Closed
GREsau opened this issue Nov 25, 2019 · 1 comment
Closed

Set "default" on generated schemas #6

GREsau opened this issue Nov 25, 2019 · 1 comment

Comments

@GREsau
Copy link
Owner

GREsau commented Nov 25, 2019

Received via email:

Is it possible to get it to output default values for the generated schema, as provide by Default/SmartDefault?

JSON Schema "default" keyword reference: https://json-schema.org/draft/2019-09/json-schema-validation.html#rfc.section.9.2

Example 1

#[derive(Debug, Default, Deserialize, Serialize, JsonSchema)]
#[serde(default)]
pub struct MyStruct {
    pub my_int: i32,
    pub my_bool: bool,
}

The generated schema for MyStruct should be:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "MyStruct",
  "type": "object",
  "properties": {
    "my_bool": {
      "type": "boolean",
      "default": false
    },
    "my_int": {
      "type": "integer",
      "format": "int32",
      "default": 0
    }
  }
}

Note that we set default on the properties, not on the top-level schema for MyStruct.

Example 2

It gets more complicated when we use #[serde(default)] on fields as well as the struct, e.g.

fn ten_and_true() -> MyStruct2 {
    MyStruct2 {
        my_int: 10,
        my_bool: true,
    }
}

fn six() -> i32 {
    6
}

#[derive(Default, Deserialize, Serialize, JsonSchema, Debug)]
#[serde(default = "ten_and_true")]
pub struct MyStruct2 {
    #[serde(default = "six")]
    pub my_int: i32,
    pub my_bool: bool,
}

The generated schema for MyStruct2 should be:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "MyStruct2",
  "type": "object",
  "properties": {
    "my_bool": {
      "type": "boolean",
      "default": true
    },
    "my_int": {
      "type": "integer",
      "format": "int32",
      "default": 6
    }
  }
}

Example 3

fn ten_and_true() -> MyStruct2 {
    MyStruct2 {
        my_int: 10,
        my_bool: true,
    }
}

fn six() -> i32 {
    6
}

#[derive(Default, Deserialize, Serialize, JsonSchema, Debug)]
#[serde(default)]
pub struct MyStruct {
    pub my_int: i32,
    pub my_bool: bool,
    pub my_struct2: MyStruct2,
}

#[derive(Default, Deserialize, Serialize, JsonSchema, Debug)]
#[serde(default = "ten_and_true")]
pub struct MyStruct2 {
    #[serde(default = "six")]
    pub my_int: i32,
    pub my_bool: bool,
}
{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "MyStruct",
  "type": "object",
  "properties": {
    "my_bool": {
      "type": "boolean",
      "default": false
    },
    "my_int": {
      "type": "integer",
      "format": "int32",
      "default": 0
    },
    "my_struct2": {
      "allOf": [{"$ref": "#/definitions/MyStruct2"}],
      "default": {
        "my_bool": false,
        "my_int": 0
      }
    }
  },
  "definitions": {
    "MyStruct2": {
      "type": "object",
      "properties": {
        "my_bool": {
          "type": "boolean",
          "default": true
        },
        "my_int": {
          "type": "integer",
          "format": "int32",
          "default": 6
        }
      }
    }
  }
}

Note that the default for the my_struct2 property has different property defaults (false/0) than the defaults in the MyStruct2 definition (true/6). This is because:

  • Deserializing {} to MyStruct results in MyStruct { my_int: 0, my_bool: false, my_struct2: MyStruct2 { my_int: 0, my_bool: false } }
  • While deserializing {"my_struct2": {}} results in MyStruct { my_int: 0, my_bool: false, my_struct2: MyStruct2 { my_int: 6, my_bool: true } }
GREsau added a commit that referenced this issue Dec 8, 2019
@GREsau
Copy link
Owner Author

GREsau commented Dec 9, 2019

Implemented in v0.6, now published to crates.io

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

1 participant