Skip to content
Merged
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
7 changes: 4 additions & 3 deletions src/AutoMapper/Execution/TypeMapPlanBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ private void CheckForCycles(HashSet<TypeMap> typeMapsPath)
}
typeMapsPath.Add(_typeMap);
var members =
_typeMap.MemberMaps.Where(pm=>pm.CanResolveValue)
_typeMap.MemberMaps
.Concat(_typeMap.IncludedDerivedTypes.Select(ResolveTypeMap).SelectMany(tm=>tm.MemberMaps))
.Where(pm=>pm.CanResolveValue)
.ToArray()
.Select(pm=> new { MemberTypeMap = ResolveMemberTypeMap(pm), MemberMap = pm })
.Where(p => p.MemberTypeMap != null && !p.MemberTypeMap.PreserveReferences && p.MemberTypeMap.MapExpression == null);
Expand All @@ -106,7 +108,6 @@ private void CheckForCycles(HashSet<TypeMap> typeMapsPath)
typeMapsPath.Remove(_typeMap);
return;
}

SetPreserveReferences(memberTypeMap);
foreach(var derivedTypeMap in memberTypeMap.IncludedDerivedTypes.Select(ResolveTypeMap))
{
Expand All @@ -126,7 +127,7 @@ void SetPreserveReferences(TypeMap memberTypeMap)

TypeMap ResolveMemberTypeMap(IMemberMap memberMap)
{
if(memberMap.SourceType == null)
if(memberMap.SourceType == null || memberMap.Types.ContainsGenericParameters)
{
return null;
}
Expand Down
2 changes: 2 additions & 0 deletions src/AutoMapper/TypePair.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ public static TypePair Create<TSource, TDestination>(TSource source, TDestinatio

public bool IsGenericTypeDefinition => SourceType.IsGenericTypeDefinition || DestinationType.IsGenericTypeDefinition;

public bool ContainsGenericParameters => SourceType.ContainsGenericParameters || DestinationType.ContainsGenericParameters;

public TypePair? GetOpenGenericTypePair()
{
if(!IsGeneric)
Expand Down
59 changes: 59 additions & 0 deletions src/UnitTests/BidirectionalRelationshipsWithoutPR.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,70 @@
using System;
using System.CodeDom;
using System.Collections.Generic;
using System.Linq;
using Shouldly;
using Xunit;

namespace AutoMapper.UnitTests
{
public class CyclesWithInheritance : AutoMapperSpecBase
{
class FlowChart
{
public FlowNode[] Nodes;
}
class FlowNode
{
}
class FlowStep : FlowNode
{
public FlowNode Next;
}
class FlowDecision : FlowNode
{
public FlowNode True;
public FlowNode False;
}
class FlowSwitch<T> : FlowNode
{
public IDictionary<T, object> Connections;
}
class FlowChartModel
{
public FlowNodeModel[] Nodes;
}
class FlowNodeModel
{
public Connection[] Connections;
}
class Connection
{
public FlowNodeModel Node;
}
protected override MapperConfiguration Configuration => new MapperConfiguration(cfg=>
{
cfg.CreateMap<FlowChart, FlowChartModel>();
cfg.CreateMap<FlowNode, FlowNodeModel>()
.Include<FlowStep, FlowNodeModel>()
.Include<FlowDecision, FlowNodeModel>()
.Include(typeof(FlowSwitch<>), typeof(FlowNodeModel))
.ForMember(d=>d.Connections, o=>o.Ignore());
cfg.CreateMap<FlowStep, FlowNodeModel>().ForMember(d => d.Connections, o => o.MapFrom(s => new[] { s.Next }));
cfg.CreateMap<FlowDecision, FlowNodeModel>().ForMember(d => d.Connections, o => o.MapFrom(s => new[] { s.True, s.False }));
cfg.CreateMap(typeof(FlowSwitch<>), typeof(FlowNodeModel));
cfg.CreateMap<FlowNode, Connection>().ForMember(d => d.Node, o => o.MapFrom(s => s));
cfg.CreateMap(typeof(KeyValuePair<,>), typeof(Connection)).ForMember("Node", o => o.MapFrom("Key"));
});
[Fact]
public void Should_map_ok()
{
var flowStep = new FlowStep();
var flowDecision = new FlowDecision { False = flowStep, True = flowStep };
flowStep.Next = flowDecision;
var source = new FlowChart { Nodes = new FlowNode[] { flowStep, flowDecision } };
var dest = Map<FlowChartModel>(source);
}
}
public class When_the_source_has_cyclical_references_with_dynamic_map : AutoMapperSpecBase
{
public class CDataTypeModel<T>
Expand Down