ihubanov 9363879ea1
fix(core): preserve duplicate object references in safeJsonStringify (#4407)
* fix(core): preserve duplicate object references in safeJsonStringify

The replacer kept a WeakSet of every object it had ever seen. JSON.stringify
calls the replacer for every key in a DFS walk, siblings included, and the
set was never trimmed when the walk unwound. So the second sibling that
pointed at the same object got replaced with [Circular]. Not a cycle, just
a duplicate reference. Same false positive for repeated array elements and
for any shared leaf that appears on more than one branch.

Track the current ancestor path instead. The replacer's `this` is the parent
of `value`, so on each call pop the stack back to wherever the walk currently
is, then check the remaining ancestors for membership. Only real cycles get
flagged.

Existing cycle tests still pass. Added five regression tests covering shared
siblings, repeated array elements, shared subtree leaves, indirect cycles,
and a mix of duplicate ref + real cycle in the same graph.

* test(core): cover deep unwinding and toJSON paths in safeJsonStringify

Four regression tests covering corners the initial five missed:

- Shared leaf reached through five levels of nesting plus a sibling branch.
  Exercises the unwind loop popping multiple frames between the deep arm
  and the sibling arm of the walk.

- Real cycle (root referenced back from depth 5). Same depth as above but
  the deep arm closes the loop, so the ancestor check must still fire.

- Shared object returned by toJSON from two sibling positions. The replacer
  sees the post-toJSON value, so duplicate-ref handling has to recognize
  these as duplicates even though the carriers are different objects.

- Cycle through a toJSON that returns an ancestor. Confirms the ancestor
  check fires on the toJSON return value, not the toJSON-bearing carrier.

Per review feedback on #4407.
2026-05-25 13:06:55 +08:00
..
2025-07-22 23:26:01 +08:00
2026-03-30 16:11:26 +08:00
2025-08-14 21:18:26 +08:00
2025-07-22 23:26:01 +08:00
2026-03-11 15:24:08 +08:00
2025-09-10 16:24:59 +08:00
2025-09-10 16:24:59 +08:00