Fix skip_serializing_if/serialize_with handling
Previously whenever a field with a default value has both `skip_serializing_if` and `with`/`serialize_with` attributes, the value would be converted to a type that performs the custom serialization before checking if it should be serialized. This would cause the wrong type to be given to the skip_serializing_if function, causing a compile error. Issue #26
This commit is contained in:
parent
d1f2c0f803
commit
63af0ceb73
7 changed files with 54 additions and 42 deletions
|
@ -316,7 +316,6 @@ fn schema_for_struct(fields: &[Field], cattrs: Option<&serde_attr::Container>) -
|
|||
read_only: field.attrs.skip_deserializing(),
|
||||
write_only: field.attrs.skip_serializing(),
|
||||
default,
|
||||
skip_default_if: field.attrs.skip_serializing_if().cloned(),
|
||||
..SchemaMetadata::from_doc_attrs(&field.original.attrs)
|
||||
};
|
||||
|
||||
|
@ -367,6 +366,22 @@ fn field_default_expr(field: &Field, container_has_default: bool) -> Option<Toke
|
|||
SerdeDefault::Path(path) => quote!(#path()),
|
||||
};
|
||||
|
||||
let default_expr = match field.attrs.skip_serializing_if() {
|
||||
Some(skip_if) => {
|
||||
quote! {
|
||||
{
|
||||
let default = #default_expr;
|
||||
if #skip_if(&default) {
|
||||
None
|
||||
} else {
|
||||
Some(default)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
None => quote!(Some(#default_expr)),
|
||||
};
|
||||
|
||||
Some(if let Some(ser_with) = field.attrs.serialize_with() {
|
||||
quote! {
|
||||
{
|
||||
|
@ -382,7 +397,7 @@ fn field_default_expr(field: &Field, container_has_default: bool) -> Option<Toke
|
|||
}
|
||||
}
|
||||
|
||||
_SchemarsDefaultSerialize(#default_expr)
|
||||
#default_expr.map(|d| _SchemarsDefaultSerialize(d))
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
use crate::attr;
|
||||
use proc_macro2::{Ident, Span, TokenStream};
|
||||
use quote::{ToTokens, TokenStreamExt};
|
||||
use syn::{Attribute, ExprPath};
|
||||
use syn::Attribute;
|
||||
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct SchemaMetadata {
|
||||
|
@ -10,7 +10,6 @@ pub struct SchemaMetadata {
|
|||
pub read_only: bool,
|
||||
pub write_only: bool,
|
||||
pub default: Option<TokenStream>,
|
||||
pub skip_default_if: Option<ExprPath>,
|
||||
}
|
||||
|
||||
impl ToTokens for SchemaMetadata {
|
||||
|
@ -41,19 +40,10 @@ impl SchemaMetadata {
|
|||
}
|
||||
|
||||
pub fn apply_to_schema(&self, schema_expr: TokenStream) -> TokenStream {
|
||||
let setters = self.make_setters();
|
||||
|
||||
if setters.is_empty() {
|
||||
return schema_expr;
|
||||
}
|
||||
|
||||
quote! {
|
||||
{
|
||||
let mut schema = #schema_expr.into();
|
||||
gen.make_extensible(&mut schema);
|
||||
let mut metadata = schema.metadata();
|
||||
#(#setters)*
|
||||
schemars::schema::Schema::Object(schema)
|
||||
let schema = #schema_expr;
|
||||
gen.apply_metadata(schema, #self)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -83,19 +73,10 @@ impl SchemaMetadata {
|
|||
});
|
||||
}
|
||||
|
||||
match (&self.default, &self.skip_default_if) {
|
||||
(Some(default), Some(skip_if)) => setters.push(quote! {
|
||||
{
|
||||
let default = #default;
|
||||
if !#skip_if(&default) {
|
||||
metadata.default = schemars::_serde_json::value::to_value(default).ok();
|
||||
}
|
||||
}
|
||||
}),
|
||||
(Some(default), None) => setters.push(quote! {
|
||||
metadata.default = schemars::_serde_json::value::to_value(#default).ok();
|
||||
}),
|
||||
_ => {}
|
||||
if let Some(default) = &self.default {
|
||||
setters.push(quote! {
|
||||
metadata.default = #default.and_then(|d| schemars::_serde_json::value::to_value(d).ok());
|
||||
});
|
||||
}
|
||||
|
||||
setters
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue