Skip to content

Commit ba5ab0f

Browse files
author
Steven van Dam
committed
Resolve Issue 93
1 parent af69b1e commit ba5ab0f

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

src/AutoMapper.Extensions.ExpressionMapping/XpressionMapperVisitor.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ Expression GetMappedMemberExpression(Expression parentExpression, List<PropertyM
102102
}
103103
}
104104

105-
protected MemberExpression GetMemberExpressionFromMemberMaps(string fullName, Expression visitedParentExpr)
105+
protected MemberExpression GetMemberExpressionFromMemberMaps(string fullName, Expression visitedParentExpr)
106106
=> ExpressionHelpers.MemberAccesses(fullName, visitedParentExpr);
107107

108108
private Expression GetMemberExpressionFromCustomExpression(PropertyMapInfo lastWithCustExpression,
@@ -130,7 +130,7 @@ Expression PrependParentMemberExpression(PrependParentNameVisitor visitor)
130130
);
131131
}
132132

133-
protected Expression GetMemberExpressionFromCustomExpression(List<PropertyMapInfo> propertyMapInfoList, PropertyMapInfo lastWithCustExpression, Expression mappedParentExpr)
133+
protected Expression GetMemberExpressionFromCustomExpression(List<PropertyMapInfo> propertyMapInfoList, PropertyMapInfo lastWithCustExpression, Expression mappedParentExpr)
134134
=> GetMemberExpressionFromCustomExpression
135135
(
136136
lastWithCustExpression,
@@ -306,7 +306,9 @@ Expression DoVisitUnary(Expression updated)
306306
protected override Expression VisitConstant(ConstantExpression node)
307307
{
308308
if (this.TypeMappings.TryGetValue(node.Type, out Type newType)
309-
&& ConfigurationProvider.ResolveTypeMap(node.Type, newType) != null)
309+
&& ConfigurationProvider.ResolveTypeMap(newType, node.Type) != null)
310+
//The destination becomes the source because to map a source expression to a destination expression,
311+
//we need the expressions used to create the source from the destination
310312
return base.VisitConstant(Expression.Constant(Mapper.MapObject(node.Value, node.Type, newType), newType));
311313
//Issue 3455 (Non-Generic Mapper.Map failing for structs in v10)
312314
//return base.VisitConstant(Expression.Constant(Mapper.Map(node.Value, node.Type, newType), newType));
@@ -410,13 +412,13 @@ private static void AddPropertyMapInfo(Type parentType, string name, List<Proper
410412
}
411413
}
412414

413-
private bool GenericTypeDefinitionsAreEquivalent(Type typeSource, Type typeDestination)
415+
private bool GenericTypeDefinitionsAreEquivalent(Type typeSource, Type typeDestination)
414416
=> typeSource.IsGenericType() && typeDestination.IsGenericType() && typeSource.GetGenericTypeDefinition() == typeDestination.GetGenericTypeDefinition();
415417

416418
protected void FindDestinationFullName(Type typeSource, Type typeDestination, string sourceFullName, List<PropertyMapInfo> propertyMapInfoList)
417419
{
418420
const string period = ".";
419-
421+
420422
if (typeSource == typeDestination)
421423
{
422424
var sourceFullNameArray = sourceFullName.Split(new[] { period[0] }, StringSplitOptions.RemoveEmptyEntries);
@@ -459,7 +461,7 @@ protected void FindDestinationFullName(Type typeSource, Type typeDestination, st
459461

460462
var sourceType = typeSource.GetFieldOrProperty(propertyName).GetMemberType();
461463
var destType = typeDestination.GetFieldOrProperty(propertyName).GetMemberType();
462-
464+
463465
TypeMappings.AddTypeMapping(ConfigurationProvider, sourceType, destType);
464466

465467
var childFullName = sourceFullName.Substring(sourceFullName.IndexOf(period, StringComparison.OrdinalIgnoreCase) + 1);

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,27 @@ public void Issue85()
3434
Assert.NotNull(mapped2);
3535
}
3636

37+
[Fact]
38+
public void Issue93()
39+
{
40+
var config = new MapperConfiguration(cfg =>
41+
{
42+
cfg.AddExpressionMapping();
43+
44+
cfg.CreateMap<Source, SourceDto>()
45+
.ForMember(o => o.Items, config => config.MapFrom(p => p.Items.Select(s => s.Name)));
46+
});
47+
48+
var mapper = config.CreateMapper();
49+
50+
Expression<Func<SourceDto, bool>> expression1 =
51+
src => ((src != null ? src : null) != null) && src.Items.Any(x => x == "item1");
52+
53+
var mapped1 = mapper.MapExpression<Expression<Func<Source, bool>>>(expression1);
54+
55+
Assert.NotNull(mapped1);
56+
}
57+
3758
public class Source { public ICollection<SubSource> Items { get; set; } }
3859

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

0 commit comments

Comments
 (0)