Skip to content

Commit 4f1759f

Browse files
authored
Fix Automapper not not propagating union-subtree through class hierarchy (#334)
1 parent 66fde89 commit 4f1759f

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed

src/FluentNHibernate.Testing/AutoMapping/TestFixtures.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,3 +525,26 @@ public enum PublisherType
525525
Mixed
526526
}
527527
}
528+
529+
namespace FluentNHibernate.Automapping.TestFixtures.UnionChain
530+
{
531+
public class BaseUnionType
532+
{
533+
public int Id { get; set; }
534+
}
535+
536+
public class ChildUnionType : BaseUnionType
537+
{
538+
public int Value { get; set; }
539+
}
540+
541+
public class GrandChildUnionType : ChildUnionType
542+
{
543+
public string Name { get; set; }
544+
}
545+
546+
public class GreatGrandChildUnionType : GrandChildUnionType
547+
{
548+
}
549+
}
550+

src/FluentNHibernate.Testing/AutoMapping/UnionSubclassConventionTests.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using FluentNHibernate.Automapping;
22
using FluentNHibernate.Automapping.TestFixtures.SuperTypes;
3+
using FluentNHibernate.Automapping.TestFixtures.UnionChain;
34
using NUnit.Framework;
45

56
namespace FluentNHibernate.Testing.Automapping
@@ -18,5 +19,29 @@ public void DefaultConventionsAreAppliedToUnionSubClasses()
1819
.HasAttribute("name", "Parent_id");
1920
//.Element("class/union-subclass").Exists();
2021
}
22+
23+
[Test]
24+
public void UnionSubtypePropagatesThroughHierarchy()
25+
{
26+
new AutoMappingTester<BaseUnionType>(
27+
AutoMap.AssemblyOf<BaseUnionType>()
28+
.Where(x => x.Namespace == typeof(BaseUnionType).Namespace)
29+
.Override<BaseUnionType>(m => m.UseUnionSubclassForInheritanceMapping()))
30+
.Element("class[@name = '" + typeof(BaseUnionType).AssemblyQualifiedName + "']")
31+
.Exists()
32+
.Element("class/union-subclass[@name='" + typeof(ChildUnionType).AssemblyQualifiedName + "']")
33+
.Exists()
34+
.Element("class/union-subclass/joined-subclass")
35+
.DoesntExist()
36+
.Element("class/union-subclass[@name='" + typeof(ChildUnionType).AssemblyQualifiedName + "']/" +
37+
"union-subclass[@name='" + typeof(GrandChildUnionType).AssemblyQualifiedName + "']")
38+
.Exists()
39+
.Element("class/union-subclass[@name='" + typeof(ChildUnionType).AssemblyQualifiedName + "']/" +
40+
"union-subclass[@name='" + typeof(GrandChildUnionType).AssemblyQualifiedName + "']/" +
41+
"union-subclass[@name='" + typeof(GreatGrandChildUnionType).AssemblyQualifiedName + "']")
42+
.Exists()
43+
;
44+
}
45+
2146
}
2247
}

src/FluentNHibernate/Automapping/AutoMapper.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,17 @@ private void MapInheritanceTree(Type classType, ClassMappingBase mapping, IList<
7878
}
7979

8080
SubclassMapping subclassMapping;
81-
81+
var tempSubClassMap = mapping as SubclassMapping;
8282
if(!tempIsNull && tempMapping.IsUnionSubclass)
8383
{
8484
subclassMapping = new SubclassMapping(SubclassType.UnionSubclass);
8585
subclassMapping.Set(x => x.Type, Layer.Defaults, inheritedClass.Type);
8686
}
87+
else if (tempSubClassMap != null && tempSubClassMap.SubclassType == SubclassType.UnionSubclass)
88+
{
89+
subclassMapping = new SubclassMapping(SubclassType.UnionSubclass);
90+
subclassMapping.Set(x => x.Type, Layer.Defaults, inheritedClass.Type);
91+
}
8792
else if(!isDiscriminated)
8893
{
8994
subclassMapping = new SubclassMapping(SubclassType.JoinedSubclass);

0 commit comments

Comments
 (0)