Skip to content

Fix CheckForCycles premature removal from typeMapsPath (#4654) - #4687

Open
SkyDevLab wants to merge 1 commit into
LuckyPennySoftware:mainfrom
SkyDevLab:fix-checkforcycles-path-mutation
Open

SkyDevLab wants to merge 1 commit into
LuckyPennySoftware:mainfrom
SkyDevLab:fix-checkforcycles-path-mutation

Conversation

@SkyDevLab

Copy link
Copy Markdown

Closes #4654

Summary of Problem

In \TypeMapPlanBuilder.CheckForCycles\ (\src/AutoMapper/Execution/TypeMapPlanBuilder.cs), \ ypeMapsPath\ is maintained across recursive calls via:

\\csharp
typeMapsPath.Add(typeMap);
foreach (var memberMap in MemberMaps())
{
...
CheckForCycles(configuration, memberTypeMap, typeMapsPath);
}
typeMapsPath.Remove(typeMap);
\\

When recursion encounters a map already on the path (a cycle), the inner recursive call called \ ypeMapsPath.Remove(typeMap)\ before returning, stripping \ ypeMap\ off the shared set while the outer call frame was still iterating that same map's subsequent members. Subsequent members then saw a shorter \ ypeMapsPath.Count, causing inlining behavior to non-deterministically depend on property declaration order.

Fix

  1. Guard entry into \CheckForCycles\ with \if (!typeMapsPath.Add(typeMap)) return;. If \ ypeMap\ is already on the traversal path (cycle detected), it will not re-enter or mutate the ancestor set owned by the outer frame.
  2. Wrap member traversal in a \ ry ... finally\ to ensure \ ypeMapsPath.Remove(typeMap)\ cleanly and reliably cleans up only when this frame was the one that added it.
  3. Added a regression test (\CheckForCyclesPathMutationTests) asserting that member inlining is deterministic regardless of whether a self-referential property is declared before or after other complex properties.

…les (LuckyPennySoftware#4654)

- Only recurse and remove typeMap if typeMapsPath.Add(typeMap) successfully added the type map (preventing cycles from popping an ancestor still owned by an outer frame)
- Ensure typeMapsPath.Remove(typeMap) runs in a finally block
- Add unit test verifying deterministic inlining regardless of property declaration order
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CheckForCycles removes the type map from typeMapsPath while its members are still being iterated

1 participant