Skip to content

Fix AnyType serialization with empty-string dictionary keys#9775

Merged
glen-84 merged 2 commits into
mainfrom
gai/anytype-empty-string-dict-key
May 26, 2026
Merged

Fix AnyType serialization with empty-string dictionary keys#9775
glen-84 merged 2 commits into
mainfrom
gai/anytype-empty-string-dict-key

Conversation

@glen-84
Copy link
Copy Markdown
Member

@glen-84 glen-84 commented May 25, 2026

Summary

  • ResultDocument.WriteDataCore early-returns on a zero-length span, fixing an ArgumentOutOfRangeException when writing an empty property name.
  • ResultDocument.ReadLocalData early-returns on a zero-length read, so properties with SizeOrLength == 0 whose Location was never backed by a rented chunk (e.g. { "": null } with no other local data) don't trip _data[startChunkIndex].
  • Removes the propertyName.Length == 0 throws from both ResultElement.SetPropertyName overloads — empty strings are valid JSON property names and the previous guard was masking the writer bug above.
  • Adds an AnyType regression test covering a resolver returning a Dictionary<string, object?> with an empty-string key and a null value (the minimal case that exercises both the zero-length write and zero-length read paths).

Test plan

  • dotnet test src/HotChocolate/Core/test/Types.Tests/HotChocolate.Types.Tests.csproj --filter "FullyQualifiedName~AnyTypeTests.Output_Return_Dictionary_With_Empty_Key" — green on net9.0.
  • dotnet test src/HotChocolate/Core/test/Types.Tests/HotChocolate.Types.Tests.csproj --filter "FullyQualifiedName~AnyType" — full AnyType suite 75/75 on net9.0.

Closes #9774

Copilot AI review requested due to automatic review settings May 25, 2026 12:44
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes AnyType JSON serialization for dictionaries that contain an empty-string key by allowing empty property names to flow through ResultElement and preventing a writer crash when attempting to write a zero-length byte span.

Changes:

  • Allow empty-string JSON property names by removing the Length == 0 exception guards in ResultElement.SetPropertyName overloads.
  • Add an early-return in ResultDocument.WriteDataCore for data.Length == 0 to avoid exceptions when writing empty property names.
  • Add a regression test validating output when a resolver returns a dictionary with an empty-string key.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
src/HotChocolate/Core/test/Types.Tests/Types/Scalars/AnyTypeTests.cs Adds a regression test for empty-string dictionary keys in AnyType output.
src/HotChocolate/Core/src/Types/Text/Json/ResultElement.cs Removes empty-name guards so empty JSON property names are permitted.
src/HotChocolate/Core/src/Types/Text/Json/ResultDocument.cs Avoids exceptions by no-op’ing WriteDataCore on zero-length writes.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/HotChocolate/Core/src/Types/Text/Json/ResultDocument.cs
@glen-84 glen-84 merged commit d0c6978 into main May 26, 2026
144 checks passed
@glen-84 glen-84 deleted the gai/anytype-empty-string-dict-key branch May 26, 2026 10:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AnyType serialization does not support Dictionary with empty string key

2 participants