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
|
@ -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