-
Notifications
You must be signed in to change notification settings - Fork 225
fix(drift): match path segments that mix literal text with parameters #2994
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
Open
ariesclark
wants to merge
2
commits into
Redocly:main
Choose a base branch
from
ariesclark:fix/drift-multi-parameter-paths
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+92
−5
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| '@redocly/cli': patch | ||
| --- | ||
|
|
||
| Fixed `drift` and `coverage` failing to match a path template whose segment mixes literal text with parameters, such as `/instances/{worldId}:{instanceId}`. | ||
| Only a segment that was entirely one parameter was recognized, so these templates were compiled as literal text and never matched any request. | ||
| Affected requests were reported as undocumented by `drift` and left out of the `coverage` figures. |
58 changes: 58 additions & 0 deletions
58
packages/cli/src/commands/drift/__tests__/compile-openapi-path.test.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import { compileOpenApiPath } from '../utils/http.js'; | ||
|
|
||
| describe('compileOpenApiPath', () => { | ||
| it('compiles a segment that is a single parameter', () => { | ||
| const { regex, params } = compileOpenApiPath('/users/{userId}'); | ||
|
|
||
| expect(params).toEqual(['userId']); | ||
| expect(regex.exec('/users/usr_abc')?.[1]).toBe('usr_abc'); | ||
| }); | ||
|
|
||
| it('does not let a parameter span a path separator', () => { | ||
| const { regex } = compileOpenApiPath('/users/{userId}'); | ||
|
|
||
| expect(regex.exec('/users/usr_abc/friends')).toBeNull(); | ||
| }); | ||
|
|
||
| it('compiles two parameters separated by a literal inside one segment', () => { | ||
| const { regex, params } = compileOpenApiPath('/instances/{worldId}:{instanceId}'); | ||
|
|
||
| expect(params).toEqual(['worldId', 'instanceId']); | ||
|
|
||
| const match = regex.exec( | ||
| '/instances/wrld_a:85981~group(grp_b)~groupAccessType(public)~region(us)' | ||
| ); | ||
| expect(match?.[1]).toBe('wrld_a'); | ||
| expect(match?.[2]).toBe('85981~group(grp_b)~groupAccessType(public)~region(us)'); | ||
| }); | ||
|
|
||
| it('splits on the first separator when the trailing value holds another', () => { | ||
| const { regex } = compileOpenApiPath('/instances/{worldId}:{instanceId}'); | ||
| const match = regex.exec('/instances/wrld_a:12345~region(us):extra'); | ||
|
|
||
| expect(match?.[1]).toBe('wrld_a'); | ||
| expect(match?.[2]).toBe('12345~region(us):extra'); | ||
| }); | ||
|
|
||
| it('keeps matching a multi-parameter segment when a suffix segment follows', () => { | ||
| const { regex } = compileOpenApiPath('/instances/{worldId}:{instanceId}/shortName'); | ||
|
|
||
| expect(regex.exec('/instances/wrld_a:123~private(usr_b)/shortName')?.[2]).toBe( | ||
| '123~private(usr_b)' | ||
| ); | ||
| }); | ||
|
|
||
| it('ranks a partially literal segment above a bare parameter', () => { | ||
| expect(compileOpenApiPath('/instances/{worldId}:{instanceId}').score).toBeGreaterThan( | ||
| compileOpenApiPath('/instances/{instanceId}').score | ||
| ); | ||
| }); | ||
|
|
||
| it('treats a segment with no parameters as a literal', () => { | ||
| const { regex, params } = compileOpenApiPath('/instances/recent'); | ||
|
|
||
| expect(params).toEqual([]); | ||
| expect(regex.exec('/instances/anything')).toBeNull(); | ||
| expect(regex.exec('/instances/recent')).not.toBeNull(); | ||
| }); | ||
| }); | ||
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
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.
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.
Many thanks for the fix — the matching logic is correct, but the generated regexes need a performance improvement before this can merge.
Each parameter compiles to a lazy ([^/]+?) group, so when a segment holds several parameters, the regex engine tries every possible way to split a non-matching value between them — super-linear backtracking. Measured on this branch, a single exec call:
Since exec is synchronous, this blocks the event loop — drift just hangs, no error, no timeout. Captures routinely contain long junk paths (scanner traffic, malformed URLs), so this is reachable in normal use, and main isn't affected today because it never emits more than one group per segment.
Could you rework the compilation so matching stays linear no matter how many parameters a segment holds? Here's a test that pins the requirement — it currently fails at ~2,000 ms:
Please also add this test which ensures that we don't introduce solution that may break trailing literal handling:
Matching behavior shouldn't change — e.g. /files/{name}.json must still match /files/report.v1.json with name = report.v1, so simply excluding the separator characters from the parameter's character class isn't enough.