Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Utf8JsonWriterHelper to not ignore null dictionary values #6689

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@
break;

case JsonDocument doc:
doc.RootElement.WriteTo(writer);
break;

Check warning on line 125 in src/HotChocolate/AspNetCore/src/Transport.Abstractions/Serialization/Utf8JsonWriterHelper.cs

View check run for this annotation

Codecov / codecov/patch

src/HotChocolate/AspNetCore/src/Transport.Abstractions/Serialization/Utf8JsonWriterHelper.cs#L124-L125

Added lines #L124 - L125 were not covered by tests

case JsonElement element:
element.WriteTo(writer);
Expand Down Expand Up @@ -194,11 +194,6 @@

foreach (var item in dict)
{
if (item.Value is null)
{
continue;
}

writer.WritePropertyName(item.Key);
WriteFieldValue(writer, item.Value);
}
Expand All @@ -214,7 +209,7 @@

for (var i = 0; i < list.Count; i++)
{
WriteFieldValue(writer, list[i]);

Check warning on line 212 in src/HotChocolate/AspNetCore/src/Transport.Abstractions/Serialization/Utf8JsonWriterHelper.cs

View check run for this annotation

Codecov / codecov/patch

src/HotChocolate/AspNetCore/src/Transport.Abstractions/Serialization/Utf8JsonWriterHelper.cs#L212

Added line #L212 was not covered by tests
}

writer.WriteEndArray();
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);
}
}
Loading