-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
fix(o2k) update path param regex to match 1 segment only #3298
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.
@@ -59,7 +59,7 @@ describe('services', () => { | |||
name: 'My_API-birds_id-get', | |||
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.
@@ -68,7 +68,7 @@ export function generateSlug(str: string, options: SlugifyOptions = {}): string | |||
const pathVariableSearchValue = /{([^}]+)}(?!:\/\/)/g; | |||
|
|||
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/yyy
as it shouldWith captures:
hello
:xxx
there
: yyyThis path also matches
/tracks/xxx/zzz/world/yyy/andthensome
but it should notWith captures:
hello
:xxx/zzz
there
:yyy/andthensome
The last one should have matched only a single path segment, but actually matches multiple.