Skip to content

MapFrom doesn't resolve constructor parameters that have no matching source property #2745

@Chakravarti-Baratam

Description

@Chakravarti-Baratam

Source/destination types

public class SourceInterval
        {
            public int Value { get; set; }
        }

        public class SourceIntervalList:List<SourceInterval>
        {
            public SourceIntervalList(string name)
            {
                this.Name = name;
            }

            public SourceIntervalList(string name, List<SourceInterval> intervals) : base(intervals)
            {
                this.Name = name;
            }

            public string Name { get; set; }
        }

        public class DestInterval
        {
            public int Value { get; set; }
        }

        public class DestIntervalList : List<DestInterval>
        {
            public DestIntervalList(string name)
            {
                this.Name = name;
            }

            public DestIntervalList(string name, List<DestInterval> intervals):base(intervals)
            {
                this.Name = name;
            }

            public string Name { get; set; }
        }

Mapping configuration

protected override MapperConfiguration Configuration { get; } = new MapperConfiguration(cfg =>
        {
            cfg.CreateMap<SourceInterval, DestInterval>();
            cfg.CreateMap<SourceIntervalList, DestIntervalList>()
            .ForCtorParam("name", opt => opt.MapFrom(src => src.Name))
            .ForCtorParam("intervals", opt => opt.MapFrom(src => src.Select(x => x)));
        });

Version: 7.0.1

Expected behavior

The destination collection should be filled.

Actual behavior

AutoMapper.AutoMapperConfigurationException: 'DestIntervalList does not have a constructor with a parameter named 'intervals'.
AutoMapper.UnitTests.Constructors.When_configuring_ctor_param_members_for_types_inheriting_from_list+DestIntervalList'

Steps to reproduce

[Fact]
        public void Should_map_correctly()
        {
            SourceIntervalList sourceIntervalList = new SourceIntervalList(
                "test", 
                new List<SourceInterval> {
                    new SourceInterval{Value = 10},
                    new SourceInterval{Value = 20},
                });
            var destIntervalList = Mapper.Map<SourceIntervalList, DestIntervalList>(sourceIntervalList);

            destIntervalList.Name.ShouldBe("test");
            destIntervalList.Count().ShouldBe(2);
        }

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions