Skip to content

Commit 0c499e6

Browse files
author
Steven van Dam
committed
Separate the null check from non-null mapping
1 parent ba5ab0f commit 0c499e6

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

src/AutoMapper.Extensions.ExpressionMapping/XpressionMapperVisitor.cs

Lines changed: 14 additions & 12 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,
@@ -305,14 +305,16 @@ Expression DoVisitUnary(Expression updated)
305305

306306
protected override Expression VisitConstant(ConstantExpression node)
307307
{
308-
if (this.TypeMappings.TryGetValue(node.Type, out Type newType)
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
312-
return base.VisitConstant(Expression.Constant(Mapper.MapObject(node.Value, node.Type, newType), newType));
313-
//Issue 3455 (Non-Generic Mapper.Map failing for structs in v10)
314-
//return base.VisitConstant(Expression.Constant(Mapper.Map(node.Value, node.Type, newType), newType));
308+
if (this.TypeMappings.TryGetValue(node.Type, out Type newType))
309+
{
310+
if (node.Value == null)
311+
return base.VisitConstant(Expression.Constant(null, newType));
315312

313+
if (ConfigurationProvider.ResolveTypeMap(node.Type, newType) != null)
314+
return base.VisitConstant(Expression.Constant(Mapper.MapObject(node.Value, node.Type, newType), newType));
315+
//Issue 3455 (Non-Generic Mapper.Map failing for structs in v10)
316+
//return base.VisitConstant(Expression.Constant(Mapper.Map(node.Value, node.Type, newType), newType));
317+
}
316318
return base.VisitConstant(node);
317319
}
318320

@@ -412,13 +414,13 @@ private static void AddPropertyMapInfo(Type parentType, string name, List<Proper
412414
}
413415
}
414416

415-
private bool GenericTypeDefinitionsAreEquivalent(Type typeSource, Type typeDestination)
417+
private bool GenericTypeDefinitionsAreEquivalent(Type typeSource, Type typeDestination)
416418
=> typeSource.IsGenericType() && typeDestination.IsGenericType() && typeSource.GetGenericTypeDefinition() == typeDestination.GetGenericTypeDefinition();
417419

418420
protected void FindDestinationFullName(Type typeSource, Type typeDestination, string sourceFullName, List<PropertyMapInfo> propertyMapInfoList)
419421
{
420422
const string period = ".";
421-
423+
422424
if (typeSource == typeDestination)
423425
{
424426
var sourceFullNameArray = sourceFullName.Split(new[] { period[0] }, StringSplitOptions.RemoveEmptyEntries);
@@ -461,7 +463,7 @@ protected void FindDestinationFullName(Type typeSource, Type typeDestination, st
461463

462464
var sourceType = typeSource.GetFieldOrProperty(propertyName).GetMemberType();
463465
var destType = typeDestination.GetFieldOrProperty(propertyName).GetMemberType();
464-
466+
465467
TypeMappings.AddTypeMapping(ConfigurationProvider, sourceType, destType);
466468

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

0 commit comments

Comments
 (0)