Skip to content

Commit 3677b77

Browse files
committed
Fix #51
1 parent 1b9bf88 commit 3677b77

2 files changed

Lines changed: 32 additions & 2 deletions

File tree

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ private static Func<FieldDescriptor, string> GetConvertNameFunc(JsonNamingPolicy
9090

9191
if (propertyName == null || !_fieldsLookup.TryGetValue(propertyName, out var fieldInfo))
9292
{
93-
reader.Skip();
93+
// We need to call TrySkip instead of Skip as Skip may throw exception when called in DeserializeAsync
94+
// context https://github.com/dotnet/runtime/issues/39795
95+
_ = reader.TrySkip();
9496
continue;
9597
}
9698

test/Protobuf.System.Text.Json.Tests/SchemaEvolutionTests.cs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1-
using System.Text.Json;
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Text.Json;
26
using System.Text.Json.Protobuf.Tests;
7+
using System.Threading.Tasks;
38
using Protobuf.System.Text.Json.Tests.Utils;
49
using Shouldly;
510
using Xunit;
@@ -29,4 +34,27 @@ public void Should_be_able_to_deserialize_array_of_schema_v2_to_array_of_schema_
2934
// Assert
3035
deserialized.ShouldHaveSingleItem();
3136
}
37+
38+
[Fact]
39+
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()
40+
{
41+
// Arrange
42+
var schemaEvolutionV2 = Enumerable.Range(0, 50_000).Select(x => new SchemaEvolutionV2
43+
{
44+
Property1 = x,
45+
Property2 = {x + 1, x + 2}
46+
}).ToList();
47+
48+
var jsonSerializerOptions = TestHelper.CreateJsonSerializerOptions();
49+
using var memoryStream = new MemoryStream();
50+
await JsonSerializer.SerializeAsync(memoryStream, schemaEvolutionV2, jsonSerializerOptions);
51+
memoryStream.Position = 0;
52+
53+
// Act
54+
var deserialized = JsonSerializer.Deserialize<SchemaEvolutionV1[]>(memoryStream, jsonSerializerOptions);
55+
56+
// Assert
57+
deserialized.ShouldNotBeNull();
58+
deserialized.Length.ShouldBe(schemaEvolutionV2.Count);
59+
}
3260
}

0 commit comments

Comments
 (0)