Skip to content

Fix MappedAs when called on an UnaryExpression #2711

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

Merged
merged 1 commit into from
Mar 31, 2021
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
52 changes: 52 additions & 0 deletions src/NHibernate.Test/Async/Linq/ByMethod/MappedAsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by AsyncGenerator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate.Linq;
using NUnit.Framework;

namespace NHibernate.Test.Linq.ByMethod
{
using System.Threading.Tasks;
[TestFixture]
public class MappedAsTestsAsync : LinqTestCase
{
[Test]
public async Task WithUnaryExpressionAsync()
{
var num = 1;
await (db.Orders.Where(o => o.Freight == (-num).MappedAs(NHibernateUtil.Decimal)).ToListAsync());
await (db.Orders.Where(o => o.Freight == ((decimal) num).MappedAs(NHibernateUtil.Decimal)).ToListAsync());
await (db.Orders.Where(o => o.Freight == ((decimal?) (decimal) num).MappedAs(NHibernateUtil.Decimal)).ToListAsync());
}

[Test]
public async Task WithNewExpressionAsync()
{
var num = 1;
await (db.Orders.Where(o => o.Freight == new decimal(num).MappedAs(NHibernateUtil.Decimal)).ToListAsync());
}

[Test]
public async Task WithMethodCallExpressionAsync()
{
var num = 1;
await (db.Orders.Where(o => o.Freight == GetDecimal(num).MappedAs(NHibernateUtil.Decimal)).ToListAsync());
}

private decimal GetDecimal(int number)
{
return number;
}
}
}
41 changes: 41 additions & 0 deletions src/NHibernate.Test/Linq/ByMethod/MappedAsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate.Linq;
using NUnit.Framework;

namespace NHibernate.Test.Linq.ByMethod
{
[TestFixture]
public class MappedAsTests : LinqTestCase
{
[Test]
public void WithUnaryExpression()
{
var num = 1;
db.Orders.Where(o => o.Freight == (-num).MappedAs(NHibernateUtil.Decimal)).ToList();
db.Orders.Where(o => o.Freight == ((decimal) num).MappedAs(NHibernateUtil.Decimal)).ToList();
db.Orders.Where(o => o.Freight == ((decimal?) (decimal) num).MappedAs(NHibernateUtil.Decimal)).ToList();
}

[Test]
public void WithNewExpression()
{
var num = 1;
db.Orders.Where(o => o.Freight == new decimal(num).MappedAs(NHibernateUtil.Decimal)).ToList();
}

[Test]
public void WithMethodCallExpression()
{
var num = 1;
db.Orders.Where(o => o.Freight == GetDecimal(num).MappedAs(NHibernateUtil.Decimal)).ToList();
}

private decimal GetDecimal(int number)
{
return number;
}
}
}
4 changes: 2 additions & 2 deletions src/NHibernate/Linq/Visitors/ExpressionParameterVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ protected override Expression VisitMethodCall(MethodCallExpression expression)
{
var rawParameter = Visit(expression.Arguments[0]);
// TODO 6.0: Remove below code and return expression as this logic is now inside ConstantTypeLocator
var parameter = rawParameter as ConstantExpression;
var parameter = ParameterTypeLocator.UnwrapUnary(rawParameter) as ConstantExpression;
var type = expression.Arguments[1] as ConstantExpression;
if (parameter == null)
throw new HibernateException(
Expand All @@ -83,7 +83,7 @@ protected override Expression VisitMethodCall(MethodCallExpression expression)

_parameters[parameter].Type = (IType)type.Value;

return parameter;
return rawParameter;
}

var method = expression.Method.IsGenericMethod
Expand Down
4 changes: 2 additions & 2 deletions src/NHibernate/Linq/Visitors/ParameterTypeLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ protected override Expression VisitMethodCall(MethodCallExpression node)
if (VisitorUtil.IsMappedAs(node.Method))
{
var rawParameter = Visit(node.Arguments[0]);
var parameter = rawParameter as ConstantExpression;
var parameter = UnwrapUnary(rawParameter) as ConstantExpression;
var type = node.Arguments[1] as ConstantExpression;
if (parameter == null)
throw new HibernateException(
Expand Down Expand Up @@ -405,7 +405,7 @@ private bool IsDynamicMember(Expression expression)
/// </summary>
/// <param name="expression">The expression to unwrap.</param>
/// <returns>The unwrapped expression.</returns>
private static Expression UnwrapUnary(Expression expression)
internal static Expression UnwrapUnary(Expression expression)
{
while (expression is UnaryExpression unaryExpression)
{
Expand Down