Skip to content

fix(asyncapi): handle circular references in oneOf/allOf schemas#365

Merged
boyney123 merged 1 commit intomainfrom
fix/asyncapi-circular-ref-oneof-allof
Mar 19, 2026
Merged

fix(asyncapi): handle circular references in oneOf/allOf schemas#365
boyney123 merged 1 commit intomainfrom
fix/asyncapi-circular-ref-oneof-allof

Conversation

@boyney123
Copy link
Contributor

Closes #364

What This PR Does

Fixes a crash in the AsyncAPI generator when processing schemas that use oneOf/allOf with discriminator patterns that create circular references. The parser resolves $refs by inlining objects, which creates cycles (e.g. batch-elementoneOftypeAallOfbatch-element). JSON.stringify throws TypeError: Converting circular structure to JSON on these.

Changes Overview

Key Changes

  • Add safeStringify helper that detects circular references and reconstructs valid $ref paths using the x-parser-schema-id property (e.g. #/components/schemas/ec-test.batch-element)
  • Replace JSON.stringify with safeStringify in schema serialization and parsed spec file serialization
  • Add test using the exact fixture file from the bug report (discriminator pattern with oneOf/allOf circular refs)
  • Test verifies the full schema output including valid $ref paths for circular references

How It Works

The AsyncAPI parser resolves $ref references by inlining the referenced objects, which creates circular JavaScript object graphs when schemas reference each other. The safeStringify function uses a WeakSet to track visited objects. When a circular reference is detected, it reads the x-parser-schema-id property from the object to reconstruct a valid JSON Schema $ref path (#/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

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-bot
Copy link

changeset-bot bot commented Mar 19, 2026

🦋 Changeset detected

Latest commit: 59f0d56

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@eventcatalog/generator-asyncapi Patch

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

@boyney123 boyney123 merged commit 9cb84cc into main Mar 19, 2026
7 of 8 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.

AsyncAPI: oneOf / allOf references can not be parsed

1 participant