diff --git a/schemars/tests/default.rs b/schemars/tests/default.rs index 0d68d0e..4623994 100644 --- a/schemars/tests/default.rs +++ b/schemars/tests/default.rs @@ -54,3 +54,14 @@ pub struct NotSerialize; fn schema_default_values() -> TestResult { test_default_generated_schema::("default") } + +#[derive(Default, Deserialize, Serialize, JsonSchema, Debug, PartialEq)] +pub struct StructWithGenericDefaults { + #[serde(default = "Vec::new")] + pub a_vec: Vec, +} + +#[test] +fn schema_with_generic_default_value() -> TestResult { + test_default_generated_schema::("generic_default") +} diff --git a/schemars/tests/expected/generic_default.json b/schemars/tests/expected/generic_default.json new file mode 100644 index 0000000..095b999 --- /dev/null +++ b/schemars/tests/expected/generic_default.json @@ -0,0 +1,14 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "StructWithGenericDefaults", + "type": "object", + "properties": { + "a_vec": { + "default": [], + "type": "array", + "items": { + "type": "string" + } + } + } +} \ No newline at end of file diff --git a/schemars_derive/src/schema_exprs.rs b/schemars_derive/src/schema_exprs.rs index c5b1672..6efa9b8 100644 --- a/schemars_derive/src/schema_exprs.rs +++ b/schemars_derive/src/schema_exprs.rs @@ -581,7 +581,7 @@ fn field_default_expr(field: &Field, container_has_default: bool) -> Option quote!(<#ty>::default()), - SerdeDefault::Path(path) => quote!(#path()), + SerdeDefault::Path(path) => quote!(|| -> #ty { #path() }()), }; let default_expr = match field.serde_attrs.skip_serializing_if() {