Skip to content
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

Fix LINQ projection of nullable enum with list #2703

Merged
merged 2 commits into from
Mar 20, 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
36 changes: 36 additions & 0 deletions src/NHibernate.Test/Async/Linq/EnumTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@


using System;
using System.Collections.Generic;
using System.Linq;
using NHibernate.Cfg.MappingSchema;
using NHibernate.Mapping.ByCode;
Expand Down Expand Up @@ -50,9 +51,24 @@ protected override HbmMapping GetMappings()
m.Type(_enumType);
m.Formula($"(case when Enum1 = {_unspecifiedValue} then null else Enum1 end)");
});
rc.Bag(x => x.Children, m =>
{
m.Cascade(Mapping.ByCode.Cascade.All);
m.Inverse(true);
},
a => a.OneToMany()
);
rc.ManyToOne(x => x.Other, m => m.Cascade(Mapping.ByCode.Cascade.All));
});

mapper.Class<EnumEntityChild>(
rc =>
{
rc.Table("EnumEntityChild");
rc.Id(x => x.Id, m => m.Generator(Generators.Guid));
rc.Property(x => x.Name);
});

return mapper.CompileMappingForAllExplicitlyAddedEntities();
}

Expand Down Expand Up @@ -184,5 +200,25 @@ public async Task CanQueryComplexExpressionOnTestEnumAsync()
Assert.That(query.Count, Is.EqualTo(0));
}
}

[Test]
public async Task CanProjectWithListTransformationAsync()
{
using (var session = OpenSession())
using (var trans = session.BeginTransaction())
{
var entities = session.Query<EnumEntity>();

var query = await (entities.Select(user => new
{
user.Name,
simple = user.Enum1,
children = user.Children,
nullableEnum1IsLarge = user.NullableEnum1 == TestEnum.Large
}).ToListAsync());

Assert.That(query.Count, Is.EqualTo(10));
}
}
}
}
46 changes: 46 additions & 0 deletions src/NHibernate.Test/Linq/EnumTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using NHibernate.Cfg.MappingSchema;
using NHibernate.Mapping.ByCode;
Expand Down Expand Up @@ -37,9 +38,24 @@ protected override HbmMapping GetMappings()
m.Type(_enumType);
m.Formula($"(case when Enum1 = {_unspecifiedValue} then null else Enum1 end)");
});
rc.Bag(x => x.Children, m =>
{
m.Cascade(Mapping.ByCode.Cascade.All);
m.Inverse(true);
},
a => a.OneToMany()
);
rc.ManyToOne(x => x.Other, m => m.Cascade(Mapping.ByCode.Cascade.All));
});

mapper.Class<EnumEntityChild>(
rc =>
{
rc.Table("EnumEntityChild");
rc.Id(x => x.Id, m => m.Generator(Generators.Guid));
rc.Property(x => x.Name);
});

return mapper.CompileMappingForAllExplicitlyAddedEntities();
}

Expand Down Expand Up @@ -171,6 +187,26 @@ public void CanQueryComplexExpressionOnTestEnum()
Assert.That(query.Count, Is.EqualTo(0));
}
}

[Test]
public void CanProjectWithListTransformation()
{
using (var session = OpenSession())
using (var trans = session.BeginTransaction())
{
var entities = session.Query<EnumEntity>();

var query = entities.Select(user => new
{
user.Name,
simple = user.Enum1,
children = user.Children,
nullableEnum1IsLarge = user.NullableEnum1 == TestEnum.Large
}).ToList();

Assert.That(query.Count, Is.EqualTo(10));
}
}
}

public class EnumEntity
Expand All @@ -181,7 +217,17 @@ public class EnumEntity
public virtual TestEnum Enum1 { get; set; }
public virtual TestEnum? NullableEnum1 { get; set; }

public virtual int? NullableInt { get; set; }

public virtual EnumEntity Other { get; set; }

public virtual IList<EnumEntityChild> Children { get; set; } = new List<EnumEntityChild>();
}

public class EnumEntityChild
{
public virtual Guid Id { get; set; }
public virtual string Name { get; set; }
}

public enum TestEnum
Expand Down
7 changes: 6 additions & 1 deletion src/NHibernate/Linq/NestedSelects/SelectClauseRewriter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Linq.Expressions;
using NHibernate.Util;
using Remotion.Linq.Clauses.Expressions;
using Remotion.Linq.Parsing;

Expand Down Expand Up @@ -41,6 +42,10 @@ public override Expression Visit(Expression expression)
protected override Expression VisitUnary(UnaryExpression node)
{
if (node.NodeType == ExpressionType.Convert &&
// We can skip a convert node only when the underlying types are equal otherwise it
// will throw an exception when trying to convert the value from an object
// (e.g. (int?)(Enum?) input[0] -> (Enum?) cast cannot be skipped)
node.Type.UnwrapIfNullable() == node.Operand.Type.UnwrapIfNullable() &&
(node.Operand is MemberExpression || node.Operand is QuerySourceReferenceExpression))
{
return AddAndConvertExpression(node.Operand, node.Type);
Expand Down Expand Up @@ -75,4 +80,4 @@ private Expression AddAndConvertExpression(Expression expression, System.Type ty
type);
}
}
}
}