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

Commit 1dfebd9

Browse files
committed
ConvertTo should copy default values as well
1 parent 99b1a0b commit 1dfebd9

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/ServiceStack.Text/AutoMappingUtils.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ public static object ConvertTo(this object from, Type toType, bool skipConverter
178178
return strDict.ToObjectDictionary().FromObjectDictionary(toType);
179179

180180
var to = toType.CreateInstance();
181-
return to.PopulateWithNonDefaultValues(from);
181+
return to.PopulateWith(from);
182182
}
183183

184184
public static MethodInfo GetImplicitCastMethod(Type fromType, Type toType)

tests/ServiceStack.Text.Tests/AutoMappingTests.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,10 +198,37 @@ public class ModelWithIgnoredFields
198198
}
199199

200200
public class ReadOnlyAttribute : AttributeBase { }
201+
202+
[EnumAsInt]
203+
public enum MyEnumAsInt
204+
{
205+
ZeroValue = 0,
206+
OneValue = 1,
207+
DefaultValue = 2,
208+
};
209+
210+
class MyEnumAsIntSource
211+
{
212+
public MyEnumAsInt MyEnumAsInt { get; set; } = MyEnumAsInt.DefaultValue;
213+
}
214+
215+
class MyEnumAsIntTarget
216+
{
217+
public MyEnumAsInt MyEnumAsInt { get; set; } = MyEnumAsInt.DefaultValue;
218+
}
201219

202220
[TestFixture]
203221
public class AutoMappingTests
204222
{
223+
[Test]
224+
public void Can_convert_Default_Enum_Values()
225+
{
226+
var from = new MyEnumAsIntSource { MyEnumAsInt = MyEnumAsInt.ZeroValue };
227+
var to = from.ConvertTo<MyEnumAsIntTarget>();
228+
229+
Assert.That(to.MyEnumAsInt, Is.EqualTo(from.MyEnumAsInt));
230+
}
231+
205232
[Test]
206233
public void Does_populate()
207234
{

0 commit comments

Comments
 (0)