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
|
@ -3,6 +3,10 @@ use schemars::JsonSchema;
|
|||
use serde::{Deserialize, Serialize};
|
||||
use util::*;
|
||||
|
||||
fn is_default<T: Default + PartialEq>(value: &T) -> bool {
|
||||
value == &T::default()
|
||||
}
|
||||
|
||||
fn ten_and_true() -> MyStruct2 {
|
||||
MyStruct2 {
|
||||
my_int: 10,
|
||||
|
@ -28,9 +32,14 @@ pub struct MyStruct {
|
|||
pub my_bool: bool,
|
||||
#[serde(serialize_with = "custom_serialize")]
|
||||
pub my_struct2: MyStruct2,
|
||||
#[serde(
|
||||
serialize_with = "custom_serialize",
|
||||
skip_serializing_if = "is_default"
|
||||
)]
|
||||
pub my_struct2_default_skipped: MyStruct2,
|
||||
}
|
||||
|
||||
#[derive(Default, Deserialize, Serialize, JsonSchema, Debug)]
|
||||
#[derive(Default, Deserialize, Serialize, JsonSchema, Debug, PartialEq)]
|
||||
#[serde(default = "ten_and_true")]
|
||||
pub struct MyStruct2 {
|
||||
#[serde(default = "six")]
|
||||
|
|
|
@ -19,6 +19,9 @@
|
|||
"$ref": "#/definitions/MyStruct2"
|
||||
}
|
||||
]
|
||||
},
|
||||
"my_struct2_default_skipped": {
|
||||
"$ref": "#/definitions/MyStruct2"
|
||||
}
|
||||
},
|
||||
"definitions": {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue