Skip to content

Commit

Permalink
Fix #51
Browse files Browse the repository at this point in the history
  • Loading branch information
Havret committed Jun 23, 2022
1 parent 1b9bf88 commit 3677b77
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
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);
}
}

0 comments on commit 3677b77

Please sign in to comment.