Skip to content

Commit

Permalink
Fix issue with deserialization of payload that contain additional rep…
Browse files Browse the repository at this point in the history
…eated fields
  • Loading branch information
Havret committed Mar 24, 2022
1 parent 0d19045 commit 066723e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/Protobuf.System.Text.Json/ProtobufConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,16 @@ public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerial
break;
}

if (reader.TokenType != JsonTokenType.PropertyName)
{
reader.Read();
continue;
}

var propertyName = reader.GetString();
reader.Read();
if (!_fieldsLookup.TryGetValue(propertyName, out var fieldInfo))

if (propertyName == null || !_fieldsLookup.TryGetValue(propertyName, out var fieldInfo))
{
continue;
}
Expand Down
20 changes: 20 additions & 0 deletions test/Protobuf.System.Text.Json.Tests/ContractEvolutionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public void Should_deserialize_a_new_version_of_a_message_using_the_old_version_
{
DoubleProperty = 1d,
FloatProperty = 2f,
RepeatedInt32Property = {1, 2, 3}
};
var jsonSerializerOptions = TestHelper.CreateJsonSerializerOptions();

Expand All @@ -27,4 +28,23 @@ public void Should_deserialize_a_new_version_of_a_message_using_the_old_version_
deserialized.ShouldNotBeNull();
deserialized.DoubleProperty.ShouldBe(msg.DoubleProperty);
}

[Fact]
public void Should_deserialize_the_old_version_of_a_message_using_the_new_version_of_the_contract()
{
// Arrange
var msg = new MessageWithVersionMismatch
{
DoubleProperty = 1d,
};
var jsonSerializerOptions = TestHelper.CreateJsonSerializerOptions();

// Act
var payload = JsonSerializer.Serialize(msg, jsonSerializerOptions);
var deserialized = JsonSerializer.Deserialize<MessageWithVersionMismatchV2>(payload, jsonSerializerOptions);

// Assert
deserialized.ShouldNotBeNull();
deserialized.DoubleProperty.ShouldBe(msg.DoubleProperty);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ message MessageWithVersionMismatch {
message MessageWithVersionMismatchV2 {
double double_property = 1;
float float_property = 2;
repeated int32 repeated_int_32_property = 3;
}

0 comments on commit 066723e

Please sign in to comment.