Repository navigation
Fix JSON collection element property named "key" colliding with the array index column - #39475
Conversation
…rray index column - Add an internal GetJsonElementColumnName hook to CreateSelect so providers can name element columns - SQL Server: rename an OPENJSON WITH column named "key" to the first free name among key0, key1... so it doesn't collide with OPENJSON's [key] index column - SQLite: do the same for the subquery alias over json_each, case-insensitively since aliases are unique case-insensitively - Names without a collision are kept Fixes dotnet#39438
d373998 to
e45170a
Compare
There was a problem hiding this comment.
🔵 Needs a closer look
The change spans the shared relational CreateSelect JSON-projection path and two provider overrides with non-obvious column aliasing, whose correctness depends on provider SQL baselines that can't be verified without executing tests here.
No open issues 🎉
What changed in this PR
This PR fixes issue #39438, where a JSON collection element property whose JSON name is key collided with the array-index column of the JSON table-valued function (OPENJSON's [key] on SQL Server, json_each's key on SQLite). Because CreateSelect names element columns after their JSON property names, the property column and the index column both became key, causing the ordering (CAST([a].[key] AS int)) to resolve to the property value and fail (SQL Server conversion error; SQLite coercion error). The fix introduces a provider-overridable hook that renames the colliding property column while leaving the index column named key.
Changes:
- Added internal
GetJsonElementColumnNamehook inCreateSelect(default returns the JSON name unchanged, so other relational providers are unaffected), and routed the three JSON column-creation sites (property, complex container, owned navigation) through it. - SQL Server renames the colliding
WITHcolumn (key→ first freekey0/key1…, detected case-sensitively) keeping the JSON path'$.key'; the override reads the renamed name back by matching on path. - SQLite renames the
json_eachsubquery alias (detected case-insensitively, matching how EF uniquifies subquery aliases) and reads the actual alias back from the pushed-down subquery projection. - Added comprehensive relational specification tests (projection,
Take,Anyfilter withWITH,Keycasing,key+key0,key0alone, nested complexkey, owned collection) with SQL Server (nvarchar(max)andjson) and SQLite baselines.
Assessment: I traced the renaming loops (no infinite-loop risk; key/key+key0/key0-alone scenarios all produce correct names), verified the index column stays key so natural-ordering/ElementAt/FindJsonElement detection keeps working, and confirmed the [EntityFrameworkInternal] base member and the overrides are both excluded from the API baseline (no baseline update required). The base behavior is unchanged for non-overriding providers, so the change is backward-compatible. All 8 new virtual tests are overridden in all three provider classes, and the Cosmos suite derives from the non-relational base, so no override guard is impacted. I did not find concrete defects; the fix is well-targeted and comprehensively tested. The one residual gap is that baseline correctness depends on provider SQL generation that I verified by tracing but could not confirm by execution in this environment.
| File | Description |
|---|---|
src/EFCore.Relational/Query/RelationalQueryableMethodTranslatingExpressionVisitor.CreateSelect.cs |
Adds the GetJsonElementColumnName hook and routes the three JSON column-name sites through it. |
src/EFCore.SqlServer/Query/Internal/SqlServerQueryableMethodTranslatingExpressionVisitor.cs |
Renames a WITH column named key and overrides the hook to resolve the renamed name by JSON path. |
src/EFCore.Sqlite.Core/Query/Internal/SqliteQueryableMethodTranslatingExpressionVisitor.cs |
Refactors element collection into a list, computes a free alias for a key-named property, and overrides the hook to read the subquery alias. |
test/EFCore.Relational.Specification.Tests/Query/JsonQueryRelationalTestBase.cs |
Adds the shared #region 39438 regression tests (projection, filter, Take, nested complex, owned) with result assertions. |
test/EFCore.SqlServer.FunctionalTests/Query/JsonQuerySqlServerTest.cs |
Provider overrides with nvarchar(max) SQL baselines. |
test/EFCore.SqlServer.FunctionalTests/Query/JsonQueryJsonTypeSqlServerTest.cs |
Provider overrides with json-type SQL baselines. |
test/EFCore.Sqlite.FunctionalTests/Query/JsonQuerySqliteTest.cs |
Provider overrides with SQLite baselines, including the Take APPLY-not-supported case. |
Review effort: Balanced
There was a problem hiding this comment.
🔵 Needs a closer look
The change introduces a new internal JSON-collection SQL generation hook with broad provider-wide impact that was only verified by reading SQL baselines, so live-database test confirmation by a human is still needed.
No open issues 🎉
Review effort: Balanced
AndriySvyryd
left a comment
There was a problem hiding this comment.
Thanks for your contribution!
Fixes #39438
A JSON property named
keyin an element of a JSON collection collided with the index column of the JSON table-valued function (OPENJSON's[key]on SQL Server,json_each'skeyon SQLite), becauseCreateSelectnames the element columns after their JSON property names.CAST(JSON_VALUE([a].[value], '$.key') AS int)instead ofCAST([a].[key] AS int), failing with a conversion error.Keyin any casing, since subquery aliases are made unique case-insensitively.Fix
GetJsonElementColumnNamehook inCreateSelect. It returns the JSON name by default, so other providers are unaffected.keyto the first name amongkey0,key1… not used by another WITH column. The JSON path stays'$.key'.json_each, and reads the actual alias back.Design notes
[key]is a built-in column on SQL Server, and on SQLite thekeyname is matched in several places (ElementAttranslation, natural ordering detection, type mapping postprocessing).keycase-sensitively: a property namedKeyalready works there (column names are compared ordinally, and[key]is only referenced once the WITH clause is removed), so renaming it would change its SQL for nothing. SQLite detects it case-insensitively, because EF makes subquery aliases unique case-insensitively, which is what brokeKeythere.Tests (
JsonQueryRelationalTestBase, with SQL baselines for SQL Servernvarchar(max)andjsoncolumns, and SQLite):projection, and projection with
Take(SQLite: known APPLY limitation);filter with
Any, where the WITH clause is kept and shows the renamed column;Keyin capitals;keynext tokey0(keybecomeskey1), andkey0alone (kept);a nested complex property named
key;an owned collection (
OwnsMany().ToJson()) with a property namedkey.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
Tests for the changes have been added (for bug fixes / features)
Code follows the same patterns and style as existing code in this repo