{{ message }}
Repository navigation
fix(url): strip trailing question mark correctly for optional params with regex quantifiers - #5209
Merged
yusukebe merged 2 commits intoAug 13, 2026
Conversation
…with regex quantifiers
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 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. |
Contributor
Author
|
Thanks @usualoma, I made necessary changes as you suggested. :) |
Member
|
@spellsaif I've confirmed it. I think it's good! |
Member
1 task
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.

Fixes
checkOptionalParameterinsrc/utils/url.tsto 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('?', '')tosegment.replace(/\?$/, '')incheckOptionalParameterso that only the trailing optional parameter indicator?at the end of the path segment is removed.Testing
checkOptionalParameter('/api/:id{[0-9]?}?')insrc/utils/url.test.ts.npm test src/utils/url.test.tspasses 100% cleanly.The author should do the following, if applicable
bun run format:fix && bun run lint:fixto format the code