AutoMapper/AutoMapper.Extensions.ExpressionMapping

Exception Mapping expression with IEnumerable<T>Contains method call on a local variable

BlaiseD opened this issue · 1 comments

Steps to reproduce:

       [Fact]
       public void TestMethod()
       {
           var config = new MapperConfiguration(cfg =>
           {
               cfg.AddExpressionMapping();

               cfg.CreateMap<Source, SourceDto>()
                   .ForMember(o => o.Items, config => config.MapFrom(p => p.Items.Select(s => s.Name)));
           });

           var mapper = config.CreateMapper();

           var items = new string[] { "item1", "item2" };
           Expression<Func<SourceDto, bool>> expression = o => items.Contains("");

           //exception thrown on MapExpression
           var mapped = mapper.MapExpression<Expression<Func<Source, bool>>>(expression);
       }

       public class Source { public ICollection<SubSource> Items { get; set; } }

       public class SubSource { public int ID { get; set; } public string Name { get; set; } }

       public class SourceDto { public string[] Items { get; set; } }
Result StackTrace:	
at System.Linq.Expressions.Expression.FindMethod(Type type, String methodName, Type[] typeArgs, Expression[] args, BindingFlags flags)
   at System.Linq.Expressions.Expression.Call(Type type, String methodName, Type[] typeArguments, Expression[] arguments)
   at AutoMapper.Extensions.ExpressionMapping.XpressionMapperVisitor.<>c__DisplayClass29_0.<VisitMethodCall>g__GetStaticExpression|3() in C:\CSharp\Code\.AutoMapper\AutoMapper.Extensions.ExpressionMapping\src\AutoMapper.Extensions.ExpressionMapping\XpressionMapperVisitor.cs:line 391
   at AutoMapper.Extensions.ExpressionMapping.XpressionMapperVisitor.VisitMethodCall(MethodCallExpression node) in C:\CSharp\Code\.AutoMapper\AutoMapper.Extensions.ExpressionMapping\src\AutoMapper.Extensions.ExpressionMapping\XpressionMapperVisitor.cs:line 381
   at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
   at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
   at AutoMapper.Extensions.ExpressionMapping.MapperExtensions.<MapExpression>g__MapBody|8_1[TDestDelegate](Dictionary`2 typeMappings, XpressionMapperVisitor visitor, <>c__DisplayClass8_0`1& ) in C:\CSharp\Code\.AutoMapper\AutoMapper.Extensions.ExpressionMapping\src\AutoMapper.Extensions.ExpressionMapping\MapperExtensions.cs:line 100
   at AutoMapper.Extensions.ExpressionMapping.MapperExtensions.<MapExpression>g__CreateVisitor|8_0[TDestDelegate](Dictionary`2 typeMappings, <>c__DisplayClass8_0`1& ) in C:\CSharp\Code\.AutoMapper\AutoMapper.Extensions.ExpressionMapping\src\AutoMapper.Extensions.ExpressionMapping\MapperExtensions.cs:line 97
   at AutoMapper.Extensions.ExpressionMapping.MapperExtensions.MapExpression[TDestDelegate](IConfigurationProvider configurationProvider, LambdaExpression expression, Type typeSourceFunc, Type typeDestFunc, Func`3 getVisitor) in C:\CSharp\Code\.AutoMapper\AutoMapper.Extensions.ExpressionMapping\src\AutoMapper.Extensions.ExpressionMapping\MapperExtensions.cs:line 94
   at AutoMapper.Extensions.ExpressionMapping.MapperExtensions.MapExpression[TDestDelegate](IMapper mapper, LambdaExpression expression, Func`3 getVisitor) in C:\CSharp\Code\.AutoMapper\AutoMapper.Extensions.ExpressionMapping\src\AutoMapper.Extensions.ExpressionMapping\MapperExtensions.cs:line 77
   at AutoMapper.Extensions.ExpressionMapping.MapperExtensions.MapExpression[TDestDelegate](IMapper mapper, LambdaExpression expression) in C:\CSharp\Code\.AutoMapper\AutoMapper.Extensions.ExpressionMapping\src\AutoMapper.Extensions.ExpressionMapping\MapperExtensions.cs:line 67
   at AutoMapper.Extensions.ExpressionMapping.UnitTests.ShouldOnlyMapExistingTypeMaps.Issue85() in C:\CSharp\Code\.AutoMapper\AutoMapper.Extensions.ExpressionMapping\tests\AutoMapper.Extensions.ExpressionMapping.UnitTests\ShouldOnlyMapExistingTypeMaps.cs:line 26
Result Message:	System.InvalidOperationException : No generic method 'Contains' on type 'System.Linq.Enumerable' is compatible with the supplied type arguments and arguments. No type arguments should be provided if the method is non-generic.


Fixed in v4.0.1-preview-4.

cc @henrbj - should work as in 3.1.2 now.