Skip to content

AutoMapper 14.0.0 breaks the order of created maps #4567

@DamienLaw

Description

@DamienLaw

I have just upgraded from AutoMapper 13.0.1 to 14.0.0 and it breaks the following codes (A working fiddle is available at https://dotnetfiddle.net/njmdIE):

public class FooBar
{
}

public class SourceBase<T>
    where T : class
{
}

public class SourceDerived : SourceBase<FooBar>
{
}

public class DestinationBase
{
}

public class DestinationDerived : DestinationBase
{
}

public class Program
{
    public static void Main()
    {
        var config = new MapperConfiguration(cfg => {
            // derived map
            cfg.CreateMap<SourceDerived, DestinationDerived>()
                .IncludeBase<SourceBase<FooBar>, DestinationBase>(); // If this statement was to swap with the statement below, the exception goes away.
	
            // base map		
            cfg.CreateMap(typeof(SourceBase<>), typeof(DestinationBase));
        });
    }
}

produces the following error:

Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
   at AutoMapper.TypeMap.get_DestinationConstructors()
   at AutoMapper.TypeMap.CanConstructorMap()
   at AutoMapper.Configuration.TypeMapConfiguration.Configure(TypeMap typeMap, List`1 sourceMembers)
   at AutoMapper.ProfileMap.Configure(TypeMap typeMap, IGlobalConfiguration configuration)
   at AutoMapper.ProfileMap.Configure(TypeMapConfiguration typeMapConfiguration, IGlobalConfiguration configuration)
   at AutoMapper.ProfileMap.Configure(IGlobalConfiguration configuration)
   at AutoMapper.MapperConfiguration.<.ctor>g__Seal|20_0()
   at AutoMapper.MapperConfiguration..ctor(MapperConfigurationExpression configurationExpression)
   at AutoMapper.MapperConfiguration..ctor(Action`1 configure)
   at Program.Main()

If the two CreateMap() statements were to be swapped, the error goes away.
I have to admit that this is a contrived example and that the "base map" should come before the "derived map". However, this used to work in the previous version 13.0.1

Moreover, in our project, maps are created in different Profiles and added using reflection, we can't control the order of the Profiles being loaded. Something like this https://dotnetfiddle.net/ibyRWE

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions