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

Simplify serde_as handling around Options #470

merged 2 commits into from Jun 6, 2022

Conversation

jonasbb
Copy link
Owner

@jonasbb jonasbb commented Jun 6, 2022

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. Options 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

…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
Document when `serde_as` will add `#[serde(default)]` on a field.
@jonasbb
Copy link
Owner Author

jonasbb commented Jun 6, 2022

bors r+

@codecov
Copy link

codecov bot commented Jun 6, 2022

Codecov Report

Merging #470 (5d84054) into master (53af356) will decrease coverage by 0.05%.
The diff coverage is 32.53%.

@@            Coverage Diff             @@
##           master     #470      +/-   ##
==========================================
- Coverage   71.34%   71.28%   -0.06%     
==========================================
  Files          52       52              
  Lines        3720     3761      +41     
==========================================
+ Hits         2654     2681      +27     
- Misses       1066     1080      +14     
Impacted Files Coverage Δ
serde_with_macros/src/lib.rs 0.00% <0.00%> (ø)
serde_with/tests/serde_as/serde_as_macro.rs 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 53af356...5d84054. Read the comment docs.

@bors
Copy link
Contributor

bors bot commented Jun 6, 2022

Build succeeded:

@bors bors bot merged commit 5897371 into master Jun 6, 2022
@bors bors bot deleted the issue-185 branch June 6, 2022 13:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Should serde_as apply default when the field is of type Option<_>
1 participant