From 630062c85f5aaa500b59c335cb2c0b3ac8d2b651 Mon Sep 17 00:00:00 2001 From: Lucian Bargaoanu Date: Tue, 22 May 2018 17:38:17 +0300 Subject: [PATCH 1/3] the destination doesn't seem useful for nullables --- .../Mappers/NullableDestinationMapper.cs | 3 +- src/UnitTests/Bug/NullableUntypedMapFrom.cs | 35 +++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 src/UnitTests/Bug/NullableUntypedMapFrom.cs diff --git a/src/AutoMapper/Mappers/NullableDestinationMapper.cs b/src/AutoMapper/Mappers/NullableDestinationMapper.cs index 63e504d424..bb487cd18b 100644 --- a/src/AutoMapper/Mappers/NullableDestinationMapper.cs +++ b/src/AutoMapper/Mappers/NullableDestinationMapper.cs @@ -16,8 +16,7 @@ public Expression MapExpression(IConfigurationProvider configurationProvider, Pr new TypePair(sourceExpression.Type, Nullable.GetUnderlyingType(destExpression.Type)), sourceExpression, contextExpression, - propertyMap, - Expression.Property(destExpression, destExpression.Type.GetDeclaredProperty("Value")) + propertyMap ); public TypePair GetAssociatedTypes(TypePair initialTypes) diff --git a/src/UnitTests/Bug/NullableUntypedMapFrom.cs b/src/UnitTests/Bug/NullableUntypedMapFrom.cs new file mode 100644 index 0000000000..190780519f --- /dev/null +++ b/src/UnitTests/Bug/NullableUntypedMapFrom.cs @@ -0,0 +1,35 @@ +using Xunit; +using Shouldly; + +namespace AutoMapper.UnitTests.Bug +{ + public class NullableUntypedMapFrom : AutoMapperSpecBase + { + private Destination _destination; + + class Source + { + public decimal? Number { get; set; } + } + class Destination + { + public decimal? OddNumber { get; set; } + } + + protected override MapperConfiguration Configuration { get; } = new MapperConfiguration(cfg => + { + cfg.CreateMap().ForMember(d => d.OddNumber, o => o.ResolveUsing(s => (object)s.Number)); + }); + + protected override void Because_of() + { + _destination = Mapper.Map(new Source { Number = 12 }); + } + + [Fact] + public void Should_map_nullable_decimal_with_ResolveUsing() + { + _destination.OddNumber.ShouldBe(12); + } + } +} \ No newline at end of file From ccff4fe0b38cbd26b9a7afed7cfca2415575e242 Mon Sep 17 00:00:00 2001 From: Lucian Bargaoanu Date: Tue, 22 May 2018 17:40:26 +0300 Subject: [PATCH 2/3] rename --- src/UnitTests/Bug/NullableUntypedMapFrom.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/UnitTests/Bug/NullableUntypedMapFrom.cs b/src/UnitTests/Bug/NullableUntypedMapFrom.cs index 190780519f..5c256d287a 100644 --- a/src/UnitTests/Bug/NullableUntypedMapFrom.cs +++ b/src/UnitTests/Bug/NullableUntypedMapFrom.cs @@ -27,7 +27,7 @@ protected override void Because_of() } [Fact] - public void Should_map_nullable_decimal_with_ResolveUsing() + public void Should_map_nullable_decimal() { _destination.OddNumber.ShouldBe(12); } From ebf0214d20fa7e6a99d6f221cc3b82d8925c7e0e Mon Sep 17 00:00:00 2001 From: Lucian Bargaoanu Date: Tue, 22 May 2018 17:51:23 +0300 Subject: [PATCH 3/3] switch to MapFrom --- src/UnitTests/Bug/NullableUntypedMapFrom.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/UnitTests/Bug/NullableUntypedMapFrom.cs b/src/UnitTests/Bug/NullableUntypedMapFrom.cs index 5c256d287a..635f10a3ee 100644 --- a/src/UnitTests/Bug/NullableUntypedMapFrom.cs +++ b/src/UnitTests/Bug/NullableUntypedMapFrom.cs @@ -18,7 +18,7 @@ class Destination protected override MapperConfiguration Configuration { get; } = new MapperConfiguration(cfg => { - cfg.CreateMap().ForMember(d => d.OddNumber, o => o.ResolveUsing(s => (object)s.Number)); + cfg.CreateMap().ForMember(d => d.OddNumber, o => o.MapFrom(s => (object)s.Number)); }); protected override void Because_of()