Respect serialize_with attr when serializing default

This commit is contained in:
Graham Esau 2019-12-08 20:04:54 +00:00
parent 7e23e2ad7a
commit b748a90c3f
6 changed files with 74 additions and 27 deletions

View file

@ -14,11 +14,19 @@ fn six() -> i32 {
6 6
} }
fn custom_serialize<S>(value: &MyStruct2, ser: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
ser.collect_str(&format_args!("i:{} b:{}", value.my_int, value.my_bool))
}
#[derive(Default, Deserialize, Serialize, JsonSchema, Debug)] #[derive(Default, Deserialize, Serialize, JsonSchema, Debug)]
#[serde(default)] #[serde(default)]
pub struct MyStruct { pub struct MyStruct {
pub my_int: i32, pub my_int: i32,
pub my_bool: bool, pub my_bool: bool,
#[serde(serialize_with = "custom_serialize")]
pub my_struct2: MyStruct2, pub my_struct2: MyStruct2,
} }

View file

@ -4,24 +4,21 @@
"type": "object", "type": "object",
"properties": { "properties": {
"my_bool": { "my_bool": {
"type": "boolean", "default": false,
"default": false "type": "boolean"
}, },
"my_int": { "my_int": {
"default": 0,
"type": "integer", "type": "integer",
"format": "int32", "format": "int32"
"default": 0
}, },
"my_struct2": { "my_struct2": {
"default": "i:0 b:false",
"allOf": [ "allOf": [
{ {
"$ref": "#/definitions/MyStruct2" "$ref": "#/definitions/MyStruct2"
} }
], ]
"default": {
"my_bool": false,
"my_int": 0
}
} }
}, },
"definitions": { "definitions": {
@ -29,13 +26,13 @@
"type": "object", "type": "object",
"properties": { "properties": {
"my_bool": { "my_bool": {
"type": "boolean", "default": true,
"default": true "type": "boolean"
}, },
"my_int": { "my_int": {
"default": 6,
"type": "integer", "type": "integer",
"format": "int32", "format": "int32"
"default": 6
} }
} }
} }

View file

@ -4,8 +4,6 @@
"type": "object", "type": "object",
"required": [ "required": [
"command_line", "command_line",
"system_cpu_time",
"user_cpu_time",
"wall_time" "wall_time"
], ],
"properties": { "properties": {
@ -13,10 +11,26 @@
"type": "string" "type": "string"
}, },
"system_cpu_time": { "system_cpu_time": {
"$ref": "#/definitions/DurationDef" "default": {
"nanos": 0,
"secs": 0
},
"allOf": [
{
"$ref": "#/definitions/DurationDef"
}
]
}, },
"user_cpu_time": { "user_cpu_time": {
"$ref": "#/definitions/DurationDef" "default": {
"nanos": 0,
"secs": 0
},
"allOf": [
{
"$ref": "#/definitions/DurationDef"
}
]
}, },
"wall_time": { "wall_time": {
"$ref": "#/definitions/DurationDef" "$ref": "#/definitions/DurationDef"

View file

@ -2,31 +2,40 @@ mod util;
use other_crate::Duration; use other_crate::Duration;
use schemars::JsonSchema; use schemars::JsonSchema;
use serde::Serialize;
use util::*; use util::*;
mod other_crate { mod other_crate {
#[derive(Debug)] #[derive(Debug, Default)]
pub struct Duration { pub struct Duration {
pub secs: i64, pub secs: i64,
pub nanos: i32, pub nanos: i32,
} }
} }
#[derive(Debug, JsonSchema)] #[derive(Debug, JsonSchema, Serialize)]
#[serde(remote = "Duration")] #[serde(remote = "Duration")]
struct DurationDef { struct DurationDef {
secs: i64, secs: i64,
nanos: i32, nanos: i32,
} }
#[derive(Debug, JsonSchema)] fn custom_serialize<S>(value: &Duration, ser: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
ser.collect_str(&format_args!("{}.{:09}s", value.secs, value.nanos))
}
#[derive(Debug, JsonSchema, Serialize)]
struct Process { struct Process {
command_line: String, command_line: String,
#[serde(with = "DurationDef")] #[serde(with = "DurationDef")]
wall_time: Duration, wall_time: Duration,
#[serde(with = "DurationDef")] #[serde(default, with = "DurationDef")]
user_cpu_time: Duration, user_cpu_time: Duration,
#[serde(deserialize_with = "some_serialize_function")] // FIXME this should serialize the default as "0.000000000s"
#[serde(default, serialize_with = "custom_serialize")]
#[schemars(with = "DurationDef")] #[schemars(with = "DurationDef")]
system_cpu_time: Duration, system_cpu_time: Duration,
} }

View file

@ -292,7 +292,6 @@ fn schema_for_struct(fields: &[Field], cattrs: &attr::Container) -> TokenStream
let name = field.attrs.name().deserialize_name(); let name = field.attrs.name().deserialize_name();
let ty = field.ty; let ty = field.ty;
// TODO respect serialize_with on field
let default = match field.attrs.default() { let default = match field.attrs.default() {
SerdeDefault::None if set_container_default.is_some() => { SerdeDefault::None if set_container_default.is_some() => {
let field_ident = field let field_ident = field
@ -305,7 +304,27 @@ fn schema_for_struct(fields: &[Field], cattrs: &attr::Container) -> TokenStream
SerdeDefault::None => None, SerdeDefault::None => None,
SerdeDefault::Default => Some(quote!(<#ty>::default())), SerdeDefault::Default => Some(quote!(<#ty>::default())),
SerdeDefault::Path(path) => Some(quote!(#path())), SerdeDefault::Path(path) => Some(quote!(#path())),
}; }
.map(|d| match field.attrs.serialize_with() {
Some(ser_with) => quote! {
{
struct _SchemarsDefaultSerialize<T>(T);
impl serde::Serialize for _SchemarsDefaultSerialize<#ty>
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer
{
#ser_with(&self.0, serializer)
}
}
_SchemarsDefaultSerialize(#d)
}
},
None => d,
});
if default.is_none() { if default.is_none() {
required.push(name.clone()); required.push(name.clone());

View file

@ -55,9 +55,9 @@ pub fn set_metadata_on_schema(schema_expr: TokenStream, metadata: &SchemaMetadat
if let Some(default) = &metadata.default { if let Some(default) = &metadata.default {
setters.push(quote! { setters.push(quote! {
metadata.default = match serde_json::json!(#default) { metadata.default = match serde_json::value::to_value(#default) {
serde_json::value::Value::Null => None, Ok(serde_json::value::Value::Null) | Err(_) => None,
d => Some(d), Ok(d) => Some(d),
}; };
}) })
} }