Skip to content

Commit b78f07b

Browse files
authored
Merge pull request #86 from AutoMapper/Issue85
Fix for Issue 85: Missing type map configuration or unsupported mapping
2 parents 6a7874f + 217b522 commit b78f07b

File tree

6 files changed

+50
-5
lines changed

6 files changed

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

src/AutoMapper.Extensions.ExpressionMapping/XpressionMapperVisitor.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,8 @@ Expression DoVisitUnary(Expression updated)
305305

306306
protected override Expression VisitConstant(ConstantExpression node)
307307
{
308-
if (this.TypeMappings.TryGetValue(node.Type, out Type newType))
308+
if (this.TypeMappings.TryGetValue(node.Type, out Type newType)
309+
&& ConfigurationProvider.ResolveTypeMap(node.Type, newType) != null)
309310
return base.VisitConstant(Expression.Constant(Mapper.MapObject(node.Value, node.Type, newType), newType));
310311
//Issue 3455 (Non-Generic Mapper.Map failing for structs in v10)
311312
//return base.VisitConstant(Expression.Constant(Mapper.Map(node.Value, node.Type, newType), newType));
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Linq.Expressions;
5+
using Xunit;
6+
7+
namespace AutoMapper.Extensions.ExpressionMapping.UnitTests
8+
{
9+
public class ShouldOnlyMapExistingTypeMaps
10+
{
11+
[Fact]
12+
public void Issue85()
13+
{
14+
var config = new MapperConfiguration(cfg =>
15+
{
16+
cfg.AddExpressionMapping();
17+
18+
cfg.CreateMap<Source, SourceDto>()
19+
.ForMember(o => o.Items, config => config.MapFrom(p => p.Items.Select(s => s.Name)));
20+
21+
cfg.CreateMap<SourceDto, Source>()
22+
.ForMember(o => o.Items, config => config.MapFrom(p => p.Items.Select(s => new SubSource { Name = s })));
23+
});
24+
25+
var mapper = config.CreateMapper();
26+
27+
Expression<Func<Source, bool>> expression1 = o => string.Equals("item1", "item2");
28+
var mapped1 = mapper.MapExpression<Expression<Func<SourceDto, bool>>>(expression1);
29+
30+
Expression<Func<SourceDto, bool>> expression2 = o => string.Equals("item1", "item2");
31+
var mapped2 = mapper.MapExpression<Expression<Func<Source, bool>>>(expression2);
32+
33+
Assert.NotNull(mapped1);
34+
Assert.NotNull(mapped2);
35+
}
36+
37+
public class Source { public ICollection<SubSource> Items { get; set; } }
38+
39+
public class SubSource { public int ID { get; set; } public string Name { get; set; } }
40+
41+
public class SourceDto { public string[] Items { get; set; } }
42+
}
43+
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1438,7 +1438,6 @@ public OrganizationProfile()
14381438
.ForMember(d => d.Bal, opt => opt.MapFrom(s => s.Balance))
14391439
.ForMember(d => d.DateCreated, opt => opt.MapFrom(s => Helpers.TruncateTime(s.CreateDate).Value))
14401440
.ForMember(d => d.Description, opt => opt.MapFrom(s => string.Concat(s.Type, " ", s.Number)))
1441-
//.ForMember(d => d.ComboName, opt => opt.ResolveUsing<CustomResolver>())
14421441
.ForMember(d => d.ThingModels, opt => opt.MapFrom(s => s.Things))
14431442
.ForMember(d => d.UserModels, opt => opt.MapFrom(s => s.Users));
14441443

@@ -1447,6 +1446,8 @@ public OrganizationProfile()
14471446
.ForMember(d => d.Things, opt => opt.MapFrom(s => s.ThingModels))
14481447
.ForMember(d => d.Users, opt => opt.MapFrom(s => s.UserModels));
14491448

1449+
CreateMap<BranchModel, Branch>();
1450+
14501451
CreateMap<Thing, ThingModel>()
14511452
.ForMember(d => d.FooModel, opt => opt.MapFrom(s => s.Foo))
14521453
.ForMember(d => d.BarModel, opt => opt.MapFrom(s => s.Bar))

0 commit comments

Comments
 (0)