Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/AutoMapper/ConstructorMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,31 @@ public bool ApplyIncludedMember(IncludedMember includedMember)
}
return canResolve;
}
public void ApplyInheritedMap(TypeMap inheritedTypeMap)
{
var inheritedMap = inheritedTypeMap.ConstructorMap;
if (CanResolve)
{
return;
}
bool canResolve = true;
for (int index = 0; index < _ctorParams.Count; index++)
{
var thisParam = _ctorParams[index];
if (thisParam.CanResolveValue)
{
continue;
}
var inheritedParam = inheritedMap[thisParam.DestinationName];
if (inheritedParam == null || !inheritedParam.CanResolveValue)
{
canResolve = false;
continue;
}
_ctorParams[index] = new(inheritedTypeMap, inheritedParam.Parameter, inheritedParam.SourceMembers);
}
_canResolve = canResolve;
}
}
[EditorBrowsable(EditorBrowsableState.Never)]
public class ConstructorParameterMap : MemberMap
Expand Down
4 changes: 4 additions & 0 deletions src/AutoMapper/TypeMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,10 @@ private void ApplyInheritedTypeMap(TypeMap inheritedTypeMap, TypeMap thisMap)
{
ApplyInheritedPropertyMaps(inheritedTypeMap, thisMap);
}
if (inheritedTypeMap.ConstructorMap != null)
{
thisMap.ConstructorMap?.ApplyInheritedMap(inheritedTypeMap);
}
var inheritedDetails = inheritedTypeMap._details;
if (inheritedDetails == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,27 @@ public class OtherDto
public string SubString { get; set; }
}

public record class RecordObject(string DifferentBaseString)
{
}

public record class RecordSubObject(string DifferentBaseString, string SubString) : RecordObject(DifferentBaseString)
{
}

public record class RecordSubObjectWithExtraParam(string DifferentBaseString, string SubString, string ExtraString) : RecordObject(DifferentBaseString)
{
}

public record class RecordOtherObject(string BaseString)
{
}

public record class RecordOtherSubObject(string BaseString, string SubString) : RecordOtherObject(BaseString)
{
}


[Fact]
public void included_mapping_should_inherit_base_mappings_should_not_throw()
{
Expand Down Expand Up @@ -320,6 +341,23 @@ public void include_should_apply_null_substitute()

dest.BaseString.ShouldBe("12345");
}

[Fact]
public void included_mapping_should_inherit_base_constructor_mappings_should_not_throw()
{
var config = new MapperConfiguration(cfg =>
{
cfg.ShouldUseConstructor = constructor => constructor.IsPublic;
cfg.CreateMap<RecordOtherObject, RecordObject>()
.ForCtorParam(nameof(RecordObject.DifferentBaseString), m => m.MapFrom(s => s.BaseString))
.Include<RecordOtherSubObject, RecordSubObject>()
.Include<RecordOtherSubObject, RecordSubObjectWithExtraParam>();
cfg.CreateMap<RecordOtherSubObject, RecordSubObject>();
cfg.CreateMap<RecordOtherSubObject, RecordSubObjectWithExtraParam>()
.ForCtorParam(nameof(RecordSubObjectWithExtraParam.ExtraString), m => m.MapFrom(s => s.BaseString + s.SubString));
});
config.AssertConfigurationIsValid();
}
}

public class OverrideDifferentMapFrom : AutoMapperSpecBase
Expand Down