fix(asyncapi): handle circular references in oneOf/allOf schemas#365
Merged
fix(asyncapi): handle circular references in oneOf/allOf schemas#365
Conversation
AsyncAPI files using discriminator patterns with oneOf/allOf that reference each other create circular object graphs after parsing. JSON.stringify crashes on these. Replace with a safe serializer that reconstructs valid $ref paths using x-parser-schema-id. Closes #364 Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
🦋 Changeset detectedLatest commit: 59f0d56 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #364
What This PR Does
Fixes a crash in the AsyncAPI generator when processing schemas that use
oneOf/allOfwith discriminator patterns that create circular references. The parser resolves$refs by inlining objects, which creates cycles (e.g.batch-element→oneOf→typeA→allOf→batch-element).JSON.stringifythrowsTypeError: Converting circular structure to JSONon these.Changes Overview
Key Changes
safeStringifyhelper that detects circular references and reconstructs valid$refpaths using thex-parser-schema-idproperty (e.g.#/components/schemas/ec-test.batch-element)JSON.stringifywithsafeStringifyin schema serialization and parsed spec file serializationoneOf/allOfcircular refs)$refpaths for circular referencesHow It Works
The AsyncAPI parser resolves
$refreferences by inlining the referenced objects, which creates circular JavaScript object graphs when schemas reference each other. ThesafeStringifyfunction uses aWeakSetto track visited objects. When a circular reference is detected, it reads thex-parser-schema-idproperty from the object to reconstruct a valid JSON Schema$refpath (#/components/schemas/<id>), producing valid output instead of crashing.Breaking Changes
None
Additional Notes
The fixture file is the exact file from the bug reporter's issue, ensuring we handle the real-world case.
🤖 Generated with Claude Code