Skip to content

Commit 4799d0c

Browse files
authored
Merge pull request #95 from AutoMapper/402Preview03
Do not convert body to destination type when mapping include expressions.
2 parents 9388b2f + 0ae8f12 commit 4799d0c

File tree

5 files changed

+46
-11
lines changed

5 files changed

+46
-11
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616

1717
- name: Set Variables
1818
run: |
19-
echo '::set-env name=VERSION_NUMBER::4.0.2-preview-2'
19+
echo '::set-env name=VERSION_NUMBER::4.0.2-preview-3'
2020
echo '::set-env name=DEPLOY_PACKAGE_URL::https://www.myget.org/F/automapperdev/api/v3/index.json'
2121
echo '::set-env name=DEPLOY_PACKAGE_API_KEY::${{ secrets.MYGET_CI_API_KEY }}'
2222
echo '::set-env name=REPO::${{ github.repository }}'

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414

1515
- name: Set Variables
1616
run: |
17-
echo '::set-env name=VERSION_NUMBER::4.0.2-preview-2'
17+
echo '::set-env name=VERSION_NUMBER::4.0.2-preview-3'
1818
echo '::set-env name=DEPLOY_PACKAGE_URL::https://api.nuget.org/v3/index.json'
1919
echo '::set-env name=DEPLOY_PACKAGE_API_KEY::${{ secrets.NUGET_API_KEY }}'
2020
echo '::set-env name=REPO::${{ github.repository }}'

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<PropertyGroup>
33
<Authors>Jimmy Bogard</Authors>
44
<LangVersion>latest</LangVersion>
5-
<VersionPrefix>4.0.2-preview-2</VersionPrefix>
5+
<VersionPrefix>4.0.2-preview-3</VersionPrefix>
66
<WarningsAsErrors>true</WarningsAsErrors>
77
<NoWarn>$(NoWarn);1701;1702;1591</NoWarn>
88
</PropertyGroup>

src/AutoMapper.Extensions.ExpressionMapping/MapperExtensions.cs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,15 @@ public static TDestDelegate MapExpression<TDestDelegate>(this IMapper mapper, La
6767
return mapper.MapExpression<TDestDelegate>
6868
(
6969
expression,
70-
(config, mappings) => new XpressionMapperVisitor(mapper, config, mappings)
70+
(config, mappings) => new XpressionMapperVisitor(mapper, config, mappings),
71+
(desType) => IsFuncType(desType)
7172
);
7273
}
7374

74-
private static TDestDelegate MapExpression<TDestDelegate>(this IMapper mapper, LambdaExpression expression, Func<IConfigurationProvider, Dictionary<Type, Type>, XpressionMapperVisitor> getVisitor)
75+
private static TDestDelegate MapExpression<TDestDelegate>(this IMapper mapper,
76+
LambdaExpression expression,
77+
Func<IConfigurationProvider, Dictionary<Type, Type>, XpressionMapperVisitor> getVisitor,
78+
Func<Type, bool> shouldConvertMappedBodyToDestType)
7579
where TDestDelegate : LambdaExpression
7680
{
7781
return MapExpression<TDestDelegate>
@@ -80,15 +84,17 @@ private static TDestDelegate MapExpression<TDestDelegate>(this IMapper mapper, L
8084
expression,
8185
expression.GetType().GetGenericArguments()[0],
8286
typeof(TDestDelegate).GetGenericArguments()[0],
83-
getVisitor
87+
getVisitor,
88+
shouldConvertMappedBodyToDestType
8489
);
8590
}
8691

8792
private static TDestDelegate MapExpression<TDestDelegate>(IConfigurationProvider configurationProvider,
8893
LambdaExpression expression,
8994
Type typeSourceFunc,
9095
Type typeDestFunc,
91-
Func<IConfigurationProvider, Dictionary<Type, Type>, XpressionMapperVisitor> getVisitor)
96+
Func<IConfigurationProvider, Dictionary<Type, Type>, XpressionMapperVisitor> getVisitor,
97+
Func<Type, bool> shouldConvertMappedBodyToDestType)
9298
where TDestDelegate : LambdaExpression
9399
{
94100
return CreateVisitor(new Dictionary<Type, Type>().AddTypeMappingsFromDelegates(configurationProvider, typeSourceFunc, typeDestFunc));
@@ -99,15 +105,15 @@ TDestDelegate CreateVisitor(Dictionary<Type, Type> typeMappings)
99105
TDestDelegate MapBody(Dictionary<Type, Type> typeMappings, XpressionMapperVisitor visitor)
100106
=> GetLambda(typeMappings, visitor, visitor.Visit(expression.Body));
101107

102-
TDestDelegate GetLambda(Dictionary<Type, Type> typeMappings, XpressionMapperVisitor visitor, Expression remappedBody)
108+
TDestDelegate GetLambda(Dictionary<Type, Type> typeMappings, XpressionMapperVisitor visitor, Expression mappedBody)
103109
{
104-
if (remappedBody == null)
110+
if (mappedBody == null)
105111
throw new InvalidOperationException(Resource.cantRemapExpression);
106112

107113
return (TDestDelegate)Lambda
108114
(
109115
typeDestFunc,
110-
typeDestFunc.IsFuncType() ? ExpressionFactory.ToType(remappedBody, typeDestFunc.GetGenericArguments().Last()) : remappedBody,
116+
shouldConvertMappedBodyToDestType(typeDestFunc) ? ExpressionFactory.ToType(mappedBody, typeDestFunc.GetGenericArguments().Last()) : mappedBody,
111117
expression.GetDestinationParameterExpressions(visitor.InfoDictionary, typeMappings)
112118
);
113119
}
@@ -145,7 +151,8 @@ public static TDestDelegate MapExpressionAsInclude<TDestDelegate>(this IMapper m
145151
return mapper.MapExpression<TDestDelegate>
146152
(
147153
expression,
148-
(config, mappings) => new MapIncludesVisitor(mapper, config, mappings)
154+
(config, mappings) => new MapIncludesVisitor(mapper, config, mappings),
155+
desType => false
149156
);
150157
}
151158

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,34 @@ public void Map_collection_includes_with_flattened_string()
9191
Assert.True(listOfCarLists.Count == 2);
9292
}
9393

94+
[Fact]
95+
public void Map_collection_includes_with_flattened_collection()
96+
{
97+
//Arrange
98+
Expression<Func<UserModel, object>> selection = s => s.AccountModel.ThingModels;
99+
100+
//Act
101+
Expression<Func<User, object>> selectionMapped = mapper.MapExpressionAsInclude<Expression<Func<User, object>>>(selection);
102+
List<object> listOfCarLists = Users.Select(selectionMapped).ToList();
103+
104+
//Assert
105+
Assert.True(listOfCarLists.Count == 2);
106+
}
107+
108+
[Fact]
109+
public void Map_collection_includes_with_flattened_collection_return_type_not_converted()
110+
{
111+
//Arrange
112+
Expression<Func<UserModel, object>> selection = s => s.AccountModel.ThingModels;
113+
114+
//Act
115+
Expression<Func<User, object>> selectionMapped = mapper.MapExpressionAsInclude<Expression<Func<User, object>>>(selection);
116+
List<object> listOfCarLists = Users.Select(selectionMapped).ToList();
117+
118+
//Assert
119+
Assert.True(selectionMapped.ToString() == "s => s.Account.Things");
120+
}
121+
94122
[Fact]
95123
public void Map_includes_trim_string_nested_in_select_using_object()
96124
{

0 commit comments

Comments
 (0)