Skip to content

Commit 066723e

Browse files
committed
Fix issue with deserialization of payload that contain additional repeated fields
1 parent 0d19045 commit 066723e

3 files changed

Lines changed: 29 additions & 2 deletions

File tree

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,16 @@ public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerial
7272
break;
7373
}
7474

75+
if (reader.TokenType != JsonTokenType.PropertyName)
76+
{
77+
reader.Read();
78+
continue;
79+
}
80+
7581
var propertyName = reader.GetString();
7682
reader.Read();
77-
78-
if (!_fieldsLookup.TryGetValue(propertyName, out var fieldInfo))
83+
84+
if (propertyName == null || !_fieldsLookup.TryGetValue(propertyName, out var fieldInfo))
7985
{
8086
continue;
8187
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public void Should_deserialize_a_new_version_of_a_message_using_the_old_version_
1616
{
1717
DoubleProperty = 1d,
1818
FloatProperty = 2f,
19+
RepeatedInt32Property = {1, 2, 3}
1920
};
2021
var jsonSerializerOptions = TestHelper.CreateJsonSerializerOptions();
2122

@@ -27,4 +28,23 @@ public void Should_deserialize_a_new_version_of_a_message_using_the_old_version_
2728
deserialized.ShouldNotBeNull();
2829
deserialized.DoubleProperty.ShouldBe(msg.DoubleProperty);
2930
}
31+
32+
[Fact]
33+
public void Should_deserialize_the_old_version_of_a_message_using_the_new_version_of_the_contract()
34+
{
35+
// Arrange
36+
var msg = new MessageWithVersionMismatch
37+
{
38+
DoubleProperty = 1d,
39+
};
40+
var jsonSerializerOptions = TestHelper.CreateJsonSerializerOptions();
41+
42+
// Act
43+
var payload = JsonSerializer.Serialize(msg, jsonSerializerOptions);
44+
var deserialized = JsonSerializer.Deserialize<MessageWithVersionMismatchV2>(payload, jsonSerializerOptions);
45+
46+
// Assert
47+
deserialized.ShouldNotBeNull();
48+
deserialized.DoubleProperty.ShouldBe(msg.DoubleProperty);
49+
}
3050
}

test/Protobuf.System.Text.Json.Tests/Protos/message_with_version_mismatch.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ message MessageWithVersionMismatch {
99
message MessageWithVersionMismatchV2 {
1010
double double_property = 1;
1111
float float_property = 2;
12+
repeated int32 repeated_int_32_property = 3;
1213
}

0 commit comments

Comments
 (0)