Skip to content

Commit 034af1a

Browse files
fredericDelaportehazzik
authored andcommitted
NH-3963 - More explicit error on MappedAs invalid usage
1 parent c1db2d1 commit 034af1a

File tree

5 files changed

+72
-3
lines changed

5 files changed

+72
-3
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System;
2+
3+
namespace NHibernate.Test.NHSpecificTest.NH3963
4+
{
5+
class Entity
6+
{
7+
public virtual Guid Id { get; set; }
8+
public virtual string Name { get; set; }
9+
}
10+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System.Linq;
2+
using NHibernate.Linq;
3+
using NUnit.Framework;
4+
using NHibernate.Type;
5+
using System;
6+
7+
namespace NHibernate.Test.NHSpecificTest.NH3963
8+
{
9+
[TestFixture]
10+
public class MappedAsFixture : BugTestCase
11+
{
12+
[Test]
13+
public void ShouldThrowExplicitErrorOnInvalidUsage()
14+
{
15+
using (ISession session = OpenSession())
16+
using (session.BeginTransaction())
17+
{
18+
var result = session.Query<Entity>()
19+
.Where(e => e.Name.MappedAs(NHibernateUtil.AnsiString) == "Bob");
20+
21+
Assert.Throws<HibernateException>(() => { result.ToList(); });
22+
}
23+
}
24+
25+
[Test]
26+
public void ShouldThrowExplicitErrorOnUnsupportedTypeUsage()
27+
{
28+
using (ISession session = OpenSession())
29+
using (session.BeginTransaction())
30+
{
31+
var result = session.Query<Entity>()
32+
.Where(e => e.Name == "Bob".MappedAs(e.Id == Guid.Empty ? (IType)NHibernateUtil.AnsiString : NHibernateUtil.StringClob));
33+
34+
Assert.Throws<HibernateException>(() => { result.ToList(); });
35+
}
36+
}
37+
}
38+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.Test" namespace="NHibernate.Test.NHSpecificTest.NH3963">
3+
4+
<class name="Entity">
5+
<id name="Id" generator="guid.comb" />
6+
<property name="Name" />
7+
</class>
8+
9+
</hibernate-mapping>

src/NHibernate.Test/NHibernate.Test.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,8 @@
741741
<Compile Include="NHSpecificTest\NH3386\Fixture.cs" />
742742
<Compile Include="NHSpecificTest\NH3961\Entity.cs" />
743743
<Compile Include="NHSpecificTest\NH3961\DateParametersComparedTo.cs" />
744+
<Compile Include="NHSpecificTest\NH3963\Entity.cs" />
745+
<Compile Include="NHSpecificTest\NH3963\MappedAsFixture.cs" />
744746
<Compile Include="NHSpecificTest\NH3950\Entity.cs" />
745747
<Compile Include="NHSpecificTest\NH3950\Fixture.cs" />
746748
<Compile Include="NHSpecificTest\NH3952\Entity.cs" />
@@ -3214,6 +3216,7 @@
32143216
<ItemGroup>
32153217
<EmbeddedResource Include="NHSpecificTest\NH3386\Mappings.hbm.xml" />
32163218
<EmbeddedResource Include="NHSpecificTest\NH3961\Mappings.hbm.xml" />
3219+
<EmbeddedResource Include="NHSpecificTest\NH3963\Mappings.hbm.xml" />
32173220
<EmbeddedResource Include="NHSpecificTest\NH3950\Mappings.hbm.xml" />
32183221
<EmbeddedResource Include="NHSpecificTest\NH3952\Mappings.hbm.xml" />
32193222
<EmbeddedResource Include="NHSpecificTest\NH2204\Mappings.hbm.xml" />

src/NHibernate/Linq/Visitors/ExpressionParameterVisitor.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,19 @@ internal static IDictionary<ConstantExpression, NamedParameter> Visit(ref Expres
4747

4848
protected override Expression VisitMethodCallExpression(MethodCallExpression expression)
4949
{
50-
if (expression.Method.Name == "MappedAs" && expression.Method.DeclaringType == typeof(LinqExtensionMethods))
50+
if (expression.Method.Name == nameof(LinqExtensionMethods.MappedAs) && expression.Method.DeclaringType == typeof(LinqExtensionMethods))
5151
{
52-
var parameter = (ConstantExpression)VisitExpression(expression.Arguments[0]);
53-
var type = (ConstantExpression)expression.Arguments[1];
52+
var rawParameter = VisitExpression(expression.Arguments[0]);
53+
var parameter = rawParameter as ConstantExpression;
54+
var type = expression.Arguments[1] as ConstantExpression;
55+
if (parameter == null)
56+
throw new HibernateException(
57+
$"{nameof(LinqExtensionMethods.MappedAs)} must be called on an expression which can be evaluated as " +
58+
$"{nameof(ConstantExpression)}. It was call on {rawParameter?.GetType().Name ?? "null"} instead.");
59+
if (type == null)
60+
throw new HibernateException(
61+
$"{nameof(LinqExtensionMethods.MappedAs)} type must be supplied as {nameof(ConstantExpression)}. " +
62+
$"It was {expression.Arguments[1]?.GetType().Name ?? "null"} instead.");
5463

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

0 commit comments

Comments
 (0)