@@ -17,6 +17,7 @@ namespace Microsoft.EntityFrameworkCore.SqlServer.Query.Internal;
1717/// * When the column type in the WITH clause is a SQL Server "CLR type" - these are incompatible with WITH (e.g. hierarchy id).
1818/// 2. Rewrite JsonScalarExpression (JSON_VALUE()) to OPENJSON for when JSON_VALUE() isn't compatible with the type (e.g. binary data
1919/// which needs to be base64-decoded).
20+ /// 3. Restore native json inputs to JSON operations when OPENJSON's WITH clause only supports nvarchar(max).
2021/// </summary>
2122/// <remarks>
2223/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
@@ -30,10 +31,15 @@ public sealed class SqlServerJsonPostprocessor(
3031 SqlAliasManager ? sqlAliasManager )
3132 : ExpressionVisitor
3233{
34+ private static readonly bool UseOldBehavior39277 =
35+ AppContext . TryGetSwitch ( "Microsoft.EntityFrameworkCore.Issue39277" , out var enabled ) && enabled ;
36+
3337 private readonly List < OuterApplyExpression > _openjsonOuterAppliesToAdd = [ ] ;
3438 private readonly Dictionary < ( string , string ) , ColumnInfo > _columnsToRewrite = [ ] ;
39+ private readonly HashSet < ( string , string ) > _jsonColumnsToCast = [ ] ;
3540
3641 private RelationalTypeMapping ? _nvarcharMaxTypeMapping ;
42+ private bool _supportsJsonTypeInOpenJson ;
3743
3844 /// <summary>
3945 /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
@@ -42,8 +48,19 @@ public sealed class SqlServerJsonPostprocessor(
4248 /// doing so can result in application failures when updating to a new Entity Framework Core release.
4349 /// </summary>
4450 public Expression Process ( Expression expression )
51+ => Process ( expression , supportsJsonTypeInOpenJson : true ) ;
52+
53+ /// <summary>
54+ /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
55+ /// the same compatibility standards as public APIs. It may be changed or removed without notice in
56+ /// any release. You should only use it directly in your code with extreme caution and knowing that
57+ /// doing so can result in application failures when updating to a new Entity Framework Core release.
58+ /// </summary>
59+ internal Expression Process ( Expression expression , bool supportsJsonTypeInOpenJson )
4560 {
4661 _columnsToRewrite . Clear ( ) ;
62+ _jsonColumnsToCast . Clear ( ) ;
63+ _supportsJsonTypeInOpenJson = supportsJsonTypeInOpenJson ;
4764
4865 return Visit ( expression ) ;
4966 }
@@ -74,6 +91,19 @@ protected override Expression VisitExtension(Expression expression)
7491 {
7592 var table = selectExpression . Tables [ i ] ;
7693
94+ if ( ! UseOldBehavior39277
95+ && ! _supportsJsonTypeInOpenJson
96+ && table . UnwrapJoin ( ) is SqlServerOpenJsonExpression { ColumnInfos : { } jsonColumnInfos } jsonTable )
97+ {
98+ foreach ( var columnInfo in jsonColumnInfos )
99+ {
100+ if ( columnInfo is { AsJson : true , TypeMapping . StoreType : "json" } )
101+ {
102+ _jsonColumnsToCast . Add ( ( jsonTable . Alias , columnInfo . Name ) ) ;
103+ }
104+ }
105+ }
106+
77107 if ( table . UnwrapJoin ( ) is SqlServerOpenJsonExpression { ColumnInfos : { } columnInfos } openJsonExpression
78108 // Condition 1: an ordering/projection still refers to the OPENJSON's [key] column - it needs to be preserved.
79109 && ( selectExpression . Orderings . Select ( o => o . Expression )
@@ -172,6 +202,19 @@ protected override Expression VisitExtension(Expression expression)
172202 _openjsonOuterAppliesToAdd . Clear ( ) ;
173203 }
174204
205+ if ( ! _supportsJsonTypeInOpenJson
206+ && result . Alias is { } selectAlias )
207+ {
208+ foreach ( var projection in result . Projection )
209+ {
210+ if ( projection . Expression is ColumnExpression column
211+ && _jsonColumnsToCast . Contains ( ( column . TableAlias , column . Name ) ) )
212+ {
213+ _jsonColumnsToCast . Add ( ( selectAlias , projection . Alias ) ) ;
214+ }
215+ }
216+ }
217+
175218 return result ;
176219 }
177220
@@ -252,6 +295,23 @@ when _columnsToRewrite.TryGetValue((columnExpression.TableAlias, columnExpressio
252295 jsonScalar . IsNullable ) ;
253296 }
254297
298+ case JsonScalarExpression
299+ {
300+ Json : ColumnExpression { TypeMapping : { StoreType : "json" } jsonTypeMapping } columnExpression ,
301+ Path . Count : > 0
302+ } jsonScalar
303+ when jsonScalar . TypeMapping is not SqlServerStructuralJsonTypeMapping
304+ && jsonScalar . TypeMapping ? . ElementTypeMapping is null
305+ && _jsonColumnsToCast . Contains ( ( columnExpression . TableAlias , columnExpression . Name ) ) :
306+ {
307+ var visited = ( JsonScalarExpression ) base . Visit ( jsonScalar ) ;
308+
309+ return visited . Update ( sqlExpressionFactory . Convert (
310+ columnExpression . ApplyTypeMapping ( jsonTypeMapping . WithStoreTypeAndSize ( "nvarchar(max)" , null ) ) ,
311+ columnExpression . Type ,
312+ jsonTypeMapping ) ) ;
313+ }
314+
255315 // The SQL Server json type cannot be compared ("The JSON data type cannot be compared or sorted, except when using the
256316 // IS NULL operator"). So we find comparisons that involve the json type, and apply a conversion to string (nvarchar(max))
257317 // to both sides. We exempt this when one of the sides is a constant null (not required).
0 commit comments