Skip to content

Commit d0c6978

Browse files
authored
Fix AnyType serialization with empty-string dictionary keys (#9775)
1 parent a9d479b commit d0c6978

3 files changed

Lines changed: 43 additions & 10 deletions

File tree

src/HotChocolate/Core/src/Types/Text/Json/ResultDocument.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,11 @@ private ReadOnlySpan<byte> ReadRawValue(DbRow row)
448448
[MethodImpl(MethodImplOptions.AggressiveInlining)]
449449
private ReadOnlySpan<byte> ReadLocalData(int location, int size)
450450
{
451+
if (size == 0)
452+
{
453+
return [];
454+
}
455+
451456
var startChunkIndex = location / JsonMemory.BufferSize;
452457
var offsetInStartChunk = location % JsonMemory.BufferSize;
453458

@@ -725,6 +730,11 @@ private void WriteByte(int position, byte value)
725730

726731
private void WriteDataCore(int position, ReadOnlySpan<byte> data)
727732
{
733+
if (data.Length == 0)
734+
{
735+
return;
736+
}
737+
728738
var chunkIndex = position / JsonMemory.BufferSize;
729739
var offset = position % JsonMemory.BufferSize;
730740
var availableInChunk = JsonMemory.BufferSize - offset;

src/HotChocolate/Core/src/Types/Text/Json/ResultElement.cs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -931,11 +931,6 @@ public void SetPropertyName(ReadOnlySpan<char> propertyName)
931931
{
932932
CheckValidInstance();
933933

934-
if (propertyName.Length == 0)
935-
{
936-
throw new ArgumentNullException(nameof(propertyName));
937-
}
938-
939934
var encoding = Encoding.UTF8;
940935
var requiredBytes = encoding.GetByteCount(propertyName);
941936
byte[]? rented = null;
@@ -961,11 +956,6 @@ public void SetPropertyName(ReadOnlySpan<byte> propertyName)
961956
{
962957
CheckValidInstance();
963958

964-
if (propertyName.Length == 0)
965-
{
966-
throw new ArgumentNullException(nameof(propertyName));
967-
}
968-
969959
_parent.AssignPropertyName(this, propertyName);
970960
}
971961

src/HotChocolate/Core/test/Types.Tests/Types/Scalars/AnyTypeTests.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,39 @@ public async Task Output_Return_RecordList()
155155
result.ToJson().MatchSnapshot();
156156
}
157157

158+
[Fact]
159+
public async Task Output_Return_Dictionary_With_Empty_Key()
160+
{
161+
// arrange
162+
var executor =
163+
await new ServiceCollection()
164+
.AddGraphQLServer()
165+
.AddQueryType(
166+
d => d
167+
.Name("Query")
168+
.Field("foo")
169+
.Type<AnyType>()
170+
.Resolve(_ => new Dictionary<string, object?> { [""] = null }))
171+
.AddJsonTypeConverter()
172+
.BuildRequestExecutorAsync();
173+
174+
// act
175+
var result = (await executor.ExecuteAsync("{ foo }")).ExpectOperationResult();
176+
177+
// assert
178+
Assert.Empty(result.Errors);
179+
result.MatchInlineSnapshot(
180+
"""
181+
{
182+
"data": {
183+
"foo": {
184+
"": null
185+
}
186+
}
187+
}
188+
""");
189+
}
190+
158191
[Fact]
159192
public async Task Output_Return_DateTime()
160193
{

0 commit comments

Comments
 (0)