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

Commit e43d901

Browse files
committed
Add nested populate test
1 parent 995e2f8 commit e43d901

File tree

2 files changed

+58
-4
lines changed

2 files changed

+58
-4
lines changed

src/ServiceStack.Text/AutoMappingUtils.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,6 @@ public static object ConvertTo(this object from, Type toType, bool skipConverter
123123
return null;
124124

125125
var fromType = from.GetType();
126-
if (fromType == toType || toType == typeof(object))
127-
return from;
128-
129126
if (ShouldIgnoreMapping(fromType, toType))
130127
return null;
131128

@@ -136,6 +133,9 @@ public static object ConvertTo(this object from, Type toType, bool skipConverter
136133
return converter(from);
137134
}
138135

136+
if (fromType == toType || toType == typeof(object))
137+
return from;
138+
139139
if (fromType.IsValueType || toType.IsValueType)
140140
return ChangeValueType(from, toType);
141141

tests/ServiceStack.Text.Tests/AutoMappingTests.cs

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1421,6 +1421,60 @@ private static void AssertAreEqual(TestClassA testA, TestClassB testB)
14211421
Assert.That(testA.ListEnums,
14221422
Is.EquivalentTo(testB.ListEnums.ConvertAll(x => x.ToEnum<TestEnum>())));
14231423
}
1424-
1424+
1425+
class Company
1426+
{
1427+
public string Name { get; set; }
1428+
public Address Address { get; set; }
1429+
}
1430+
1431+
class Address
1432+
{
1433+
public string City { get; set; }
1434+
public string PostCode { get; set; }
1435+
}
1436+
1437+
[Test]
1438+
public void Can_register_converters_to_PopulateWithNonDefaultValues()
1439+
{
1440+
Company Create()
1441+
{
1442+
return new Company {
1443+
Name = "Compnay1",
1444+
Address = new Address {
1445+
City = "NY",
1446+
PostCode = "1234",
1447+
}
1448+
};
1449+
}
1450+
1451+
var defaults = Create();
1452+
var original = Create();
1453+
1454+
original.PopulateWithNonDefaultValues(new Company {
1455+
Address = new Address {
1456+
PostCode = "4321"
1457+
}
1458+
});
1459+
1460+
Assert.That(original.Name, Is.EqualTo(defaults.Name));
1461+
Assert.That(original.Address.City, Is.Null);
1462+
Assert.That(original.Address.PostCode, Is.EqualTo("4321"));
1463+
1464+
original = Create();
1465+
1466+
var updated = new Company {
1467+
Address = new Address {
1468+
PostCode = "4321"
1469+
}
1470+
};
1471+
original.Address.PopulateWithNonDefaultValues(updated.Address);
1472+
updated.Address = null;
1473+
original.PopulateWithNonDefaultValues(updated);
1474+
1475+
Assert.That(original.Name, Is.EqualTo(defaults.Name));
1476+
Assert.That(original.Address.City, Is.EqualTo(defaults.Address.City));
1477+
Assert.That(original.Address.PostCode, Is.EqualTo("4321"));
1478+
}
14251479
}
14261480
}

0 commit comments

Comments
 (0)