Repository navigation
Conversation
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}.
commit: |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 declaredidparameter, so Swagger UI cannot fill it in and client generators produce broken URLs. The generated operationId also picks up the regex, for exampleget_a_slug[a-z]2,3_xfor/a/:slug{[a-z]{2,3}}/x.Root cause
normalizePath()converts parameters with.replaceAll(/:(\w+)/g, "{$1}"), which only replaces the:namepart 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_PATTERNthat matches:nameplus 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 throughnormalizePath()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_itemsinstead of one containing the regex). The itty-router adapter sharesnormalizePath(), but itty has no{...}syntax, so its paths (including:id.:formatand: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
operationIdvalues 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
patternon the parameter schema, since the parameter schema comes from the endpoint (it may bez.number(), for example). Adding it aspatternfor guessed string parameters could be a follow-up if that is wanted.Type of change
How was this tested?
Added
drops hono regex constraints from path parameterstotests/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/12still validatesidas12. Onmainit fails with/posts/{id}{[0-9]+},/codes/{code}{[a-z]{2,3}}/itemsand/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 onmain),npm run lintandnpm run buildpass. Added a patch changeset.Checklist
npm testandnpm run lintpassnpx changeset, empty changeset for internal changes)