Skip to content

Regression - Exception in System.Linq from NullableSourceMapper #2054

@Silvenga

Description

@Silvenga

Consider the following case:

int? Value -> IncorrectMapDest Value

Under AutoMapper 5.2.0 the above correctly failed to map, after upgrading to 6.0.2 the following exception in System.Linq is thrown:

System.ArgumentException with message "Argument types do not match"
     at System.Linq.Expressions.Expression.Condition(Expression test, Expression ifTrue, Expression ifFalse)
     at AutoMapper.Mappers.NullableSourceMapper.MapExpression(IConfigurationProvider configurationProvider, ProfileMap profileMap, PropertyMap propertyMap, Expression sourceExpression, Expression destExpression, Expression contextExpression)
     at AutoMapper.Execution.TypeMapPlanBuilder.MapExpression(IConfigurationProvider configurationProvider, ProfileMap profileMap, TypePair typePair, Expression sourceParameter, Expression contextParameter, PropertyMap propertyMap, Expression destinationParameter)
     at AutoMapper.Execution.TypeMapPlanBuilder.CreatePropertyMapFunc(PropertyMap propertyMap)
     at AutoMapper.Execution.TypeMapPlanBuilder.TryPropertyMap(PropertyMap propertyMap)
     at AutoMapper.Execution.TypeMapPlanBuilder.CreateAssignmentFunc(Expression destinationFunc, Boolean constructorMapping)
     at AutoMapper.Execution.TypeMapPlanBuilder.CreateMapperLambda()
     at AutoMapper.TypeMap.Seal(IConfigurationProvider configurationProvider)
     at AutoMapper.MapperConfiguration.ResolveTypeMap(TypePair typePair)
     at AutoMapper.MapperConfiguration.BuildExecutionPlan(MapRequest mapRequest)
     at AutoMapper.MapperConfiguration.CreateMapperFuncs(MapRequest mapRequest)
     at AutoMapper.LockingConcurrentDictionary`2.<>c__DisplayClass2_1.<.ctor>b__1()
     at System.Lazy`1.CreateValue()
     at System.Lazy`1.LazyInitValue()
     at System.Lazy`1.get_Value()
     at AutoMapper.MapperConfiguration.GetUntypedMapperFunc(MapRequest mapRequest)
     at AutoMapper.Mapper.AutoMapper.IMapper.Map[TDestination](Object source)

Example:

void Main()
{
	IMapper mapper = new Mapper(new MapperConfiguration(cfg =>
	{
		cfg.CreateMissingTypeMaps = true;
	}));

	var a = new A();
	var b = mapper.Map<B>(a);
}

public class A
{
	public int? Value { get; set; }
}

public class B
{
	public SomeObject Value { get; set; }
}

public class SomeObject
{
	public int Something { get; set; }
	
	public SomeObject(string a)
	{
	}
}

Result (5.2.0)

AutoMapperMappingException: Error mapping types.

Mapping types:
A -> B
UserQuery+A -> UserQuery+B

Type Map configuration:
A -> B
UserQuery+A -> UserQuery+B

Property:
Value

Result (6.0.2)

Argument types do not match

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions