Skip to content

Fix JSON_CONTAINS for nested JSON documents - #39715

Merged
cincuranet merged 13 commits into
mainfrom
cincuranet-fix-json-contains-nested-document
Dec 4, 2026
Merged

cincuranet merged 13 commits into
mainfrom
cincuranet-fix-json-contains-nested-document

Conversation

@cincuranet

@cincuranet cincuranet commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #39626.

Stacked on #39706.

OPENJSON ... WITH can return an nvarchar(max) fragment even when EF maps it as json. Cast text fragments before passing them to JSON_CONTAINS, while leaving native json documents unchanged.

Validation (after . .\activate.ps1):

  • dotnet test .\test\EFCore.SqlServer.Tests\EFCore.SqlServer.Tests.csproj --no-restore -- --filter-class '*SqlServerJsonContainsTest' — passed, 10 tests.
  • dotnet test .\test\EFCore.SqlServer.FunctionalTests\EFCore.SqlServer.FunctionalTests.csproj --no-restore -- --filter-method '*Json_collection_of_primitives*' — passed, 24 tests.
  • dotnet test .\test\EFCore.Sqlite.FunctionalTests\EFCore.Sqlite.FunctionalTests.csproj --no-restore -- --filter-method '*Json_collection_of_primitives_in_json_collection_contains' — passed, 2 tests.

The full suite was not run. Azure-specific SQL generation was covered by unit tests; no separate Azure execution was run.

  • I've read the guidelines for contributing and seen the walkthrough
  • I've posted a comment on an issue with a detailed description of how I am planning to contribute and got approval from a member of the team
  • The code builds and tests pass locally (also verified by our automated build checks)
  • Commit messages follow this format:
        Summary of the changes
        - Detail 1
        - Detail 2

        Fixes #bugnumber
  • Tests for the changes have been added (for bug fixes / features)
  • Code follows the same patterns and style as existing code in this repo

cincuranet and others added 3 commits November 18, 2026 13:14
Use OPENJSON when collection values cannot be passed to JSON_CONTAINS, and add regression coverage for GUID, temporal, and converted collection elements.

Fixes #39625

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
- Cast text JSON fragments before passing them to JSON_CONTAINS
- Add nested-collection regression coverage

Fixes #39626

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@cincuranet
cincuranet requested review from a team and wtgodbe as code owners November 19, 2026 08:56
@github-actions

github-actions Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Agent evaluation: query-pipeline

Status: running for 99663c6f333b02f67de670b55a89fce1301bc055

View workflow run

@cincuranet
cincuranet added this pull request to stack #39716 November 19, 2026 08:57
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 19, 2026 09:08

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

JSON document detection misses set-operation projections, omitting a required cast and causing Concat queries using JSON_CONTAINS to fail with error 8116 on Azure SQL at compatibility level 170.

1 open issue
What changed in this PR

This PR addresses #39626 by casting nested JSON text fragments before passing them to SQL Server’s JSON_CONTAINS, while preserving native json inputs. It builds on #39706’s search-value type handling.

Changes:

  • Track document sources during JSON postprocessing to identify fragments requiring casts.
  • Pass SQL Server options into the postprocessor for provider-specific decisions.
  • Add nested-collection regression coverage and document the type-mapping caveat.
File Description
test/​EFCore.SqlServer.Tests/​Query/​SqlServerJsonContainsTest.cs Adds SQL-generation coverage for nested fragments and native JSON.
test/​EFCore.SqlServer.FunctionalTests/​Query/​JsonQuerySqlServerTest.cs Adds the text-storage SQL baseline.
test/​EFCore.SqlServer.FunctionalTests/​Query/​JsonQueryJsonTypeSqlServerTest.cs Adds native JSON and Azure SQL baselines.
test/​EFCore.Sqlite.FunctionalTests/​Query/​JsonQuerySqliteTest.cs Adds the SQLite regression baseline.
test/​EFCore.Relational.Specification.Tests/​Query/​JsonQueryRelationalTestBase.cs Adds shared nested-collection behavior coverage.
src/​EFCore.SqlServer/​Query/​Internal/​SqlServerSqlNullabilityProcessor.cs Supplies options when constructing the JSON postprocessor.
src/​EFCore.SqlServer/​Query/​Internal/​SqlServerQueryTranslationPostprocessorFactory.cs Injects and forwards SQL Server options.
src/​EFCore.SqlServer/​Query/​Internal/​SqlServerQueryTranslationPostprocessor.cs Passes options to JSON postprocessing.
src/​EFCore.SqlServer/​Query/​Internal/​SqlServerJsonPostprocessor.cs Tracks document sources and inserts required JSON casts.
.agents/​skills/​query-pipeline/​SKILL.md Documents differences between mapped and actual JSON types.

Review effort: Balanced

@github-code-quality

github-code-quality Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Code coverage overview

Languages: C#

C# / code-coverage

main ed88619 cincuranet-fix-json-... 6824a41 +/-
77% 77% 0%
View coverage of the 10 most impacted files.
File main ed88619 cincuranet-fix-json-... 6824a41 +/-
src/EFCore.SqlS...tionsBuilder.cs 54% 38% -16%
src/EFCore.SqlS...onsExtension.cs 75% 71% -4%
src/EFCore/Diag...erExtensions.cs 94% 93% -1%
src/EFCore.SqlS...ostprocessor.cs 94% 93% -1%
src/EFCore.Rela...osticsLogger.cs 99% 98% -1%
src/EFCore.SqlS...erExtensions.cs 79% 78% -1%
src/EFCore.SqlS...ssionVisitor.cs 89% 90% +1%
src/EFCore.Rela...CommandCache.cs 98% 100% +2%
src/Microsoft.D...nnectionPool.cs 97% 100% +3%
src/EFCore.Sqli...ryRepository.cs 91% 97% +6%

