Skip to content

Commit f79b6a3

Browse files
authored
Merge pull request #88 from AutoMapper/Issue87
Fix for Issue #87 Error when mapping expression with generic IEnumerable.Contains.
2 parents b78f07b + 855dfad commit f79b6a3

File tree

5 files changed

+57
-4
lines changed

5 files changed

+57
-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: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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("item1");
27+
Expression<Func<SourceDto, bool>> expression2 = o => items.Contains("");
28+
Expression<Func<SourceDto, bool>> expression3 = o => o.Items.Contains("item1");
29+
Expression<Func<SourceDto, bool>> expression4 = o => o.Items.Contains("B");
30+
31+
var mapped1 = mapper.MapExpression<Expression<Func<Source, bool>>>(expression1);
32+
var mapped2 = mapper.MapExpression<Expression<Func<Source, bool>>>(expression2);
33+
var mapped3 = mapper.MapExpression<Expression<Func<Source, bool>>>(expression3);
34+
var mapped4 = mapper.MapExpression<Expression<Func<Source, bool>>>(expression4);
35+
36+
Assert.Equal(1, new Source[] { new Source { } }.AsQueryable().Where(mapped1).Count());
37+
Assert.Equal(0, new Source[] { new Source { } }.AsQueryable().Where(mapped2).Count());
38+
Assert.Equal(1, new Source[] { new Source { Items = new List<SubSource> { new SubSource { Name = "item1" } } } }.AsQueryable().Where(mapped3).Count());
39+
Assert.Equal(0, new Source[] { new Source { Items = new List<SubSource> { new SubSource { Name = "" } } } }.AsQueryable().Where(mapped4).Count());
40+
}
41+
42+
public class Source { public ICollection<SubSource> Items { get; set; } }
43+
44+
public class SubSource { public int ID { get; set; } public string Name { get; set; } }
45+
46+
public class SourceDto { public string[] Items { get; set; } }
47+
}
48+
}

0 commit comments

Comments
 (0)