Do not serialize schema default if it would be skipped by skip_serializing_if attribute

This commit is contained in:
Graham Esau 2019-12-09 12:34:28 +00:00
parent 8301a38b8f
commit 26c4099bbe
6 changed files with 42 additions and 42 deletions

View file

@ -103,7 +103,11 @@ pub struct SchemaObject {
/// The `const` keyword.
///
/// See [JSON Schema Validation 6.1.3. "const"](https://tools.ietf.org/html/draft-handrews-json-schema-validation-02#section-6.1.3)
#[serde(rename = "const", skip_serializing_if = "Option::is_none")]
#[serde(
rename = "const",
skip_serializing_if = "Option::is_none",
deserialize_with = "allow_null"
)]
pub const_value: Option<Value>,
/// Properties of the [`SchemaObject`] which define validation assertions in terms of other schemas.
#[serde(flatten, deserialize_with = "skip_if_default")]
@ -130,6 +134,15 @@ pub struct SchemaObject {
pub extensions: Map<String, Value>,
}
// Deserializing "null" to `Option<Value>` directly results in `None`,
// this function instead makes it deserialize to `Some(Value::Null)`.
fn allow_null<'de, D>(de: D) -> Result<Option<Value>, D::Error>
where
D: serde::Deserializer<'de>,
{
Value::deserialize(de).map(Option::Some)
}
fn skip_if_default<'de, D, T>(deserializer: D) -> Result<Option<Box<T>>, D::Error>
where
D: serde::Deserializer<'de>,
@ -226,7 +239,10 @@ pub struct Metadata {
/// The `default` keyword.
///
/// See [JSON Schema Validation 9.2. "default"](https://tools.ietf.org/html/draft-handrews-json-schema-validation-02#section-9.2).
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(
skip_serializing_if = "Option::is_none",
deserialize_with = "allow_null"
)]
pub default: Option<Value>,
/// The `deprecated` keyword.
///

View file

