Skip to content

fix: handle circular references in diff command (fixes #2093)#2113

Open
armorbreak001 wants to merge 1 commit intoasyncapi:masterfrom
armorbreak001:bounty/2093-circular-diff
Open

fix: handle circular references in diff command (fixes #2093)#2113
armorbreak001 wants to merge 1 commit intoasyncapi:masterfrom
armorbreak001:bounty/2093-circular-diff

Conversation

@armorbreak001
Copy link
Copy Markdown

Description

Closes #2093

The diff command crashed with TypeError: Converting circular structure to JSON when AsyncAPI documents contained circular references (e.g., schemas referencing themselves via anyOf + $ref cycles).

Root Cause

When parsed documents contain circular object references, calling .json() and passing the result to diff.diff() eventually triggers JSON.stringify() on the circular structure, which throws a TypeError.

Solution

Added internal helper functions safeJson() and safeValue() that:

  • Traverse the document graph using a WeakSet to detect already-visited objects
  • Replace circular back-references with a [Circular] placeholder string
  • Preserve all non-circular data intact

Changes

  • src/apps/cli/commands/diff.ts

    • Added safeJson() / safeValue() helpers (~30 lines)
    • Wrapped both .json() calls in diff.diff() with safeJson()
  • test/unit/commands/diff-circular.test.ts (new)

    • 5 test cases covering: normal docs, circular refs, self-references, data preservation, primitive handling

Test Reproduction

```yaml

circular.yml

asyncapi: 3.1.0
info:
title: Testing
version: 1.0.0
components:
schemas:
filters:
type: array
items:
anyOf:
- $ref: "#/components/schemas/filters"
- type: string
entity-created:
type: object
properties:
filters:
$ref: "#/components/schemas/filters"
```

Before: TypeError: Converting circular structure to JSON
After: Diff completes successfully with [Circular] placeholders in output

The diff command crashed with TypeError when documents contained
circular references (e.g., schemas referencing themselves via
anyOf + $ref cycles). Added safeJson() helper that traverses
the document graph and replaces circular refs with [Circular]
placeholders, preventing JSON.stringify from throwing.

- Add safeJson() and safeValue() internal helpers
- Wrap .json() calls in diff command with safeJson()
- Add 5 unit tests for circular reference handling

Fixes asyncapi#2093
@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Apr 13, 2026

⚠️ No Changeset found

Latest commit: d1cd063

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@sonarqubecloud
Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: To Triage

Development

Successfully merging this pull request may close these issues.

[BUG] Diff command doesn't work when circular reference

1 participant