Repository navigation
fix(generate-code): keep leading slash of the request target for URLs with a query but no path - #9536
Conversation
…URLs with a query but no path
|
Important Review skippedReview was skipped as selected files did not have any reviewable changes.
|
| Layer / File(s) | Summary |
|---|---|
Query-only replacement and regression coverage packages/bruno-app/src/components/Sidebar/Collections/Collection/CollectionItem/GenerateCodeItem/utils/snippet-generator.js, packages/bruno-app/src/components/Sidebar/Collections/Collection/CollectionItem/GenerateCodeItem/utils/snippet-generator.spec.js |
The generator prefixes / when a bare query is used as a request target. It retains the bare query when it follows an authority. Tests cover encoding settings, request-target outputs, full URLs, and URLs with existing paths. |
Estimated code review effort: 2 (Simple) | ~10 minutes
Change: Bug fix · Severity of issue fixed: Low
Merge Risk: 
Minimal · previous review bbc4b
-
The change addresses missing slashes in query-only request targets. No material merge risk was identified.
Pre-merge checks: 
5
-
Full details: Title check
-
Explanation
The title names the main change: Generate Code now keeps the leading slash of the request target for URLs that have a query but no path. The raw summary and PR objectives both describe this fix for HTTP, Node.js/Native, and Python targets, so the title matches the changeset. It is specific and follows conventional commit style, and it does not mention the spec file tests, which is acceptable.
Full details: Linked Issues check
-
Explanation
[
#9522] requires origin-form request targets to begin with/for HTTP/HTTP 1.1, Node.js/Native, and Python/Python3 when the URL has a query and no path. The whole-PR diff ofsnippet-generator.js(merge base to head) adds a branch fordesiredPathstarting with?. In that branch, each occurrence of the HTTPSnippet path is replaced through a callback. The callback checks the character before the match. If that character is not whitespace, a quote, a backtick,(,=, or,, the code keepsdesiredPathas typed. This keeps the full-URL form (for example curlhttp://host?x=1). Otherwise the code writes/${desiredPath}. For the request-line form (GET ?x=1, preceded by a space), the Node form (path: '?x=1', preceded by'), and the Python form ("?x=1", preceded by"), this yields/?x=1. The replacement callback signature (match, offset, string) is used correctly because the pattern has no capture groups. The spec file reportedly covers the three affected targets withencodeUrlon and off, raw and encoded queries, and an invariant over all 39 target/client combinations. I did not run the tests. Paths with a path component are not changed because the new branch runs only whendesiredPathstarts with?.
Full details: Out of Scope Changes check
-
Explanation
The whole-PR changes touch two files:
snippet-generator.js(+13/-1 lines, per the summary and the diff) and its spec file (+90 lines of regression tests). The source change applies only whendesiredPathstarts with?, which is the case#9522describes. The tests exercise the same behavior, including controls for curl and for URLs with a path. The PR states thatstripOriginand the output for URLs with a path are unchanged, and the diff supports this because the existingelsebranch keeps the originalreplaceAll. No unrelated product behavior is changed.
-
Finishing Touches
-
Generate unit tests (beta)
-
- Create a new PR
-
- Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
A query finds its proper path
A slash now leads the request
Full URLs keep their query
Encoded keys get their test
Existing paths stay in place
Snippets follow the URL
|
Hi @harxh07, thanks for raising this and linking a fix. It looks like it resolves the Generate Code bug. I'm moving it to the engineering team for review and will update here. |
rahul05ranjan
left a comment
There was a problem hiding this comment.
Traced the bug against main and it's exactly what the description says: for http://example.com?x=1&y=2, legacy url.parse reports pathname /, so httpSnippetPath becomes /?x=1&y=2 while stripOrigin(displayRawUrl) yields ?x=1&y=2 — the old replaceAll then rewrote GET /?x=1&y=2 HTTP/1.1 into GET ?x=1&y=2 HTTP/1.1, i.e. an invalid request line. Same for path: '/?x=1&y=2' and the python target.
I re-ran the string-level swap on both variants to confirm the new lookbehind behavior: with (?<=[^\s"'(=,])the post-authority occurrence incurl 'http://example.com/?x=1&y=2'` still gets swapped, while the request-target occurrences (preceded by space, quote, or paren) are left untouched. The (=, exclusions are a nice touch — those cover the path appearing as a value inside JSON-ish output rather than after the host.
One consequence worth noting (fine as a tradeoff, just documenting it): for path-less URLs with encodeUrl OFF, the request-target targets now keep the encoded path+query since the display swap is skipped there entirely. That's strictly better than the broken request line it replaces.
Also good call writing the spec against the real HTTPSnippet instead of a mock — the bug only shows up in the interaction between the swap and what each target actually prints.
|
Thanks @rahul05ranjan for the careful review, and for spotting the edge case in the last paragraph. I reproduced it: with URL Encoding off, I pushed a follow-up commit (0552041) that replaces the lookbehind regex with a small callback: right after the authority the URL is still shown as typed, and in request-target positions the raw query is now swapped in behind the |
…tor.spec.js Addresses review feedback: keep these tests in the existing spec file instead of a new one.
|
Congrats on the merge, @harxh07! Glad the review helped — the edge case turned out to be worth chasing. |
Description
Generate Code prints an invalid request target when the request URL has a query string but no path, e.g.
http://example.com?x=1&y=2.Fixes #9522
Problem
generateSnippetswaps the encodedpath?querythat HTTPSnippet printed (/?x=1&y=2) for the URL as typed. For a path-less URL,stripOriginreturns a bare?x=1&y=2(the existingquery onlyunit test expects that). That is right where the snippet prints the full URL (http://example.com?x=1&y=2in curl), but it drops the mandatory leading/where the snippet prints only the request target.I ran every target/client from
availableTargets()(39 combinations) withhttp://example.com?x=1&y=2; three were affected, with the URL Encoding setting on or off:GET ?x=1&y=2 HTTP/1.1GET /?x=1&y=2 HTTP/1.1path: '?x=1&y=2',path: '/?x=1&y=2',conn.request("GET", "?x=1&y=2")conn.request("GET", "/?x=1&y=2")On a real server, curl sends
GET /?x=1&y=2(200), while the three broken snippets get400 Bad Request. Bruno's own request (axios) also sendsGET /?x=1&y=2, so only the generated snippet was wrong.Fix
When the desired path starts with
?, every occurrence of the encoded path is still swapped for the raw one, but the swap now looks at what precedes it:http://host?x=1, as before;(,=or,): the raw query goes behind a/, because that is the request target.This also keeps a path-less URL consistent with a URL that has a path when URL encoding is off:
http://example.com?filter[name]=xshowsGET /?filter[name]=x, just likehttp://example.com/api?filter[name]=xshowsGET /api?filter[name]=x. As for URLs with a path, the characters are shown as typed in that mode (a literal space stays a space in the Node and Python snippets; the HTTP target keeps using%20). The first revision of this PR showed the encoded query here instead, which @rahul05ranjan pointed out in review; I confirmed the inconsistency and changed it.stripOriginis untouched, and the output for URLs with a path is unchanged.Testing
snippet-generator.path-less-url.spec.jsuses the real HTTPSnippet (the existing spec mocks it): the three affected targets xencodeUrlon/off, the raw and encoded query cases, controls for curl (URL still shown as typed) and for a URL that has a path, and an invariant over all 39 target/client combinations:http://host?x=1must print the same snippet ashttp://host/?x=1, apart from the full URL shown as typed.mainand pass with the fix; the raw-query and invariant cases fail on the first revision of this PR and pass now.GenerateCodeItemjest suites: 3 suites, 194 tests pass. ESLint is clean on both files.generateSnippet, not by clicking through the desktop app.Contribution Checklist:
Summary by CodeRabbit