diff --git a/schemars_derive/src/schema_exprs.rs b/schemars_derive/src/schema_exprs.rs index 1f55084b..271418ae 100644 --- a/schemars_derive/src/schema_exprs.rs +++ b/schemars_derive/src/schema_exprs.rs @@ -490,15 +490,16 @@ fn expr_for_struct( let (ty, type_def) = type_for_field_schema(field); let maybe_insert_required = match (&default, field.validation_attrs.required()) { - (Some(_), _) => TokenStream::new(), - (None, false) => { + (Ok(_), _) => TokenStream::new(), + (Err(true), _) => TokenStream::new(), + (Err(false), false) => { quote! { if !<#ty as schemars::JsonSchema>::_schemars_private_is_option() { object_validation.required.insert(#name.to_owned()); } } } - (None, true) => quote! { + (Err(false), true) => quote! { object_validation.required.insert(#name.to_owned()); }, }; @@ -506,7 +507,7 @@ fn expr_for_struct( let metadata = SchemaMetadata { read_only: field.serde_attrs.skip_deserializing(), write_only: field.serde_attrs.skip_serializing(), - default, + default: default.ok(), ..field.attrs.as_metadata() }; @@ -574,10 +575,13 @@ fn expr_for_struct( } } -fn field_default_expr(field: &Field, container_has_default: bool) -> Option { +fn field_default_expr(field: &Field, container_has_default: bool) -> Result { let field_default = field.serde_attrs.default(); - if field.serde_attrs.skip_serializing() || (field_default.is_none() && !container_has_default) { - return None; + if field_default.is_none() && !container_has_default { + return Err(false); + } + if field.serde_attrs.skip_serializing() { + return Err(true); } let ty = field.ty; @@ -606,7 +610,7 @@ fn field_default_expr(field: &Field, container_has_default: bool) -> Option quote!(Some(#default_expr)), }; - Some(if let Some(ser_with) = field.serde_attrs.serialize_with() { + Ok(if let Some(ser_with) = field.serde_attrs.serialize_with() { quote! { { struct _SchemarsDefaultSerialize(T);