Skip to content

fix: handle circular references from recursive schemas - #231

Merged
aayushmau5 merged 1 commit into
asyncapi:masterfrom
willbamford:fix/circular-references
Nov 15, 2026
Merged

aayushmau5 merged 1 commit into
asyncapi:masterfrom
willbamford:fix/circular-references

Conversation

@willbamford

Copy link
Copy Markdown
Contributor

Description

Diffing parsed documents (document.json()) that contain a recursive schema throws TypeError: Converting circular structure to JSON. Resolved $refs turn the schema into a circular object, and fast-json-patch's compare clones its inputs with JSON.stringify.

generateDiff now copies both documents first, replacing each reference back to an ancestor with a marker that names the ancestor's JSON pointer ([Circular /components/schemas/filters]). Using a single [Circular] placeholder would hide a recursive reference that gets redirected to a different ancestor, so the pointer is part of the marker. Shared, non-recursive references are copied as before.

Tests cover a v3 spec with a recursive anyOf schema through the parser, which fails on master, plus a redirected recursive reference and pointer escaping.

Related issue(s)

Same crash as asyncapi/cli#2093. asyncapi/cli#2113 works around it in the CLI only; fixing it here covers every consumer of the library.

via Claude Opus 5.5

Resolved $refs turn a recursive schema into a circular object, and
fast-json-patch's compare throws "Converting circular structure to JSON"
when it clones it. Replace each back-reference with a marker naming the
ancestor's JSON pointer before comparing, so a redirected recursive
reference still shows up as a change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Welcome to AsyncAPI. Thanks a lot for creating your first pull request. Please check out our contributors guide useful for opening a pull request.
Keep in mind there are also other channels you can use to interact with AsyncAPI community. For more details check out this issue.

@sonarqubecloud

Copy link
Copy Markdown

@willbamford
willbamford marked this pull request as ready for review November 12, 2026 13:19
@aayushmau5

Copy link
Copy Markdown
Member

Hi @willbamford Thanks for the PR.
Acknowledging this, I will take a look as soon as I can.

@aayushmau5 aayushmau5 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems fine to me.

@aayushmau5
aayushmau5 merged commit 7538d2b into asyncapi:master Nov 15, 2026
27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants