You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Fix JSON_CONTAINS for nested JSON documents - #39715
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.
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>
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.
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.
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
Cast scalar subquery JSON projections for OPENJSON results
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.
[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.
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.
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.
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.
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.
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.
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.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #39626.
Stacked on #39706.
OPENJSON ... WITHcan return annvarchar(max)fragment even when EF maps it asjson. Cast text fragments before passing them toJSON_CONTAINS, while leaving nativejsondocuments 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.