Avoid setting ObjectValidation unnecessarily

This commit is contained in:
Graham Esau 2020-05-16 12:10:54 +01:00
parent 1190b1fad9
commit bfb3f06253

View file

@ -362,8 +362,14 @@ fn expr_for_struct(fields: &[Field], cattrs: Option<&serde_attr::Container>) ->
}) })
.collect(); .collect();
let deny_unknown_fields = cattrs let deny_unknown_fields = cattrs.map_or(false, |attrs| attrs.deny_unknown_fields());
.map_or(false, |attrs| attrs.deny_unknown_fields()); let set_additional_properties = if deny_unknown_fields {
quote! {
schema_object.object().additional_properties = Some(Box::new(false.into()));
}
} else {
TokenStream::new()
};
quote! { quote! {
{ {
@ -371,16 +377,9 @@ fn expr_for_struct(fields: &[Field], cattrs: Option<&serde_attr::Container>) ->
#set_container_default #set_container_default
let mut schema_object = schemars::schema::SchemaObject { let mut schema_object = schemars::schema::SchemaObject {
instance_type: Some(schemars::schema::InstanceType::Object.into()), instance_type: Some(schemars::schema::InstanceType::Object.into()),
object: Some(Box::new(schemars::schema::ObjectValidation {
additional_properties: if #deny_unknown_fields {
Some(Box::new(false.into()))
} else {
None
},
..Default::default()
})),
..Default::default() ..Default::default()
}; };
#set_additional_properties
#(#properties)* #(#properties)*
schemars::schema::Schema::Object(schema_object) schemars::schema::Schema::Object(schema_object)
#(#flattens)* #(#flattens)*