Skip to content

Commit

Permalink
use ReverseTypeMaps when configuring closed generic TypeMaps
Browse files Browse the repository at this point in the history
  • Loading branch information
duncanbrown committed Jan 7, 2017
1 parent fde5637 commit 748fe55
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/AutoMapper/ProfileMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ public TypeMap ConfigureConventionTypeMap(TypeMapRegistry typeMapRegistry, TypeP
public TypeMap ConfigureClosedGenericTypeMap(TypeMapRegistry typeMapRegistry, TypePair closedTypes, TypePair requestedTypes)
{
var openMapConfig = _openTypeMapConfigs
.SelectMany(tm => tm.ReverseTypeMap == null ? new[] { tm } : new[] { tm, tm.ReverseTypeMap })
//.Where(tm => tm.IsOpenGeneric)
.Where(tm =>
tm.Types.SourceType.GetGenericTypeDefinitionIfGeneric() == closedTypes.SourceType.GetGenericTypeDefinitionIfGeneric() &&
Expand Down
35 changes: 35 additions & 0 deletions src/UnitTests/ReverseMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -284,5 +284,40 @@ public void WhenSecondCallTo_GetUnmappedPropertyNames_ShouldReturnBoo()
unmappedPropertyNames[0].ShouldEqual("Boo");
}
}

public class When_reverse_mapping_open_generics : AutoMapperSpecBase
{
private Source<int> _source;

public class Source<T>
{
public T Value { get; set; }
}
public class Destination<T>
{
public T Value { get; set; }
}

protected override MapperConfiguration Configuration { get; } = new MapperConfiguration(cfg =>
{
cfg.CreateMap(typeof(Source<>), typeof(Destination<>))
.ReverseMap();
});

protected override void Because_of()
{
var dest = new Destination<int>
{
Value = 10
};
_source = Mapper.Map<Destination<int>, Source<int>>(dest);
}

[Fact]
public void Should_create_a_map_with_the_reverse_items()
{
_source.Value.ShouldEqual(10);
}
}
}
}

0 comments on commit 748fe55

Please sign in to comment.