-
-
Notifications
You must be signed in to change notification settings - Fork 793
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
Struct with tag
and deny_unknown_fields
cannot deserialize
#2666
Comments
I'm currently working around this using #[derive(Deserialize, Serialize, Debug, Clone, Eq, PartialEq)]
#[serde(tag = "struct_tag")]
#[serde(deny_unknown_fields)]
struct ROFLtagV1 {
name: String,
#[serde(default, skip_serializing)]
struct_tag: serde::de::IgnoredAny,
} It'd be great to have this behaving as expected. |
Thats an interesting idea. Can you link me to a playground link or post a raw rust example you're using? That snippet cannot compile as
I'm unfamiliar with using that type, I tried making a newtype wrapper to see if I could iml a custom |
Ah, I didn't see the |
Hello, thanks for serde!
Context
I want to prevent accidentally deserializing data from one struct into another when they share fields, as the semantics might be completely different. This is a part of my work here heroku/buildpacks-ruby#246 (comment) where I'm trying to handle different versions of a toml file stored on disk that would map to different structs.
Expected
I would expect that when I use
#[serde(tag = "struct_tag", deny_unknown_fields)]
that I can serialize a struct to a string, then deserialize that same string back to the original struct.Actual
This code results in an error
Playground link: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=d8e02a6d877bc57d24d60dcf93a1df11
Result:
It serializes as I would expect, but then it gives an error saying that it doesn't know about the
struct_tag
field.Addendum
I understand that the tag feature is originally for enums so this might be an unexpected use case. If there's a better way to tell serde that it should preserve the struct name (or some other unique key/value combination) in order to be strict about deserializing then please let me know.
Update:
The text was updated successfully, but these errors were encountered: