Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
93 changes: 51 additions & 42 deletions src/AutoMapper/Execution/TypeMapPlanBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,66 +101,75 @@ void IncludeMembers()
private static void CheckForCycles(IGlobalConfiguration configuration, TypeMap typeMap,
HashSet<TypeMap> typeMapsPath)
{
typeMapsPath.Add(typeMap);
foreach (var memberMap in MemberMaps())
if (!typeMapsPath.Add(typeMap))
{
var memberTypeMap = ResolveMemberTypeMap(memberMap);
if (memberTypeMap == null || memberTypeMap.HasTypeConverter)
{
continue;
}

if (memberMap.Inline && (memberTypeMap.PreserveReferences ||
typeMapsPath.Count == configuration.MaxExecutionPlanDepth))
{
memberMap.Inline = false;
TraceInline(typeMap, memberMap);
}

if (memberTypeMap.PreserveReferences || memberTypeMap.MapExpression != null)
{
continue;
}
return;
}

if (typeMapsPath.Contains(memberTypeMap))
try
{
foreach (var memberMap in MemberMaps())
{
if (memberTypeMap.SourceType.IsValueType || memberTypeMap.DestinationType.IsValueType)
var memberTypeMap = ResolveMemberTypeMap(memberMap);
if (memberTypeMap == null || memberTypeMap.HasTypeConverter)
{
if (memberTypeMap.MaxDepth == 0)
{
memberTypeMap.MaxDepth = 10;
}

continue;
}

memberTypeMap.PreserveReferences = true;
if (memberTypeMap.MaxDepth == 0)
{
memberTypeMap.MaxDepth = 64;
}
Trace(typeMap, memberTypeMap, memberMap);
if (memberMap.Inline)
if (memberMap.Inline && (memberTypeMap.PreserveReferences ||
typeMapsPath.Count == configuration.MaxExecutionPlanDepth))
{
memberMap.Inline = false;
TraceInline(typeMap, memberMap);
}

foreach (var derivedTypeMap in configuration.GetIncludedTypeMaps(memberTypeMap))
if (memberTypeMap.PreserveReferences || memberTypeMap.MapExpression != null)
{
derivedTypeMap.PreserveReferences = true;
if (derivedTypeMap.MaxDepth == 0)
continue;
}

if (typeMapsPath.Contains(memberTypeMap))
{
if (memberTypeMap.SourceType.IsValueType || memberTypeMap.DestinationType.IsValueType)
{
if (memberTypeMap.MaxDepth == 0)
{
memberTypeMap.MaxDepth = 10;
}

continue;
}

memberTypeMap.PreserveReferences = true;
if (memberTypeMap.MaxDepth == 0)
{
derivedTypeMap.MaxDepth = memberTypeMap.MaxDepth;
memberTypeMap.MaxDepth = 64;
}
Trace(typeMap, memberTypeMap, memberMap);
if (memberMap.Inline)
{
memberMap.Inline = false;
TraceInline(typeMap, memberMap);
}

foreach (var derivedTypeMap in configuration.GetIncludedTypeMaps(memberTypeMap))
{
derivedTypeMap.PreserveReferences = true;
if (derivedTypeMap.MaxDepth == 0)
{
derivedTypeMap.MaxDepth = memberTypeMap.MaxDepth;
}
Trace(typeMap, derivedTypeMap, memberMap);
}
Trace(typeMap, derivedTypeMap, memberMap);
}
}

CheckForCycles(configuration, memberTypeMap, typeMapsPath);
CheckForCycles(configuration, memberTypeMap, typeMapsPath);
}
}
finally
{
typeMapsPath.Remove(typeMap);
}

typeMapsPath.Remove(typeMap);
return;

IEnumerable<MemberMap> MemberMaps()
Expand Down
71 changes: 71 additions & 0 deletions src/UnitTests/Bug/CheckForCyclesPathMutationTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
namespace AutoMapper.UnitTests.Bug;

public class CheckForCyclesPathMutationTests : AutoMapperSpecBase
{
public class Order
{
public LineWithParentFirst Line { get; set; }
}

public class LineWithParentFirst
{
public LineWithParentFirst Parent { get; set; }
public Service Services { get; set; }
}

public class OrderWithServicesFirst
{
public LineWithServicesFirst Line { get; set; }
}

public class LineWithServicesFirst
{
public Service Services { get; set; }
public LineWithServicesFirst Parent { get; set; }
}

public class Service
{
public string Name { get; set; }
}

public class OrderDto
{
public LineDto Line { get; set; }
}

public class LineDto
{
public LineDto Parent { get; set; }
public ServiceDto Services { get; set; }
}

public class ServiceDto
{
public string Name { get; set; }
}

protected override MapperConfiguration CreateConfiguration() => new(cfg =>
{
cfg.CreateMap<Order, OrderDto>();
cfg.CreateMap<LineWithParentFirst, LineDto>();

cfg.CreateMap<OrderWithServicesFirst, OrderDto>();
cfg.CreateMap<LineWithServicesFirst, LineDto>();

cfg.CreateMap<Service, ServiceDto>();
});

[Fact]
public void Inlining_Should_Be_Deterministic_Regardless_Of_Member_Order()
{
var lineWithParentFirstMap = Configuration.FindTypeMapFor<LineWithParentFirst, LineDto>();
var lineWithServicesFirstMap = Configuration.FindTypeMapFor<LineWithServicesFirst, LineDto>();

var servicesMemberParentFirst = lineWithParentFirstMap.MemberMaps.Single(m => m.DestinationName == nameof(LineDto.Services));
var servicesMemberServicesFirst = lineWithServicesFirstMap.MemberMaps.Single(m => m.DestinationName == nameof(LineDto.Services));

// Both should have identical inlining behavior regardless of declaration order
servicesMemberParentFirst.Inline.ShouldBe(servicesMemberServicesFirst.Inline);
}
}