Skip to content

Commit 8d33224

Browse files
committed
Fixign target dictionary key generation when mapping from runtime-typed, nested dictionaries, re: issue #8
1 parent 99e1c05 commit 8d33224

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

AgileMapper.UnitTests/Dictionaries/WhenMappingToNewDictionaryMembers.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,5 +108,25 @@ public void ShouldMapANestedEnumerableOfArraysToANestedEnumerableTypedDictionary
108108
result.Value["[0]"].ShouldBe("1", "2", "3");
109109
result.Value["[1]"].ShouldBe("4", "5", "6");
110110
}
111+
112+
// See https://github.com/agileobjects/AgileMapper/issues/8
113+
[Fact]
114+
public void ShouldMapFromARuntimeTypedNestedDictionary()
115+
{
116+
var source = new Dictionary<string, object>
117+
{
118+
["Value1"] = "I'm Value1!",
119+
["Value2"] = new Dictionary<string, object>
120+
{
121+
["Value"] = "I'm nested!"
122+
}
123+
};
124+
125+
var result = Mapper.Map(source).ToANew<PublicTwoFields<string, PublicProperty<string>>>();
126+
127+
result.Value1.ShouldBe("I'm Value1!");
128+
result.Value2.ShouldNotBeNull();
129+
result.Value2.Value.ShouldBe("I'm nested!");
130+
}
111131
}
112132
}

AgileMapper/Members/DictionaryMemberMapperDataExtensions.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@ public static Expression GetTargetMemberDictionaryKey(this IMemberMapperData map
2424

2525
var keyParts = GetTargetMemberDictionaryKeyParts(mapperData);
2626

27+
if (keyParts.Any() && (keyParts[0].NodeType == ExpressionType.Constant))
28+
{
29+
var firstKeyPart = (ConstantExpression)keyParts[0];
30+
var firstKeyPartValue = (string)firstKeyPart.Value;
31+
32+
if (firstKeyPartValue.StartsWith('.'))
33+
{
34+
keyParts[0] = firstKeyPartValue.Substring(1).ToConstantExpression();
35+
}
36+
}
37+
2738
return keyParts.GetStringConcatCall();
2839
}
2940

@@ -91,7 +102,7 @@ private static bool IsRootDictionaryContext(
91102
bool isTargetDictionary,
92103
IMemberMapperData mapperData)
93104
{
94-
if (targetMember.IsRoot)
105+
if (targetMember.IsRoot || mapperData.Context.IsStandalone)
95106
{
96107
return true;
97108
}

0 commit comments

Comments
 (0)