Skip to content

Fix query dynamic component on unmapped interface #3154

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 7 commits into from
Sep 8, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,22 @@ select p
Assert.That(product.Properties["Name"], Is.EqualTo("First Product"));
}
}

[Test(Description = "GH-3150")]
public async Task Query_DynamicComponentByInterfaceAsync()
{
using (var session = OpenSession())
{
var product = await ((
from p in session.Query<IProduct>()
where (string) p.Properties["Name"] == "First Product"
select p
).SingleAsync());

Assert.That(product, Is.Not.Null);
Assert.That(product.Properties["Name"], Is.EqualTo("First Product"));
}
}

[Test]
public async Task Multiple_Query_Does_Not_CacheAsync()
Expand All @@ -121,4 +137,4 @@ select p
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,22 @@ from p in session.Query<Product>()
select p
).SingleAsync());

Assert.That(product, Is.Not.Null);
Assert.That(product.Details.Properties["Name"], Is.EqualTo("First Product"));
}
}

[Test(Description = "GH-3150")]
public async Task CanQueryDynamicComponentInComponentByInterfaceAsync()
{
using (var session = OpenSession())
{
var product = await ((
from p in session.Query<IProduct>()
where (string) p.Details.Properties["Name"] == "First Product"
select p
).SingleAsync());

Assert.That(product, Is.Not.Null);
Assert.That(product.Details.Properties["Name"], Is.EqualTo("First Product"));
}
Expand Down Expand Up @@ -106,4 +122,4 @@ select p
}
}
}
}
}
18 changes: 17 additions & 1 deletion src/NHibernate.Test/NHSpecificTest/NH2664Generic/Fixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,22 @@ select p
Assert.That(product.Properties["Name"], Is.EqualTo("First Product"));
}
}

[Test(Description = "GH-3150")]
public void Query_DynamicComponentByInterface()
{
using (var session = OpenSession())
{
var product = (
from p in session.Query<IProduct>()
where (string) p.Properties["Name"] == "First Product"
select p
).Single();

Assert.That(product, Is.Not.Null);
Assert.That(product.Properties["Name"], Is.EqualTo("First Product"));
}
}

[Test]
public void Multiple_Query_Does_Not_Cache()
Expand Down Expand Up @@ -131,4 +147,4 @@ from a in session.Query<Product>()
}
}
}
}
}
10 changes: 10 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH2664Generic/IProduct.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Collections.Generic;

namespace NHibernate.Test.NHSpecificTest.NH2664Generic
{
public interface IProduct
{
string ProductId { get; set; }
IDictionary<string, object> Properties { get; set; }
}
}
4 changes: 2 additions & 2 deletions src/NHibernate.Test/NHSpecificTest/NH2664Generic/Product.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace NHibernate.Test.NHSpecificTest.NH2664Generic
{
public class Product
public class Product : IProduct
{
public virtual string ProductId { get; set; }

Expand All @@ -21,4 +21,4 @@ public virtual IDictionary<string, object> Properties
set { _properties = value; }
}
}
}
}
18 changes: 17 additions & 1 deletion src/NHibernate.Test/NHSpecificTest/NH3571Generic/Fixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,22 @@ from p in session.Query<Product>()
select p
).Single();

Assert.That(product, Is.Not.Null);
Assert.That(product.Details.Properties["Name"], Is.EqualTo("First Product"));
}
}

[Test(Description = "GH-3150")]
public void CanQueryDynamicComponentInComponentByInterface()
{
using (var session = OpenSession())
{
var product = (
from p in session.Query<IProduct>()
where (string) p.Details.Properties["Name"] == "First Product"
select p
).Single();

Assert.That(product, Is.Not.Null);
Assert.That(product.Details.Properties["Name"], Is.EqualTo("First Product"));
}
Expand Down Expand Up @@ -119,4 +135,4 @@ from a in session.Query<Product>()
}
}
}
}
}
11 changes: 11 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH3571Generic/IProduct.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Collections.Generic;

