diff --git a/src/Protobuf.System.Text.Json/ProtobufConverter.cs b/src/Protobuf.System.Text.Json/ProtobufConverter.cs index 1dbb6b2..a2645f3 100644 --- a/src/Protobuf.System.Text.Json/ProtobufConverter.cs +++ b/src/Protobuf.System.Text.Json/ProtobufConverter.cs @@ -87,13 +87,14 @@ private static Func GetConvertNameFunc(JsonNamingPolicy } var propertyName = reader.GetString(); - reader.Read(); - + if (propertyName == null || !_fieldsLookup.TryGetValue(propertyName, out var fieldInfo)) { + reader.Skip(); continue; } + reader.Read(); fieldInfo.Converter ??= InternalConverterFactory.Create(fieldInfo); fieldInfo.Converter.Read(ref reader, obj, fieldInfo.FieldType, options, fieldInfo.Accessor); } diff --git a/test/Protobuf.System.Text.Json.Tests/Protos/schema_evolution.proto b/test/Protobuf.System.Text.Json.Tests/Protos/schema_evolution.proto new file mode 100644 index 0000000..ed94726 --- /dev/null +++ b/test/Protobuf.System.Text.Json.Tests/Protos/schema_evolution.proto @@ -0,0 +1,12 @@ +syntax = "proto3"; + +option csharp_namespace = "System.Text.Json.Protobuf.Tests"; + +message SchemaEvolutionV1 { + int32 property_1 = 1; +} + +message SchemaEvolutionV2 { + int32 property_1 = 1; + repeated int32 property_2 = 2; +} \ No newline at end of file diff --git a/test/Protobuf.System.Text.Json.Tests/SchemaEvolutionTests.cs b/test/Protobuf.System.Text.Json.Tests/SchemaEvolutionTests.cs new file mode 100644 index 0000000..cdae434 --- /dev/null +++ b/test/Protobuf.System.Text.Json.Tests/SchemaEvolutionTests.cs @@ -0,0 +1,32 @@ +using System.Text.Json; +using System.Text.Json.Protobuf.Tests; +using Protobuf.System.Text.Json.Tests.Utils; +using Shouldly; +using Xunit; + +namespace Protobuf.System.Text.Json.Tests; + +public class SchemaEvolutionTests +{ + [Fact] + public void Should_be_able_to_deserialize_array_of_schema_v2_to_array_of_schema_v1_when_schema_v2_has_additional_repeated_field() + { + // Arrange + var schemaEvolutionV2 = new[] + { + new SchemaEvolutionV2 + { + Property1 = 1, + Property2 = { 2, 3 } + } + }; + var jsonSerializerOptions = TestHelper.CreateJsonSerializerOptions(); + var serialized = JsonSerializer.Serialize(schemaEvolutionV2, jsonSerializerOptions); + + // Act + var deserialized = JsonSerializer.Deserialize(serialized, jsonSerializerOptions); + + // Assert + deserialized.ShouldHaveSingleItem(); + } +} \ No newline at end of file