Skip to content

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

Open
gavin913427-hash wants to merge 1 commit intoasyncapi:masterfrom
gavin913427-hash:fix/circular-reference-diff
Open

fix: handle circular references in diff command gracefully (fixes #2093)#2167
gavin913427-hash wants to merge 1 commit intoasyncapi:masterfrom
gavin913427-hash:fix/circular-reference-diff

Conversation

@gavin913427-hash
Copy link
Copy Markdown

What

Handle circular references in the diff command gracefully instead of crashing.

Why

When AsyncAPI documents contain circular $ref references, the json() method on parsed documents throws a TypeError with a message about circular structures. The diff command didn't handle this case, resulting in an unhelpful error for users.

How

  • Added a try-catch around the json() calls
  • When a circular reference is detected (TypeError with 'circular' in message), show a clear error message
  • The message guides users to resolve circular references before running diff

Changes

File Change
src/apps/cli/commands/diff.ts Add error handling for circular references

Testing

# Create a file with circular reference
cat > circular.yaml <<EOF
asyncapi: 3.1.0
info:
  title: Testing
  version: 1.0.0
channels:
  my_entity_created:
    messages:
      entity-created:
        payload:
          \$ref: '#/components/schemas/entity-created'
components:
  schemas:
    entity-created:
      type: object
      properties:
        self:
          \$ref: '#/components/schemas/entity-created'
EOF

# Before: crashes with TypeError
asyncapi diff circular.yaml circular.yaml

# After: clear error message
# "Cannot diff documents with circular references. Please resolve the circular \$ref references in your AsyncAPI documents before running diff."

Fixes #2093

When AsyncAPI documents contain circular $ref references, the diff
command now catches the error and provides a clear message instead
of crashing with an unhelpful TypeError.

The error message guides users to resolve circular references before
running the diff command.

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

changeset-bot Bot commented May 4, 2026

⚠️ No Changeset found

Latest commit: a36e5c3

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

sonarqubecloud Bot commented May 4, 2026

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