Skip to content

fix(openapi): handle circular references when writing request body schemas#409

Merged
boyney123 merged 1 commit into
mainfrom
fix/openapi-circular-request-body
May 19, 2026
Merged

fix(openapi): handle circular references when writing request body schemas#409
boyney123 merged 1 commit into
mainfrom
fix/openapi-circular-request-body

Conversation

@boyney123

Copy link
Copy Markdown
Contributor

What This PR Does

The OpenAPI generator previously only guarded response-schema serialization against circular references. Request bodies that contained circular $refs (for example self-referencing oneOf / allOf constructs) would cause JSON.stringify to throw, aborting the schema file write for that message. This PR reuses a single circular-safe stringifier for both request bodies and responses so recursive schemas still produce a request-body.json / response-*.json with cycles marked as "[Circular]".

Changes Overview

Key Changes

  • Extract a stringifySchema helper that walks the schema with a WeakSet and replaces seen objects with "[Circular]".
  • Use the helper for both request-body.json and response-*.json writes in processMessagesForOpenAPISpec.
  • Add a regression test plus a circular-request-body.json fixture exercising deeply nested oneOf / allOf cycles.
  • Minor: ignore .pnpm-store and fix trailing newline in root .gitignore.

How It Works

stringifySchema is a thin wrapper around JSON.stringify with a replacer that tracks visited objects in a WeakSet. When the same object reference is encountered a second time during serialization, it is emitted as the string "[Circular]" instead of recursing. The existing response-side try/catch + inline replacer is removed in favor of calling this helper directly, and the request-body branch is updated to use it as well.

The new test feeds an OpenAPI spec whose request body schema has a cycle via oneOfallOf back to an ancestor, then asserts:

  • Non-circular fields still serialize with their original types.
  • The cycle point in the request body is replaced with the "[Circular]" sentinel.

Breaking Changes

None. Output for non-circular schemas is byte-identical; only previously-failing circular request bodies are affected.

Additional Notes

  • Changeset: @eventcatalog/generator-openapi patch.
  • No public API changes; helper is module-local.

…hemas

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@changeset-bot

changeset-bot Bot commented May 19, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: e941b6e

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-openapi 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 79a0620 into main May 19, 2026
5 of 6 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.

1 participant