Skip to content

Commit

Permalink
fix display classes in resolver lambdas
Browse files Browse the repository at this point in the history
  • Loading branch information
maumar committed Apr 7, 2024
1 parent ed221aa commit 35fe741
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics.CodeAnalysis;
using System.Text.Encodings.Web;
using System.Text.Json;
using Microsoft.EntityFrameworkCore.ChangeTracking.Internal;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
Expand Down Expand Up @@ -1623,6 +1624,9 @@ private sealed class JsonEntityMaterializerRewriter : ExpressionVisitor
private static readonly PropertyInfo JsonEncodedTextEncodedUtf8BytesProperty
= typeof(JsonEncodedText).GetProperty(nameof(JsonEncodedText.EncodedUtf8Bytes))!;

private static readonly MethodInfo JsonEncodedTextEncodeMethod
= typeof(JsonEncodedText).GetMethod(nameof(JsonEncodedText.Encode), [typeof(string), typeof(JavaScriptEncoder)])!;

// keep track which variable corresponds to which navigation - we need that info for fixup
// which happens at the end (after we read everything to guarantee that we can instantiate the entity
private readonly Dictionary<string, ParameterExpression> _navigationVariableMap = new();
Expand Down Expand Up @@ -1870,6 +1874,7 @@ void ProcessFixup(IDictionary<string, LambdaExpression> fixupMap)
foreach (var valueBufferTryReadValueMethodToProcess in valueBufferTryReadValueMethodsToProcess)
{
var property = valueBufferTryReadValueMethodToProcess.Arguments[2].GetConstantValue<IProperty>();
var jsonPropertyName = property.GetJsonPropertyName()!;
testExpressions.Add(
Call(
Field(
Expand All @@ -1878,9 +1883,16 @@ void ProcessFixup(IDictionary<string, LambdaExpression> fixupMap)
Utf8JsonReaderValueTextEqualsMethod,
Property(
_liftableConstantFactory.CreateLiftableConstant(
JsonEncodedText.Encode(property.GetJsonPropertyName()!),
_ => JsonEncodedText.Encode(property.GetJsonPropertyName()!, null),
property.Name + "EncodedProperty",
JsonEncodedText.Encode(jsonPropertyName),
Lambda<Func<MaterializerLiftableConstantContext, object>>(
Convert(
Call(
JsonEncodedTextEncodeMethod,
Constant(jsonPropertyName),
Default(typeof(JavaScriptEncoder))),
typeof(object)),
Parameter(typeof(MaterializerLiftableConstantContext), "_")),
jsonPropertyName + "EncodedProperty",
typeof(JsonEncodedText)),
JsonEncodedTextEncodedUtf8BytesProperty)));

Expand Down Expand Up @@ -1916,8 +1928,15 @@ void ProcessFixup(IDictionary<string, LambdaExpression> fixupMap)
Utf8JsonReaderValueTextEqualsMethod,
Property(
_liftableConstantFactory.CreateLiftableConstant(
JsonEncodedText.Encode(innerShaperMapElement.Key),
_ => JsonEncodedText.Encode(innerShaperMapElement.Key, null),
JsonEncodedText.Encode(innerShaperMapElementKey),
Lambda<Func<MaterializerLiftableConstantContext, object>>(
Convert(
Call(
JsonEncodedTextEncodeMethod,
Constant(innerShaperMapElementKey),
Default(typeof(JavaScriptEncoder))),
typeof(object)),
Parameter(typeof(MaterializerLiftableConstantContext), "_")),
innerShaperMapElementKey + "EncodedNavigation",
typeof(JsonEncodedText)),
JsonEncodedTextEncodedUtf8BytesProperty)));
Expand Down
13 changes: 10 additions & 3 deletions src/EFCore/Query/LiftableConstantProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,11 +266,18 @@ public void Check(Expression expression)
}

protected override Expression VisitConstant(ConstantExpression node)
=> LiftableConstantExpressionHelpers.IsLiteral(node.Value)
? node
: throw new InvalidOperationException(
{
if (LiftableConstantExpressionHelpers.IsLiteral(node.Value))
{
return node;
}
else
{
throw new InvalidOperationException(
$"Shaper expression contains a non-literal constant of type '{node.Value!.GetType().Name}'. " +
$"Use a {nameof(LiftableConstantExpression)} to reference any non-literal constants.");
}
}
}

private sealed class LiftedConstantOptimizer : ExpressionVisitor
Expand Down

0 comments on commit 35fe741

Please # to comment.