@ -72,7 +72,6 @@
},
"definitions": {
"description": "The `$defs` keyword.\n\nThis is currently serialized as `definitions` for backwards compatibility.\n\nSee [JSON Schema 8.2.5. Schema Re-Use With \"$defs\"](https://tools.ietf.org/html/draft-handrews-json-schema-02#section-8.2.5).",
"default": {},
"type": "object",
"additionalProperties": {
"$ref": "#/components/schemas/Schema"
@ -80,7 +79,6 @@
},
"deprecated": {
"description": "The `deprecated` keyword.\n\nSee [JSON Schema Validation 9.3. \"deprecated\"](https://tools.ietf.org/html/draft-handrews-json-schema-validation-02#section-9.3).",
"default": false,
"type": "boolean"
},
"description": {
@ -222,7 +220,6 @@
},
"patternProperties": {
"description": "The `patternProperties` keyword.\n\nSee [JSON Schema 9.3.2.2. \"patternProperties\"](https://tools.ietf.org/html/draft-handrews-json-schema-02#section-9.3.2.2).",
"default": {},
"type": "object",
"additionalProperties": {
"$ref": "#/components/schemas/Schema"
@ -230,7 +227,6 @@
},
"properties": {
"description": "The `properties` keyword.\n\nSee [JSON Schema 9.3.2.1. \"properties\"](https://tools.ietf.org/html/draft-handrews-json-schema-02#section-9.3.2.1).",
"default": {},
"type": "object",
"additionalProperties": {
"$ref": "#/components/schemas/Schema"
@ -247,12 +243,10 @@
},
"readOnly": {
"description": "The `readOnly` keyword.\n\nSee [JSON Schema Validation 9.4. \"readOnly\" and \"writeOnly\"](https://tools.ietf.org/html/draft-handrews-json-schema-validation-02#section-9.4).",
"default": false,
"type": "boolean"
},
"required": {
"description": "The `required` keyword.\n\nSee [JSON Schema Validation 6.5.3. \"required\"](https://tools.ietf.org/html/draft-handrews-json-schema-validation-02#section-6.5.3).",
"default": [],
"type": "array",
"items": {
"type": "string"
@ -288,7 +282,6 @@
},
"writeOnly": {
"description": "The `writeOnly` keyword.\n\nSee [JSON Schema Validation 9.4. \"readOnly\" and \"writeOnly\"](https://tools.ietf.org/html/draft-handrews-json-schema-validation-02#section-9.4).",
"default": false,
"type": "boolean"
}
},
@ -390,7 +383,6 @@
},
"deprecated": {
"description": "The `deprecated` keyword.\n\nSee [JSON Schema Validation 9.3. \"deprecated\"](https://tools.ietf.org/html/draft-handrews-json-schema-validation-02#section-9.3).",
"default": false,
"type": "boolean"
},
"description": {
@ -532,7 +524,6 @@
},
"patternProperties": {
"description": "The `patternProperties` keyword.\n\nSee [JSON Schema 9.3.2.2. \"patternProperties\"](https://tools.ietf.org/html/draft-handrews-json-schema-02#section-9.3.2.2).",
"default": {},
"type": "object",
"additionalProperties": {
"$ref": "#/components/schemas/Schema"
@ -540,7 +531,6 @@
},
"properties": {
"description": "The `properties` keyword.\n\nSee [JSON Schema 9.3.2.1. \"properties\"](https://tools.ietf.org/html/draft-handrews-json-schema-02#section-9.3.2.1).",
"default": {},
"type": "object",
"additionalProperties": {
"$ref": "#/components/schemas/Schema"
@ -557,12 +547,10 @@
},
"readOnly": {
"description": "The `readOnly` keyword.\n\nSee [JSON Schema Validation 9.4. \"readOnly\" and \"writeOnly\"](https://tools.ietf.org/html/draft-handrews-json-schema-validation-02#section-9.4).",
"default": false,
"type": "boolean"
},
"required": {
"description": "The `required` keyword.\n\nSee [JSON Schema Validation 6.5.3. \"required\"](https://tools.ietf.org/html/draft-handrews-json-schema-validation-02#section-6.5.3).",
"default": [],
"type": "array",
"items": {
"type": "string"
@ -598,7 +586,6 @@
},
"writeOnly": {
"description": "The `writeOnly` keyword.\n\nSee [JSON Schema Validation 9.4. \"readOnly\" and \"writeOnly\"](https://tools.ietf.org/html/draft-handrews-json-schema-validation-02#section-9.4).",
"default": false,
"type": "boolean"
}
},

View file

@ -86,7 +86,6 @@
},
"definitions": {
"description": "The `$defs` keyword.\n\nThis is currently serialized as `definitions` for backwards compatibility.\n\nSee [JSON Schema 8.2.5. Schema Re-Use With \"$defs\"](https://tools.ietf.org/html/draft-handrews-json-schema-02#section-8.2.5).",
"default": {},
"type": "object",
"additionalProperties": {
"$ref": "#/definitions/Schema"
@ -94,7 +93,6 @@
},
"deprecated": {
"description": "The `deprecated` keyword.\n\nSee [JSON Schema Validation 9.3. \"deprecated\"](https://tools.ietf.org/html/draft-handrews-json-schema-validation-02#section-9.3).",
"default": false,
"type": "boolean"
},
"description": {
@ -276,7 +274,6 @@
},
"patternProperties": {
"description": "The `patternProperties` keyword.\n\nSee [JSON Schema 9.3.2.2. \"patternProperties\"](https://tools.ietf.org/html/draft-handrews-json-schema-02#section-9.3.2.2).",
"default": {},
"type": "object",
"additionalProperties": {
"$ref": "#/definitions/Schema"
@ -284,7 +281,6 @@
},
"properties": {
"description": "The `properties` keyword.\n\nSee [JSON Schema 9.3.2.1. \"properties\"](https://tools.ietf.org/html/draft-handrews-json-schema-02#section-9.3.2.1).",
"default": {},
"type": "object",
"additionalProperties": {
"$ref": "#/definitions/Schema"
@ -303,12 +299,10 @@
},
"readOnly": {
"description": "The `readOnly` keyword.\n\nSee [JSON Schema Validation 9.4. \"readOnly\" and \"writeOnly\"](https://tools.ietf.org/html/draft-handrews-json-schema-validation-02#section-9.4).",
"default": false,
"type": "boolean"
},
"required": {
"description": "The `required` keyword.\n\nSee [JSON Schema Validation 6.5.3. \"required\"](https://tools.ietf.org/html/draft-handrews-json-schema-validation-02#section-6.5.3).",
"default": [],
"type": "array",
"items": {
"type": "string"
@ -352,7 +346,6 @@
},
"writeOnly": {
"description": "The `writeOnly` keyword.\n\nSee [JSON Schema Validation 9.4. \"readOnly\" and \"writeOnly\"](https://tools.ietf.org/html/draft-handrews-json-schema-validation-02#section-9.4).",
"default": false,
"type": "boolean"
}
},
@ -466,7 +459,6 @@
},
"deprecated": {
"description": "The `deprecated` keyword.\n\nSee [JSON Schema Validation 9.3. \"deprecated\"](https://tools.ietf.org/html/draft-handrews-json-schema-validation-02#section-9.3).",
"default": false,
"type": "boolean"
},
"description": {
@ -648,7 +640,6 @@
},
"patternProperties": {
"description": "The `patternProperties` keyword.\n\nSee [JSON Schema 9.3.2.2. \"patternProperties\"](https://tools.ietf.org/html/draft-handrews-json-schema-02#section-9.3.2.2).",
"default": {},
"type": "object",
"additionalProperties": {
"$ref": "#/definitions/Schema"
@ -656,7 +647,6 @@
},
"properties": {
"description": "The `properties` keyword.\n\nSee [JSON Schema 9.3.2.1. \"properties\"](https://tools.ietf.org/html/draft-handrews-json-schema-02#section-9.3.2.1).",
"default": {},
"type": "object",
"additionalProperties": {
"$ref": "#/definitions/Schema"
@ -675,12 +665,10 @@
},
"readOnly": {
"description": "The `readOnly` keyword.\n\nSee [JSON Schema Validation 9.4. \"readOnly\" and \"writeOnly\"](https://tools.ietf.org/html/draft-handrews-json-schema-validation-02#section-9.4).",
"default": false,
"type": "boolean"
},
"required": {
"description": "The `required` keyword.\n\nSee [JSON Schema Validation 6.5.3. \"required\"](https://tools.ietf.org/html/draft-handrews-json-schema-validation-02#section-6.5.3).",
"default": [],
"type": "array",
"items": {
"type": "string"
@ -724,7 +712,6 @@
},
"writeOnly": {
"description": "The `writeOnly` keyword.\n\nSee [JSON Schema Validation 9.4. \"readOnly\" and \"writeOnly\"](https://tools.ietf.org/html/draft-handrews-json-schema-validation-02#section-9.4).",
"default": false,
"type": "boolean"
}
},