Skip to content

Commit

Permalink
Handling local constant expression. (#181)
Browse files Browse the repository at this point in the history
  • Loading branch information
BlaiseD authored Sep 14, 2024
1 parent 9f77f08 commit 2e4ea0c
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,9 @@ protected override Expression VisitConstant(ConstantExpression node)
return base.VisitConstant(Expression.Constant(Mapper.MapObject(node.Value, node.Type, newType), newType));
//Issue 3455 (Non-Generic Mapper.Map failing for structs in v10)
//return base.VisitConstant(Expression.Constant(Mapper.Map(node.Value, node.Type, newType), newType));

if (typeof(Expression).IsAssignableFrom(node.Type))
return Expression.Constant(this.Visit((Expression)node.Value), newType);
}
return base.VisitConstant(node);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Xunit;

namespace AutoMapper.Extensions.ExpressionMapping.UnitTests
{
public class CanMapExpressionWithLocalExpressionConstant
{
[Fact]
public void Map_expression_wchich_includes_local_constant()
{
//Arrange
var config = new MapperConfiguration
(
cfg =>
{
cfg.CreateMap<EntityModel, Entity>();
cfg.CreateMap<Entity, EntityModel>();
}
);
config.AssertConfigurationIsValid();
var mapper = config.CreateMapper();
List<Entity> source = [
new Entity { Id = 1 },
new Entity { Id = 3 }
];

//act
Expression<Func<EntityModel, bool>> filter = f => f.Id > 2;
Expression<Func<IQueryable<EntityModel>, IQueryable<EntityModel>>> queryableExpression = q => q.Where(filter);
Expression<Func<IQueryable<Entity>, IQueryable<Entity>>> queryableExpressionMapped = mapper.MapExpression<Expression<Func<IQueryable<Entity>, IQueryable<Entity>>>>(queryableExpression);

//assert
Assert.Equal(1, queryableExpressionMapped.Compile()(source.AsQueryable()).Count());
}

public record Entity
{
public int Id { get; init; }
}

public record EntityModel
{
public int Id { get; init; }
}
}
}

0 comments on commit 2e4ea0c

Please sign in to comment.