Skip to content

Commit 3dc1b94

Browse files
committed
Support schema evolution
1 parent 3452956 commit 3dc1b94

3 files changed

Lines changed: 44 additions & 3 deletions

File tree

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,14 @@ public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerial
7272
}
7373

7474
var propertyName = reader.GetString();
75-
75+
reader.Read();
76+
7677
if (!_fieldsLookup.TryGetValue(propertyName, out var fieldInfo))
7778
{
7879
continue;
7980
}
8081

8182
fieldInfo.Converter ??= InternalConverterFactory.Create(fieldInfo);
82-
83-
reader.Read();
8483
fieldInfo.Converter.Read(ref reader, obj, fieldInfo.FieldType, options, fieldInfo.Accessor);
8584
}
8685

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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 ContractEvolutionTests
10+
{
11+
[Fact]
12+
public void Should_deserialize_a_new_version_of_a_message_using_the_old_version_of_the_contract()
13+
{
14+
// Arrange
15+
var msg = new MessageWithVersionMismatchV2
16+
{
17+
DoubleProperty = 1d,
18+
FloatProperty = 2f,
19+
};
20+
var jsonSerializerOptions = TestHelper.CreateJsonSerializerOptions();
21+
22+
// Act
23+
var payload = JsonSerializer.Serialize(msg, jsonSerializerOptions);
24+
var deserialized = JsonSerializer.Deserialize<MessageWithVersionMismatch>(payload, jsonSerializerOptions);
25+
26+
// Assert
27+
deserialized.ShouldNotBeNull();
28+
deserialized.DoubleProperty.ShouldBe(msg.DoubleProperty);
29+
}
30+
}
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 MessageWithVersionMismatch {
6+
double double_property = 1;
7+
}
8+
9+
message MessageWithVersionMismatchV2 {
10+
double double_property = 1;
11+
float float_property = 2;
12+
}

0 commit comments

Comments
 (0)