aspnetcore
aspnetcore copied to clipboard
gRPC JSON transcoding: Support deserializing non-nested enum from string
Fixes https://github.com/dotnet/aspnetcore/issues/45270
Proto messages support enums. These enums can either be nested in a message or specified in the root scope of the proto file.
// root scope
enum Alpha3CountryCode
{
ALPHA_3_COUNTRY_CODE_UNSPECIFIED = 0;
ALPHA_3_COUNTRY_CODE_AFG = 4;
}
// nested
message DataTypes {
enum NestedEnum {
NESTED_ENUM_UNSPECIFIED = 0;
FOO = 1;
BAR = 2;
BAZ = 3;
NEG = -1; // Intentionally negative.
}
}
There is a bug in the current serializer that means root scope enum values can't be deserialized from strings. How the enum descriptor (like reflection but for proto messages) is resolved today only works for nested enum types.
This PR adds a registry for descriptors populated as services are registered. The registry is used to find the descriptor for enum types.
There is no workaround in .NET 7. Should be backported.