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

Simplify serde_as handling around Options #470

Merged
merged 2 commits into from
Jun 6, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions serde_with/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,23 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

### Changed

* Make `#[serde_as]` behave more intuitive on `Option<T>` fields.

The `#[serde_as]` macro now detects if a `#[serde_as(as = "Option<S>")]` is used on a field of type `Option<T>` and applies `#[serde(default)]` to the field.
This restores the ability to deserialize with missing fields and fixes a common annoyance (#183, #185, #311, #417).
This is a breaking change, since now deserialization will pass where it did not before and this might be undesired.

The `Option` field and transformation are detected by directly matching on the type name.
These variants are detected as `Option`.
* `Option`
* `std::option::Option`, with or without leading `::`
* `core::option::Option`, with or without leading `::`

If an existing `default` attribute is detected, the attribute is not applied again.
This behavior can be supressed by using `#[serde_as(no_default)]` or `#[serde_as(as = "Option<S>", no_default)]`.

## [1.14.0] - 2022-05-29

### Added
Expand Down
116 changes: 116 additions & 0 deletions serde_with/tests/serde_as/serde_as_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,119 @@ fn test_serde_as_macro_multiple_field_attributes() {
}"#]],
);
}

/// Ensure that `serde_as` applies `default` if both the field and the conversion are option.
#[test]
fn test_default_on_option() {
#[serde_as]
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize)]
struct Data {
#[serde_as(as = "Option<DisplayFromStr>")]
a: Option<u32>,
}

is_equal(
Data { a: None },
expect![[r#"
{
"a": null
}"#]],
);
is_equal(
Data { a: Some(123) },
expect![[r#"
{
"a": "123"
}"#]],
);
check_deserialization(Data { a: None }, "{}");

#[serde_as]
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize)]
struct DataNoDefault {
#[serde_as(as = "Option<DisplayFromStr>", no_default)]
a: Option<u32>,
}

is_equal(
DataNoDefault { a: None },
expect![[r#"
{
"a": null
}"#]],
);
is_equal(
DataNoDefault { a: Some(123) },
expect![[r#"
{
"a": "123"
}"#]],
);
check_error_deserialization::<DataNoDefault>(
"{}",
expect!["missing field `a` at line 1 column 2"],
);

fn default_555() -> Option<u32> {
Some(555)
}

#[serde_as]
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize)]
struct DataExplicitDefault {
#[serde_as(as = "Option<DisplayFromStr>")]
#[serde(default = "default_555")]
a: Option<u32>,
}

is_equal(
DataExplicitDefault { a: None },
expect![[r#"
{
"a": null
}"#]],
);
is_equal(
DataExplicitDefault { a: Some(123) },
expect![[r#"
{
"a": "123"
}"#]],
);
check_deserialization(DataExplicitDefault { a: Some(555) }, "{}");

#[serde_as]
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize)]
struct DataString {
#[serde_as(as = "NoneAsEmptyString")]
a: Option<String>,
}

is_equal(
DataString { a: None },
expect![[r#"
{
"a": ""
}"#]],
);
is_equal(
DataString {
a: Some("123".to_string()),
},
expect![[r#"
{
"a": "123"
}"#]],
);
check_deserialization(DataString { a: None }, r#"{"a": ""}"#);
check_deserialization(
DataString {
a: Some("555".to_string()),
},
r#"{"a": "555"}"#,
);
check_error_deserialization::<DataString>(
"{}",
expect!["missing field `a` at line 1 column 2"],
);
}
19 changes: 18 additions & 1 deletion serde_with_macros/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,26 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

### Changed

* Make `#[serde_as]` behave more intuitive on `Option<T>` fields.

The `#[serde_as]` macro now detects if a `#[serde_as(as = "Option<S>")]` is used on a field of type `Option<T>` and applies `#[serde(default)]` to the field.
This restores the ability to deserialize with missing fields and fixes a common annoyance (#183, #185, #311, #417).
This is a breaking change, since now deserialization will pass where it did not before and this might be undesired.

The `Option` field and transformation are detected by directly matching on the type name.
These variants are detected as `Option`.
* `Option`
* `std::option::Option`, with or without leading `::`
* `core::option::Option`, with or without leading `::`

If an existing `default` attribute is detected, the attribute is not applied again.
This behavior can be supressed by using `#[serde_as(no_default)]` or `#[serde_as(as = "Option<S>", no_default)]`.

### Fixed

* Make the documentation clearer by stating that the `#[serde_as]` and `#[skip_serializing_none]` attributes must always be places before `#[derive]`.
* Make the documentation clearer by stating that the `#[serde_as]` and `#[skip_serializing_none]` attributes must always be placed before `#[derive]`.

## [1.5.2] - 2022-04-07

Expand Down