Skip to content

Commit

Permalink
Merge pull request #86 from AutoMapper/Issue85
Browse files Browse the repository at this point in the history
Fix for Issue 85: Missing type map configuration or unsupported mapping
  • Loading branch information
BlaiseD authored Aug 12, 2020
2 parents 6a7874f + 217b522 commit b78f07b
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:

- name: Set Variables
run: |
echo '::set-env name=VERSION_NUMBER::4.0.1-preview-2'
echo '::set-env name=VERSION_NUMBER::4.0.1-preview-3'
echo '::set-env name=DEPLOY_PACKAGE_URL::https://www.myget.org/F/automapperdev/api/v3/index.json'
echo '::set-env name=DEPLOY_PACKAGE_API_KEY::${{ secrets.MYGET_CI_API_KEY }}'
echo '::set-env name=REPO::${{ github.repository }}'
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:

- name: Set Variables
run: |
echo '::set-env name=VERSION_NUMBER::4.0.1-preview-2'
echo '::set-env name=VERSION_NUMBER::4.0.1-preview-3'
echo '::set-env name=DEPLOY_PACKAGE_URL::https://api.nuget.org/v3/index.json'
echo '::set-env name=DEPLOY_PACKAGE_API_KEY::${{ secrets.NUGET_API_KEY }}'
echo '::set-env name=REPO::${{ github.repository }}'
Expand Down
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<Authors>Jimmy Bogard</Authors>
<LangVersion>latest</LangVersion>
<VersionPrefix>4.0.1-preview-2</VersionPrefix>
<VersionPrefix>4.0.1-preview-3</VersionPrefix>
<WarningsAsErrors>true</WarningsAsErrors>
<NoWarn>$(NoWarn);1701;1702;1591</NoWarn>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,8 @@ Expression DoVisitUnary(Expression updated)

protected override Expression VisitConstant(ConstantExpression node)
{
if (this.TypeMappings.TryGetValue(node.Type, out Type newType))
if (this.TypeMappings.TryGetValue(node.Type, out Type newType)
&& ConfigurationProvider.ResolveTypeMap(node.Type, newType) != null)
return base.VisitConstant(Expression.Constant(Mapper.MapObject(node.Value, node.Type, newType), newType));
//Issue 3455 (Non-Generic Mapper.Map failing for structs in v10)
//return base.VisitConstant(Expression.Constant(Mapper.Map(node.Value, node.Type, newType), newType));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Xunit;

namespace AutoMapper.Extensions.ExpressionMapping.UnitTests
{
public class ShouldOnlyMapExistingTypeMaps
{
[Fact]
public void Issue85()
{
var config = new MapperConfiguration(cfg =>
{
cfg.AddExpressionMapping();

cfg.CreateMap<Source, SourceDto>()
.ForMember(o => o.Items, config => config.MapFrom(p => p.Items.Select(s => s.Name)));

cfg.CreateMap<SourceDto, Source>()
.ForMember(o => o.Items, config => config.MapFrom(p => p.Items.Select(s => new SubSource { Name = s })));
});

var mapper = config.CreateMapper();

Expression<Func<Source, bool>> expression1 = o => string.Equals("item1", "item2");
var mapped1 = mapper.MapExpression<Expression<Func<SourceDto, bool>>>(expression1);

Expression<Func<SourceDto, bool>> expression2 = o => string.Equals("item1", "item2");
var mapped2 = mapper.MapExpression<Expression<Func<Source, bool>>>(expression2);

Assert.NotNull(mapped1);
Assert.NotNull(mapped2);
}

public class Source { public ICollection<SubSource> Items { get; set; } }

public class SubSource { public int ID { get; set; } public string Name { get; set; } }

public class SourceDto { public string[] Items { get; set; } }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1438,7 +1438,6 @@ public OrganizationProfile()
.ForMember(d => d.Bal, opt => opt.MapFrom(s => s.Balance))
.ForMember(d => d.DateCreated, opt => opt.MapFrom(s => Helpers.TruncateTime(s.CreateDate).Value))
.ForMember(d => d.Description, opt => opt.MapFrom(s => string.Concat(s.Type, " ", s.Number)))
//.ForMember(d => d.ComboName, opt => opt.ResolveUsing<CustomResolver>())
.ForMember(d => d.ThingModels, opt => opt.MapFrom(s => s.Things))
.ForMember(d => d.UserModels, opt => opt.MapFrom(s => s.Users));

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

CreateMap<BranchModel, Branch>();

CreateMap<Thing, ThingModel>()
.ForMember(d => d.FooModel, opt => opt.MapFrom(s => s.Foo))
.ForMember(d => d.BarModel, opt => opt.MapFrom(s => s.Bar))
Expand Down

0 comments on commit b78f07b

Please sign in to comment.