Skip to content

Commit cf1443d

Browse files
committed
Fixing null checks on custom data source linq expression string parameters, re: #225
1 parent cba0e12 commit cf1443d

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

AgileMapper.UnitTests/Configuration/DataSources/WhenConfiguringDataSources.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,37 @@ public void ShouldApplyAConfiguredExpression()
234234
}
235235
}
236236

237+
[Fact]
238+
public void ShouldApplyAConfiguredLinqExpression()
239+
{
240+
using (var mapper = Mapper.CreateNew())
241+
{
242+
mapper.WhenMapping
243+
.From<Issue225.Address>()
244+
.To<Address>()
245+
.Map((sourceAddress, _) => string.Join(", ", new[]
246+
{
247+
sourceAddress.SubBuilding,
248+
sourceAddress.Building,
249+
sourceAddress.BuildingNumber,
250+
sourceAddress.DependentThoroughfare,
251+
sourceAddress.Thoroughfare
252+
}.Where(addressPart => !string.IsNullOrWhiteSpace(addressPart))
253+
.Select(addressPart => addressPart.Trim())))
254+
.To(targetAddress => targetAddress.Line1);
255+
256+
var source = new Issue225.Address
257+
{
258+
SubBuilding = "Building",
259+
BuildingNumber = "1",
260+
DependentThoroughfare = "DependentThoroughfare"
261+
};
262+
var result = mapper.Map(source).ToANew<Address>();
263+
264+
result.Line1.ShouldBe("Building, 1, DependentThoroughfare");
265+
}
266+
}
267+
237268
[Fact]
238269
public void ShouldApplyASourceMemberConditionally()
239270
{
@@ -1587,6 +1618,23 @@ internal T ReturnInstance<T>()
15871618
return new T();
15881619
}
15891620

1621+
public static class Issue225
1622+
{
1623+
public class Address
1624+
{
1625+
public string SubBuilding { get; set; }
1626+
1627+
public string Building { get; set; }
1628+
1629+
public string BuildingNumber { get; set; }
1630+
1631+
public string DependentThoroughfare { get; set; }
1632+
1633+
public string Thoroughfare { get; set; }
1634+
}
1635+
}
1636+
1637+
15901638
#endregion
15911639
}
15921640
}

AgileMapper/Members/MemberExtensions/NestedAccessChecksFactory.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,11 +307,15 @@ private bool AccessSubjectCouldBeNull(Expression expression)
307307
memberAccess = (MemberExpression)expression;
308308
subject = memberAccess.Expression;
309309
}
310-
else
310+
else if (expression.NodeType == Call)
311311
{
312312
memberAccess = null;
313313
subject = ((MethodCallExpression)expression).Object;
314314
}
315+
else
316+
{
317+
return expression.Type.CanBeNull();
318+
}
315319

316320
if (subject == null)
317321
{

0 commit comments

Comments
 (0)