Skip to content

Commit e5e189b

Browse files
authored
Merge pull request #47 from BlaiseD/master
Fix for Issue #45 (Mapping of conditional expressions throws a null reference exception)
2 parents 740c718 + c69da46 commit e5e189b

File tree

4 files changed

+43
-3
lines changed

4 files changed

+43
-3
lines changed

AutoMapper.Extensions.ExpressionMapping.sln

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio 15
4-
VisualStudioVersion = 15.0.27130.2020
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.29001.49
55
MinimumVisualStudioVersion = 15.0.26124.0
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AutoMapper.Extensions.ExpressionMapping", "src\AutoMapper.Extensions.ExpressionMapping\AutoMapper.Extensions.ExpressionMapping.csproj", "{24DF305C-EE59-460A-BA97-4B7CD5505434}"
77
EndProject

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<PropertyGroup>
33
<Authors>Jimmy Bogard</Authors>
44
<LangVersion>latest</LangVersion>
5-
<VersionPrefix>3.0.2</VersionPrefix>
5+
<VersionPrefix>3.0.3-preview01</VersionPrefix>
66
<WarningsAsErrors>true</WarningsAsErrors>
77
<NoWarn>$(NoWarn);1701;1702;1591</NoWarn>
88
</PropertyGroup>

src/AutoMapper.Extensions.ExpressionMapping/Extensions/VisitorExtensions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ private static bool IsMemberOrParameterExpression(Expression expression)
4848
/// <returns></returns>
4949
public static string GetPropertyFullName(this Expression expression)
5050
{
51+
if (expression == null)
52+
return string.Empty;
53+
5154
const string period = ".";
5255

5356
//the node represents parameter of the expression

tests/AutoMapper.Extensions.ExpressionMapping.UnitTests/XpressionMapperTests.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,19 @@ public void Can_map_expression_when_mapped_when_members_parent_is_a_method()
757757
//Assert
758758
Assert.True(res.Count == 1);
759759
}
760+
761+
[Fact]
762+
public void Can_map_expression_with_condittional_logic_while_deflattening()
763+
{
764+
/*
765+
CreateMap<TestEntity, TestDTO>()
766+
.ForMember(dst => dst.NestedClass, opt => opt.MapFrom(src => src.ConditionField == 1 ? src : default));*/
767+
Expression<Func<TestDTO, bool>> expr = x => x.NestedClass.NestedField == 1;
768+
769+
var mappedExpression = mapper.MapExpression<Expression<Func<TestEntity, bool>>>(expr);
770+
771+
Assert.NotNull(mappedExpression);
772+
}
760773
#endregion Tests
761774

762775
private static void SetupAutoMapper()
@@ -1192,6 +1205,23 @@ internal class EventModel
11921205
public DateTime EventDate { get; set; }
11931206
}
11941207

1208+
public class TestEntity
1209+
{
1210+
public Guid Id { get; set; }
1211+
public int ConditionField { get; set; }
1212+
public int ToBeNestedField { get; set; }
1213+
}
1214+
1215+
public class TestDTO
1216+
{
1217+
public Guid Id { get; set; }
1218+
public TestDTONestedClass NestedClass { get; set; }
1219+
}
1220+
public class TestDTONestedClass
1221+
{
1222+
public int? NestedField { get; set; }
1223+
}
1224+
11951225
public class OrganizationProfile : Profile
11961226
{
11971227
public OrganizationProfile()
@@ -1297,6 +1327,13 @@ public OrganizationProfile()
12971327

12981328
CreateMap<EmployeeModel, EmployeeEntity>().ReverseMap();
12991329
CreateMap<EventModel, EventEntity>().ReverseMap();
1330+
1331+
CreateMap<TestEntity, TestDTO>()
1332+
.ForMember(dst => dst.Id, opt => opt.MapFrom(src => src.Id))
1333+
.ForMember(dst => dst.NestedClass, opt => opt.MapFrom(src => src.ConditionField == 1 ? src : default));
1334+
1335+
CreateMap<TestEntity, TestDTONestedClass>()
1336+
.ForMember(dst => dst.NestedField, opt => opt.MapFrom(src => (int?)src.ToBeNestedField));
13001337
}
13011338
}
13021339

0 commit comments

Comments
 (0)