Skip to content

Commit 22a1a46

Browse files
kakoneStéphane Mitermite
andauthored
Fixes #149 (#148)
Co-authored-by: Stéphane Mitermite <[email protected]>
1 parent 5309cfa commit 22a1a46

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

src/AutoMapper.Extensions.ExpressionMapping/MapIncludesVisitor.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Linq.Expressions;
5-
using System.Reflection;
6-
using AutoMapper.Internal;
75
using AutoMapper.Extensions.ExpressionMapping.Extensions;
86
using AutoMapper.Extensions.ExpressionMapping.Structures;
97

@@ -18,6 +16,9 @@ public MapIncludesVisitor(IMapper mapper, IConfigurationProvider configurationPr
1816

1917
protected override Expression VisitLambda<T>(Expression<T> node)
2018
{
19+
if (!node.Body.Type.IsLiteralType())
20+
return base.VisitLambda(node);
21+
2122
var ex = this.Visit(node.Body);
2223

2324
var mapped = Expression.Lambda(ex, node.GetDestinationParameterExpressions(this.InfoDictionary, this.TypeMappings));
@@ -27,6 +28,9 @@ protected override Expression VisitLambda<T>(Expression<T> node)
2728

2829
protected override Expression VisitMember(MemberExpression node)
2930
{
31+
if (!node.Type.IsLiteralType())
32+
return base.VisitMember(node);
33+
3034
var parameterExpression = node.GetParameterExpression();
3135
if (parameterExpression == null)
3236
return base.VisitMember(node);

tests/AutoMapper.Extensions.ExpressionMapping.UnitTests/XpressionMapperTests.cs

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,20 @@ public void Map_includes_trim_string_nested_in_select_using_explicit_types()
147147
Assert.True(cars.Count == 4);
148148
}
149149

150+
private static bool IncludeTest<T, TProperty>(IQueryable<T> model, Expression<Func<T, TProperty>> navigationPropertyPath)
151+
{
152+
return !model.Equals(navigationPropertyPath);
153+
}
154+
155+
[Fact]
156+
public void Map_includes_with_include_method_call()
157+
{
158+
Expression<Func<IQueryable<PurchaseOrderDTO>, bool>> selection = s => IncludeTest(s, s => s.ValidLines);
159+
var expression = mapper.MapExpressionAsInclude<Expression<Func<IQueryable<PurchaseOrder>, bool>>>(selection);
160+
161+
Assert.True(expression.Compile()(new List<PurchaseOrder>() { new PurchaseOrder() }.AsQueryable()));
162+
}
163+
150164
[Fact]
151165
public void Map_object_type_change()
152166
{
@@ -625,8 +639,8 @@ public void Map_orderBy_thenBy_To_Dictionary_Select_expression_without_generic_t
625639
//Act
626640
Expression<Func<IQueryable<User>, IEnumerable<object>>> expMapped = (Expression<Func<IQueryable<User>, IEnumerable<object>>>)mapper.MapExpression
627641
(
628-
grouped,
629-
typeof(Expression<Func<IQueryable<UserModel>, IEnumerable<object>>>),
642+
grouped,
643+
typeof(Expression<Func<IQueryable<UserModel>, IEnumerable<object>>>),
630644
typeof(Expression<Func<IQueryable<User>, IEnumerable<object>>>)
631645
);
632646

@@ -1019,7 +1033,7 @@ public void Can_map_expression_with_multiple_destination_parameters_of_the_same_
10191033

10201034
Assert.Equal
10211035
(//parameters ch and c are of the same type
1022-
"c => ((c.Finished == null) AndAlso Not(c.Part.History.Any(ch => (ch.Started > c.Started))))",
1036+
"c => ((c.Finished == null) AndAlso Not(c.Part.History.Any(ch => (ch.Started > c.Started))))",
10231037
mappedExpression.ToString()
10241038
);
10251039
}
@@ -1094,7 +1108,7 @@ private static void SetupQueryableCollection()
10941108
new Thing { Bar = "Bar4", Car = new Car { Color = "White", Year = 2015 } }
10951109
},
10961110
Type = "Business",
1097-
Users = new User[]
1111+
Users = new User[]
10981112
{
10991113
new User
11001114
{
@@ -1265,6 +1279,16 @@ public interface IUserModel : IIdentifiable
12651279
bool IsActive { get; set; }
12661280
}
12671281

1282+
public class PurchaseOrder
1283+
{
1284+
public IList<OrderLine> Lines { get; set; }
1285+
}
1286+
1287+
public class PurchaseOrderDTO
1288+
{
1289+
public IEnumerable<OrderLineDTO> ValidLines { get; set; }
1290+
}
1291+
12681292
public class OrderLine
12691293
{
12701294
public int Id { get; set; }
@@ -1415,7 +1439,7 @@ class ItemEntity
14151439
public string Name { get; set; }
14161440
}
14171441

1418-
class ListParentExtension
1442+
class ListParentExtension
14191443
{
14201444
public ListExtension List { get; set; }
14211445
}
@@ -1588,6 +1612,9 @@ public OrganizationProfile()
15881612
CreateMap<Item, ItemDto>()
15891613
.ForMember(dest => dest.CreateDate, opts => opts.MapFrom(x => x.Date));
15901614

1615+
CreateMap<PurchaseOrder, PurchaseOrderDTO>()
1616+
.ForMember(d => d.ValidLines, opt => opt.MapFrom(s => s.Lines.Where(l => l.Quantity > 0)));
1617+
15911618
CreateMap<OrderLine, ItemDTO>()
15921619
.ForMember(dto => dto.Name, conf => conf.MapFrom(src => src.ItemName));
15931620
CreateMap<OrderLine, OrderLineDTO>()

0 commit comments

Comments
 (0)