ℹ️ Calculations represent line coverage. Results may change as calculations are updated.


Updated December 04, 2026 09:30 UTC

Copilot AI balanced review requested due to automatic review settings November 19, 2026 10:22

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

A scalar subquery retaining its JSON mapping bypasses the cast, causing JsonContains to fail with SQL error 8116 at compatibility level 170; the shape needs handling and a regression test.

No open issues 🎉

1 previously missed issue

In code that hasn't changed since last review

Medium severity Cast scalar subquery JSON projections for OPENJSON results

src/​EFCore.SqlServer/​Query/​Internal/​SqlServerJsonPostprocessor.cs:341

ScalarSubqueryExpression retains its projection's json mapping but falls through to false, leaving text returned by Azure OPENJSON uncast. At compatibility level 170, passing e.Details.Tags.OrderBy(t => t.Names.Count).Select(t => t.Names).First() to EF.Functions.JsonContains(..., "a") therefore still produces a text document argument and fails with error 8116. Recurse into the scalar subquery's sole projection and add a regression test for this shape.

1 resolved since last review

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings November 19, 2026 10:30

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

The fix excludes path-qualified JSON_CONTAINS calls, leaving three-argument calls over nested OPENJSON fragments failing with SQL Server error 8116.

No open issues 🎉

1 previously missed issue

In code that hasn't changed since last review

Medium severity Path-qualified JsonContains calls still fail on nvarchar(max) fragments

src/​EFCore.SqlServer/​Query/​Internal/​SqlServerJsonPostprocessor.cs:204

[Medium] The exact two-argument pattern leaves path-qualified calls outside this fix. With UseAzureSql at compatibility level 170, e.Details.Tags.Any(t => EF.Functions.JsonContains(t.Names, "a", "$") == 1) produces a three-argument function over the same nvarchar(max) OPENJSON fragment, so it still fails with error 8116. Match calls with optional path/search-mode arguments as well, preserve those arguments when updating the function, and cover a path-qualified nested-document call.

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings November 19, 2026 10:55

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Human assessment is needed to evaluate a conditional-expression casting edge case that can cause JSON_CONTAINS to fail at runtime.

No open issues 🎉

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings November 19, 2026 12:03

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Human assessment is needed because multiple JSON expression shapes can bypass required casts on Azure SQL compatibility level 170, causing runtime error 8116.

No open issues 🎉

Review effort: Balanced

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Human assessment is needed because JSON equality and inequality comparisons can bypass nested JSON_CONTAINS processing, leaving some Azure SQL compatibility-level-170 queries failing with error 8116.

No open issues 🎉

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings November 19, 2026 13:46
@github-actions

Copy link
Copy Markdown
Contributor

Agent harness evaluation is stale

Evaluation does not run automatically for new commits. A contributor with write access can comment /eval to run all affected components or /eval component-name to run one component.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Azure SQL 170 JSON_QUERY typing remains unresolved: RequiresJsonCast inherits its input type, documentation describes nvarchar(max), and native-json baseline assumptions were not verified through Azure execution.

No open issues 🎉

Review effort: Balanced

@cincuranet

Copy link
Copy Markdown
Contributor Author

Validated on Azure SQL at compatibility level 170: JSON_CONTAINS(JSON_QUERY(native_json, '$.Names'), N'a') and the casted OPENJSON fragment both return 1; the uncast nvarchar(max) fragment fails with the expected argument-type error. This confirms the current cast placement.

Base automatically changed from cincuranet-fix-issue-39625 to main November 24, 2026 19:40
Copilot AI balanced review requested due to automatic review settings December 3, 2026 12:02

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Azure coverage checks only generated SQL, leaving the fix’s runtime JSON typing across derived documents unverified pending execution on Azure SQL.

No open issues 🎉

Review effort: Balanced

…ontains-nested-document

# Conflicts:
#	.agents/skills/query-pipeline/SKILL.md
#	src/EFCore.SqlServer/Query/Internal/SqlServerJsonPostprocessor.cs
#	src/EFCore.SqlServer/Query/Internal/SqlServerQueryTranslationPostprocessor.cs
#	test/EFCore.Relational.Specification.Tests/Query/PrimitiveCollectionsQueryRelationalTestBase.cs
#	test/EFCore.SqlServer.FunctionalTests/Query/PrimitiveCollectionsQuerySqlServerJsonTypeTest.cs
#	test/EFCore.SqlServer.FunctionalTests/Query/PrimitiveCollectionsQuerySqlServerTest.cs
#	test/EFCore.SqlServer.Tests/Query/SqlServerJsonContainsTest.cs
#	test/EFCore.Sqlite.FunctionalTests/Query/PrimitiveCollectionsQuerySqliteTest.cs
@cincuranet
cincuranet requested review from AndriySvyryd and a balanced review from Copilot December 3, 2026 12:21

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Azure SQL execution remains unverified for engine-specific JSON typing in mixed text/native CASE and COALESCE expressions, which have only SQL-generation coverage.

No open issues 🎉

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings December 4, 2026 09:00

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Azure SQL compatibility-level-170 runtime validation remains missing for native-versus-text JSON behavior across nested queries, set operations, and mixed expressions.

No open issues 🎉

Review effort: Balanced

@cincuranet
cincuranet merged commit b30752b into main Dec 4, 2026
20 checks passed
@cincuranet
cincuranet deleted the cincuranet-fix-json-contains-nested-document branch December 4, 2026 10:11
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.

UseAzureSql at 170: Contains over a list inside a JSON collection applies JSON_CONTAINS to an nvarchar(max) fragment (error 8116)

3 participants