Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/AutoMapper/ConfigurationValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,16 @@ private void DryRunTypeMap(ICollection<TypeMap> 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);
}
}
}

Expand Down Expand Up @@ -119,8 +125,7 @@ private void CheckPropertyMaps(ICollection<TypeMap> 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;
Expand Down
34 changes: 34 additions & 0 deletions src/UnitTests/Bug/NullableToInvalid.cs
Original file line number Diff line number Diff line change
@@ -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<Source, Destination>();
});

[Fact]
public void Should_not_validate()
{
new Action(Configuration.AssertConfigurationIsValid).ShouldThrow<AutoMapperConfigurationException>();
}
}
}
1 change: 1 addition & 0 deletions src/UnitTests/UnitTests.Net4.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
<Compile Include="AssertionExtensions.cs" />
<Compile Include="BasicFlattening.cs" />
<Compile Include="Bug\CannotProjectStringToNullableEnum.cs" />
<Compile Include="Bug\NullableToInvalid.cs" />
<Compile Include="Bug\OneSourceWithMultipleDestinationsAndPreserveReferences.cs" />
<Compile Include="Bug\ConventionCreateMapsWithCircularReference.cs" />
<Compile Include="Bug\ConvertMapperThreading.cs" />
Expand Down