Skip to content

Commit 9324f30

Browse files
authored
Fix reading null struct property value in gRPC JSON transcoding (#52442)
1 parent df46227 commit 9324f30

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
@@ -120,6 +120,46 @@ public void RepeatedStrings()
120120
AssertReadJson<HelloRequest>(json);
121121
}
122122

123+
[Fact]
124+
public void Struct_NullProperty()
125+
{
126+
var json = @"{ ""prop"": null }";
127+
128+
AssertReadJson<Struct>(json);
129+
}
130+
131+
[Fact]
132+
public void Value_Null()
133+
{
134+
var json = "null";
135+
136+
AssertReadJson<Value>(json);
137+
}
138+
139+
[Fact]
140+
public void Value_Integer()
141+
{
142+
var json = "1";
143+
144+
AssertReadJson<Value>(json);
145+
}
146+
147+
[Fact]
148+
public void Value_String()
149+
{
150+
var json = @"""string!""";
151+
152+
AssertReadJson<Value>(json);
153+
}
154+
155+
[Fact]
156+
public void Value_Boolean()
157+
{
158+
var json = "true";
159+
160+
AssertReadJson<Value>(json);
161+
}
162+
123163
[Fact]
124164
public void DataTypes_DefaultValues()
125165
{

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)