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(deny_unknown_fields)]
does not work as expected on unit variants of tagged enum
#2294
Comments
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. |
Skgland
changed the title
'#![serde(deny_unknown_fields)]' does not work as expected with tagged enum
Jan 28, 2023
#![serde(deny_unknown_fields)]
does not work as expected with tagged enum
Skgland
changed the title
Jan 28, 2023
#![serde(deny_unknown_fields)]
does not work as expected with tagged enum#![serde(deny_unknown_fields)]
does not work as expected on unit variants of tagged enum
gnoliyil
pushed a commit
to gnoliyil/fuchsia
that referenced
this issue
Jan 27, 2024
serde doesn't correctly enforce deny_unknown_fields when deserializing a bare enum variant: serde-rs/serde#2294. Icfa57c7b2d9a9c02f7586f7344d550076bb534bd makes these struct variants which causes the driver-manager config attribute we don't yet support to fail to build. It might take time for that CL to land, so we workaround the bug with this change in the interim. Bug: 99074 Change-Id: Ia7487aa5b68e6a40540b3000f17a7c3a1710c3c3 Reviewed-on: https://fuchsia-review.googlesource.com/c/fuchsia/+/794100 Fuchsia-Auto-Submit: Adam Perry <adamperry@google.com> Reviewed-by: Sarah Chan <spqchan@google.com> Commit-Queue: Auto-Submit <auto-submit@fuchsia-infra.iam.gserviceaccount.com>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 #2294 (comment)
play-ground
The text was updated successfully, but these errors were encountered: