Skip to content

Commit 3f9a181

Browse files
committed
Fix IOE thrown when 'UseStringProtoEnumValueNames' enabled
1 parent ae1710b commit 3f9a181

5 files changed

Lines changed: 11 additions & 4 deletions

src/Protobuf.System.Text.Json/ProtobufConverter.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,14 @@ public ProtobufConverter(JsonSerializerOptions jsonSerializerOptions, JsonProtob
2929

3030
_fields = messageDescriptor.Fields.InDeclarationOrder().Select(fieldDescriptor =>
3131
{
32+
var enumType = jsonProtobufSerializerOptions.UseStringProtoEnumValueNames && fieldDescriptor.FieldType == FieldType.Enum
33+
? fieldDescriptor.EnumType
34+
: null;
3235
var fieldInfo = new FieldInfo
3336
{
3437
Accessor = fieldDescriptor.Accessor,
3538
IsRepeated = fieldDescriptor.IsRepeated,
36-
EnumType = jsonProtobufSerializerOptions.UseStringProtoEnumValueNames ? fieldDescriptor.EnumType : null,
39+
EnumType = enumType,
3740
IsMap = fieldDescriptor.IsMap,
3841
FieldType = FieldTypeResolver.ResolverFieldType(fieldDescriptor, propertyTypeLookup),
3942
JsonName = convertNameFunc(fieldDescriptor),
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
2-
"enumField": 99
2+
"enumField": 99,
3+
"regularField": 0
34
}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
2-
"enumField": "FIRST_OPTION"
2+
"enumField": "FIRST_OPTION",
3+
"regularField": 0
34
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
2-
"enumField": 1
2+
"enumField": 1,
3+
"regularField": 0
34
}

test/Protobuf.System.Text.Json.Tests/Protos/message_with_enum.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ option csharp_namespace = "System.Text.Json.Protobuf.Tests";
44

55
message MessageWithEnum {
66
TestEnum enum_field = 1;
7+
int32 regular_field = 2;
78
}
89

910
enum TestEnum {

0 commit comments

Comments
 (0)