-
Notifications
You must be signed in to change notification settings - Fork 15.6k
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
editions: strange behavior with open enums and unrecognized values in message literals #16757
Comments
lol this one took me a while to figure out. The issue here is that you're specifically using extension 1000 and field 1. As far as protoc is concerned, this corresponds to pb.CppFeatures.legacy_closed_enum, meaning that every enum field in the file will be treated as closed. That solved the mystery of why the option becomes an error when you add the feature. I'm not sure if this is WAI or a bug, I guess we could treat extension 1000 specially and ban it from being used? Why the feature alone parses badly is similar. It treats this as a boolean when it moves the FeatureSet object into the generated pool, and then when it serializes it again it's just 1 (corresponding to ONE). If you change the extension number to anything else this should work as expected This example has a number of weird edge cases that we might want to just outright ban:
|
This will prevent users from accidentally overriding these with different types (e.g. protocolbuffers#16757 and protocolbuffers#16756). PiperOrigin-RevId: 631518986
This will prevent users from accidentally overriding these with different types (e.g. protocolbuffers#16757 and protocolbuffers#16756). PiperOrigin-RevId: 633760581
The text format, which is used in message literals for options with message types in Protobuf source files, allows the use of numeric values for enum fields, instead of enum value names. Furthermore, if the enum is open, it allows the use of unrecognized numeric values.
However, some strange things happen if we try combining this capability with features (all this was done using the most recent release candidate, v27.0-rc1).
First, a base line file. This exhibits expected behavior:
If we examine the descriptor produced from this, it looks right:
But, if we move that enum field into a custom feature, things get a little weird:
The
-321
value has been transformed into the valueONE
(which had numeric value 1).Looking at the actual raw output, using
--decode_raw
, we see that it was in fact serialized with the value 1. So this appears to be a serialization issue, not de-serialization.Things get even weirder if we try to have both an option and a feature:
This won't even compile:
Wha?? This is the same as the first example above, except that the presence of the feature seems to confound it. (The enum is not defined as closed, so this should be allowed, and was allowed in the first two examples.)
If we explicitly set the enum to be closed (via
option features.enum_type = CLOSED
), the error remains the same. As already described in #16756, if we do that but only have the feature present (not the custom option), there is no error (it incorrectly accepts the unrecognized numeric value), but like the second case directly above, the value is serialized as 1 instead of -321.The text was updated successfully, but these errors were encountered: