Skip to content

Possible regression introduced in v11 regarding interface mapping #4374

@cidzoo

Description

@cidzoo

I'm opening this issue because I may have found a possible regression between major 10 and 11.

To summarize the problem, the problem occurs when mapping:

  • between existing instances
  • between object properties
  • between interfaces

Source/destination types

public interface ISourceModel
{
    object Id { get; set; }
}

public interface IDestModel
{
    object Id { get; set; }
}

public class SourceModel : ISourceModel
{
    public object Id { get; set; }
}

public class DestModel : IDestModel
{
    public object Id { get; set; }
}

Mapping configuration

cfg.CreateMap<ISourceModel, IDestModel>();

Version: 11.0.0

Usecase is mapping between existing types like so:

mapper.Map(src, dst);

...and not mapping to a new instance. Otherwise, this would need AsProxy() and I'm aware of this. Just to be sure, I tried to add it but it has no effect.

Expected behavior

Mapping success and no error.

This was working in previous major v10.y.z This is also not working in the last major v12.y.z.

Actual behavior

Mapping exception:

[...] threw exception: 
AutoMapper.AutoMapperMappingException: Missing type map configuration or unsupported mapping.

Mapping types:
SourceModel -> DestModel

Steps to reproduce

Download and execute the attached minimal example.

automapper-mapping-interface-example.zip

Here is a printout of the source file. I've also added comments to show what I've already tried and possible workarounds.

using AutoMapper;
using System.Diagnostics;

class Program
{
	static void Main(string[] args)
	{
        var mapperConfig = new MapperConfiguration(cfg =>
        {
            //cfg.CreateMap<object, object>(); // <-- with this mapping will work

            cfg.CreateMap<ISourceModel, IDestModel>()
                //.ForMember(dst => dst.Id, o => o.Ignore()) // <-- with this mapping will work
                //.ForMember(dst => dst.Id, o => o.MapFrom(src => 1)) // <-- with this mapping will work
                //.ForMember(dst => dst.Id, o => o.MapFrom(src => src.Id.ToString())) // <-- with this mapping will work
                ;
        });

        var mapper = mapperConfig.CreateMapper();

        SourceModel src = new()
        {
            Id = 1,
        };

        DestModel dst = new();

        mapper.Map(src, dst); // <-- Missing type map exception between SourceModel -> DestModel

        // condition used for the case where Id is ignored
        if (dst.Id != null)
        {
            // if mapping passed without exception, verify if it worked
            Debug.Assert(string.Equals(src.Id.ToString(), dst.Id.ToString()));
        }
	}
}

public interface ISourceModel
{
    // change the type to string, int, etc. and test will pass
    object Id { get; set; }
}

public interface IDestModel
{
    // or/and change this type to string, int. etc. and test will pass
    object Id { get; set; }
}

public class SourceModel : ISourceModel
{
    public object Id { get; set; }
}

public class DestModel : IDestModel
{
    public object Id { get; set; }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions