Skip to content

Commit 2eb7493

Browse files
committed
Test and fix for grandchild name part of child name
1 parent bf8e28e commit 2eb7493

File tree

4 files changed

+50
-3
lines changed

4 files changed

+50
-3
lines changed

Slapper.AutoMapper.Tests/ComplexMapsParentsAndChlidTest.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Dynamic;
34
using System.Linq;
45
using System.Text;
56
using NUnit.Framework;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using NUnit.Framework;
4+
5+
namespace Slapper.Tests
6+
{
7+
public class MatchingChildNameTests
8+
{
9+
public class SubMember
10+
{
11+
public int Id { get; set; }
12+
}
13+
14+
public class Member
15+
{
16+
public int Id { get; set; }
17+
public IList<SubMember> SubMembers { get; set; }
18+
}
19+
20+
public class Club
21+
{
22+
public int Id { get; set; }
23+
public IList<Member> Members { get; set; }
24+
}
25+
26+
[Test]
27+
public void Can_map_grandchild_with_parts_of_same_name_as_child()
28+
{
29+
var data = new List<Dictionary<string, object>> {
30+
new Dictionary<string, object> {
31+
{"Id", 1},
32+
{"Members_Id", 1},
33+
{"Members_SubMembers_Id", 1}
34+
}
35+
};
36+
37+
var club = AutoMapper.MapDynamic<Club>(data).Single();
38+
39+
Assert.That(club.Members.Count == 1);
40+
Assert.NotNull(club.Members[0].SubMembers);
41+
Assert.That(club.Members[0].SubMembers.Count == 1);
42+
}
43+
}
44+
}

Slapper.AutoMapper.Tests/Slapper.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
<Compile Include="MappingToGuidTests.cs" />
5252
<Compile Include="MappingToNullableTypesTests.cs" />
5353
<Compile Include="MappingToEnumTests.cs" />
54+
<Compile Include="MatchingChildNameTests.cs" />
5455
<Compile Include="NoIdentifierTests.cs" />
5556
<Compile Include="NullTests.cs" />
5657
<Compile Include="Properties\AssemblyInfo.cs" />

Slapper.AutoMapper/Slapper.AutoMapper.InternalHelpers.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ dynamic data into static types and populate complex nested child objects.
3636
using System.Linq;
3737
using System.Reflection;
3838
using System.Runtime.Remoting.Messaging;
39+
using System.Text.RegularExpressions;
3940

4041
namespace Slapper
4142
{
@@ -458,9 +459,9 @@ internal static object Map(IDictionary<string, object> dictionary, object instan
458459

459460
continue;
460461
}
461-
462-
var newDictionary = nestedDictionary.ToDictionary(pair => pair.Key.ToLower()
463-
.Replace(memberName + "_", string.Empty), pair => pair.Value, StringComparer.OrdinalIgnoreCase);
462+
var regex = new Regex(Regex.Escape(memberName + "_"));
463+
var newDictionary = nestedDictionary.ToDictionary(pair => regex.Replace(pair.Key.ToLower(), string.Empty, 1),
464+
pair => pair.Value, StringComparer.OrdinalIgnoreCase);
464465

465466
// Try to get the value of the complex member. If the member
466467
// hasn't been initialized, then this will return null.

0 commit comments

Comments
 (0)