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"
}
},

View file

@ -294,7 +294,9 @@ fn schema_for_struct(fields: &[Field], cattrs: &attr::Container) -> TokenStream
let ty = field.ty;
let default = match field.attrs.default() {
SerdeDefault::None if set_container_default.is_some() => {
_ if field.attrs.skip_serializing() => None,
SerdeDefault::None if set_container_default.is_none() => None,
SerdeDefault::None => {
let field_ident = field
.original
.ident
@ -302,7 +304,6 @@ fn schema_for_struct(fields: &[Field], cattrs: &attr::Container) -> TokenStream
.expect("This is not a tuple struct, so field should be named");
Some(quote!(cdefault.#field_ident))
}
SerdeDefault::None => None,
SerdeDefault::Default => Some(quote!(<#ty>::default())),
SerdeDefault::Path(path) => Some(quote!(#path())),
}
@ -341,6 +342,7 @@ fn schema_for_struct(fields: &[Field], cattrs: &attr::Container) -> TokenStream
read_only: field.attrs.skip_deserializing(),
write_only: field.attrs.skip_serializing(),
default,
skip_default_if: field.attrs.skip_serializing_if().cloned(),
..get_metadata_from_docs(&field.original.attrs)
};
let schema_expr = set_metadata_on_schema(schema_expr, &metadata);

View file

@ -1,6 +1,6 @@
use crate::doc_attrs;
use proc_macro2::TokenStream;
use syn::Attribute;
use syn::{Attribute, ExprPath};
#[derive(Debug, Clone, Default)]
pub struct SchemaMetadata {
@ -9,6 +9,7 @@ pub struct SchemaMetadata {
pub read_only: bool,
pub write_only: bool,
pub default: Option<TokenStream>,
pub skip_default_if: Option<ExprPath>,
}
pub fn set_metadata_on_schema_from_docs(
@ -34,32 +35,38 @@ pub fn set_metadata_on_schema(schema_expr: TokenStream, metadata: &SchemaMetadat
if let Some(title) = &metadata.title {
setters.push(quote! {
metadata.title = Some(#title.to_owned());
})
});
}
if let Some(description) = &metadata.description {
setters.push(quote! {
metadata.description = Some(#description.to_owned());
})
});
}
if metadata.read_only {
setters.push(quote! {
metadata.read_only = true;
})
});
}
if metadata.write_only {
setters.push(quote! {
metadata.write_only = true;
})
});
}
if let Some(default) = &metadata.default {
setters.push(quote! {
metadata.default = match serde_json::value::to_value(#default) {
Ok(serde_json::value::Value::Null) | Err(_) => None,
Ok(d) => Some(d),
};
})
match (&metadata.default, &metadata.skip_default_if) {
(Some(default), Some(skip_if)) => setters.push(quote! {
{
let default = #default;
if !#skip_if(&default) {
metadata.default = serde_json::value::to_value(default).ok();
}
}
}),
(Some(default), None) => setters.push(quote! {
metadata.default = serde_json::value::to_value(#default).ok();
}),
_ => {}
}
if setters.is_empty() {

View file

@ -18,6 +18,7 @@ static SERDE_KEYWORDS: &[&str] = &[
"alias",
"skip",
"skip_serializing",
"skip_serializing_if",
"skip_deserializing",
"other",
"flatten",