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
94 changes: 51 additions & 43 deletions src/AutoMapper/Execution/TypeMapPlanBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,66 +101,74 @@ void IncludeMembers()
private static void CheckForCycles(IGlobalConfiguration configuration, TypeMap typeMap,
HashSet<TypeMap> typeMapsPath)
{
typeMapsPath.Add(typeMap);
foreach (var memberMap in MemberMaps())
var added = typeMapsPath.Add(typeMap);
try
{
var memberTypeMap = ResolveMemberTypeMap(memberMap);
if (memberTypeMap == null || memberTypeMap.HasTypeConverter)
foreach (var memberMap in MemberMaps())
{
continue;
}

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

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

if (typeMapsPath.Contains(memberTypeMap))
{
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)
{
continue;
}

if (typeMapsPath.Contains(memberTypeMap))
{
derivedTypeMap.PreserveReferences = true;
if (derivedTypeMap.MaxDepth == 0)
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
{
if (added)
{
typeMapsPath.Remove(typeMap);
}
}

typeMapsPath.Remove(typeMap);
return;

IEnumerable<MemberMap> MemberMaps()
Expand Down
90 changes: 89 additions & 1 deletion src/UnitTests/MaxExecutionPlanDepth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -320,4 +320,92 @@ class Destination
protected override MapperConfiguration CreateConfiguration() => new(cfg => cfg.CreateMap<Source, Destination>());
[Fact]
public void Should_set_inline_accordingly() => FindTypeMapFor<Source, Destination>().PropertyMaps.First().Inline.ShouldBeFalse();
}
}

public class CycleCheckTypeMapsPathInlining : AutoMapperSpecBase
{
class OrderSource
{
public LineSource Line { get; set; }
}
class OrderDestination
{
public LineDestination Line { get; set; }
}
class LineSource
{
public OrderSource Parent { get; set; }
public ServiceSource Services { get; set; }
}
class LineDestination
{
public OrderDestination Parent { get; set; }
public ServiceDestination Services { get; set; }
}
class ServiceSource
{
public int Id { get; set; }
}
class ServiceDestination
{
public int Id { get; set; }
}

protected override MapperConfiguration CreateConfiguration() => new(cfg =>
{
cfg.CreateMap<OrderSource, OrderDestination>();
cfg.CreateMap<LineSource, LineDestination>();
cfg.CreateMap<ServiceSource, ServiceDestination>();
});

[Fact]
public void Should_not_inline_services_when_parent_cycle_declared_first()
{
var lineMap = FindTypeMapFor<LineSource, LineDestination>();
lineMap.PropertyMaps.Single(m => m.DestinationMember.Name == nameof(LineDestination.Services)).Inline.ShouldBeFalse();
}
}

public class CycleCheckTypeMapsPathInliningReversed : AutoMapperSpecBase
{
class OrderSource
{
public LineSource Line { get; set; }
}
class OrderDestination
{
public LineDestination Line { get; set; }
}
class LineSource
{
public ServiceSource Services { get; set; }
public OrderSource Parent { get; set; }
}
class LineDestination
{
public ServiceDestination Services { get; set; }
public OrderDestination Parent { get; set; }
}
class ServiceSource
{
public int Id { get; set; }
}
class ServiceDestination
{
public int Id { get; set; }
}

protected override MapperConfiguration CreateConfiguration() => new(cfg =>
{
cfg.CreateMap<OrderSource, OrderDestination>();
cfg.CreateMap<LineSource, LineDestination>();
cfg.CreateMap<ServiceSource, ServiceDestination>();
});

[Fact]
public void Should_not_inline_services_when_services_declared_first()
{
var lineMap = FindTypeMapFor<LineSource, LineDestination>();
lineMap.PropertyMaps.Single(m => m.DestinationMember.Name == nameof(LineDestination.Services)).Inline.ShouldBeFalse();
}
}