Skip to content

Commit 21b0636

Browse files
committed
null source member should overwrite exsting destination member
1 parent 73ba400 commit 21b0636

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

src/AutoMapper/Execution/ExpressionBuilder.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,7 @@ public static Expression MapExpression(this IGlobalConfiguration configuration,
7373
bool nullCheck;
7474
if (typeMap != null)
7575
{
76-
var allowNull = memberMap?.AllowNull;
77-
nullCheck = !typeMap.HasTypeConverter && allowNull.HasValue && allowNull != profileMap.AllowNullDestinationValues;
76+
nullCheck = !typeMap.HasTypeConverter;
7877
if (!typeMap.HasDerivedTypesToInclude)
7978
{
8079
typeMap.Seal(configuration);

src/UnitTests/NullBehavior.cs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,34 @@
11
namespace AutoMapper.UnitTests.NullBehavior;
2-
2+
public class NullToExistingValue : AutoMapperSpecBase
3+
{
4+
private record Person
5+
{
6+
public string Name { get; set; }
7+
public Address TheAddress { get; set; } = new();
8+
}
9+
private record Address
10+
{
11+
public string Street { get; set; }
12+
public int Number { get; set; }
13+
}
14+
private record PersonModel
15+
{
16+
public string Name { get; set; }
17+
public AddressModel TheAddress { get; set; }
18+
}
19+
private record AddressModel
20+
{
21+
public string Street { get; set; }
22+
public int Number { get; set; }
23+
}
24+
protected override MapperConfiguration CreateConfiguration() => new(c =>
25+
{
26+
c.CreateMap<PersonModel, Person>();
27+
c.CreateMap<AddressModel, Address>();
28+
});
29+
[Fact]
30+
public void Should_overwrite() => Mapper.Map(new PersonModel(), new Person()).TheAddress.ShouldBeNull();
31+
}
332
public class NullCheckDefault : AutoMapperSpecBase
433
{
534
class Source

0 commit comments

Comments
 (0)