Conversation
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #137.
Problem
A JSON body with
//comments is sent as a quoted JSON string instead of an object. The same collection works in the desktop app and in the CLI, because both run the body throughdecommentfirst:packages/bruno-electron/src/ipc/network/prepare-request.jspackages/bruno-cli/src/runner/prepare-request.jsSo a collection authored in the desktop app — where JSON body comments have been supported since usebruno/bruno#396 — silently breaks when the same
.brufiles are run from VS Code. In our shared collection that was 80 of 127 requests with a JSON body. On a strict API it surfaces as a confusing validation error; ours (Django/DRF) answersInvalid data. Expected a dictionary, but got str.Two places build the body here, and both have the bug:
1.
getRequestDatainipc/network/index.ts— this is the one that runs for a normal send:The raw commented text becomes
request.data.interpolateVarstakes the string branch, and axios'stringifySafelycannot parse it, so it falls back toJSON.stringify(rawString)and the request goes out double-encoded. Its own doc comment says "Similar to bruno-cli's prepare-request.js" — which is exactly where the CLI appliesdecomment.2.
prepareBodyinipc/network/prepare-request.ts— reached whendatais undefined:Fix
decommentis not a dependency of this repo (script-runner.tsdeclares a localdecommentthat currently returns the script untouched), so rather than pulling the package in, this adds a small dependency-free helper: a JSON body has no regular expressions or template literals, so a string-aware scan is enough to tell a comment from a//that happens to live inside a value.getRequestDatareturnsstripJsonComments(body.json)— same shape and type as before, just comment-free, mirroringdecomment(request.body.json)in the CLI.prepareBodyparses the stripped text, and falls back to the stripped text rather than tobody.json. A body that mixes comments with unquoted variables ("quantity": {{qty}}) is still not valid JSON after stripping, so it takes that path; returning the raw text would handinterpolateVarsa string that still contains//and the interpolated result would be quoted all the same. It also returnsundefinedfor an empty body, so nothing is sent.If you would rather match
bruno-electronbyte for byte by depending ondecomment, swapping the helper is a one-line change — I kept it dependency-free to avoid touching the lockfile.Tests
src/extension/ipc/network/prepare-request.spec.tscoversprepareBodywith a line comment, a comment on its own line, a block comment, a//inside a string value, escaped quotes inside a value, the still-invalid fallback, an empty body, and the content-type default. (getRequestDatais module-private, so it is not covered directly; happy to export it if you want a spec on it too.)I verified the behaviour against the
decommentthat ships with the desktop app (4.1.0), comparing the body builder's output after variable interpolation and axios'stringifySafelyrule; the two agree on every case except the ones that are invalid JSON in every client anyway, such as a trailing comma left behind by commenting a field out.I also patched the shipped 5.0.2 bundle locally with the same change and confirmed against a real API that the request now arrives as an object.
Note: I used AI assistance to investigate and prepare this change.