Skip to content

openapi: replace hand-rolled $ref walker with @readme/openapi-parser - #353

Closed
RhysSullivan wants to merge 1 commit into
rs/openapi-encode-non-json-bodiesfrom
rs/openapi-swagger-parser
Closed

RhysSullivan wants to merge 1 commit into
rs/openapi-encode-non-json-bodiesfrom
rs/openapi-swagger-parser

Conversation

@RhysSullivan

@RhysSullivan RhysSullivan commented Apr 20, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • Replaces the hand-rolled $ref walker with @readme/openapi-parser 6.x (same json-schema-ref-parser core as @apidevtools/swagger-parser, but with a proper dual ESM/CJS exports map — friction-free in bundlers and Workers).
  • parse.ts now calls dereference(api, { resolve: { external: false }, dereference: { circular: true } }) after the JSON/YAML decode + 3.x version gate.
  • Circular cycles are preserved via object identity so the walker terminates instead of blowing the stack. External $refs are deliberately not resolved (Workers-safe) and broken internal pointers surface as OpenApiParseError.
  • openapi-utils.ts DocResolver collapses to a thin identity passthrough — the public API is preserved but there's no pointer walking left.

Why

The hand-written resolver didn't handle transitive cycles (mutual recursion, allOf cycles) and had a few bugs around array-indexed pointers on real-world specs. Moving to the upstream parser means we follow the spec (RFC 6901 JSON Pointer, OpenAPI 3.x rules) by default.

Test plan

  • bunx vitest run in packages/plugins/openapi — 47/47 pass
  • circular-ref.test.ts (new) — 4 tests covering self-cycle, mutual recursion, extract-over-cycles, identity preservation
  • malformed-spec.test.ts (new) — 7 tests for empty/garbage input, Swagger 2.x rejection, broken $ref, tagged-error matching
  • Cloudflare 16MB spec dereferences in ~770ms
  • bunx tsc --noEmit clean

parse.ts now dereferences specs via @readme/openapi-parser (6.x), which
uses @apidevtools/json-schema-ref-parser under the hood. Fixes two known
bugs in our previous walker:
  - circular $refs no longer hang — cycles preserved via object identity
  - RFC 6901 pointer escapes (~0, ~1) handled correctly

Dereference runs with resolve.external: false so we never reach out to
the network from the parser (Cloudflare Workers compatibility), and
with dereference.circular: true so cyclic specs resolve without error.

Broken internal $refs now surface as OpenApiParseError on the Effect
channel instead of silently returning a partial extraction.

DocResolver stays exported but collapses to a passthrough now that the
doc is already dereferenced at parse time — extract.ts and preview.ts
keep working unchanged.

Adds circular-ref.test.ts and malformed-spec.test.ts covering the new
behaviour; existing 36 tests (including the 16MB Cloudflare fixture)
continue to pass.

RhysSullivan commented Apr 20, 2026 •

Copy link
Copy Markdown
Collaborator Author

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Apr 20, 2026 •

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Updated (UTC)
✅ Deployment successful!
View logs
executor-cloud 0328aea Apr 20 2026, 10:22 PM

@pkg-pr-new

pkg-pr-new Bot commented Apr 20, 2026

Copy link
Copy Markdown

Open in StackBlitz

@executor/sdk

npm i https://pkg.pr.new/RhysSullivan/executor/@executor/sdk@353

@executor/plugin-file-secrets

npm i https://pkg.pr.new/RhysSullivan/executor/@executor/plugin-file-secrets@353

@executor/plugin-google-discovery

npm i https://pkg.pr.new/RhysSullivan/executor/@executor/plugin-google-discovery@353

@executor/plugin-graphql

npm i https://pkg.pr.new/RhysSullivan/executor/@executor/plugin-graphql@353

@executor/plugin-keychain

npm i https://pkg.pr.new/RhysSullivan/executor/@executor/plugin-keychain@353

@executor/plugin-mcp

npm i https://pkg.pr.new/RhysSullivan/executor/@executor/plugin-mcp@353

@executor/plugin-oauth2

npm i https://pkg.pr.new/RhysSullivan/executor/@executor/plugin-oauth2@353

@executor/plugin-onepassword

npm i https://pkg.pr.new/RhysSullivan/executor/@executor/plugin-onepassword@353

@executor/plugin-openapi

npm i https://pkg.pr.new/RhysSullivan/executor/@executor/plugin-openapi@353

@executor/plugin-workos-vault

npm i https://pkg.pr.new/RhysSullivan/executor/@executor/plugin-workos-vault@353

executor

npm i https://pkg.pr.new/RhysSullivan/executor@353

commit: 0328aea

@RhysSullivan
RhysSullivan deleted the rs/openapi-swagger-parser branch June 11, 2026 08:01
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