Skip to content

safeStringify should only mark true cycles as circular references, not shared objects #86

@matej21

Description

@matej21

Problem

The current implementation of safeStringify in src/to-response.ts marks any repeated object instance as a circular reference, even when it is not an actual cycle (i.e., the object appears more than once in the output, but not in its own ancestor chain). This is stricter than standard JSON.stringify behavior and breaks valid cases where non-circular but multiply-referenced objects should appear in full multiple times.

Example

const shared = { foo: 1 };
const obj = { a: shared, b: shared };
// Current safeStringify output:
// {"a":{"foo":1},"b":"[Circular ref-0]"}
// Desired behavior (and JSON.stringify):
// {"a":{"foo":1},"b":{"foo":1}}

Proposed Solution

  • Change the cycle detection logic to only treat an object as circular if it is encountered again in the current traversal path (ancestor chain), not just if it is seen multiple times.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions