From 47680847fe2937287b1483cf5cb3e6a319951220 Mon Sep 17 00:00:00 2001 From: Havret Date: Tue, 4 Jul 2023 07:54:23 +0200 Subject: [PATCH] Fix IOE thrown when 'UseStringProtoEnumValueNames' enabled --- src/Protobuf.System.Text.Json/ProtobufConverter.cs | 5 ++++- ...when_using_proto_value_name_is_not_possible.approved.json | 3 ++- ...lize_enum_value_using_proto_enum_value_name.approved.json | 3 ++- ...ts.Should_serialize_message_with_enum_field.approved.json | 3 ++- .../Protos/message_with_enum.proto | 1 + 5 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/Protobuf.System.Text.Json/ProtobufConverter.cs b/src/Protobuf.System.Text.Json/ProtobufConverter.cs index cb6ce2f..15d9a9d 100644 --- a/src/Protobuf.System.Text.Json/ProtobufConverter.cs +++ b/src/Protobuf.System.Text.Json/ProtobufConverter.cs @@ -29,11 +29,14 @@ public ProtobufConverter(JsonSerializerOptions jsonSerializerOptions, JsonProtob _fields = messageDescriptor.Fields.InDeclarationOrder().Select(fieldDescriptor => { + var enumType = jsonProtobufSerializerOptions.UseStringProtoEnumValueNames && fieldDescriptor.FieldType == FieldType.Enum + ? fieldDescriptor.EnumType + : null; var fieldInfo = new FieldInfo { Accessor = fieldDescriptor.Accessor, IsRepeated = fieldDescriptor.IsRepeated, - EnumType = jsonProtobufSerializerOptions.UseStringProtoEnumValueNames ? fieldDescriptor.EnumType : null, + EnumType = enumType, IsMap = fieldDescriptor.IsMap, FieldType = FieldTypeResolver.ResolverFieldType(fieldDescriptor, propertyTypeLookup), JsonName = convertNameFunc(fieldDescriptor), diff --git a/test/Protobuf.System.Text.Json.Tests/MessageWithEnumFieldTests.Should_serialize_enum_value_as_number_when_using_proto_value_name_is_not_possible.approved.json b/test/Protobuf.System.Text.Json.Tests/MessageWithEnumFieldTests.Should_serialize_enum_value_as_number_when_using_proto_value_name_is_not_possible.approved.json index 7277345..97f6ed8 100644 --- a/test/Protobuf.System.Text.Json.Tests/MessageWithEnumFieldTests.Should_serialize_enum_value_as_number_when_using_proto_value_name_is_not_possible.approved.json +++ b/test/Protobuf.System.Text.Json.Tests/MessageWithEnumFieldTests.Should_serialize_enum_value_as_number_when_using_proto_value_name_is_not_possible.approved.json @@ -1,3 +1,4 @@ { - "enumField": 99 + "enumField": 99, + "regularField": 0 } \ No newline at end of file diff --git a/test/Protobuf.System.Text.Json.Tests/MessageWithEnumFieldTests.Should_serialize_enum_value_using_proto_enum_value_name.approved.json b/test/Protobuf.System.Text.Json.Tests/MessageWithEnumFieldTests.Should_serialize_enum_value_using_proto_enum_value_name.approved.json index e55ecdf..7db7a56 100644 --- a/test/Protobuf.System.Text.Json.Tests/MessageWithEnumFieldTests.Should_serialize_enum_value_using_proto_enum_value_name.approved.json +++ b/test/Protobuf.System.Text.Json.Tests/MessageWithEnumFieldTests.Should_serialize_enum_value_using_proto_enum_value_name.approved.json @@ -1,3 +1,4 @@ { - "enumField": "FIRST_OPTION" + "enumField": "FIRST_OPTION", + "regularField": 0 } \ No newline at end of file diff --git a/test/Protobuf.System.Text.Json.Tests/MessageWithEnumFieldTests.Should_serialize_message_with_enum_field.approved.json b/test/Protobuf.System.Text.Json.Tests/MessageWithEnumFieldTests.Should_serialize_message_with_enum_field.approved.json index 328f523..8ba61f3 100644 --- a/test/Protobuf.System.Text.Json.Tests/MessageWithEnumFieldTests.Should_serialize_message_with_enum_field.approved.json +++ b/test/Protobuf.System.Text.Json.Tests/MessageWithEnumFieldTests.Should_serialize_message_with_enum_field.approved.json @@ -1,3 +1,4 @@ { - "enumField": 1 + "enumField": 1, + "regularField": 0 } \ No newline at end of file diff --git a/test/Protobuf.System.Text.Json.Tests/Protos/message_with_enum.proto b/test/Protobuf.System.Text.Json.Tests/Protos/message_with_enum.proto index 95d6688..4c7e443 100644 --- a/test/Protobuf.System.Text.Json.Tests/Protos/message_with_enum.proto +++ b/test/Protobuf.System.Text.Json.Tests/Protos/message_with_enum.proto @@ -4,6 +4,7 @@ option csharp_namespace = "System.Text.Json.Protobuf.Tests"; message MessageWithEnum { TestEnum enum_field = 1; + int32 regular_field = 2; } enum TestEnum {