From 2e3590ddf128994e488d94808fd03b5e85dff42d Mon Sep 17 00:00:00 2001 From: David Driscoll Date: Sat, 11 Nov 2023 04:37:26 -0500 Subject: [PATCH] Fixed Utf8JsonWriterHelper to not ignore null dictionary values (#6689) --- .../Serialization/Utf8JsonWriterHelper.cs | 5 --- .../OperationRequestTests.cs | 37 +++++++++++++++++++ 2 files changed, 37 insertions(+), 5 deletions(-) create mode 100644 src/HotChocolate/AspNetCore/test/Transport.Http.Tests/OperationRequestTests.cs diff --git a/src/HotChocolate/AspNetCore/src/Transport.Abstractions/Serialization/Utf8JsonWriterHelper.cs b/src/HotChocolate/AspNetCore/src/Transport.Abstractions/Serialization/Utf8JsonWriterHelper.cs index 484e509c578..ef7035838c2 100644 --- a/src/HotChocolate/AspNetCore/src/Transport.Abstractions/Serialization/Utf8JsonWriterHelper.cs +++ b/src/HotChocolate/AspNetCore/src/Transport.Abstractions/Serialization/Utf8JsonWriterHelper.cs @@ -194,11 +194,6 @@ private static void WriteDictionary( foreach (var item in dict) { - if (item.Value is null) - { - continue; - } - writer.WritePropertyName(item.Key); WriteFieldValue(writer, item.Value); } diff --git a/src/HotChocolate/AspNetCore/test/Transport.Http.Tests/OperationRequestTests.cs b/src/HotChocolate/AspNetCore/test/Transport.Http.Tests/OperationRequestTests.cs new file mode 100644 index 00000000000..ed07f32ab1a --- /dev/null +++ b/src/HotChocolate/AspNetCore/test/Transport.Http.Tests/OperationRequestTests.cs @@ -0,0 +1,37 @@ +using System.Text; +using System.Text.Json; +using CookieCrumble; +using Moq; + +namespace HotChocolate.Transport.Http.Tests; + +public class OperationRequestTests +{ + [Fact] + public async Task Should_WriteNullValues() + { + // arrange + var request = new OperationRequest( + null, + "abc", + "myOperation", + variables: new Dictionary() + { + ["abc"] = "def", + ["hij"] = null + }); + + using var memory = new MemoryStream(); + await using var writer = new Utf8JsonWriter(memory); + + // act + request.WriteTo(writer); + await writer.FlushAsync(); + + // assert + var result = Encoding.UTF8.GetString(memory.ToArray()); + Assert.Equal( + """{"id":"abc","operationName":"myOperation","variables":{"abc":"def","hij":null}}""", + result); + } +}