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

Commit 3ee77c9

Browse files
committed
Handle converting FromObjectDictionary to object property
1 parent aa53ee5 commit 3ee77c9

File tree

2 files changed

+50
-19
lines changed

2 files changed

+50
-19
lines changed

src/ServiceStack.Text/PlatformExtensions.cs

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -630,31 +630,34 @@ public void SetValue(object instance, object value)
630630
if (SetValueFn == null)
631631
return;
632632

633-
if (value is IEnumerable<KeyValuePair<string, object>> dictionary)
633+
if (Type != typeof(object))
634634
{
635-
value = dictionary.FromObjectDictionary(Type);
636-
}
635+
if (value is IEnumerable<KeyValuePair<string, object>> dictionary)
636+
{
637+
value = dictionary.FromObjectDictionary(Type);
638+
}
637639

638-
if (!Type.IsInstanceOfType(value))
639-
{
640-
lock (this)
640+
if (!Type.IsInstanceOfType(value))
641641
{
642-
//Only caches object converter used on first use
643-
if (ConvertType == null)
642+
lock (this)
644643
{
645-
ConvertType = value.GetType();
646-
ConvertValueFn = TypeConverter.CreateTypeConverter(ConvertType, Type);
644+
//Only caches object converter used on first use
645+
if (ConvertType == null)
646+
{
647+
ConvertType = value.GetType();
648+
ConvertValueFn = TypeConverter.CreateTypeConverter(ConvertType, Type);
649+
}
647650
}
648-
}
649651

650-
if (ConvertType.IsInstanceOfType(value))
651-
{
652-
value = ConvertValueFn(value);
653-
}
654-
else
655-
{
656-
var tempConvertFn = TypeConverter.CreateTypeConverter(value.GetType(), Type);
657-
value = tempConvertFn(value);
652+
if (ConvertType.IsInstanceOfType(value))
653+
{
654+
value = ConvertValueFn(value);
655+
}
656+
else
657+
{
658+
var tempConvertFn = TypeConverter.CreateTypeConverter(value.GetType(), Type);
659+
value = tempConvertFn(value);
660+
}
658661
}
659662
}
660663

tests/ServiceStack.Text.Tests/AutoMappingTests.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -857,6 +857,34 @@ public void Can_convert_between_different_types_of_Dictionaries_and_KVP_keys()
857857
Assert.That(stringKvps.ConvertTo<List<KeyValuePair<object,string>>>(), Is.EquivalentTo(kvpsStringValue));
858858
Assert.That(stringKvps.ConvertTo<List<KeyValuePair<string,string>>>(), Is.EquivalentTo(stringKvps));
859859
}
860+
861+
class HasObject
862+
{
863+
public object Value { get; set; }
864+
}
865+
866+
[Test]
867+
public void Can_convert_dictionary_to_HasObject()
868+
{
869+
var objDict = new ObjectDictionary {
870+
["value"] = new ObjectDictionary {
871+
["a"] = 1
872+
}
873+
};
874+
875+
var to = objDict.FromObjectDictionary<HasObject>();
876+
Assert.That(to.Value, Is.EqualTo(objDict["value"]));
877+
878+
var kvps = new ObjectDictionary {
879+
["value"] = new List<KeyValuePair<string,int>> {
880+
new KeyValuePair<string, int>("a",1)
881+
}
882+
};
883+
884+
to = objDict.FromObjectDictionary<HasObject>();
885+
Assert.That(to.Value, Is.EqualTo(objDict["value"]));
886+
}
887+
860888
}
861889

862890
public enum ClassWithEnumType

0 commit comments

Comments
 (0)