-
Notifications
You must be signed in to change notification settings - Fork 2.2k
fix(o2k) update path param regex to match 1 segment only #3298
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
b229a3d to
d5dc70d
Compare
5af7d2a to
4db0363
Compare
|
the CI failure doesn't provide a meaningfull hint (at least to me) as to what failed... any help appreciated |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| expect(pathVariablesToRegex('/foo/{bar}/{baz}')).toBe( | ||
| '/foo/(?<bar>[^\\/\\r\\n\\t\\f\\v ]+)/(?<baz>[^\\/\\r\\n\\t\\f\\v ]+)$', | ||
| ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| strip_path: false, | ||
| methods: ['GET'], | ||
| paths: ['/birds/(?<id>\\S+)$'], | ||
| paths: ['/birds/(?<id>[^\\/\\r\\n\\t\\f\\v ]+)$'], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
|
|
||
| export function pathVariablesToRegex(p: string): string { | ||
| const result = p.replace(pathVariableSearchValue, '(?<$1>\\S+)'); | ||
| const result = p.replace(pathVariableSearchValue, '(?<$1>[^\\/\\s]+)'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be worth adding a comment here to explain this regex, something like
| const result = p.replace(pathVariableSearchValue, '(?<$1>[^\\/\\s]+)'); | |
| // match any non-whitespace character, excluding / | |
| const result = p.replace(pathVariableSearchValue, '(?<$1>[^\\/\\s]+)'); |
existing regex '\S+' is equivalent to '[^\s]+' by adding the
'/' character as not allowed, we limit the regex to match only 1 path
segment
This path: "/tracks/{hello}/world/{there}”
Would create: “/tracks/(?<hello>\S+)/world/(?<there>\S+)$”
This path works as expected:
/tracks/xxx/world/yyy
With captures:
hello: xxx
there: yyy
This path also works, unexpected:
/tracks/xxx/zzz/world/yyy/andthensome
With captures:
hello: xxx/zzz
there: yyy/andthensome
The last one should have matched only a single path segment, but
actually matches multiple.
4db0363 to
65c6a01
Compare



existing regex
\S+is equivalent to[^\s]+by adding the/character as not allowed, we limit the regex to match only 1 path segmentThis path:
/tracks/{hello}/world/{there}Would create:
/tracks/(?<hello>\S+)/world/(?<there>\S+)$When published, this path matches
/tracks/xxx/world/yyyas it shouldWith captures:
hello:xxxthere: yyyThis path also matches
/tracks/xxx/zzz/world/yyy/andthensomebut it should notWith captures:
hello:xxx/zzzthere:yyy/andthensomeThe last one should have matched only a single path segment, but actually matches multiple.