From 52713d163e68cc966411ed918f37d4368c25539c Mon Sep 17 00:00:00 2001 From: Lucian Bargaoanu Date: Fri, 12 May 2017 16:38:29 +0300 Subject: [PATCH] check the underlying type --- src/AutoMapper/ConfigurationValidator.cs | 11 +++++--- src/UnitTests/Bug/NullableToInvalid.cs | 34 ++++++++++++++++++++++++ src/UnitTests/UnitTests.Net4.csproj | 1 + 3 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 src/UnitTests/Bug/NullableToInvalid.cs diff --git a/src/AutoMapper/ConfigurationValidator.cs b/src/AutoMapper/ConfigurationValidator.cs index 811eb7d728..22a5078c20 100644 --- a/src/AutoMapper/ConfigurationValidator.cs +++ b/src/AutoMapper/ConfigurationValidator.cs @@ -85,10 +85,16 @@ private void DryRunTypeMap(ICollection typeMapsChecked, TypePair types, } var context = new ValidationContext(types, propertyMap, mapperToUse); _config.Validate(context); - if (mapperToUse is ArrayMapper || mapperToUse is EnumerableMapper || mapperToUse is CollectionMapper) + if(mapperToUse is ArrayMapper || mapperToUse is EnumerableMapper || mapperToUse is CollectionMapper) { CheckElementMaps(typeMapsChecked, types, propertyMap); } + else if(mapperToUse is NullableSourceMapper) + { + var newTypePair = new TypePair(Nullable.GetUnderlyingType(types.SourceType), types.DestinationType); + var memberTypeMap = _config.ResolveTypeMap(newTypePair.SourceType, newTypePair.DestinationType); + DryRunTypeMap(typeMapsChecked, newTypePair, memberTypeMap, propertyMap); + } } } @@ -119,8 +125,7 @@ private void CheckPropertyMaps(ICollection typeMapsChecked, TypeMap typ return; var destinationType = propertyMap.DestinationProperty.GetMemberType(); - var memberTypeMap = _config.ResolveTypeMap(sourceType, - destinationType); + var memberTypeMap = _config.ResolveTypeMap(sourceType, destinationType); if (typeMapsChecked.Any(tm => Equals(tm, memberTypeMap))) continue; diff --git a/src/UnitTests/Bug/NullableToInvalid.cs b/src/UnitTests/Bug/NullableToInvalid.cs new file mode 100644 index 0000000000..7b35438013 --- /dev/null +++ b/src/UnitTests/Bug/NullableToInvalid.cs @@ -0,0 +1,34 @@ +using System; +using Should; +using Xunit; + +namespace AutoMapper.UnitTests.Bug +{ + public class NullableToInvalid : NonValidatingSpecBase + { + public class Source + { + public int? Value { get; set; } + } + + public class Destination + { + public SomeObject Value { get; set; } + } + + public class SomeObject + { + } + + protected override MapperConfiguration Configuration { get; } = new MapperConfiguration(cfg => + { + cfg.CreateMap(); + }); + + [Fact] + public void Should_not_validate() + { + new Action(Configuration.AssertConfigurationIsValid).ShouldThrow(); + } + } +} diff --git a/src/UnitTests/UnitTests.Net4.csproj b/src/UnitTests/UnitTests.Net4.csproj index adc9a18c5d..36d2faec81 100644 --- a/src/UnitTests/UnitTests.Net4.csproj +++ b/src/UnitTests/UnitTests.Net4.csproj @@ -86,6 +86,7 @@ +