Skip to content

fix: drop hono regex constraints from OpenAPI path parameters - #404

Open
kwy404 wants to merge 1 commit into
cloudflare:mainfrom
kwy404:fix/hono-regex-path-params
Open

kwy404 wants to merge 1 commit into
cloudflare:mainfrom
kwy404:fix/hono-regex-path-params

Conversation

@kwy404

@kwy404 kwy404 commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Summary

A Hono route with a regex constraint on a path parameter, for example router.get("/posts/:id{[0-9]+}", Endpoint), is documented in the OpenAPI schema as /posts/{id}{[0-9]+} instead of /posts/{id}. The request itself is routed and validated fine, but the generated path does not match the declared id parameter, so Swagger UI cannot fill it in and client generators produce broken URLs. The generated operationId also picks up the regex, for example get_a_slug[a-z]2,3_x for /a/:slug{[a-z]{2,3}}/x.

Root cause

normalizePath() converts parameters with .replaceAll(/:(\w+)/g, "{$1}"), which only replaces the :name part and leaves Hono's {pattern} suffix in the path. registerRoute() extracts the parameter names with the same :(\w+) pattern, so a regex that contains a colon followed by a word (like :time{\d+:\d+}) also added a bogus extra parameter name.

Fix

Both places now use one PATH_PARAM_PATTERN that matches :name plus an optional {...} constraint, allowing one level of nested braces such as {[a-z]{2,3}}. The constraint is dropped from the documented path, and parameter names are taken from the same match. Nested routers mounted on a path with a regex (router.route("/users/:userId{[0-9]+}/posts", child)) are fixed the same way, since they go through normalizePath() too.

Paths without a regex constraint are unchanged, including optional (:id?), greedy (:id+) and wildcard routes. Routes with a regex now get a clean operationId (get_codes_code_items instead of one containing the regex). The itty-router adapter shares normalizePath(), but itty has no {...} syntax, so its paths (including :id.:format and :id+) are unchanged. A regex with more than one level of nested braces is not matched and keeps the old output.

This is visible in generated schemas for regex routes: the path key and the generated operationId change. Explicit operationId values are not affected. Two routes that differ only in the regex (/posts/:id{[0-9]+} and /posts/:id{[a-z]+}) now map to the same OpenAPI path and method, so only the last one is documented; OpenAPI cannot express both anyway. The changeset says this.

The constraint is dropped rather than turned into a pattern on the parameter schema, since the parameter schema comes from the endpoint (it may be z.number(), for example). Adding it as pattern for guessed string parameters could be a follow-up if that is wanted.

Type of change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation
  • Internal (tests, CI, refactor)

How was this tested?

Added drops hono regex constraints from path parameters to tests/integration/openapi-output.test.ts. It registers a regex route with an endpoint, one with a nested-brace regex and no schema, and a nested router mounted under a regex parameter, then checks the documented paths, the operationId, the guessed path parameters, and that /posts/12 still validates id as 12. On main it fails with /posts/{id}{[0-9]+}, /codes/{code}{[a-z]{2,3}}/items and /users/{userId}{[0-9]+}/posts/{id}{[0-9]+}; with the fix it passes.

I also compared the old and new path conversion for about 20 inputs (root, trailing and double slashes, several parameters, optional, greedy and wildcard parameters, :user-id, literal {x}, regexes with colons, dots and nested braces, :id{[0-9]+}.json). Only paths with a {...} constraint after a parameter changed.

npm test (tsc plus vitest, 513 tests, 512 on main), npm run lint and npm run build pass. Added a patch changeset.

Checklist

  • Tests added or updated
  • npm test and npm run lint pass
  • Changeset added (npx changeset, empty changeset for internal changes)
  • Docs updated (if user-facing)
  • No breaking change, or the breaking change is documented

A Hono route like /posts/:id{[0-9]+} was documented as
/posts/{id}{[0-9]+}, and its generated operationId included the regex.
The path parameter pattern now also matches the optional regex
constraint, so the path is documented as /posts/{id}.
@kwy404
kwy404 requested a review from G4brym as a code owner November 26, 2026 22:37
@pkg-pr-new

pkg-pr-new Bot commented Nov 26, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/chanfana@404

commit: a061f37

This branch has not been deployed

No deployments
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