Skip to content

Commit 599595c

Browse files
[release/8.0] Fix reading null struct property value in gRPC JSON transcoding (#52469)
* Fix reading null struct property value * Better fix --------- Co-authored-by: James Newton-King <[email protected]>
1 parent ea2a01b commit 599595c

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

src/Grpc/JsonTranscoding/src/Microsoft.AspNetCore.Grpc.JsonTranscoding/Internal/Json/ValueConverter.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ namespace Microsoft.AspNetCore.Grpc.JsonTranscoding.Internal.Json;
1010

1111
internal sealed class ValueConverter<TMessage> : SettingsConverterBase<TMessage> where TMessage : IMessage, new()
1212
{
13+
public override bool HandleNull => true;
14+
1315
public ValueConverter(JsonContext context) : base(context)
1416
{
1517
}

src/Grpc/JsonTranscoding/test/Microsoft.AspNetCore.Grpc.JsonTranscoding.Tests/ConverterTests/JsonConverterReadTests.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,46 @@ public void RepeatedStrings()
8484
AssertReadJson<HelloRequest>(json);
8585
}
8686

87+
[Fact]
88+
public void Struct_NullProperty()
89+
{
90+
var json = @"{ ""prop"": null }";
91+
92+
AssertReadJson<Struct>(json);
93+
}
94+
95+
[Fact]
96+
public void Value_Null()
97+
{
98+
var json = "null";
99+
100+
AssertReadJson<Value>(json);
101+
}
102+
103+
[Fact]
104+
public void Value_Integer()
105+
{
106+
var json = "1";
107+
108+
AssertReadJson<Value>(json);
109+
}
110+
111+
[Fact]
112+
public void Value_String()
113+
{
114+
var json = @"""string!""";
115+
116+
AssertReadJson<Value>(json);
117+
}
118+
119+
[Fact]
120+
public void Value_Boolean()
121+
{
122+
var json = "true";
123+
124+
AssertReadJson<Value>(json);
125+
}
126+
87127
[Fact]
88128
public void DataTypes_DefaultValues()
89129
{

src/Grpc/JsonTranscoding/test/Microsoft.AspNetCore.Grpc.JsonTranscoding.Tests/ConverterTests/JsonConverterWriteTests.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,23 @@ public void Value_Nested()
334334
AssertWrittenJson(helloRequest);
335335
}
336336

337+
[Fact]
338+
public void Struct_NullValue()
339+
{
340+
var helloRequest = new HelloRequest
341+
{
342+
ValueValue = Value.ForStruct(new Struct
343+
{
344+
Fields =
345+
{
346+
["prop"] = Value.ForNull()
347+
}
348+
})
349+
};
350+
351+
AssertWrittenJson(helloRequest);
352+
}
353+
337354
[Fact]
338355
public void Value_Root()
339356
{
@@ -351,6 +368,14 @@ public void Value_Root()
351368
AssertWrittenJson(value);
352369
}
353370

371+
[Fact]
372+
public void Value_Null()
373+
{
374+
var value = Value.ForNull();
375+
376+
AssertWrittenJson(value);
377+
}
378+
354379
[Fact]
355380
public void Struct_Nested()
356381
{

0 commit comments

Comments
 (0)