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

Should serde_as apply default when the field is of type Option<_> #185

Closed
jonasbb opened this issue Oct 2, 2020 · 0 comments · Fixed by #470
Closed

Should serde_as apply default when the field is of type Option<_> #185

jonasbb opened this issue Oct 2, 2020 · 0 comments · Fixed by #470
Labels
enhancement New feature or request

Comments

@jonasbb
Copy link
Owner

jonasbb commented Oct 2, 2020

Using deserialize_with or serde_as on a Option field makes them mandatory while deserializing. In order to make the behavior match more closely with the default serde behavior, serde_as could apply the default attribute on the field. This would make the field optional again while deserializing.

The downside would be that is complicates the macro and would introduce additional behavior.
Right now it only translates the serde_as attribute into serde's deserialize_with and serialize_with.

Originally proposed in #183
Related to #184

@jonasbb jonasbb added the enhancement New feature or request label Nov 2, 2020
jonasbb added a commit that referenced this issue Jun 5, 2022
…store support for missing fields

A `with` or `deserialize_with` on an `Option` field removes the ability to allow missing fields.
This modification detects if the field is `Option` and the transformation is `Option<T>`. In this
case it is likely that the missing field behavior is desired.
A `no_default` can be added to supress this new behavior.

This commit does not include any documentation changes.

Closes #185
bors bot added a commit that referenced this issue Jun 6, 2022
470: Simplify `serde_as` handling around `Option`s r=jonasbb a=jonasbb

Using `#[serde_as]` on a field of type `Option` changes the behavior, in that the field can no longer be missing.
This often times was unexpected and lead to confusion (e.g., #183, #185, #311, #417).

Now if both the field and the transformation type are `Option` the `#[serde(default)]` attribute is added. `Option`s are identified by exact text matches and these variants are supported:
* `Option`
* `std::option::Option`, with or without leading `::`
* `core::option::Option`, with or without leading `::`

If a `default` attribute already exists nothing happens. This new behavior can be suppressed by using `#[serde_as(no_default)]` on the field. If the transformation type is not `Option`, e.g., like `NoneAsEmptyString`, nothing happens either.

Closes #185

Co-authored-by: Jonas Bushart <jonas@bushart.org>
@bors bors bot closed this as completed in e7f2f5e Jun 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant