Skip to content

fix(generate-code): keep leading slash of the request target for URLs with a query but no path - #9536

Merged
bijin-bruno merged 4 commits into
usebruno:mainfrom
harxh07:bugfix/generate-code-path-less-url
Nov 3, 2026
Merged

bijin-bruno merged 4 commits into
usebruno:mainfrom
harxh07:bugfix/generate-code-path-less-url

Conversation

@harxh07

@harxh07 harxh07 commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

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

generateSnippet swaps the encoded path?query that HTTPSnippet printed (/?x=1&y=2) for the URL as typed. For a path-less URL, stripOrigin returns a bare ?x=1&y=2 (the existing query only unit test expects that). That is right where the snippet prints the full URL (http://example.com?x=1&y=2 in curl), but it drops the mandatory leading / where the snippet prints only the request target.

I ran every target/client from availableTargets() (39 combinations) with http://example.com?x=1&y=2; three were affected, with the URL Encoding setting on or off:

Target Before After
HTTP / HTTP 1.1 GET ?x=1&y=2 HTTP/1.1 GET /?x=1&y=2 HTTP/1.1
Node.js / Native path: '?x=1&y=2', path: '/?x=1&y=2',
Python / Python3 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 get 400 Bad Request. Bruno's own request (axios) also sends GET /?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:

  • right after the authority (the full URL in curl and friends): the URL is shown as typed, http://host?x=1, as before;
  • anywhere else (a request line, a quoted string, after (, = 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]=x shows GET /?filter[name]=x, just like http://example.com/api?filter[name]=x shows GET /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. stripOrigin is untouched, and the output for URLs with a path is unchanged.

Testing

  • New snippet-generator.path-less-url.spec.js uses the real HTTPSnippet (the existing spec mocks it): the three affected targets x encodeUrl on/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=1 must print the same snippet as http://host/?x=1, apart from the full URL shown as typed.
  • The request-target cases fail on main and pass with the fix; the raw-query and invariant cases fail on the first revision of this PR and pass now.
  • GenerateCodeItem jest suites: 3 suites, 194 tests pass. ESLint is clean on both files.
  • Verified through generateSnippet, not by clicking through the desktop app.

Contribution Checklist:

  • I've used AI significantly to create this pull request (written with Claude; I reviewed the output and ran the tests above)
  • The pull request only addresses one issue or adds one feature.
  • The pull request does not introduce any breaking changes
  • I have added screenshots or gifs to help explain the change if applicable. (text before/after above; no UI change)
  • I have read the contribution guidelines.
  • Create an issue and link to the pull request.
  • I've run the claude code review skill locally.

Summary by CodeRabbit

  • Bug Fixes
    • Generated code snippets now correctly handle URLs that have a query string but no path. Request-target formats include the required leading slash, while full URLs keep the query directly after the host.
    • Query characters respect the selected encoding setting, and URLs that already include a path continue to generate snippets as before.

@harxh07
harxh07 requested review from a team and a balanced review from Copilot October 13, 2026 15:36

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@coderabbitai

coderabbitai Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Important

Review skipped

Review was skipped as selected files did not have any reviewable changes.

Run configuration
Commits

Reviewing files that changed from the base of the PR and between bbc4b2a and 01916ec.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review

No actionable comments were generated in the recent review.

Recent review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between 0552041 and bbc4b2a.

Files selected for processing: 1
  • packages/bruno-app/src/components/Sidebar/Collections/Collection/CollectionItem/GenerateCodeItem/utils/snippet-generator.spec.js

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

Walkthrough

The snippet generator now adds a leading slash to query-only request targets while preserving bare queries in full URLs. Regression tests cover encoding settings, multiple targets and clients, and URLs with explicit paths.

Changes

Query-only URL handling

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: Passed 5
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed 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 HT…
Linked Issues check Passed [#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 of snippet-generator.js (…
Out of Scope Changes check Passed 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 when `desire…
Docstring Coverage Passed Docstring coverage is inconclusive: files from earlier reviews still need to be rechecked within the check's analysis limits. Not blocking.
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 of snippet-generator.js (merge base to head) adds a branch for desiredPath starting 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 keeps desiredPath as typed. This keeps the full-URL form (for example curl http://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 with encodeUrl on 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 when desiredPath starts 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 when desiredPath starts with ?, which is the case #9522 describes. The tests exercise the same behavior, including controls for curl and for URLs with a path. The PR states that stripOrigin and the output for URLs with a path are unchanged, and the diff supports this because the existing else branch keeps the original replaceAll. 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.

Share

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

@aditya-bruno

Copy link
Copy Markdown
Collaborator

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 rahul05ranjan left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@harxh07

harxh07 commented Oct 19, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @rahul05ranjan for the careful review, and for spotting the edge case in the last paragraph.

I reproduced it: with URL Encoding off, http://example.com?filter[name]=x printed GET /?filter%5Bname%5D=x while http://example.com/api?filter[name]=x printed GET /api?filter[name]=x, so a path-less URL lost the "show the URL as typed" behavior that the display-swap exists for.

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 /. A path-less URL now prints the same snippet as the same URL with an explicit / for all 39 target/client combinations (added as a test). The PR description is updated accordingly.

…tor.spec.js

Addresses review feedback: keep these tests in the existing spec file instead of a new one.
@pull-request-size pull-request-size Bot added size/L and removed size/M labels Nov 1, 2026
@bijin-bruno
bijin-bruno merged commit cea8beb into usebruno:main Nov 3, 2026
17 of 19 checks passed
@rahul05ranjan

Copy link
Copy Markdown

Congrats on the merge, @harxh07! Glad the review helped — the edge case turned out to be worth chasing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Generate Code: URL with a query but no path produces an invalid request target (GET ?x=1) for HTTP, Node native and Python http.client

6 participants