Skip to content

Commit

Permalink
Merge branch 'lbargaoanu-ReverseMap_ReplaceMemberName_bug_832' into d…
Browse files Browse the repository at this point in the history
…evelop

Fixes #832
  • Loading branch information
jbogard committed Aug 20, 2015
2 parents a0d4dce + 6ed6861 commit c5dc67f
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 9 deletions.
11 changes: 11 additions & 0 deletions src/AutoMapper/IProfileExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ public interface IProfileExpression
/// <returns>Mapping expression for more configuration options</returns>
IMappingExpression<TSource, TDestination> CreateMap<TSource, TDestination>(MemberList memberList);

/// <summary>
/// Creates a mapping configuration from the <typeparamref name="TSource"/> type to the <typeparamref name="TDestination"/> type.
/// Specify the member list to validate against during configuration validation.
/// </summary>
/// <typeparam name="TSource">Source type</typeparam>
/// <typeparam name="TDestination">Destination type</typeparam>
/// <param name="profileName">Profile name</param>
/// <param name="memberList">Member list to validate</param>
/// <returns>Mapping expression for more configuration options</returns>
IMappingExpression<TSource, TDestination> CreateMap<TSource, TDestination>(string profileName, MemberList memberList);

/// <summary>
/// Create a mapping configuration from the source type to the destination type.
/// Use this method when the source and destination type are known at runtime and not compile time.
Expand Down
2 changes: 1 addition & 1 deletion src/AutoMapper/Internal/MappingExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ public void ExplicitExpansion()

public IMappingExpression<TDestination, TSource> ReverseMap()
{
var mappingExpression = _configurationContainer.CreateMap<TDestination, TSource>(MemberList.Source);
var mappingExpression = _configurationContainer.CreateMap<TDestination, TSource>(TypeMap.Profile, MemberList.Source);

foreach (var destProperty in TypeMap.GetPropertyMaps().Where(pm => pm.IsIgnored()))
{
Expand Down
7 changes: 5 additions & 2 deletions src/AutoMapper/Profile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,12 @@ public IEnumerable<AliasedMember> Aliases

public IMappingExpression<TSource, TDestination> CreateMap<TSource, TDestination>(MemberList memberList)
{
var map = _configurator.CreateMap<TSource, TDestination>(ProfileName, memberList);
return _configurator.CreateMap<TSource, TDestination>(ProfileName, memberList);
}

return map;
public IMappingExpression<TSource, TDestination> CreateMap<TSource, TDestination>(string profileName, MemberList memberList)
{
return _configurator.CreateMap<TSource, TDestination>(profileName, memberList);
}

public IMappingExpression CreateMap(Type sourceType, Type destinationType)
Expand Down
11 changes: 5 additions & 6 deletions src/AutoMapper/TypeMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,17 +132,17 @@ public string[] GetUnmappedPropertyNames()

IEnumerable<string> properties;

if (ConfiguredMemberList == MemberList.Destination)
if(ConfiguredMemberList == MemberList.Destination)
{
properties = _destinationType.PublicWriteAccessors
.Select(p => p.Name)
.Except(autoMappedProperties)
.Except(inheritedProperties);
}
else
{
var redirectedSourceMembers = _propertyMaps
.Where(pm => pm.IsMapped())
.Where(pm => pm.CustomExpression != null)
.Where(pm => pm.SourceMember != null)
.Where(pm => pm.IsMapped() && pm.SourceMember != null && pm.SourceMember.Name != pm.DestinationProperty.Name)
.Select(pm => pm.SourceMember.Name);

var ignoredSourceMembers = _sourceMemberConfigs
Expand All @@ -154,8 +154,7 @@ public string[] GetUnmappedPropertyNames()
.Except(autoMappedProperties)
.Except(inheritedProperties)
.Except(redirectedSourceMembers)
.Except(ignoredSourceMembers)
;
.Except(ignoredSourceMembers);
}

return properties.Where(memberName => !IgnorePropertiesStartingWith.Any(memberName.StartsWith)).ToArray();
Expand Down
107 changes: 107 additions & 0 deletions src/UnitTests/Bug/ReverseMapReplaceMemberName.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
using Xunit;
using Should;
using System;

namespace AutoMapper.UnitTests.Bug
{
public class ReverseMapAndReplaceMemberName : AutoMapperSpecBase
{
const string SomeId = "someId";
const string SomeOtherId = "someOtherId";
private Source _source;
private Destination _destination;

class Source
{
public string AccountId { get; set; }
}
class Destination
{
public string UserId { get; set; }
}

protected override void Establish_context()
{
Mapper.Initialize(cfg =>
{
cfg.ReplaceMemberName("Account", "User");
cfg.ReplaceMemberName("User", "Account");
cfg.CreateMap<Source, Destination>().ReverseMap();
});
}

protected override void Because_of()
{
_source = Mapper.Map<Destination, Source>(new Destination
{
UserId = SomeId
});
_destination = Mapper.Map<Source, Destination>(new Source
{
AccountId = SomeOtherId
});
}

[Fact]
public void Should_work_together()
{
_source.AccountId.ShouldEqual(SomeId);
_destination.UserId.ShouldEqual(SomeOtherId);
}
}

public class ReverseMapAndReplaceMemberNameWithProfile : AutoMapperSpecBase
{
const string SomeId = "someId";
const string SomeOtherId = "someOtherId";
private Source _source;
private Destination _destination;

class Source
{
public string AccountId { get; set; }
}

class Destination
{
public string UserId { get; set; }
}

class MyProfile : Profile
{
protected override void Configure()
{
ReplaceMemberName("Account", "User");
ReplaceMemberName("User", "Account");
CreateMap<Source, Destination>().ReverseMap();
}
}

protected override void Establish_context()
{
Mapper.Initialize(cfg =>
{
cfg.AddProfile<MyProfile>();
});
}

protected override void Because_of()
{
_source = Mapper.Map<Destination, Source>(new Destination
{
UserId = SomeId
});
_destination = Mapper.Map<Source, Destination>(new Source
{
AccountId = SomeOtherId
});
}

[Fact]
public void Should_work_together()
{
_source.AccountId.ShouldEqual(SomeId);
_destination.UserId.ShouldEqual(SomeOtherId);
}
}
}
1 change: 1 addition & 0 deletions src/UnitTests/UnitTests.Net4.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
<Compile Include="Bug\CannotConvertEnumToNullable.cs" />
<Compile Include="Bug\BaseMapWithIncludesAndUnincludedMappings.cs" />
<Compile Include="Bug\CollectionBaseClassGetConvention.cs" />
<Compile Include="Bug\ReverseMapReplaceMemberName.cs" />
<Compile Include="Bug\TargetISet.cs" />
<Compile Include="Bug\CollectionsNullability.cs" />
<Compile Include="Bug\ConditionBug.cs" />
Expand Down

0 comments on commit c5dc67f

Please sign in to comment.