Skip to content

Materialize null elements in JSON complex collections - #39831

Open
thalysmarciobn wants to merge 1 commit into
dotnet:mainfrom
thalysmarciobn:fix/39781-json-complex-collection-null-element
Open

thalysmarciobn wants to merge 1 commit into
dotnet:mainfrom
thalysmarciobn:fix/39781-json-complex-collection-null-element

Conversation

@thalysmarciobn

@thalysmarciobn thalysmarciobn commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Reading a complex collection mapped to JSON throws an InvalidOperationException when the stored array contains a null element, even though the element type is nullable. MaterializeJsonEntityCollection and IncludeJsonEntityCollection reject every null element, yet they also serve complex collections, which the update pipeline already writes as JSON null.

Keep the element for a complex collection, as a primitive collection does, and keep throwing for a collection navigation, which never allowed them. Add it directly rather than through IClrCollectionAccessor, whose AddStandalone skips a value already in the collection and would collapse consecutive nulls, shifting the ordinals after them.

Add regression tests for the entity query and the collection projection, covering a nested complex collection, consecutive nulls, tracking and no-tracking, and a save and reload round trip on SQLite and SQL Server.

Validation:

  • All seven new tests fail before the fix and pass afterward.
  • 2,143 JSON and association tests pass on SQLite and 1,313 on SQL Server across both JSON column types.

Cosmos rejects a null array element the same way, left out since I can't run its tests locally.

Fixes #39781

- Add a null element to the collection instead of throwing, bypassing the collection
  accessor so that consecutive nulls keep their own ordinals
- Keep rejecting null elements in collection navigations, which never allowed them

Fixes dotnet#39781
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.

Null element in JSON complex collection fails with "Invalid token type: 'Null'" when querying

1 participant