Closed wbuck closed 1 year ago
@BlaiseD Thanks for the quick turn around. This was the cause of the issue using the IN
operator in the AutoMapper.Extensions.OData repo.
@BlaiseD One concern I have regarding your fix is the need to create an explicit mapping between the source
and destination
enum
types.
If I'm not mistaken Automapper
does not require an explicit mapping between enum
types (assuming they're compatible), take a look at Jimmy's answer here.
By using ResolveTypeMap
in CanMapConstant
this of course would require the user to create a explicit mapping between their enum
types (which you've done in the tests).
Would it make sense to add a special case for enum
types here?
First checking if the enum
types are compatible (first by name and if that fails by value)?
Or just return true
from CanMapConstant
if the srcType
and destType
are enum
types and let Automapper
proper determine if the enum
types are compatible?
It's also a bit confusing for the user because they have to create an opposite type map. E.g.,
// Mapping from model to entity.
cfg.CreateMap<EntityModel, Entity>();
// Reverse the mapping for the enum.
cfg.CreateMap<SimpleEnum, SimpleEnumModel>();
I guess I'm just a bit worried about the consequences of requiring an explicit enum
mapping on the users part.
You are correct - type maps are not needed for enums.
Synopsis
When attempting to convert an
Expression
generated from an OData query aSystem.InvalidOperationException
is thrown:I've been able to reproduce the error below.
It happens when the
Type
(in this case anenum
) argument for a collection needs to be mapped to a differentType
. For example:List<SimpleEnum>
->List<SimpleEnumModel>
Entities
Models
Mappings
Expression using
List<T>.Contains
Expression using
Enumerable.Contains<T>
extension methodThe exception is getting thrown here
Cause of exception when mapping
List<T>.Contains
The reason the exception is thrown in this case is because the
MethodCallExpression node
argumentsMethod
property has the wrongMethodInfo
information. ThatMethodInfo
is passed toExpression.Call
.node.Method == List<SimpleEnum>.Contains
this causes a problem as theConstantExpression.Type == SimpleEnumModel
.Cause of exception when mapping
Enumerable.Contains<T>
The reason the exception is thrown in this case is because the
listOfArgumentsForNewMethod
contains an argument with an incorrectType
.ConstantExpression.Type == List<SimpleEnum>
PropertyExpression.Type == SimpleEnumModel
In this case the first argument is incorrect and should be a constant
Expression
with theType
==List<SimpleEnumModel>
.