AutoMapper/AutoMapper.Extensions.ExpressionMapping

ArgumentException: Argument types do not match, 4.0.1 regression

Erythnul opened this issue · 0 comments

Hi,

We updated our package version to 4.0.1 and after the update, began to see ArgumentExceptions being thrown in our code.

I've traced the problem to change #86 implemented in 4.0.1, resolving #85.

We have more complex versions of expressions like this:

Expression<Func<SourceDto, bool>> expression1 = src => ((src != null ? src : null) != null) && src.Items.Any(x => x == "item1");

(the src != null part looks weird, in our code it's different but it reproduces the issue.)
We were previously able to successfully map these kind of expressions, but now the null constant is no longer mapped from it's SourceDto type to Source type.

I believe the reason is that the change to solve #85 has got the resolution for the type mappings switched around.
ConfigurationProvider.ResolveTypeMap(node.Type, newType) != null)
Should be
ConfigurationProvider.ResolveTypeMap(newType, node.Type) != null)

As noted in your code elsewhere:

//The destination becomes the source because to map a source expression to a destination expression,
//we need the expressions used to create the source from the destination

I've made the change and a unitTest to reproduce the scenario, the test fails with the pre-change code and succeeds after the change.
I'll make a PR with these changes.

Kind regards,

Steven.