Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support generic default values #83

Merged
merged 4 commits into from
May 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## [0.8.9] - **in-dev**
### Changed:
- Support generic default values in `default` attributes (https://github.com/GREsau/schemars/pull/83)

## [0.8.8] - 2021-11-25
### Added:
- Implement `JsonSchema` for types from `rust_decimal` and `bigdecimal` crates (https://github.com/GREsau/schemars/pull/101)
Expand Down
11 changes: 11 additions & 0 deletions schemars/tests/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,14 @@ pub struct NotSerialize;
fn schema_default_values() -> TestResult {
test_default_generated_schema::<MyStruct>("default")
}

#[derive(JsonSchema, Debug)]
pub struct StructWithGenericDefaults {
#[serde(default = "Vec::new")]
pub a_vec: Vec<String>,
}

#[test]
fn schema_with_generic_default_value() -> TestResult {
test_default_generated_schema::<StructWithGenericDefaults>("generic_default")
}
14 changes: 14 additions & 0 deletions schemars/tests/expected/generic_default.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
}
2 changes: 1 addition & 1 deletion schemars_derive/src/schema_exprs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ fn field_default_expr(field: &Field, container_has_default: bool) -> Option<Toke
quote!(container_default.#member)
}
SerdeDefault::Default => quote!(<#ty>::default()),
SerdeDefault::Path(path) => quote!(#path()),
SerdeDefault::Path(path) => quote!({ #path() as #ty }),
};

let default_expr = match field.serde_attrs.skip_serializing_if() {
Expand Down