Skip to content

fix(swagger-ui): load the standalone preset when urls is set - #2241

Open
pedrorubinger wants to merge 1 commit into
honojs:mainfrom
pedrorubinger:fix/swagger-ui-urls-standalone-preset
Open

pedrorubinger wants to merge 1 commit into
honojs:mainfrom
pedrorubinger:fix/swagger-ui-urls-standalone-preset

Conversation

@pedrorubinger

Copy link
Copy Markdown

Fixes #1480

Problem

Passing urls to swaggerUI() renders "No API definition provided." and never loads a definition.

Swagger UI only loads a definition from urls in the TopBar plugin (its componentDidMount calls loadSpec(urls[selectedIndex].url)). TopBar ships in swagger-ui-standalone-preset.js, which the middleware never loads, so urls is parsed as config but nothing fetches it. Users can't work around it with presets: ['SwaggerUIStandalonePreset'] either, because that global is not on the page.

Change

When urls is set:

  • also load swagger-ui-standalone-preset.js (it respects version and baseUrl)
  • default to presets: [SwaggerUIBundle.presets.apis, SwaggerUIStandalonePreset] and layout: 'StandaloneLayout'

User-provided presets and layout are kept as is. Pages that only use url render exactly as before.

This follows the approach @sor4chi described in the issue, except that it turns on automatically with urls instead of behind a new enableStandalone flag. Since urls does not work without the preset, enabling it automatically does not change any working setup, and it avoids a new option. Happy to switch to an explicit flag if you prefer.

It also joins the rendered asset tags. With two scripts, the array was stringified with a comma that showed up as text on the page. That removed two now-unused entries from eslint-suppressions.json.

Testing

  • Updated the urls rendering test and added tests for user-provided presets/layout, for url not loading the preset, and for remoteAssets({ standalonePreset: true }).
  • Checked in Chrome against a local Hono app:
    • before: "No API definition provided."
    • after: the first definition loads, the top bar dropdown switches between definitions, and ?urls.primaryName=... deep links open the selected one
    • works with version, baseUrl (unpkg), and specs served by @hono/zod-openapi doc() routes
    • url only: unchanged, no extra script

The author should do the following, if applicable

  • Add tests
  • Run tests
  • pnpm changeset at the top of this repo and push the changeset
  • Follow the contribution guide

Swagger UI only loads a definition from `urls` through the TopBar plugin,
which ships in swagger-ui-standalone-preset.js. The middleware never loaded
that script, so `urls` rendered "No API definition provided.".

When `urls` is given, also load the standalone preset and default to
`presets: [SwaggerUIBundle.presets.apis, SwaggerUIStandalonePreset]` and
`layout: 'StandaloneLayout'`. User-provided `presets` and `layout` are kept.

Also join the rendered asset tags so multiple scripts are not separated by
a stray comma in the page.

Fixes honojs#1480
@changeset-bot

changeset-bot Bot commented Nov 7, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 52df529

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

This PR includes changesets to release 1 package
Name Type
@hono/swagger-ui 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

@codecov

codecov Bot commented Nov 7, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.70%. Comparing base (d813df3) to head (52df529).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2241      +/-   ##
==========================================
+ Coverage   92.69%   92.70%   +0.01%     
==========================================
  Files         119      119              
  Lines        4365     4374       +9     
  Branches     1139     1145       +6     
==========================================
+ Hits         4046     4055       +9     
  Misses        284      284              
  Partials       35       35              
Flag Coverage Δ
swagger-ui 96.22% <100.00%> (+0.77%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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.

Swagger UI "urls" config shows "No API definition provided."

1 participant