Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for collection aggregate mapping issue #171

Closed

Conversation

mycroes
Copy link

@mycroes mycroes commented Aug 8, 2023

This PR relates to AutoMapper/AutoMapper#4329. While the bug is in AutoMapper, the result is quite unexpected. The first test will pass, but the second test throws the following exception:

System.InvalidOperationException
variable 'd' of type 'AutoMapper.Extensions.ExpressionMapping.UnitTests.MappingCollectionAggregateTests+Dest' referenced from scope '', but it is not defined
   at System.Linq.Expressions.Compiler.VariableBinder.Reference(ParameterExpression node, VariableStorageKind storage)
   at System.Linq.Expressions.Compiler.VariableBinder.VisitParameter(ParameterExpression node)
   at System.Linq.Expressions.ParameterExpression.Accept(ExpressionVisitor visitor)
   at System.Linq.Expressions.Compiler.VariableBinder.Visit(Expression node)
   at System.Linq.Expressions.MemberExpression.Accept(ExpressionVisitor visitor)
   at System.Linq.Expressions.Compiler.VariableBinder.Visit(Expression node)
   at System.Linq.Expressions.MemberExpression.Accept(ExpressionVisitor visitor)
   at System.Linq.Expressions.Compiler.VariableBinder.Visit(Expression node)
   at System.Linq.Expressions.MemberExpression.Accept(ExpressionVisitor visitor)
   at System.Linq.Expressions.Compiler.VariableBinder.Visit(Expression node)
   at System.Linq.Expressions.ExpressionVisitor.VisitBinary(BinaryExpression node)
   at System.Linq.Expressions.BinaryExpression.Accept(ExpressionVisitor visitor)
   at System.Linq.Expressions.Compiler.VariableBinder.Visit(Expression node)
   at System.Linq.Expressions.ExpressionVisitor.Visit(ReadOnlyCollection`1 nodes)
   at System.Linq.Expressions.Compiler.VariableBinder.VisitLambda[T](Expression`1 node)
   at System.Linq.Expressions.Expression`1.Accept(ExpressionVisitor visitor)
   at System.Linq.Expressions.Compiler.VariableBinder.Visit(Expression node)
   at System.Dynamic.Utils.ExpressionVisitorUtils.VisitArguments(ExpressionVisitor visitor, IArgumentProvider nodes)
   at System.Linq.Expressions.ExpressionVisitor.VisitMethodCall(MethodCallExpression node)
   at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
   at System.Linq.Expressions.Compiler.VariableBinder.Visit(Expression node)
   at System.Dynamic.Utils.ExpressionVisitorUtils.VisitArguments(ExpressionVisitor visitor, IArgumentProvider nodes)
   at System.Linq.Expressions.ExpressionVisitor.VisitMethodCall(MethodCallExpression node)
   at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
   at System.Linq.Expressions.Compiler.VariableBinder.Visit(Expression node)
   at System.Linq.Expressions.ExpressionVisitor.Visit(ReadOnlyCollection`1 nodes)
   at System.Linq.Expressions.Compiler.VariableBinder.VisitLambda[T](Expression`1 node)
   at System.Linq.Expressions.Expression`1.Accept(ExpressionVisitor visitor)
   at System.Linq.Expressions.Compiler.VariableBinder.Visit(Expression node)
   at System.Linq.Expressions.Compiler.LambdaCompiler.Compile(LambdaExpression lambda)
   at System.Linq.Expressions.Expression`1.Compile()
   at System.Linq.EnumerableQuery`1.GetEnumerator()
   at System.Linq.EnumerableQuery`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator()
   at System.Collections.Generic.LargeArrayBuilder`1.AddRange(IEnumerable`1 items)
   at System.Collections.Generic.EnumerableHelpers.ToArray[T](IEnumerable`1 source)
   at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
   at AutoMapper.Extensions.ExpressionMapping.Impl.SourceSourceInjectedQuery`2.GetEnumerator() in C:\Users\michaelc\Documents\Projects\AutoMapper.Extensions.ExpressionMapping\src\AutoMapper.Extensions.ExpressionMapping\Impl\SourceInjectedQuery.cs:line 63
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at AutoMapper.Extensions.ExpressionMapping.UnitTests.MappingCollectionAggregateTests.Maps_Date_Value_Filter() in C:\Users\michaelc\Documents\Projects\AutoMapper.Extensions.ExpressionMapping\tests\AutoMapper.Extensions.ExpressionMapping.UnitTests\MappingCollectionAggregateTests.cs:line 62

I'm not sure if AutoMapper or AutoMapper.Extensions.ExpressionMapping is to blame for this rather inconvenient exception, but using the workaround described in the AutoMapper issue does resolve this problem as well. Nevertheless it would be nice if AutoMapper.Extensions.ExpressionMapping could fail early while building the expression, with a proper reason why it can't resolve the correct expression. If AutoMapper is still to blame for this part as well, then maybe a unit test can be produced to prevent future regressions in that area.

@mycroes
Copy link
Author

mycroes commented Aug 8, 2023

@mycroes please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.

@dotnet-policy-service agree [company="{your company}"]

Options:

  • (default - no company specified) I have sole ownership of intellectual property rights to my Submissions and I am not making Submissions in the course of work for my employer.
@dotnet-policy-service agree
  • (when company given) I am making Submissions in the course of work for my employer (or my employer has intellectual property rights in my Submissions by contract or applicable law). I have permission from my employer to make Submissions and enter into this Agreement on behalf of my employer. By signing below, the defined term “You” includes me and my employer.
@dotnet-policy-service agree company="Microsoft"

Contributor License Agreement

@dotnet-policy-service agree company="Viscon Factory Intelligence B.V."

@BlaiseD
Copy link
Member

BlaiseD commented Aug 8, 2023

If you try the following you'll get a meaningful exception:

        Expression<Func<Dest, bool>> expression = d => d.ItemsTimestampMax.Value.Date == DateTime.Today;
        var mapper = Configuration.CreateMapper();
        Expression<Func<Source, bool>> expressionMapped = mapper.MapExpression<Expression<Func<Source, bool>>>(expression);

Consider submitting a fix with your test using similar code for UseAsDataSource .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants