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

Commit f332443

Browse files
committed
support camelCase names in FromObjectDictionary
1 parent b4bc95b commit f332443

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/ServiceStack.Text/PlatformExtensions.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1339,7 +1339,9 @@ public static object FromObjectDictionary(this Dictionary<string, object> values
13391339
foreach (var entry in values)
13401340
{
13411341
ObjectDictionaryFieldDefinition fieldDef;
1342-
if (!def.FieldsMap.TryGetValue(entry.Key, out fieldDef) || entry.Value == null)
1342+
if (!def.FieldsMap.TryGetValue(entry.Key, out fieldDef) &&
1343+
!def.FieldsMap.TryGetValue(entry.Key.ToPascalCase(), out fieldDef)
1344+
|| entry.Value == null)
13431345
continue;
13441346

13451347
fieldDef.SetValue(to, entry.Value);

tests/ServiceStack.Text.Tests/AutoMappingObjectDictionaryTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,22 @@ public void Can_Convert_from_ObjectDictionary_with_Different_Types()
6060
Assert.That(fromDict.Car.Age, Is.EqualTo(10));
6161
Assert.That(fromDict.Car.Name, Is.EqualTo("SubCar"));
6262
}
63+
64+
[Test]
65+
public void Can_Convert_from_ObjectDictionary_with_Different_Types_with_camelCase_names()
66+
{
67+
var map = new Dictionary<string, object>
68+
{
69+
{ "firstName", 1 },
70+
{ "lastName", true },
71+
{ "car", new SubCar { Age = 10, Name = "SubCar", Custom = "Custom"} },
72+
};
73+
74+
var fromDict = (User)map.FromObjectDictionary(typeof(User));
75+
Assert.That(fromDict.FirstName, Is.EqualTo("1"));
76+
Assert.That(fromDict.LastName, Is.EqualTo(bool.TrueString));
77+
Assert.That(fromDict.Car.Age, Is.EqualTo(10));
78+
Assert.That(fromDict.Car.Name, Is.EqualTo("SubCar"));
79+
}
6380
}
6481
}

0 commit comments

Comments
 (0)