Skip to content

Commit 262a3b2

Browse files
Fix Linq failure on null parameter value
HQL is ill equipped for guessing types of null parameters. We should keep doing that on Linq side. Fix #2833
1 parent 13e0f10 commit 262a3b2

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

src/NHibernate.Test/Async/Linq/LinqQuerySamples.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using System.Linq;
1515
using NHibernate.DomainModel.Northwind.Entities;
1616
using NSubstitute;
17+
using NSubstitute.ExceptionExtensions;
1718
using NUnit.Framework;
1819
using NHibernate.Linq;
1920

@@ -1370,5 +1371,19 @@ from s2 in sup2.DefaultIfEmpty()
13701371
Assert.That(GetTotalOccurrences(sql, "left outer join"), Is.EqualTo(2));
13711372
}
13721373
}
1374+
1375+
[Test]
1376+
public void ReplaceFunctionWithNullArgumentAsync()
1377+
{
1378+
var query = from e in db.Employees
1379+
select e.FirstName.Replace(e.LastName, null);
1380+
List<string> results = null;
1381+
Assert.That(
1382+
async () =>
1383+
{
1384+
results = await (query.ToListAsync());
1385+
}, Throws.Nothing, "Expected REPLACE(FirstName, LastName, NULL) to be supported");
1386+
Assert.That(results, Is.Not.Null);
1387+
}
13731388
}
13741389
}

src/NHibernate.Test/Linq/LinqQuerySamples.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Linq;
55
using NHibernate.DomainModel.Northwind.Entities;
66
using NSubstitute;
7+
using NSubstitute.ExceptionExtensions;
78
using NUnit.Framework;
89

910
namespace NHibernate.Test.Linq
@@ -1955,6 +1956,20 @@ public void DLinq2C()
19551956

19561957
Assert.That(!q.Any(orderid => withNullShippingDate.Contains(orderid)));
19571958
}
1959+
1960+
[Test]
1961+
public void ReplaceFunctionWithNullArgument()
1962+
{
1963+
var query = from e in db.Employees
1964+
select e.FirstName.Replace(e.LastName, null);
1965+
List<string> results = null;
1966+
Assert.That(
1967+
() =>
1968+
{
1969+
results = query.ToList();
1970+
}, Throws.Nothing, "Expected REPLACE(FirstName, LastName, NULL) to be supported");
1971+
Assert.That(results, Is.Not.Null);
1972+
}
19581973
}
19591974

19601975
public class ParentChildBatch<T, TKey, TSub>

src/NHibernate/Linq/Visitors/ParameterTypeLocator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ private static IType GetParameterType(
156156
return candidateType;
157157
}
158158

159-
if (visitor.NotGuessableConstants.Contains(constantExpression))
159+
if (visitor.NotGuessableConstants.Contains(constantExpression) && constantExpression.Value != null)
160160
{
161161
return null;
162162
}

0 commit comments

Comments
 (0)