-
Notifications
You must be signed in to change notification settings - Fork 184
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
Fix unknown enum handling #853
base: master
Are you sure you want to change the base?
Conversation
I think we have to add a new value for "unknown" to each enum, then we can also (perhaps?) migrate to using real dart enums. That would give us exhaustiveness checking! See https://b.corp.google.com/issues/287930910 for more context. We can still do this PR first - but we should consider it. |
...Of course it is not trivial to use actual dart enums, because we want to store the integer value behind the "unknown" for re-serialization. I can only see how this could work by having a getter that translates the enum to a dart enum, but that is syntactically breaking. We could also try to think in sealed classes - not sure how well that would work. |
It looks like resetting the top-level fields when the value is an unknown enum was deliberate as we have tests about it, but it doesn't make sense to me as resetting a field means we're not really ignoring the value (i.e. it has an effect on the message). As far as I can see proto3 spec doesn't mention this case, so I'll check other implementations and see what they do about this. Java is skipping the field: https://github.com/protocolbuffers/protobuf/blob/f71a9263f9380836c4fc356b67710203e691bc95/java/util/src/main/java/com/google/protobuf/util/JsonFormat.java#L1491-L1493 |
Now I realize this PR is in terms of proto3 json decoding. Here we (usually) only have the name of the enum value, so we cannot translate it to the index... Resetting vs skipping:
|
If you mean as in
I think ignoring makes sense. Why do you say it's questionable? The flag says "ignore" so it should ignore. I agree, proto3 JSON format is a bit strange.. |
That's what I meant :) .
Well - one thing is what the flag says, another one is what is ergonomic for actual use. If I have one proto represented by
('A' is a known enum value). And I merge it with another proto
If these were messages in the binary protocol, the unknown value would be preserved, and we would be able to see that from the merged proto. I find it somehow surprising that now But again, if that is what other implementations do, I guess we should just follow along. |
proto3 JSON (at least as we implement it in this library, AFAICS this is unspecified) does not support unknown fields, so the behavior you describe is not possible (you can't preserve the unknown value). You have these two options instead: (1) fail (the default) (2) ignore the unknown value (opt-in, with the flag). The default is (1). Binary format has (3) which is store the unknown field in unknown field set. |
Yeah - I argue that we get closer to option 3 by resetting. But that is all splitting hairs. I don't hope anyone is actually using the proto3 json format for anything serious 🙈 |
This fixes handling of unknown enum values in repeated and map fields and in
top-level fields.
Current behavior:
With this PR we just "ignore" the values:
default.
Fixes #849.