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

serde_with fails on options that are missing #417

Closed
Pzixel opened this issue Mar 6, 2022 · 1 comment
Closed

serde_with fails on options that are missing #417

Pzixel opened this issue Mar 6, 2022 · 1 comment

Comments

@Pzixel
Copy link

Pzixel commented Mar 6, 2022

#[serde_as]
#[derive(Debug, Deserialize)]
pub struct Foo {
    #[serde_as(deserialize_as = "Option<serde_with::Same>")]
    pub a: Option<u32>,
}

let x = serde_json::from_str("{}");

Expected result: deserialized successfully with a: None
Actual result: called Result::unwrap() on an Err value: Error("missing field a")

@Pzixel
Copy link
Author

Pzixel commented Mar 6, 2022

Closed as duplicate #185

@Pzixel Pzixel closed this as completed Mar 6, 2022
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>
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

No branches or pull requests

1 participant