Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[release/3.1] Check if parameter.Name is not null before comparing it with query pa… #20521

Merged
merged 1 commit into from
Apr 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -609,12 +609,25 @@ protected override Expression VisitExtension(Expression extensionExpression)

protected override Expression VisitParameter(ParameterExpression parameterExpression)
{
if (parameterExpression.Name.StartsWith(CompiledQueryParameterPrefix, StringComparison.Ordinal))
if (AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue20485", out var enabled) && enabled)
{
return Expression.Call(
_getParameterValueMethodInfo.MakeGenericMethod(parameterExpression.Type),
QueryCompilationContext.QueryContextParameter,
Expression.Constant(parameterExpression.Name));
if (parameterExpression.Name.StartsWith(CompiledQueryParameterPrefix, StringComparison.Ordinal))
{
return Expression.Call(
_getParameterValueMethodInfo.MakeGenericMethod(parameterExpression.Type),
QueryCompilationContext.QueryContextParameter,
Expression.Constant(parameterExpression.Name));
}
}
else
{
if (parameterExpression.Name?.StartsWith(CompiledQueryParameterPrefix, StringComparison.Ordinal) == true)
{
return Expression.Call(
_getParameterValueMethodInfo.MakeGenericMethod(parameterExpression.Type),
QueryCompilationContext.QueryContextParameter,
Expression.Constant(parameterExpression.Name));
}
}

throw new InvalidOperationException(CoreStrings.TranslationFailed(parameterExpression.Print()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -429,8 +429,26 @@ private Expression VisitContainsMethodCall(MethodCallExpression methodCallExpres

rewrittenSource = Expression.Constant(keyList, keyListType);
}
else if (AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue20485", out var enabled) && enabled
&& newSource is ParameterExpression listParam2
&& listParam2.Name.StartsWith(CompiledQueryCache.CompiledQueryParameterPrefix, StringComparison.Ordinal))
{
// The source list is a parameter. Add a runtime parameter that will contain a list of the extracted keys for each execution.
var lambda = Expression.Lambda(
Expression.Call(
_parameterListValueExtractor.MakeGenericMethod(entityType.ClrType, keyProperty.ClrType.MakeNullable()),
QueryCompilationContext.QueryContextParameter,
Expression.Constant(listParam2.Name, typeof(string)),
Expression.Constant(keyProperty, typeof(IProperty))),
QueryCompilationContext.QueryContextParameter
);

var newParameterName =
$"{RuntimeParameterPrefix}{listParam2.Name.Substring(CompiledQueryCache.CompiledQueryParameterPrefix.Length)}_{keyProperty.Name}";
rewrittenSource = _queryCompilationContext.RegisterRuntimeParameter(newParameterName, lambda);
}
else if (newSource is ParameterExpression listParam
&& listParam.Name.StartsWith(CompiledQueryCache.CompiledQueryParameterPrefix, StringComparison.Ordinal))
&& listParam.Name?.StartsWith(CompiledQueryCache.CompiledQueryParameterPrefix, StringComparison.Ordinal) == true)
{
// The source list is a parameter. Add a runtime parameter that will contain a list of the extracted keys for each execution.
var lambda = Expression.Lambda(
Expand Down Expand Up @@ -935,24 +953,49 @@ private Expression CreatePropertyAccessExpression(Expression target, IProperty p
return Expression.Constant(property.GetGetter().GetClrValue(value), property.ClrType.MakeNullable());
}

// If the target is a query parameter, we can't simply add a property access over it, but must instead cause a new
// parameter to be added at runtime, with the value of the property on the base parameter.
if (target is ParameterExpression baseParameterExpression
if (AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue20485", out var enabled) && enabled)
{
// If the target is a query parameter, we can't simply add a property access over it, but must instead cause a new
// parameter to be added at runtime, with the value of the property on the base parameter.
if (target is ParameterExpression baseParameterExpression
&& baseParameterExpression.Name.StartsWith(CompiledQueryCache.CompiledQueryParameterPrefix, StringComparison.Ordinal))
{
// Generate an expression to get the base parameter from the query context's parameter list, and extract the
// property from that
var lambda = Expression.Lambda(
Expression.Call(
_parameterValueExtractor.MakeGenericMethod(property.ClrType.MakeNullable()),
QueryCompilationContext.QueryContextParameter,
Expression.Constant(baseParameterExpression.Name, typeof(string)),
Expression.Constant(property, typeof(IProperty))),
QueryCompilationContext.QueryContextParameter);

var newParameterName =
$"{RuntimeParameterPrefix}{baseParameterExpression.Name.Substring(CompiledQueryCache.CompiledQueryParameterPrefix.Length)}_{property.Name}";
return _queryCompilationContext.RegisterRuntimeParameter(newParameterName, lambda);
}
}
else
{
// Generate an expression to get the base parameter from the query context's parameter list, and extract the
// property from that
var lambda = Expression.Lambda(
Expression.Call(
_parameterValueExtractor.MakeGenericMethod(property.ClrType.MakeNullable()),
QueryCompilationContext.QueryContextParameter,
Expression.Constant(baseParameterExpression.Name, typeof(string)),
Expression.Constant(property, typeof(IProperty))),
QueryCompilationContext.QueryContextParameter);

var newParameterName =
$"{RuntimeParameterPrefix}{baseParameterExpression.Name.Substring(CompiledQueryCache.CompiledQueryParameterPrefix.Length)}_{property.Name}";
return _queryCompilationContext.RegisterRuntimeParameter(newParameterName, lambda);
// If the target is a query parameter, we can't simply add a property access over it, but must instead cause a new
// parameter to be added at runtime, with the value of the property on the base parameter.
if (target is ParameterExpression baseParameterExpression
&& baseParameterExpression.Name?.StartsWith(CompiledQueryCache.CompiledQueryParameterPrefix, StringComparison.Ordinal) == true)
{
// Generate an expression to get the base parameter from the query context's parameter list, and extract the
// property from that
var lambda = Expression.Lambda(
Expression.Call(
_parameterValueExtractor.MakeGenericMethod(property.ClrType.MakeNullable()),
QueryCompilationContext.QueryContextParameter,
Expression.Constant(baseParameterExpression.Name, typeof(string)),
Expression.Constant(property, typeof(IProperty))),
QueryCompilationContext.QueryContextParameter);

var newParameterName =
$"{RuntimeParameterPrefix}{baseParameterExpression.Name.Substring(CompiledQueryCache.CompiledQueryParameterPrefix.Length)}_{property.Name}";
return _queryCompilationContext.RegisterRuntimeParameter(newParameterName, lambda);
}
}

return target.CreateEFPropertyExpression(property);
Expand Down Expand Up @@ -1036,7 +1079,8 @@ protected struct EntityOrDtoType
public static EntityOrDtoType FromEntityReferenceExpression(EntityReferenceExpression ere)
=> new EntityOrDtoType
{
EntityType = ere.IsEntityType ? ere.EntityType : null, DtoType = ere.IsDtoType ? ere.DtoType : null
EntityType = ere.IsEntityType ? ere.EntityType : null,
DtoType = ere.IsDtoType ? ere.DtoType : null
};

public static EntityOrDtoType FromDtoType(Dictionary<string, EntityOrDtoType> dtoType)
Expand Down
24 changes: 24 additions & 0 deletions test/EFCore.Specification.Tests/Query/SimpleQueryTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.EntityFrameworkCore.Query.Internal;
using Microsoft.EntityFrameworkCore.TestModels.Northwind;
using Microsoft.EntityFrameworkCore.TestUtilities;
using Xunit;
Expand Down Expand Up @@ -5630,6 +5631,29 @@ public virtual Task AsQueryable_in_query_server_evals(bool isAsync)
elementAsserter: (e, a) => AssertCollection(e, a, ordered: true));
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual async Task Null_parameter_name_works(bool isAsync)
{
using var context = CreateContext();
var customerDbSet = context.Set<Customer>().AsQueryable();

var parameter = Expression.Parameter(typeof(Customer));
var body = Expression.Equal(parameter, Expression.Default(typeof(Customer)));
var queryExpression = Expression.Call(
QueryableMethods.Where.MakeGenericMethod(typeof(Customer)),
customerDbSet.Expression,
Expression.Quote(Expression.Lambda(body, parameter)));

var query = ((IAsyncQueryProvider)customerDbSet.Provider).CreateQuery<Customer>(queryExpression);

var result = isAsync
? (await query.ToListAsync())
: query.ToList();

Assert.Empty(result);
}

protected async Task AssertTranslationFailed(Func<Task> testCode)
{
Assert.Contains(
Expand Down