namespace NHibernate.Test.NHSpecificTest.NH3571Generic
{
public interface IProduct
{
ProductDetails Details { get; set; }
IList<ProductDetails> DetailsList { get; set; }
string ProductId { get; set; }
}
}
4 changes: 2 additions & 2 deletions src/NHibernate.Test/NHSpecificTest/NH3571Generic/Product.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace NHibernate.Test.NHSpecificTest.NH3571Generic
{
public class Product
public class Product : IProduct
{
public Product()
{
Expand Down Expand Up @@ -40,4 +40,4 @@ public virtual IDictionary<string, object> Properties
set { _properties = value; }
}
}
}
}
2 changes: 1 addition & 1 deletion src/NHibernate/Linq/Visitors/ExpressionParameterVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ protected override Expression VisitMethodCall(MethodCallExpression expression)
_collectionParameters.Add(collectionParameter);
}

if (VisitorUtil.IsDynamicComponentDictionaryGetter(expression, _sessionFactory))
if (VisitorUtil.TryGetPotentialDynamicComponentDictionaryMember(expression, out _))
{
return expression;
}
Expand Down
53 changes: 10 additions & 43 deletions src/NHibernate/Linq/Visitors/VisitorUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,64 +4,31 @@
using System.Collections;
using System.Reflection;
using NHibernate.Util;
using Remotion.Linq.Clauses.Expressions;
using Remotion.Linq.Parsing.ExpressionVisitors;
using NHibernate.Engine;
using System;

namespace NHibernate.Linq.Visitors
{
public static class VisitorUtil
{
public static bool IsDynamicComponentDictionaryGetter(MethodInfo method, Expression targetObject, IEnumerable<Expression> arguments, ISessionFactory sessionFactory, out string memberName)
{
if (!TryGetPotentialDynamicComponentDictionaryMember(method, targetObject, arguments, out memberName))
{
return false;
}

var member = (MemberExpression) targetObject;
var memberPath = member.Member.Name;
var metaData = sessionFactory.GetClassMetadata(member.Expression.Type);

//Walk backwards if the owning member is not a mapped class (i.e a possible Component)
targetObject = member.Expression;
while (metaData == null && targetObject != null &&
(targetObject.NodeType == ExpressionType.MemberAccess || targetObject.NodeType == ExpressionType.Parameter ||
targetObject is QuerySourceReferenceExpression))
{
System.Type memberType;
if (targetObject is QuerySourceReferenceExpression)
{
var querySourceExpression = (QuerySourceReferenceExpression) targetObject;
memberType = querySourceExpression.Type;
}
else if (targetObject.NodeType == ExpressionType.Parameter)
{
var parameterExpression = (ParameterExpression) targetObject;
memberType = parameterExpression.Type;
}
else //targetObject.NodeType == ExpressionType.MemberAccess
{
var memberExpression = ((MemberExpression) targetObject);
memberPath = memberExpression.Member.Name + "." + memberPath;
memberType = memberExpression.Type;
targetObject = memberExpression.Expression;
}
metaData = sessionFactory.GetClassMetadata(memberType);
}

if (metaData == null)
return false;

// IDictionary can be mapped as collection or component - is it mapped as a component?
var propertyType = metaData.GetPropertyType(memberPath);
return (propertyType != null && propertyType.IsComponentType);
return TryGetPotentialDynamicComponentDictionaryMember(method, targetObject, arguments, out memberName)
&& ExpressionsHelper.TryGetMappedType((ISessionFactoryImplementor) sessionFactory, targetObject, out var mappedType, out _, out _, out _)
// IDictionary can be mapped as collection or component - is it mapped as a component?
&& mappedType.IsComponentType;
}

// Since 5.4
[Obsolete("This method has no more usages and will be removed in a future version")]
public static bool IsDynamicComponentDictionaryGetter(MethodCallExpression expression, ISessionFactory sessionFactory, out string memberName)
{
return IsDynamicComponentDictionaryGetter(expression.Method, expression.Object, expression.Arguments, sessionFactory, out memberName);
}

// Since 5.4
[Obsolete("This method has no more usages and will be removed in a future version")]
public static bool IsDynamicComponentDictionaryGetter(MethodCallExpression expression, ISessionFactory sessionFactory)
{
string memberName;
Expand Down