Open Skgland opened 1 year ago
Smaller repro:
#[derive(serde::Deserialize)]
#[serde(tag = "type", deny_unknown_fields)]
enum Enum {
A,
B,
}
fn main() {
serde_json::from_str::<Enum>(r#"{"type": "A", "token": "testToken"}"#).unwrap();
}
I'm building a JSON Typedef derive lib and stumbled into this. Any updates? I should treat this as a bug and not expected behavior, right?
I hit this as well when changing a variant from no payload to an empty struct payload (for reasons), here's an interesting repro:
#[derive(Debug, serde::Deserialize)]
#[serde(tag = "type", deny_unknown_fields)]
enum EnumBareVariant {
A,
}
#[derive(Debug, serde::Deserialize)]
#[serde(tag = "type", deny_unknown_fields)]
enum EnumEmptyStructVariant {
A {},
}
fn main() {
let with_unknown_field = r#"{"type": "A", "token": "testToken"}"#;
serde_json::from_str::<EnumEmptyStructVariant>(with_unknown_field).expect_err("this passes");
serde_json::from_str::<EnumBareVariant>(with_unknown_field).expect_err("this fails");
}
It seems that this gap is specific to having a bare enum variant.
I would expect deny_unknown_fields to work with unit variants of tagged enums such that in the following example, both test pass.
Instead the test using the
NewEnumUsingUnit
enum fails as thetoken
field is ignored unexpectedly.Edit: Updated Example after https://github.com/serde-rs/serde/issues/2294#issuecomment-1407182878
play-ground