Skip to content

Commit efbc140

Browse files
committed
Removing map-to-null application from collection element mapping
1 parent 954640c commit efbc140

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

AgileMapper.UnitTests/Configuration/WhenMappingToNull.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
namespace AgileObjects.AgileMapper.UnitTests.Configuration
22
{
33
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
46
using AgileMapper.Configuration;
57
using Shouldly;
68
using TestClasses;
@@ -130,6 +132,31 @@ public void ShouldApplyConfiguredConditionsToDerivedTypes()
130132
}
131133
}
132134

135+
[Fact]
136+
public void ShouldNotMapCollectionElementsToNull()
137+
{
138+
using (var mapper = Mapper.CreateNew())
139+
{
140+
mapper.WhenMapping
141+
.To<Address>()
142+
.If((o, a) => a.Line2 == "Delete me")
143+
.MapToNull();
144+
145+
var source = new[]
146+
{
147+
new Address { Line1 = "Delete me" },
148+
new Address { Line2 = "Delete me" }
149+
};
150+
var result = mapper.Map(source).ToANew<ICollection<Address>>();
151+
152+
result.First().ShouldNotBeNull();
153+
result.First().Line1.ShouldBe("Delete me");
154+
155+
result.Second().ShouldNotBeNull();
156+
result.Second().Line2.ShouldBe("Delete me");
157+
}
158+
}
159+
133160
[Fact]
134161
public void ShouldErrorIfConditionsAreConfiguredForTheSameType()
135162
{

AgileMapper/Configuration/MapToNullCondition.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ public override bool ConflictsWith(UserConfiguredItemBase otherConfiguredItem)
2222
return otherCondition._targetType == _targetType;
2323
}
2424

25+
public override bool AppliesTo(IBasicMapperData mapperData)
26+
{
27+
if (mapperData.TargetMemberIsEnumerableElement())
28+
{
29+
return false;
30+
}
31+
32+
return base.AppliesTo(mapperData);
33+
}
34+
2535
protected override Expression GetConditionOrNull(IMemberMapperData mapperData, CallbackPosition position)
2636
{
2737
mapperData.Context.UsesMappingDataObjectAsParameter =

0 commit comments

Comments
 (0)