Skip to content

Improve handling of null values in JSON properties mapped to primitive collections#38498

Draft
AndriySvyryd wants to merge 1 commit into
mainfrom
Issue38454
Draft

Improve handling of null values in JSON properties mapped to primitive collections#38498
AndriySvyryd wants to merge 1 commit into
mainfrom
Issue38454

Conversation

@AndriySvyryd

@AndriySvyryd AndriySvyryd commented Jun 27, 2026

Copy link
Copy Markdown
Member

Fixes #38454

Changes materialization behavior when JSON contains the null token for primitive collections (both when mapped to a dedicated column and when nested inside a JSON document), aiming to return null for optional properties and throw a clearer, property-named exception for required ones.

Changes:

  • Add a shaper-side fast path for primitive collections mapped to columns to detect JSON null before invoking CollectionToJsonStringConverter<>.
  • Improve error reporting for required primitive collections nested in JSON by throwing RelationalStrings.NullValueInRequiredJsonProperty(...) instead of a reader/writer token-type exception.
  • Add functional tests covering nullable/required primitive collections and converter ConvertsNulls behavior.

Copilot AI left a comment

Copy link
Copy Markdown

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 addresses issue #38454 by improving materialization behavior when JSON contains the null token for primitive collections (both when mapped to a dedicated column and when nested inside a JSON document), aiming to return null for optional properties and throw a clearer, property-named exception for required ones.

Changes:

  • Add a shaper-side fast path for primitive collections mapped to columns to detect JSON null before invoking CollectionToJsonStringConverter<>.
  • Improve error reporting for required primitive collections nested in JSON by throwing RelationalStrings.NullValueInRequiredJsonProperty(...) instead of a reader/writer token-type exception.
  • Add functional tests covering nullable/required primitive collections and converter ConvertsNulls behavior.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
test/EFCore.SqlServer.FunctionalTests/Query/AdHocJsonQuerySqlServerTestBase.cs Adds SQL Server functional tests for JSON null handling in primitive collections and converter null-conversion semantics.
src/EFCore.Relational/Query/RelationalShapedQueryCompilingExpressionVisitor.ShaperProcessingExpressionVisitor.cs Updates the shaper expression generation to special-case primitive collection column materialization and to throw clearer errors for required JSON collections.
src/EFCore.Relational/Query/RelationalShapedQueryCompilingExpressionVisitor.ShaperProcessingExpressionVisitor.ClientMethods.cs Introduces an internal helper used by the shaper to parse primitive collection JSON strings and handle the null token.

@AndriySvyryd AndriySvyryd changed the title Fixes #38454 Improve handling of null values in JSON properties mapped to primitive collections Jun 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JSON collection mapped to nullable reference collection fails on JSON null token (Invalid token type: 'Null')

2 participants