Java / MapStruct Java Interview questions part 2
How does MapStruct handle circular references between objects?
MapStruct does not automatically detect or break cycles in an object graph the way libraries like Jackson can with identity tracking. If A references B and B references back to A, generated mapping code that naively maps both directions can recurse indefinitely and overflow the stack.
The usual mitigations are to break the cycle deliberately: ignore the back-reference on one side with @Mapping(target = "...", ignore = true), use an ID reference instead of the full nested object on the reverse side, or pass a @Context object through the mapping call to track already-visited instances and short-circuit when one is seen again.
More Related questions...