Skip to content

Commit 8614e6b

Browse files
CopilotHavret
andcommitted
Add comprehensive tests for PropertyNamingSource with custom json_name
Co-authored-by: Havret <9103861+Havret@users.noreply.github.com>
1 parent 7a58d42 commit 8614e6b

2 files changed

Lines changed: 118 additions & 0 deletions

File tree

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
using System.Text.Json;
2+
using System.Text.Json.Protobuf.Tests;
3+
using Xunit;
4+
5+
namespace Protobuf.System.Text.Json.Tests;
6+
7+
public class PropertyNamingSourceTests
8+
{
9+
[Fact]
10+
public void Should_use_custom_json_name_when_PropertyNamingSource_is_ProtobufJsonName()
11+
{
12+
// Arrange
13+
var msg = new MessageWithCustomJsonName
14+
{
15+
DoubleProperty = 2.5d,
16+
StringProperty = "test"
17+
};
18+
var jsonSerializerOptions = new JsonSerializerOptions();
19+
jsonSerializerOptions.AddProtobufSupport(options => options.PropertyNamingSource = PropertyNamingSource.ProtobufJsonName);
20+
21+
// Act
22+
var serialized = JsonSerializer.Serialize(msg, jsonSerializerOptions);
23+
24+
// Assert
25+
// double_property has json_name = "customDoubleProperty"
26+
Assert.Contains("\"customDoubleProperty\"", serialized);
27+
// string_property doesn't have json_name, so it should be camelCased to "stringProperty"
28+
Assert.Contains("\"stringProperty\"", serialized);
29+
}
30+
31+
[Fact]
32+
public void Should_use_proto_field_name_when_PropertyNamingSource_is_ProtobufFieldName()
33+
{
34+
// Arrange
35+
var msg = new MessageWithCustomJsonName
36+
{
37+
DoubleProperty = 2.5d,
38+
StringProperty = "test"
39+
};
40+
var jsonSerializerOptions = new JsonSerializerOptions();
41+
jsonSerializerOptions.AddProtobufSupport(options => options.PropertyNamingSource = PropertyNamingSource.ProtobufFieldName);
42+
43+
// Act
44+
var serialized = JsonSerializer.Serialize(msg, jsonSerializerOptions);
45+
46+
// Assert
47+
// Both fields should use their original proto field names
48+
Assert.Contains("\"double_property\"", serialized);
49+
Assert.Contains("\"string_property\"", serialized);
50+
// Should NOT use the custom json_name
51+
Assert.DoesNotContain("\"customDoubleProperty\"", serialized);
52+
}
53+
54+
[Fact]
55+
public void Should_deserialize_using_custom_json_name_when_PropertyNamingSource_is_ProtobufJsonName()
56+
{
57+
// Arrange
58+
var json = "{\"customDoubleProperty\": 2.5, \"stringProperty\": \"test\"}";
59+
var jsonSerializerOptions = new JsonSerializerOptions();
60+
jsonSerializerOptions.AddProtobufSupport(options => options.PropertyNamingSource = PropertyNamingSource.ProtobufJsonName);
61+
62+
// Act
63+
var deserialized = JsonSerializer.Deserialize<MessageWithCustomJsonName>(json, jsonSerializerOptions);
64+
65+
// Assert
66+
Assert.NotNull(deserialized);
67+
Assert.Equal(2.5d, deserialized.DoubleProperty);
68+
Assert.Equal("test", deserialized.StringProperty);
69+
}
70+
71+
[Fact]
72+
public void Should_deserialize_using_proto_field_name_when_PropertyNamingSource_is_ProtobufFieldName()
73+
{
74+
// Arrange
75+
var json = "{\"double_property\": 2.5, \"string_property\": \"test\"}";
76+
var jsonSerializerOptions = new JsonSerializerOptions();
77+
jsonSerializerOptions.AddProtobufSupport(options => options.PropertyNamingSource = PropertyNamingSource.ProtobufFieldName);
78+
79+
// Act
80+
var deserialized = JsonSerializer.Deserialize<MessageWithCustomJsonName>(json, jsonSerializerOptions);
81+
82+
// Assert
83+
Assert.NotNull(deserialized);
84+
Assert.Equal(2.5d, deserialized.DoubleProperty);
85+
Assert.Equal("test", deserialized.StringProperty);
86+
}
87+
88+
[Fact]
89+
public void Should_round_trip_with_ProtobufFieldName()
90+
{
91+
// Arrange
92+
var original = new MessageWithCustomJsonName
93+
{
94+
DoubleProperty = 2.5d,
95+
StringProperty = "test"
96+
};
97+
var jsonSerializerOptions = new JsonSerializerOptions();
98+
jsonSerializerOptions.AddProtobufSupport(options => options.PropertyNamingSource = PropertyNamingSource.ProtobufFieldName);
99+
100+
// Act
101+
var serialized = JsonSerializer.Serialize(original, jsonSerializerOptions);
102+
var deserialized = JsonSerializer.Deserialize<MessageWithCustomJsonName>(serialized, jsonSerializerOptions);
103+
104+
// Assert
105+
Assert.NotNull(deserialized);
106+
Assert.Equal(original.DoubleProperty, deserialized.DoubleProperty);
107+
Assert.Equal(original.StringProperty, deserialized.StringProperty);
108+
}
109+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
syntax = "proto3";
2+
3+
option csharp_namespace = "System.Text.Json.Protobuf.Tests";
4+
5+
message MessageWithCustomJsonName {
6+
double double_property = 1 [json_name = "customDoubleProperty"];
7+
8+
string string_property = 2;
9+
}

0 commit comments

Comments
 (0)