fix(url): strip trailing question mark correctly for optional params with regex quantifiers by spellsaif · Pull Request #5209 · honojs/hono · GitHub
Skip to content

fix(url): strip trailing question mark correctly for optional params with regex quantifiers - #5209

Merged
yusukebe merged 2 commits into
honojs:mainfrom
spellsaif:fix/url-optional-param-regex-quantifier
Aug 13, 2026
Merged

yusukebe merged 2 commits into
honojs:mainfrom
spellsaif:fix/url-optional-param-regex-quantifier

Conversation

@spellsaif

Copy link
Copy Markdown
Contributor

Fixes checkOptionalParameter in src/utils/url.ts to properly strip the trailing ? optional parameter indicator instead of stripping ? quantifiers inside custom parameter regexes (e.g., /:id{[0-9]?}?).

Motivation & Problem

When defining routes with optional parameters containing a RegExp with a ? quantifier (such as /:id{[0-9]?}? or /:lang{[a-z]{2}?}?), segment.replace('?', '') replaces the first ? encountered in the segment string.

This corrupts the inner RegExp (changing [0-9]? to [0-9]) and leaves the trailing ? intact at the end of the segment (/api/:id{[0-9]}?), causing malformed route registration in the router matcher.

Solution

Change segment.replace('?', '') to segment.replace(/\?$/, '') in checkOptionalParameter so that only the trailing optional parameter indicator ? at the end of the path segment is removed.

Testing

  • Added unit test case checkOptionalParameter('/api/:id{[0-9]?}?') in src/utils/url.test.ts.
  • Verified npm test src/utils/url.test.ts passes 100% cleanly.

The author should do the following, if applicable

  • Add tests
  • Run tests
  • bun run format:fix && bun run lint:fix to format the code
  • Add TSDoc/JSDoc to document the code

@spellsaif spellsaif changed the title fix(url): strip trailing question mark correctly for optional params … fix(url): strip trailing question mark correctly for optional params with regex quantifiers Aug 10, 2026
@codecov

codecov Bot commented Aug 11, 2026 •

Copy link
Copy Markdown

@usualoma

Copy link
Copy Markdown
Member

Hi @spellsaif,

Thanks for catching this. This is indeed a bug, and your proposed change fixes the reported case.

While reviewing it, I found that the preceding condition was also incorrect:

if (/\?/.test(segment)) {

This treats any ? in the segment, including quantifiers inside a custom RegExp, as an optional parameter marker. Could you update the fix to cover both the detection and removal, so that only a trailing ? is treated as the marker? Something like this:

diff --git a/src/utils/url.ts b/src/utils/url.ts
index ea92ff93..12493648 100644
--- a/src/utils/url.ts
+++ b/src/utils/url.ts
@@ -187,13 +187,13 @@ export const checkOptionalParameter = (path: string): string[] | null => {
     if (segment !== '' && !/\:/.test(segment)) {
       basePath += '/' + segment
     } else if (/\:/.test(segment)) {
-      if (/\?/.test(segment)) {
+      if (segment.charCodeAt(segment.length - 1) === 63) {
         if (results.length === 0 && basePath === '') {
           results.push('/')
         } else {
           results.push(basePath)
         }
-        const optionalSegment = segment.replace('?', '')
+        const optionalSegment = segment.slice(0, -1)
         basePath += '/' + optionalSegment
         results.push(basePath)
       } else {

Thanks again for reporting this.

@spellsaif

Copy link
Copy Markdown
Contributor Author

Thanks @usualoma, I made necessary changes as you suggested. :)

@usualoma

Copy link
Copy Markdown
Member

@spellsaif I've confirmed it. I think it's good!

@yusukebe yusukebe left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@yusukebe

Copy link
Copy Markdown
Member

@yusukebe
yusukebe merged commit d982f63 into honojs:main Aug 13, 2026
20 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.

3 participants