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
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();
}
}