diff --git a/src/AutoMapper/ProfileMap.cs b/src/AutoMapper/ProfileMap.cs index 477892d0..8f24c3ad 100644 --- a/src/AutoMapper/ProfileMap.cs +++ b/src/AutoMapper/ProfileMap.cs @@ -168,7 +168,7 @@ public void Configure(IGlobalConfiguration configuration) } private void Configure(TypeMapConfiguration typeMapConfiguration, IGlobalConfiguration configuration) { - var typeMap = typeMapConfiguration.TypeMap; + var typeMap = configuration.FindTypeMapFor(typeMapConfiguration.Types) ?? typeMapConfiguration.TypeMap; if (typeMap.IncludeAllDerivedTypes) { IncludeAllDerived(configuration, typeMap); diff --git a/src/UnitTests/Bug/OpenGenericInheritanceOrder.cs b/src/UnitTests/Bug/OpenGenericInheritanceOrder.cs new file mode 100644 index 00000000..4f8dcee6 --- /dev/null +++ b/src/UnitTests/Bug/OpenGenericInheritanceOrder.cs @@ -0,0 +1,38 @@ +namespace AutoMapper.UnitTests.Bug; + +public class OpenGenericInheritanceOrder +{ + class FooBar { } + class SourceBase { public string Name { get; set; } } + class DestinationBase { public string Name { get; set; } } + class SourceDerived : SourceBase { } + class DestinationDerived : DestinationBase { } + + [Fact] + public void Should_work_when_derived_map_declared_before_open_generic_base_map() + { + var config = new MapperConfiguration(cfg => + { + cfg.CreateMap() + .IncludeBase, DestinationBase>(); + cfg.CreateMap(typeof(SourceBase<>), typeof(DestinationBase)); + }); + + var result = config.CreateMapper().Map(new SourceDerived { Name = "test" }); + result.Name.ShouldBe("test"); + } + + [Fact] + public void Should_work_when_open_generic_base_map_declared_first() + { + var config = new MapperConfiguration(cfg => + { + cfg.CreateMap(typeof(SourceBase<>), typeof(DestinationBase)); + cfg.CreateMap() + .IncludeBase, DestinationBase>(); + }); + + var result = config.CreateMapper().Map(new SourceDerived { Name = "test" }); + result.Name.ShouldBe("test"); + } +}