Skip to content

Commit c535af3

Browse files
committed
Mapping non List nested collections fix (lile HashSet etc), Tests.
1 parent 4af5743 commit c535af3

File tree

2 files changed

+67
-6
lines changed

2 files changed

+67
-6
lines changed

Slapper.AutoMapper.Tests/MapCollectionsTypedTest.cs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,5 +87,67 @@ public void I_Can_Map_Value_SpecialStringTyped_Collection()
8787
Assert.That(customers.Single().Names[1] == "Name 2");
8888
}
8989

90+
public class Merchant
91+
{
92+
public Merchant()
93+
{
94+
Addresses = new HashSet<MerchantAddress>();
95+
}
96+
97+
public long Id { set; get; }
98+
public string Name { set; get; }
99+
100+
public ICollection<MerchantAddress> Addresses { set; get; }
101+
}
102+
103+
public class MerchantAddress
104+
{
105+
public long Id { set; get; }
106+
public string AddressLine { set; get; }
107+
public long MerchantId { set; get; }
108+
}
109+
110+
[Test]
111+
public void I_Can_Map_Any_Typed_ICollection()
112+
{
113+
// this strings was received from database (or another flat storage)
114+
List<Dictionary<string, object>> flat = new List<Dictionary<string, object>>()
115+
{
116+
new Dictionary<string, object>()
117+
{
118+
{ "Id", 1 } ,
119+
{"Name", "Merchant name" } ,
120+
{ "Addresses_Id", 1} ,
121+
{ "Addresses_AddressLine", "Address line 1"} ,
122+
{ "Addresses_MerchantId", 1}
123+
},
124+
new Dictionary<string, object>()
125+
{
126+
{ "Id", 1 } ,
127+
{"Name", "Merchant name" } ,
128+
{ "Addresses_Id", 2} ,
129+
{ "Addresses_AddressLine", "Address line 2"} ,
130+
{ "Addresses_MerchantId", 1}
131+
},
132+
new Dictionary<string, object>()
133+
{
134+
{ "Id", 1 } ,
135+
{"Name", "Merchant name" } ,
136+
{ "Addresses_Id", 3} ,
137+
{ "Addresses_AddressLine", "Address line 3"} ,
138+
{ "Addresses_MerchantId", 1}
139+
},
140+
};
141+
Slapper.AutoMapper.Configuration.AddIdentifiers(typeof(Merchant), new [] { "Id" });
142+
Slapper.AutoMapper.Configuration.AddIdentifiers(typeof(MerchantAddress), new[] { "Id" });
143+
var result = Slapper.AutoMapper.MapDynamic<Merchant>(flat);
144+
Assert.That(result.Count() == 1);
145+
var merchant = result.First();
146+
Assert.That(merchant.Addresses.Count == 3);
147+
Assert.AreEqual("Address line 1", merchant.Addresses.First().AddressLine);
148+
Assert.AreEqual("Address line 2", merchant.Addresses.Skip(1).First().AddressLine);
149+
Assert.AreEqual("Address line 3", merchant.Addresses.Skip(2).First().AddressLine);
150+
}
151+
90152
}
91153
}

Slapper.AutoMapper/Slapper.AutoMapper.InternalHelpers.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -542,12 +542,11 @@ internal static object Map(IDictionary<string, object> dictionary, object instan
542542
internal static object MapCollection(Type type, IDictionary<string, object> dictionary, object instance, object parentInstance = null)
543543
{
544544
Type baseListType = typeof(List<>);
545-
546-
Type listType = baseListType.MakeGenericType(type);
545+
Type collectionType = instance == null ? baseListType.MakeGenericType(type) : instance.GetType();
547546

548547
if (instance == null)
549548
{
550-
instance = CreateInstance(listType);
549+
instance = CreateInstance(collectionType);
551550
}
552551

553552
// If the dictionnary only contains null values, we return an empty instance
@@ -577,14 +576,14 @@ internal static object MapCollection(Type type, IDictionary<string, object> dict
577576
}
578577
else
579578
{
580-
MethodInfo addMethod = listType.GetMethod("Add");
579+
MethodInfo addMethod = collectionType.GetMethod("Add");
581580

582581
addMethod.Invoke(instance, new[] { instanceToAddToCollectionInstance });
583582
}
584583
}
585584
else
586585
{
587-
MethodInfo containsMethod = listType.GetMethod("Contains");
586+
MethodInfo containsMethod = collectionType.GetMethod("Contains");
588587

589588
var alreadyContainsInstance = (bool)containsMethod.Invoke(instance, new[] { instanceToAddToCollectionInstance });
590589

@@ -598,7 +597,7 @@ internal static object MapCollection(Type type, IDictionary<string, object> dict
598597
}
599598
else
600599
{
601-
MethodInfo addMethod = listType.GetMethod("Add");
600+
MethodInfo addMethod = collectionType.GetMethod("Add");
602601

603602
addMethod.Invoke(instance, new[] { instanceToAddToCollectionInstance });
604603
}

0 commit comments

Comments
 (0)