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
Unknown fields are denied for tagged newtype variant with unit struct or unit type #2304
Comments
First I must correct myself. The example enum in my previous comment should of course not has the But I have thought a bit more about this. The reason it feels natural to allow unknown fields for internally tagged newtype variants over unit or unit structs is because we are deserializing/parsing from a map structure. The default behaviour if we deserialize into a struct type from a map is indeed to allow unknown fields, now we're still deserializing from a map so I think it makes alot sense to accept unknown fields here as well. Unless the But that makes us think, in what other cases do we deserialize something from a map and want to allow/deny unknown fields? And it turns out that internally tagged unit variants is such a case. However here it seems that unknown fields are allowed even though #[derive(Deserialize, Debug)]
#[serde(tag = "result")]
enum AllowUnknownFields {
Success,
}
#[derive(Deserialize, Debug)]
#[serde(tag = "result", deny_unknown_fields)]
enum DenyUnknownFields {
Success,
} Here both I split this into two features:
I think that 1. is very easy to fix. I have the code in a fork here (on the master branch) and I can make a PR from that. Number 2 is however a bit more contraversal since it might break existing code. I've also implemented this (just for fun) and you can find the code on my fork on the unit_variant_allow_unknown_fields branch. I think that both 1 and 2 together is most consistant, but I'm not sure whether the possible breakage that might be introduced by 2 is acceptable. |
Can you edit the original post to fix that mistake? The code snippet is quite confusing to read, currently |
This issue is unrelated to enums. The fundamental reason is that serde parses JSON objects If you change the code to #[derive(Deserialize, Debug)]
struct ErrorStruct {}
#[derive(Deserialize, Debug)]
#[serde(tag = "result")]
enum Response {
Success(()),
Error(ErrorStruct),
} deserializing from |
The original post is fixed now. |
But then unknown fields should neither be allowed on unit variants. #[derive(Deserialize, Debug)]
#[serde(tag = "result")]
enum Response {
Success,
Error(()),
} Now |
It seems that this is a duplicate of #2294 or at least the reasons the same |
This issue is closely related to #2303 .
Let say we have the following unit struct and internally tagged enum:
Here,
{"result": "Success"}
can be deserialized intoResponse::Success(())
and{"result": "Error"}
intoResponse::Error(ErrorStruct)
. But if we add more unknown fields it doesn't work. We cannot for example deserialize{"result": "Error", "msg": "Something really bad"}
intoResponse::Error(ErrorStruct)
.This should indeed be the behaviour if the
deny_unknown_fields
attribute is present, but I think that extra unknown fields should be allowed otherwise. Or does someone think differently?I would be willing to make a PR if this feature is accepted.
The text was updated successfully, but these errors were encountered: