Skip to content

Commit

Permalink
Fix deserialization of additional repeated list
Browse files Browse the repository at this point in the history
  • Loading branch information
Havret committed Jun 22, 2022
1 parent d511a44 commit 1b9bf88
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/Protobuf.System.Text.Json/ProtobufConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,14 @@ private static Func<FieldDescriptor, string> 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);
}
Expand Down
12 changes: 12 additions & 0 deletions test/Protobuf.System.Text.Json.Tests/Protos/schema_evolution.proto
Original file line number Diff line number Diff line change
@@ -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;
}
32 changes: 32 additions & 0 deletions test/Protobuf.System.Text.Json.Tests/SchemaEvolutionTests.cs
Original file line number Diff line number Diff line change
@@ -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<SchemaEvolutionV1[]>(serialized, jsonSerializerOptions);

// Assert
deserialized.ShouldHaveSingleItem();
}
}

0 comments on commit 1b9bf88

Please sign in to comment.