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
4 changes: 3 additions & 1 deletion src/AutoMapper/Mappers/CollectionMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ void UseDestinationValue()
Expression CheckContext()
{
var elementTypeMap = configuration.ResolveTypeMap(sourceElementType, destinationElementType);
return elementTypeMap == null ? null : ExpressionBuilder.CheckContext(elementTypeMap);
var checkElement = elementTypeMap == null ? null : ExpressionBuilder.CheckContext(elementTypeMap);
// OverMaxDepth uses the containing type map, so honor its context requirements too
return checkElement ?? (memberMap?.TypeMap is { } containingTypeMap ? ExpressionBuilder.CheckContext(containingTypeMap) : null);
}
}
}
Expand Down
44 changes: 44 additions & 0 deletions src/UnitTests/Bug/CollectionMaxDepthContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
namespace AutoMapper.UnitTests.Bug;

public class CollectionMaxDepthContext
{
public class Service { public string Description { get; set; } }
public class ServiceDto { public string Description { get; set; } }

// Self-referential, so CheckForCycles auto-enables PreserveReferences and MaxDepth
public class RecursiveLine { public RecursiveLine Parent { get; set; } public List<Service> Services { get; set; } }
public class RecursiveLineDto { public RecursiveLineDto Parent { get; set; } public IList<ServiceDto> Services { get; set; } }

// Plain type mapping the same List<Service> -> IList<ServiceDto> pair
public class PlainLine { public List<Service> Services { get; set; } }
public class PlainLineDto { public IList<ServiceDto> Services { get; set; } }

static IMapper CreateMapper() => new MapperConfiguration(cfg =>
{
cfg.CreateMap<Service, ServiceDto>();
cfg.CreateMap<RecursiveLine, RecursiveLineDto>().ForMember(d => d.Services, o => o.MapAtRuntime());
cfg.CreateMap<PlainLine, PlainLineDto>().ForMember(d => d.Services, o => o.MapAtRuntime());
}).CreateMapper();

static RecursiveLine NewRecursiveLine() => new() { Services = [new Service { Description = "recursive" }] };
static PlainLine NewPlainLine() => new() { Services = [new Service { Description = "plain" }] };

// The collection execution plan is cached per type pair, ignoring the member map it was built
// for. When the recursive line compiles it first, the plan carries a max depth check for the
// containing type map, which needs a non default context in every other caller too.
[Fact]
public void Should_map_the_plain_line_after_the_recursive_line()
{
var mapper = CreateMapper();
mapper.Map<RecursiveLineDto>(NewRecursiveLine()).Services.Count.ShouldBe(1);
mapper.Map<PlainLineDto>(NewPlainLine()).Services.Count.ShouldBe(1);
}

[Fact]
public void Should_map_the_recursive_line_after_the_plain_line()
{
var mapper = CreateMapper();
mapper.Map<PlainLineDto>(NewPlainLine()).Services.Count.ShouldBe(1);
mapper.Map<RecursiveLineDto>(NewRecursiveLine()).Services.Count.ShouldBe(1);
}
}
101 changes: 101 additions & 0 deletions src/UnitTests/Bug/RegistrationOrderMaxDepthContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
namespace AutoMapper.UnitTests.Bug;

public class RegistrationOrderMaxDepthContext
{
public class InitiativeEntity { public List<EpicEntity> Epics { get; set; } = []; }
public class InitiativeModel { public List<EpicModel> Epics { get; set; } = []; }
public class EpicEntity
{
public Guid EpicId { get; set; }
public string Name { get; set; }
public List<StoryEntity> Stories { get; set; } = [];
public List<WorkItemEntity> WorkItems { get; set; } = [];
}
public class StoryEntity
{
public Guid StoryId { get; set; }
public EpicEntity Epic { get; set; }
public Guid EpicId { get; set; }
public List<WorkItemEntity> WorkItems { get; set; }
}
public class WorkItemEntity
{
public Guid WorkItemId { get; set; }
public EpicEntity Epic { get; set; }
public Guid EpicId { get; set; }
public List<EstimateEntity> Estimates { get; set; }
public List<StoryEntity> Stories { get; set; }
}
public class EstimateEntity
{
public Guid EstimateId { get; set; }
public WorkItemEntity WorkItem { get; set; }
public Guid WorkItemId { get; set; }
}
public class EpicModel
{
public Guid EpicId { get; set; }
public string Name { get; set; }
public List<WorkItemModel> WorkItems { get; set; }
public List<StoryModel> Stories { get; set; }
}
public class StoryModel { public Guid StoryId { get; set; } public Guid EpicId { get; set; } }
public class WorkItemModel
{
public Guid WorkItemId { get; set; }
public Guid EpicId { get; set; }
public List<EstimateModel> Estimates { get; set; }
public List<StoryModel> Stories { get; set; }
}
public class EstimateModel { public Guid EstimateId { get; set; } public WorkItemModel WorkItem { get; set; } }

static EpicEntity NewEpic()
{
var epic = new EpicEntity { EpicId = Guid.NewGuid(), Name = "Epic" };
var story = new StoryEntity { StoryId = Guid.NewGuid(), Epic = epic, EpicId = epic.EpicId };
var workItem = new WorkItemEntity { WorkItemId = Guid.NewGuid(), Epic = epic, EpicId = epic.EpicId };
var estimate = new EstimateEntity { WorkItem = workItem, WorkItemId = workItem.WorkItemId };
epic.Stories.Add(story);
epic.WorkItems.Add(workItem);
story.WorkItems = [workItem];
workItem.Stories = [story];
workItem.Estimates = [estimate];
return epic;
}

static void AssertMapped(EpicModel model)
{
model.Stories.Count.ShouldBe(1);
model.WorkItems.Count.ShouldBe(1);
model.WorkItems[0].Estimates.Count.ShouldBe(1);
model.WorkItems[0].Estimates[0].WorkItem.ShouldBeSameAs(model.WorkItems[0]);
}

[Fact]
public void Should_map_with_initiative_registered_first()
{
var mapper = new MapperConfiguration(cfg =>
{
cfg.CreateMap<InitiativeEntity, InitiativeModel>();
cfg.CreateMap<EpicEntity, EpicModel>();
cfg.CreateMap<WorkItemEntity, WorkItemModel>();
cfg.CreateMap<EstimateEntity, EstimateModel>();
cfg.CreateMap<StoryEntity, StoryModel>();
}).CreateMapper();
AssertMapped(mapper.Map<EpicModel>(NewEpic()));
}

[Fact]
public void Should_map_with_epic_registered_first()
{
var mapper = new MapperConfiguration(cfg =>
{
cfg.CreateMap<EpicEntity, EpicModel>();
cfg.CreateMap<InitiativeEntity, InitiativeModel>();
cfg.CreateMap<WorkItemEntity, WorkItemModel>();
cfg.CreateMap<EstimateEntity, EstimateModel>();
cfg.CreateMap<StoryEntity, StoryModel>();
}).CreateMapper();
AssertMapped(mapper.Map<EpicModel>(NewEpic()));
}
}
Loading