Skip to content

Commit

Permalink
Fix Utf8JsonWriterHelper to not ignore null dictionary values
Browse files Browse the repository at this point in the history
This would avoid sending any null values from StrawberryShake in such a way that if you wanted to set a null value on a request, you could not.  They will always come back as unassigned.
  • Loading branch information
david-driscoll committed Nov 10, 2023
1 parent 0f7c4a8 commit 3fbcbb7
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,15 @@ private static void WriteDictionary(

foreach (var item in dict)
{
writer.WritePropertyName(item.Key);
if (item.Value is null)
{
continue;
writer.WriteNullValue();
}
else
{
WriteFieldValue(writer, item.Value);
}

writer.WritePropertyName(item.Key);
WriteFieldValue(writer, item.Value);
}

writer.WriteEndObject();
Expand Down
Original file line number Diff line number Diff line change
@@ -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<string, object?>()
{
["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);
}
}

0 comments on commit 3fbcbb7

Please sign in to comment.