diff --git a/src/Protobuf.System.Text.Json/ProtobufConverter.cs b/src/Protobuf.System.Text.Json/ProtobufConverter.cs index a2645f3..d52bd97 100644 --- a/src/Protobuf.System.Text.Json/ProtobufConverter.cs +++ b/src/Protobuf.System.Text.Json/ProtobufConverter.cs @@ -90,7 +90,9 @@ private static Func GetConvertNameFunc(JsonNamingPolicy if (propertyName == null || !_fieldsLookup.TryGetValue(propertyName, out var fieldInfo)) { - reader.Skip(); + // We need to call TrySkip instead of Skip as Skip may throw exception when called in DeserializeAsync + // context https://github.com/dotnet/runtime/issues/39795 + _ = reader.TrySkip(); continue; } diff --git a/test/Protobuf.System.Text.Json.Tests/SchemaEvolutionTests.cs b/test/Protobuf.System.Text.Json.Tests/SchemaEvolutionTests.cs index cdae434..33d4949 100644 --- a/test/Protobuf.System.Text.Json.Tests/SchemaEvolutionTests.cs +++ b/test/Protobuf.System.Text.Json.Tests/SchemaEvolutionTests.cs @@ -1,5 +1,10 @@ -using System.Text.Json; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text.Json; using System.Text.Json.Protobuf.Tests; +using System.Threading.Tasks; using Protobuf.System.Text.Json.Tests.Utils; using Shouldly; using Xunit; @@ -29,4 +34,27 @@ public void Should_be_able_to_deserialize_array_of_schema_v2_to_array_of_schema_ // Assert deserialized.ShouldHaveSingleItem(); } + + [Fact] + public async Task Should_be_able_to_deserialize_array_of_schema_v2_to_array_of_schema_v1_when_schema_v2_has_additional_repeated_field_2() + { + // Arrange + var schemaEvolutionV2 = Enumerable.Range(0, 50_000).Select(x => new SchemaEvolutionV2 + { + Property1 = x, + Property2 = {x + 1, x + 2} + }).ToList(); + + var jsonSerializerOptions = TestHelper.CreateJsonSerializerOptions(); + using var memoryStream = new MemoryStream(); + await JsonSerializer.SerializeAsync(memoryStream, schemaEvolutionV2, jsonSerializerOptions); + memoryStream.Position = 0; + + // Act + var deserialized = JsonSerializer.Deserialize(memoryStream, jsonSerializerOptions); + + // Assert + deserialized.ShouldNotBeNull(); + deserialized.Length.ShouldBe(schemaEvolutionV2.Count); + } } \ No newline at end of file