From d5ada953d53cf2546f648c7091ba705ad9c705d2 Mon Sep 17 00:00:00 2001 From: Jimmy Bogard Date: Thu, 29 Oct 2026 08:46:17 -0500 Subject: [PATCH] Add regression test for registration-order MaxDepth context failure (#4657) Reproduces #4657, already fixed by #4651. Unlike that fix's test, this needs no MapAtRuntime and a single Map call: cycle detection turns the collection members into runtime calls, WorkItemModel.Stories compiles the shared List -> List plan with WorkItem's MaxDepth check, and EpicModel.Stories then reuses it under the default context. Registering InitiativeEntity -> InitiativeModel first is what makes the Epic members runtime calls, so the test covers both registration orders. Co-Authored-By: Claude Opus 5.5 --- .../Bug/RegistrationOrderMaxDepthContext.cs | 101 ++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 src/UnitTests/Bug/RegistrationOrderMaxDepthContext.cs diff --git a/src/UnitTests/Bug/RegistrationOrderMaxDepthContext.cs b/src/UnitTests/Bug/RegistrationOrderMaxDepthContext.cs new file mode 100644 index 0000000000..0237af60fc --- /dev/null +++ b/src/UnitTests/Bug/RegistrationOrderMaxDepthContext.cs @@ -0,0 +1,101 @@ +namespace AutoMapper.UnitTests.Bug; + +public class RegistrationOrderMaxDepthContext +{ + public class InitiativeEntity { public List Epics { get; set; } = []; } + public class InitiativeModel { public List Epics { get; set; } = []; } + public class EpicEntity + { + public Guid EpicId { get; set; } + public string Name { get; set; } + public List Stories { get; set; } = []; + public List WorkItems { get; set; } = []; + } + public class StoryEntity + { + public Guid StoryId { get; set; } + public EpicEntity Epic { get; set; } + public Guid EpicId { get; set; } + public List WorkItems { get; set; } + } + public class WorkItemEntity + { + public Guid WorkItemId { get; set; } + public EpicEntity Epic { get; set; } + public Guid EpicId { get; set; } + public List Estimates { get; set; } + public List 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 WorkItems { get; set; } + public List 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 Estimates { get; set; } + public List 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(); + cfg.CreateMap(); + cfg.CreateMap(); + cfg.CreateMap(); + cfg.CreateMap(); + }).CreateMapper(); + AssertMapped(mapper.Map(NewEpic())); + } + + [Fact] + public void Should_map_with_epic_registered_first() + { + var mapper = new MapperConfiguration(cfg => + { + cfg.CreateMap(); + cfg.CreateMap(); + cfg.CreateMap(); + cfg.CreateMap(); + cfg.CreateMap(); + }).CreateMapper(); + AssertMapped(mapper.Map(NewEpic())); + } +}