Skip to content
This repository has been archived by the owner on Aug 2, 2019. It is now read-only.

Commit

Permalink
Recognize enumerable collections.
Browse files Browse the repository at this point in the history
The code that recognizes whether a property is a collection did not check the actual type of the collection for being an enumerable interface.

Bug: #23
  • Loading branch information
pvginkel committed Sep 27, 2015
1 parent 82a71d1 commit 1664a30
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -6,3 +6,4 @@ Thumbs.db
/*.shfbproj_*
/*.dotCover
_ReSharper.*
/.vs
4 changes: 4 additions & 0 deletions NHibernate.OData.Test/Domain/Model.hbm.xml
Expand Up @@ -13,6 +13,10 @@
<key column="ParentId"/>
<many-to-many column="RelatedParentId" class="Parent"/>
</set>
<set name="EnumerableRelatedParents">
<key column="ParentId"/>
<many-to-many column="RelatedParentId" class="Parent"/>
</set>
</class>
<class name="Child">
<id name="Id" type="int">
Expand Down
2 changes: 2 additions & 0 deletions NHibernate.OData.Test/Domain/Parent.cs
Expand Up @@ -21,6 +21,8 @@ public class Parent : IEntity

public virtual ISet<Parent> RelatedParents { get; set; }

public virtual IEnumerable<Parent> EnumerableRelatedParents { get; set; }

public override string ToString()
{
return Name;
Expand Down
23 changes: 23 additions & 0 deletions NHibernate.OData.Test/Issues/Issue23Fixture.cs
@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate.OData.Test.Domain;
using NHibernate.OData.Test.Support;
using NUnit.Framework;

namespace NHibernate.OData.Test.Issues
{
[TestFixture]
internal class Issue23Fixture : DomainTestFixture
{
[Test]
public void InterfacePropertNotRecognizedAsCollection()
{
Verify(
"EnumerableRelatedParents/any(o: o/Int32 eq 9)",
Session.QueryOver<Parent>().Where(x => x.Name == "Parent 10").List()
);
}
}
}
1 change: 1 addition & 0 deletions NHibernate.OData.Test/NHibernate.OData.Test.csproj
Expand Up @@ -79,6 +79,7 @@
<Compile Include="Interface\EntityNameInterface.cs" />
<Compile Include="Interface\ParsedQueryString.cs" />
<Compile Include="Inverse\Basics.cs" />
<Compile Include="Issues\Issue23Fixture.cs" />
<Compile Include="Issues\Issue13Fixture.cs" />
<Compile Include="Issues\Issue6Fixture.cs" />
<Compile Include="Parser\CollectionMethodCalls.cs" />
Expand Down
4 changes: 3 additions & 1 deletion NHibernate.OData.Test/Support/DomainTestFixture.cs
Expand Up @@ -80,7 +80,7 @@ private void PopulateDatabase()
{ "DynamicInt", i },
{ "DynamicChildRef", previousChild },
},
RelatedParents = new HashSet<Parent>(parents),
RelatedParents = new HashSet<Parent>(parents)
};

if (i == 10)
Expand All @@ -105,6 +105,8 @@ private void PopulateDatabase()
if (i == 10)
parent.RelatedParents = new HashSet<Parent>(parents.Where(x => x.Int32 >= 5));

parent.EnumerableRelatedParents = parent.RelatedParents;

session.Save(parent);
parents.Add(parent);

Expand Down
10 changes: 9 additions & 1 deletion NHibernate.OData/TypeUtil.cs
Expand Up @@ -12,11 +12,19 @@ public static System.Type TryGetCollectionItemType(System.Type collectionType)
if (collectionType == null)
return null;

System.Type enumerableType = collectionType.GetInterfaces().FirstOrDefault(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IEnumerable<>));
if (collectionType.IsInterface && IsCollectionType(collectionType))
return collectionType.GetGenericArguments().Single();

System.Type enumerableType = collectionType.GetInterfaces().FirstOrDefault(IsCollectionType);
if (enumerableType == null)
return null;

return enumerableType.GetGenericArguments().Single();
}

private static bool IsCollectionType(System.Type type)
{
return type.IsGenericType && type.GetGenericTypeDefinition() == typeof(IEnumerable<>);
}
}
}

0 comments on commit 1664a30

Please sign in to comment.