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

internally tagged newtype variant containing unit struct fails to deserialize when extra keys exist #2755

Open
PookieBuns opened this issue Jun 11, 2024 · 0 comments

Comments

@PookieBuns
Copy link

Thanks to #1085, internally tagged unit structs are able to be deserialized properly.

However, this only works if the only key in the input is the tag name. If any other key exists, the serialization will fail. This issue does not occur when the newtype variant is an empty struct.

Code to reproduce

use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug)]
struct MyStruct1 {}

#[derive(Serialize, Deserialize, Debug)]
struct MyStruct2;

#[derive(Serialize, Deserialize, Debug)]
#[serde(tag = "variant")]
enum MyEnum {
    A(MyStruct1),
    B(MyStruct2),
}

fn main() {
    let only_tag_json = r#"{"variant":"A"}"#;
    let this_works: MyEnum = serde_json::from_str(only_tag_json).unwrap();
    println!("{this_works:#?}");
    let with_extra_keys_json_a = r#"{"variant":"A", "k":"v"}"#;
    let this_also_works: MyEnum = serde_json::from_str(with_extra_keys_json_a).unwrap();
    println!("{this_also_works:#?}");
    let with_extra_keys_json_b = r#"{"variant":"B", "k":"v"}"#;
    let this_doesnt_work: MyEnum = serde_json::from_str(with_extra_keys_json_b).unwrap();
    println!("{this_doesnt_work:#?}");
}

Ideally, the unit struct should also be able to ignore the extra key when serializing and succeed, since all information is known to serialize properly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant