Skip to content

Commit 2d9b1b1

Browse files
committed
Fix for Issue #87 Error when mapping expression with generic IEnumerable.Contains.
1 parent b78f07b commit 2d9b1b1

File tree

5 files changed

+51
-4
lines changed

5 files changed

+51
-4
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.1-preview-3'
19+
echo '::set-env name=VERSION_NUMBER::4.0.1-preview-4'
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.1-preview-3'
17+
echo '::set-env name=VERSION_NUMBER::4.0.1-preview-4'
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.1-preview-3</VersionPrefix>
5+
<VersionPrefix>4.0.1-preview-4</VersionPrefix>
66
<WarningsAsErrors>true</WarningsAsErrors>
77
<NoWarn>$(NoWarn);1701;1702;1591</NoWarn>
88
</PropertyGroup>

src/AutoMapper.Extensions.ExpressionMapping/MapperExtensions.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,11 @@ private static void DoAddTypeMappings(this Dictionary<Type, Type> typeMappings,
375375
}
376376
}
377377

378+
private static Type GetSourceMemberType(this PropertyMap propertyMap)
379+
=> propertyMap.CustomMapExpression != null
380+
? propertyMap.CustomMapExpression.ReturnType
381+
: propertyMap.SourceMember.GetMemberType();
382+
378383
private static void FindChildPropertyTypeMaps(this Dictionary<Type, Type> typeMappings, IConfigurationProvider ConfigurationProvider, Type source, Type dest)
379384
{
380385
//The destination becomes the source because to map a source expression to a destination expression,
@@ -395,7 +400,7 @@ void FindMaps(List<PropertyMap> maps)
395400
AddChildMappings
396401
(
397402
source.GetFieldOrProperty(pm.DestinationMember.Name).GetMemberType(),
398-
pm.SourceMember.GetMemberType()
403+
pm.GetSourceMemberType()
399404
);
400405
void AddChildMappings(Type sourcePropertyType, Type destPropertyType)
401406
{
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Linq.Expressions;
5+
using System.Text;
6+
using Xunit;
7+
8+
namespace AutoMapper.Extensions.ExpressionMapping.UnitTests
9+
{
10+
public class EnumerableDotContainsWorksOnLocalVariables
11+
{
12+
[Fact]
13+
public void Issue87()
14+
{
15+
var config = new MapperConfiguration(cfg =>
16+
{
17+
cfg.AddExpressionMapping();
18+
19+
cfg.CreateMap<Source, SourceDto>()
20+
.ForMember(o => o.Items, config => config.MapFrom(p => p.Items.Select(s => s.Name)));
21+
});
22+
23+
var mapper = config.CreateMapper();
24+
25+
var items = new string[] { "item1", "item2" };
26+
Expression<Func<SourceDto, bool>> expression1 = o => items.Contains("");
27+
Expression<Func<SourceDto, bool>> expression2 = o => o.Items.Contains("");
28+
29+
var mapped1 = mapper.MapExpression<Expression<Func<Source, bool>>>(expression1);
30+
var mapped2 = mapper.MapExpression<Expression<Func<Source, bool>>>(expression2);
31+
32+
Assert.NotNull(mapped1);
33+
Assert.NotNull(mapped2);
34+
}
35+
36+
public class Source { public ICollection<SubSource> Items { get; set; } }
37+
38+
public class SubSource { public int ID { get; set; } public string Name { get; set; } }
39+
40+
public class SourceDto { public string[] Items { get; set; } }
41+
}
42+
}

0 commit comments

Comments
 (0)