Skip to content

Commit b28aabf

Browse files
committed
fix: fix bug around order of open generic registration
1 parent 534436c commit b28aabf

2 files changed

Lines changed: 39 additions & 1 deletion

File tree

src/AutoMapper/ProfileMap.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ public void Configure(IGlobalConfiguration configuration)
168168
}
169169
private void Configure(TypeMapConfiguration typeMapConfiguration, IGlobalConfiguration configuration)
170170
{
171-
var typeMap = typeMapConfiguration.TypeMap;
171+
var typeMap = configuration.FindTypeMapFor(typeMapConfiguration.Types) ?? typeMapConfiguration.TypeMap;
172172
if (typeMap.IncludeAllDerivedTypes)
173173
{
174174
IncludeAllDerived(configuration, typeMap);
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
namespace AutoMapper.UnitTests.Bug;
2+
3+
public class OpenGenericInheritanceOrder
4+
{
5+
class FooBar { }
6+
class SourceBase<T> { public string Name { get; set; } }
7+
class DestinationBase { public string Name { get; set; } }
8+
class SourceDerived : SourceBase<FooBar> { }
9+
class DestinationDerived : DestinationBase { }
10+
11+
[Fact]
12+
public void Should_work_when_derived_map_declared_before_open_generic_base_map()
13+
{
14+
var config = new MapperConfiguration(cfg =>
15+
{
16+
cfg.CreateMap<SourceDerived, DestinationDerived>()
17+
.IncludeBase<SourceBase<FooBar>, DestinationBase>();
18+
cfg.CreateMap(typeof(SourceBase<>), typeof(DestinationBase));
19+
});
20+
21+
var result = config.CreateMapper().Map<DestinationDerived>(new SourceDerived { Name = "test" });
22+
result.Name.ShouldBe("test");
23+
}
24+
25+
[Fact]
26+
public void Should_work_when_open_generic_base_map_declared_first()
27+
{
28+
var config = new MapperConfiguration(cfg =>
29+
{
30+
cfg.CreateMap(typeof(SourceBase<>), typeof(DestinationBase));
31+
cfg.CreateMap<SourceDerived, DestinationDerived>()
32+
.IncludeBase<SourceBase<FooBar>, DestinationBase>();
33+
});
34+
35+
var result = config.CreateMapper().Map<DestinationDerived>(new SourceDerived { Name = "test" });
36+
result.Name.ShouldBe("test");
37+
}
38+
}

0 commit comments

Comments
 (0)