Skip to content

gRPC JSON transcoding — Fixed parsing string values into enum when provided string is not valid enum value #52402

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@ public EnumConverter(JsonContext context) : base(context)
{
throw new InvalidOperationException($"Unable to resolve descriptor for {typeToConvert}.");
}
var valueDescriptor = enumDescriptor.FindValueByName(reader.GetString()!);

var value = reader.GetString()!;
var valueDescriptor = enumDescriptor.FindValueByName(value);
if (valueDescriptor == null)
{
throw new InvalidOperationException(@$"Error converting value ""{value}"" to enum type {typeToConvert}.");
}

return ConvertFromInteger(valueDescriptor.Number);
case JsonTokenType.Number:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,31 @@ public void Enum_ReadNumber(int value)
AssertReadJson<HelloRequest.Types.DataTypes>(json);
}

[Theory]
[InlineData("FOO")]
[InlineData("BAR")]
[InlineData("NEG")]
public void Enum_ReadString(string value)
{
var serviceDescriptorRegistry = new DescriptorRegistry();
serviceDescriptorRegistry.RegisterFileDescriptor(JsonTranscodingGreeter.Descriptor.File);

var json = @$"{{ ""singleEnum"": ""{value}"" }}";

AssertReadJson<HelloRequest.Types.DataTypes>(json, descriptorRegistry: serviceDescriptorRegistry);
}

[Fact]
public void Enum_ReadString_NotAllowedValue()
{
var serviceDescriptorRegistry = new DescriptorRegistry();
serviceDescriptorRegistry.RegisterFileDescriptor(JsonTranscodingGreeter.Descriptor.File);

var json = @"{ ""singleEnum"": ""INVALID"" }";

AssertReadJsonError<HelloRequest.Types.DataTypes>(json, ex => Assert.Equal(@"Error converting value ""INVALID"" to enum type Transcoding.HelloRequest+Types+DataTypes+Types+NestedEnum.", ex.Message), descriptorRegistry: serviceDescriptorRegistry, deserializeOld: false);
}

[Fact]
public void Timestamp_Nested()
{
Expand Down Expand Up @@ -539,7 +564,7 @@ public void JsonNamePriority_FieldNameFallback()
return objectNew;
}

private void AssertReadJsonError<TValue>(string value, Action<Exception> assertException, GrpcJsonSettings? settings = null, DescriptorRegistry? descriptorRegistry = null) where TValue : IMessage, new()
private void AssertReadJsonError<TValue>(string value, Action<Exception> assertException, GrpcJsonSettings? settings = null, DescriptorRegistry? descriptorRegistry = null, bool deserializeOld = true) where TValue : IMessage, new()
{
var typeRegistery = TypeRegistry.FromFiles(
HelloRequest.Descriptor.File,
Expand All @@ -552,12 +577,15 @@ public void JsonNamePriority_FieldNameFallback()
var ex = Assert.ThrowsAny<Exception>(() => JsonSerializer.Deserialize<TValue>(value, jsonSerializerOptions));
assertException(ex);

var formatter = new JsonParser(new JsonParser.Settings(
recursionLimit: int.MaxValue,
typeRegistery));
if (deserializeOld)
{
var formatter = new JsonParser(new JsonParser.Settings(
recursionLimit: int.MaxValue,
typeRegistery));

ex = Assert.ThrowsAny<Exception>(() => formatter.Parse<TValue>(value));
assertException(ex);
ex = Assert.ThrowsAny<Exception>(() => formatter.Parse<TValue>(value));
assertException(ex);
}
}

internal static JsonSerializerOptions CreateSerializerOptions(GrpcJsonSettings? settings, TypeRegistry? typeRegistery, DescriptorRegistry descriptorRegistry)
Expand Down