Skip to content
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
4 changes: 3 additions & 1 deletion src/Protobuf.System.Text.Json/ProtobufConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ private static Func<FieldDescriptor, string> 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;
}

Expand Down
30 changes: 29 additions & 1 deletion test/Protobuf.System.Text.Json.Tests/SchemaEvolutionTests.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<SchemaEvolutionV1[]>(memoryStream, jsonSerializerOptions);

// Assert
deserialized.ShouldNotBeNull();
deserialized.Length.ShouldBe(schemaEvolutionV2.Count);
}
}