Skip to content

Commit 1b9bf88

Browse files
committed
Fix deserialization of additional repeated list
1 parent d511a44 commit 1b9bf88

3 files changed

Lines changed: 47 additions & 2 deletions

File tree

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,14 @@ private static Func<FieldDescriptor, string> GetConvertNameFunc(JsonNamingPolicy
8787
}
8888

8989
var propertyName = reader.GetString();
90-
reader.Read();
91-
90+
9291
if (propertyName == null || !_fieldsLookup.TryGetValue(propertyName, out var fieldInfo))
9392
{
93+
reader.Skip();
9494
continue;
9595
}
9696

97+
reader.Read();
9798
fieldInfo.Converter ??= InternalConverterFactory.Create(fieldInfo);
9899
fieldInfo.Converter.Read(ref reader, obj, fieldInfo.FieldType, options, fieldInfo.Accessor);
99100
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
syntax = "proto3";
2+
3+
option csharp_namespace = "System.Text.Json.Protobuf.Tests";
4+
5+
message SchemaEvolutionV1 {
6+
int32 property_1 = 1;
7+
}
8+
9+
message SchemaEvolutionV2 {
10+
int32 property_1 = 1;
11+
repeated int32 property_2 = 2;
12+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System.Text.Json;
2+
using System.Text.Json.Protobuf.Tests;
3+
using Protobuf.System.Text.Json.Tests.Utils;
4+
using Shouldly;
5+
using Xunit;
6+
7+
namespace Protobuf.System.Text.Json.Tests;
8+
9+
public class SchemaEvolutionTests
10+
{
11+
[Fact]
12+
public void Should_be_able_to_deserialize_array_of_schema_v2_to_array_of_schema_v1_when_schema_v2_has_additional_repeated_field()
13+
{
14+
// Arrange
15+
var schemaEvolutionV2 = new[]
16+
{
17+
new SchemaEvolutionV2
18+
{
19+
Property1 = 1,
20+
Property2 = { 2, 3 }
21+
}
22+
};
23+
var jsonSerializerOptions = TestHelper.CreateJsonSerializerOptions();
24+
var serialized = JsonSerializer.Serialize(schemaEvolutionV2, jsonSerializerOptions);
25+
26+
// Act
27+
var deserialized = JsonSerializer.Deserialize<SchemaEvolutionV1[]>(serialized, jsonSerializerOptions);
28+
29+
// Assert
30+
deserialized.ShouldHaveSingleItem();
31+
}
32+
}

0 commit comments

Comments
 (0)