2

It's best to demonstrate this issue with a fiddle. The following fiddle is using AutoMapper 12.0.1 and will fail the test.

https://dotnetfiddle.net/Qtvc1Z

The following is the same codes but using AutoMapper 12.0.0 which works judging from the absence of exception.

https://dotnetfiddle.net/yki6GC

The child has 3 levels of inheritance with the Parent property should have been ignored in the mapping of the base classes. However it seems like it wasn't in the derived classes.

I'm including the codes below for completeness.

#nullable enable
using System;
using AutoMapper;

#region Child

public class ChildSourceBase
{
}

public class ChildDestinationBase
{
    public ParentDestination Parent { get; set; } = null!;
}


public class ChildSourceMiddle : ChildSourceBase
{
}

public class ChildDestinationDerived: ChildDestinationBase
{
}


public class ChildSourceDerived : ChildSourceMiddle
{
}

#endregion

#region Parent

public class ParentSourceBase
{
}

public class ParentDestination
{
    public ChildDestinationBase Child { get; set; } = null!;
}


public class ParentSourceDerived : ParentSourceBase
{
    public ChildSourceDerived Child { get; set; } = null!;
}

#endregion

public class Program
{
    public static void Main()
    {
        var config = new MapperConfiguration(cfg => {
            #region Parent
                
            cfg.CreateMap<ParentSourceBase, ParentDestination>()
                .ForMember(dest => dest.Child, opt => opt.Ignore());
            
            cfg.CreateMap<ParentSourceDerived, ParentDestination>()
                .IncludeBase<ParentSourceBase, ParentDestination>()
                .ForMember(dest => dest.Child, opt => opt.MapFrom(src => src.Child))
                ;
            
            #endregion
            
            #region Child
                
            cfg.CreateMap<ChildSourceBase, ChildDestinationBase>()
                .ForMember(dest => dest.Parent, opt => opt.Ignore()); // `Parent` property should've been ignored here.
            
            cfg.CreateMap<ChildSourceMiddle, ChildDestinationDerived>()
                .IncludeBase<ChildSourceBase, ChildDestinationBase>();
            
            cfg.CreateMap<ChildSourceDerived, ChildDestinationDerived>() // however, here is complaining that `Parent` property is not mapped.
                .IncludeBase<ChildSourceMiddle, ChildDestinationDerived>();
            
            #endregion
        });     
        config.AssertConfigurationIsValid();
    }
}

This happens only to the child (nested object). If there's no nesting involved, multi-level inheritance doesn't throw any exception.

Twisted Whisper
  • 1,166
  • 2
  • 15
  • 27

1 Answers1

0

Try the MyGet build. This is already solved. See https://github.com/AutoMapper/AutoMapper/issues/4213.

Lucian Bargaoanu
  • 3,336
  • 3
  • 14
  • 19