Skip to content

Commit 210a2a7

Browse files
fredericDelaportePleasantDbahusoid
authored
Fix NRE in linq processing of custom components (nhibernate#2941)
fix nhibernate#2937 Co-authored-by: Duncan M <[email protected]> Co-authored-by: Roman Artiukhin <[email protected]>
1 parent 2452db8 commit 210a2a7

File tree

3 files changed

+34
-9
lines changed

3 files changed

+34
-9
lines changed

src/NHibernate.Test/Async/Hql/EntityJoinHqlTest.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -346,9 +346,9 @@ public async Task NullableEntityProjectionAsync()
346346
var withNullManyToOneList = await (session.Query<NullableOwner>().Where(x => x.ManyToOne == null).ToListAsync());
347347
var withNullManyToOneJoinedList =
348348
await ((from x in session.Query<NullableOwner>()
349-
from x2 in session.Query<NullableOwner>()
350-
where x == x2 && x.ManyToOne == null && x.OneToOne.Name == null
351-
select x2).ToListAsync());
349+
from x2 in session.Query<NullableOwner>()
350+
where x == x2 && x.ManyToOne == null && x.OneToOne.Name == null
351+
select x2).ToListAsync());
352352
Assert.That(fullList.Count, Is.EqualTo(2));
353353
Assert.That(withValidManyToOneList.Count, Is.EqualTo(0));
354354
Assert.That(withValidManyToOneList2.Count, Is.EqualTo(0));

src/NHibernate.Test/Linq/TryGetMappedTests.cs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,19 @@ public void NestedComponentPropertyCastTest()
272272
o => o?.Name == "component[OtherProperty1]");
273273
}
274274

275+
[Test(Description = "GH-2937")]
276+
public void CompositeUserTypePropertyTest()
277+
{
278+
var query = session.Query<Glarch>().Select(o => o.Multiple.count);
279+
AssertSupported(
280+
query,
281+
typeof(Glarch).FullName,
282+
"Multiple.count",
283+
o => o is Int32Type,
284+
o => o?.Name == typeof(MultiplicityType).FullName,
285+
null);
286+
}
287+
275288
[Test]
276289
public void ManyToOneTest()
277290
{
@@ -723,9 +736,10 @@ private void AssertSupported(
723736
string expectedEntityName,
724737
string expectedMemberPath,
725738
Predicate<IType> expectedMemberType,
726-
Predicate<IAbstractComponentType> expectedComponentType = null)
739+
Predicate<IAbstractComponentType> expectedComponentType = null,
740+
bool? nullability = true)
727741
{
728-
AssertResult(query, true, true, expectedEntityName, expectedMemberPath, expectedMemberType, expectedComponentType);
742+
AssertResult(query, true, true, expectedEntityName, expectedMemberPath, expectedMemberType, expectedComponentType, nullability);
729743
}
730744

731745
private void AssertSupported(
@@ -768,7 +782,7 @@ private void AssertResult(
768782
string expectedMemberPath,
769783
Predicate<IType> expectedMemberType,
770784
Predicate<IAbstractComponentType> expectedComponentType = null,
771-
bool nullability = true)
785+
bool? nullability = true)
772786
{
773787
expectedComponentType = expectedComponentType ?? (o => o == null);
774788

@@ -809,8 +823,12 @@ private void AssertResult(
809823

810824
if (found)
811825
{
812-
Assert.That(_tryGetMappedNullability(Sfi, queryModel.SelectClause.Selector, out var isNullable), Is.True, "Expression should be supported");
813-
Assert.That(nullability, Is.EqualTo(isNullable), "Nullability is not correct");
826+
Assert.That(
827+
_tryGetMappedNullability(Sfi, queryModel.SelectClause.Selector, out var isNullable),
828+
Is.EqualTo(nullability.HasValue),
829+
$"Expression should be {(nullability.HasValue ? "supported" : "unsupported")}");
830+
if (nullability.HasValue)
831+
Assert.That(nullability, Is.EqualTo(isNullable), "Nullability is not correct");
814832
}
815833
}
816834
}

src/NHibernate/Util/ExpressionsHelper.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,17 @@ internal static bool TryGetMappedNullability(
160160
int index;
161161
if (componentType != null)
162162
{
163+
var propertyNullability = componentType.PropertyNullability;
164+
if (propertyNullability == null)
165+
{
166+
nullable = false;
167+
return false;
168+
}
169+
163170
index = Array.IndexOf(
164171
componentType.PropertyNames,
165172
memberPath.Substring(memberPath.LastIndexOf('.') + 1));
166-
nullable = componentType.PropertyNullability[index];
173+
nullable = propertyNullability[index];
167174
return true;
168175
}
169176

0 commit comments

Comments
 (0)