Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 7b70512

Browse files
committed
Fix FromObjectDictionary conversion test
1 parent e2cf86b commit 7b70512

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

src/ServiceStack.Text/AutoMappingUtils.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,9 @@ public static object ConvertTo(this object from, Type toType, bool skipConverter
154154
return listResult;
155155
}
156156

157+
if (from is IEnumerable<KeyValuePair<string, object>> objDict)
158+
return objDict.FromObjectDictionary(toType);
159+
157160
var to = toType.CreateInstance();
158161
return to.PopulateWithNonDefaultValues(from);
159162
}

tests/ServiceStack.Text.Tests/AutoMappingObjectDictionaryTests.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,43 @@ public void Can_convert_from_ObjectDictionary_into_AutoQuery_DTO()
129129
Assert.That(request.Take, Is.EqualTo(5));
130130
Assert.That(request.Meta, Is.EquivalentTo(new Dictionary<string, object> {{"foo", "bar"}}));
131131
}
132+
133+
public class PersonWithIdentities
134+
{
135+
public string Name { get; set; }
136+
public List<OtherName> OtherNames { get;set; }
137+
}
138+
139+
public class OtherName
140+
{
141+
public string Name { get; set; }
142+
}
143+
144+
[Test]
145+
public void Can_Convert_from_ObjectDictionary_Containing_Another_Object_Dictionary()
146+
{
147+
var map = new Dictionary<string, object>
148+
{
149+
{ "name", "Foo" },
150+
{ "otherNames", new List<object>
151+
{
152+
new Dictionary<string, object> { { "name", "Fu" } },
153+
new Dictionary<string, object> { { "name", "Fuey" } }
154+
}
155+
}
156+
};
157+
158+
var fromDict = map.FromObjectDictionary<PersonWithIdentities>();
159+
160+
Assert.That(fromDict.Name, Is.EqualTo("Foo"));
161+
Assert.That(fromDict.OtherNames.Count, Is.EqualTo(2));
162+
Assert.That(fromDict.OtherNames.First().Name, Is.EqualTo("Fu"));
163+
Assert.That(fromDict.OtherNames.Last().Name, Is.EqualTo("Fuey"));
164+
165+
var toDict = fromDict.ToObjectDictionary();
166+
Assert.That(toDict["Name"], Is.EqualTo("Foo"));
167+
Assert.That(toDict["OtherNames"], Is.EqualTo(fromDict.OtherNames));
168+
}
132169

133170
public class Employee
134171
{

0 commit comments

Comments
 (0)