Skip to content
Permalink
Browse files

ConvertTo should copy default values as well

  • Loading branch information...
mythz committed Aug 20, 2019
1 parent 99b1a0b commit 1dfebd96852a502d3c69c395009f55e832dba9bd
Showing with 28 additions and 1 deletion.
  1. +1 −1 src/ServiceStack.Text/AutoMappingUtils.cs
  2. +27 −0 tests/ServiceStack.Text.Tests/AutoMappingTests.cs
@@ -178,7 +178,7 @@ public static object ConvertTo(this object from, Type toType, bool skipConverter
return strDict.ToObjectDictionary().FromObjectDictionary(toType);

var to = toType.CreateInstance();
return to.PopulateWithNonDefaultValues(from);
return to.PopulateWith(from);
}

public static MethodInfo GetImplicitCastMethod(Type fromType, Type toType)
@@ -198,10 +198,37 @@ public class ModelWithIgnoredFields
}

public class ReadOnlyAttribute : AttributeBase { }

[EnumAsInt]
public enum MyEnumAsInt
{
ZeroValue = 0,
OneValue = 1,
DefaultValue = 2,
};

class MyEnumAsIntSource
{
public MyEnumAsInt MyEnumAsInt { get; set; } = MyEnumAsInt.DefaultValue;
}

class MyEnumAsIntTarget
{
public MyEnumAsInt MyEnumAsInt { get; set; } = MyEnumAsInt.DefaultValue;
}

[TestFixture]
public class AutoMappingTests
{
[Test]
public void Can_convert_Default_Enum_Values()
{
var from = new MyEnumAsIntSource { MyEnumAsInt = MyEnumAsInt.ZeroValue };
var to = from.ConvertTo<MyEnumAsIntTarget>();

Assert.That(to.MyEnumAsInt, Is.EqualTo(from.MyEnumAsInt));
}

[Test]
public void Does_populate()
{

0 comments on commit 1dfebd9

Please sign in to comment.
You can’t perform that action at this time.