Skip to content

Fix NRE when having subselect and nullable value selection for Linq query provider #2428

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
Jul 14, 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
33 changes: 33 additions & 0 deletions src/NHibernate.Test/Async/Linq/SelectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,39 @@ public async Task CanSelectConditionalEntityValueWithEntityComparisonAsync()
Assert.That(fatherInsteadOfChild, Has.Exactly(2).EqualTo("5678"));
}

[Test]
public async Task CanExecuteMethodWithNullObjectAndSubselectAsync()
{
var list1 = await (db.Animals.Select(
a => new
{
NullableId = (int?) a.Father.Father.Id,
})
.ToListAsync());
Assert.That(list1, Has.Count.GreaterThan(0));
Assert.That(list1[0].NullableId, Is.Null);

var list2 = await (db.Animals.Select(
a => new
{
Descriptions = a.Children.Select(z => z.Description)
})
.ToListAsync());
Assert.That(list2, Has.Count.GreaterThan(0));
Assert.That(list2[0].Descriptions, Is.Not.Null);

var list3 = await (db.Animals.Select(
a => new
{
NullableId = (int?) a.Father.Father.Id,
Descriptions = a.Children.Select(z => z.Description)
})
.ToListAsync());
Assert.That(list3, Has.Count.GreaterThan(0));
Assert.That(list3[0].NullableId, Is.Null);
Assert.That(list3[0].Descriptions, Is.Not.Null);
}

[Test]
public async Task CanSelectConditionalEntityValueWithEntityComparisonRepeatAsync()
{
Expand Down
33 changes: 33 additions & 0 deletions src/NHibernate.Test/Linq/SelectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,39 @@ public void CanSelectConditionalEntityValueWithEntityComparison()
Assert.That(fatherInsteadOfChild, Has.Exactly(2).EqualTo("5678"));
}

[Test]
public void CanExecuteMethodWithNullObjectAndSubselect()
{
var list1 = db.Animals.Select(
a => new
{
NullableId = (int?) a.Father.Father.Id,
})
.ToList();
Assert.That(list1, Has.Count.GreaterThan(0));
Assert.That(list1[0].NullableId, Is.Null);

var list2 = db.Animals.Select(
a => new
{
Descriptions = a.Children.Select(z => z.Description)
})
.ToList();
Assert.That(list2, Has.Count.GreaterThan(0));
Assert.That(list2[0].Descriptions, Is.Not.Null);

var list3 = db.Animals.Select(
a => new
{
NullableId = (int?) a.Father.Father.Id,
Descriptions = a.Children.Select(z => z.Description)
})
.ToList();
Assert.That(list3, Has.Count.GreaterThan(0));
Assert.That(list3[0].NullableId, Is.Null);
Assert.That(list3[0].Descriptions, Is.Not.Null);
}

[Test]
public void CanSelectConditionalEntityValueWithEntityComparisonRepeat()
{
Expand Down
18 changes: 17 additions & 1 deletion src/NHibernate/Linq/NestedSelects/SelectClauseRewriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ public override Expression Visit(Expression expression)
return base.Visit(expression);
}

protected override Expression VisitUnary(UnaryExpression node)
{
if (node.NodeType == ExpressionType.Convert &&
(node.Operand is MemberExpression || node.Operand is QuerySourceReferenceExpression))
{
return AddAndConvertExpression(node.Operand, node.Type);
}

return base.VisitUnary(node);
}

protected override Expression VisitMember(MemberExpression expression)
{
return AddAndConvertExpression(expression);
Expand All @@ -49,14 +60,19 @@ protected override Expression VisitQuerySourceReference(QuerySourceReferenceExpr
}

private Expression AddAndConvertExpression(Expression expression)
{
return AddAndConvertExpression(expression, expression.Type);
}

private Expression AddAndConvertExpression(Expression expression, System.Type type)
{
expressions.Add(new ExpressionHolder { Expression = expression, Tuple = tuple });

return Expression.Convert(
Expression.ArrayIndex(
Expression.Property(parameter, Tuple.ItemsProperty),
Expression.Constant(expressions.Count - 1)),
expression.Type);
type);
}
}
}