-
-
Notifications
You must be signed in to change notification settings - Fork 772
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(default = "...")] does not work on flatten
-ed fields.
#1879
Comments
I seem to be running into the same issue with Any workarounds for this? |
Same thing, use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug, PartialEq)]
struct User {
#[serde(flatten, default)]
contact: Contact,
}
#[derive(Serialize, Deserialize, Debug, PartialEq, Default)]
struct Contact {
email: String,
phone: String,
}
fn main() {
let json = r#"{
"email": "[email protected]"
}"#;
let user: User = serde_json::from_str(json).unwrap();
let expected_user = User { contact: Contact { email: "[email protected]".to_string(), phone: "".to_string() } };
assert_eq!(user, expected_user);
} panics with:
|
This is duplicate of #1626 |
…tion The `serde` crate has a bug that prevents the proper deserialization of attributes that are using the `flatten` and `default` directives. See [1] and [2]. This bug affects the freshly introduced `ObjectList.types` attribute. It's not possible to deserialize an `ObjectList` that is missing the `apiVersion` and/or the `kind` values. This commit introduces a custom deserializer for the `types` attribute of `ObjectList`. On top of handling the deserialization case, the custom deserializer provides better default values compared to the ones of the stock `TypeMeta`. The `TypeMeta` struct has empty strings as default values. However, in the context of `ObjectList`, proper default values should be `v1` and `List` instead. [1] serde-rs/serde#1626 [2] serde-rs/serde#1879 Signed-off-by: Flavio Castelli <[email protected]>
* fix: ObjectList handle missing apiVersion and kind during deserialization The `serde` crate has a bug that prevents the proper deserialization of attributes that are using the `flatten` and `default` directives. See [1] and [2]. This bug affects the freshly introduced `ObjectList.types` attribute. It's not possible to deserialize an `ObjectList` that is missing the `apiVersion` and/or the `kind` values. This commit introduces a custom deserializer for the `types` attribute of `ObjectList`. On top of handling the deserialization case, the custom deserializer provides better default values compared to the ones of the stock `TypeMeta`. The `TypeMeta` struct has empty strings as default values. However, in the context of `ObjectList`, proper default values should be `v1` and `List` instead. [1] serde-rs/serde#1626 [2] serde-rs/serde#1879 Signed-off-by: Flavio Castelli <[email protected]> * ObjectList: derive the `Clone` trait Ensure `ObjectList` derive the `Clone` trait. Signed-off-by: Flavio Castelli <[email protected]> * chore: fix rustfmt warning Signed-off-by: Flavio Castelli <[email protected]> --------- Signed-off-by: Flavio Castelli <[email protected]>
I have a struct A that – along with other fields – contains a flattened B struct and a collection of B structs. I would like B's missing fields to take up a different default value if they are in the flattened field.
The problem
No matter what
default
attribute I set on theflatten
-ed field, it will fall back to its type'sdefault
implementation.For flattened structs, it would mean more sense that
default
applies to each field separately, as it's a way more common use case than wanting to fall back to the struct's Default implementation unless all of its fields are missing. But I am not even sure if that's how it's currently behaving.Minimal example
These are my data structures:
And I want to deserialize the following TOML file:
Expected results when deserialized into a
value: A
:Here is the same thing in the Rust Playground (currently panics.)
The text was updated successfully, but these errors were encountered: