Repository navigation
feat: enhance request and response translation capabilities - #6981
Conversation
- Added translations for req.method, req.headers, and req.body to their Postman equivalents. - Implemented handling for req.setUrl, req.setMethod, req.setBody, and req.setHeaders, converting them to appropriate Postman assignments and method calls. - Introduced support for translating res.responseTime and res.headers properties. - Updated tests to cover new translations and ensure accuracy in request and response handling.
WalkthroughExtended the Bruno-to-Postman translator with direct property mappings for request and response objects (req.method, req.headers, req.body, res.responseTime, res.headers) and implemented complex transformations for request setter methods. Comprehensive test coverage validates all new translation patterns. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Suggested labels
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
Tip 🧪 Unit Test Generation v2 is now available!We have significantly improved our unit test generation capabilities. To enable: Add this to your reviews:
finishing_touches:
unit_tests:
enabled: trueTry it out by using the Have feedback? Share your thoughts on our Discord thread! 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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@packages/bruno-converters/src/utils/bruno-to-postman-translator.js`:
- Around line 300-357: The for...in generated for the req.setHeaders transform
currently iterates inherited enumerable properties; update the transformation
that builds the forInStatement (variables headersVar `_headers` and keyVar
`key`) to guard the loop body with an own-property check (e.g., if
(Object.prototype.hasOwnProperty.call(_headers, key)) { ... }) before calling
pm.request.headers.upsert, or alternatively replace the for-in with an iteration
over Object.keys(_headers) so only own keys are processed; adjust the
j.blockStatement used as the loop body in the j.forInStatement (or replace the
loop node) to include this guard so inherited properties are not upserted.
| // req.setHeaders(headers) -> loop calling pm.request.headers.upsert() for each header | ||
| { | ||
| pattern: 'req.setHeaders', | ||
| transform: (path) => { | ||
| const callExpr = path.value; | ||
| const args = callExpr.arguments; | ||
| if (!args || args.length === 0) { | ||
| // No arguments, return the property access | ||
| return buildMemberExpressionFromString('pm.request.headers'); | ||
| } | ||
| const headersArg = args[0]; | ||
|
|
||
| // Transform req.setHeaders(obj) to a for...in loop that calls upsert for each property | ||
| // Generate: for (const key in headersObj) { pm.request.headers.upsert({key: key, value: headersObj[key]}); } | ||
| const headersVar = j.identifier('_headers'); | ||
| const keyVar = j.identifier('key'); | ||
|
|
||
| // Create: for (const key in _headers) { pm.request.headers.upsert({key: key, value: _headers[key]}); } | ||
| const forLoop = j.forInStatement( | ||
| j.variableDeclaration('const', [j.variableDeclarator(keyVar)]), | ||
| headersVar, | ||
| j.blockStatement([ | ||
| j.expressionStatement( | ||
| j.callExpression( | ||
| j.memberExpression( | ||
| buildMemberExpressionFromString('pm.request.headers'), | ||
| j.identifier('upsert') | ||
| ), | ||
| [ | ||
| j.objectExpression([ | ||
| j.property('init', j.identifier('key'), keyVar), | ||
| j.property('init', j.identifier('value'), j.memberExpression(headersVar, keyVar, true)) | ||
| ]) | ||
| ] | ||
| ) | ||
| ) | ||
| ]) | ||
| ); | ||
|
|
||
| // We need to replace the call expression with a block that includes the variable declaration and loop | ||
| // But the current architecture only replaces the call expression itself | ||
| // So we'll create an IIFE (Immediately Invoked Function Expression) that contains both | ||
| const iife = j.callExpression( | ||
| j.functionExpression( | ||
| null, | ||
| [], | ||
| j.blockStatement([ | ||
| j.variableDeclaration('const', [ | ||
| j.variableDeclarator(headersVar, headersArg) | ||
| ]), | ||
| forLoop | ||
| ]) | ||
| ), | ||
| [] | ||
| ); | ||
|
|
||
| return iife; | ||
| } |
There was a problem hiding this comment.
Guard against inherited header keys in the for...in loop.
for...in can enumerate inherited enumerable props; adding a hasOwnProperty check avoids unexpected headers.
Proposed fix
- // Create: for (const key in _headers) { pm.request.headers.upsert({key: key, value: _headers[key]}); }
+ // Create: for (const key in _headers) { if (!Object.prototype.hasOwnProperty.call(_headers, key)) continue; ... }
+ const hasOwnPropCall = j.callExpression(
+ j.memberExpression(
+ j.memberExpression(j.identifier('Object'), j.identifier('prototype')),
+ j.identifier('hasOwnProperty')
+ ),
+ [headersVar, keyVar]
+ );
+
const forLoop = j.forInStatement(
j.variableDeclaration('const', [j.variableDeclarator(keyVar)]),
headersVar,
j.blockStatement([
+ j.ifStatement(
+ j.unaryExpression('!', hasOwnPropCall),
+ j.continueStatement()
+ ),
j.expressionStatement(
j.callExpression(
j.memberExpression(
buildMemberExpressionFromString('pm.request.headers'),
j.identifier('upsert')🤖 Prompt for AI Agents
In `@packages/bruno-converters/src/utils/bruno-to-postman-translator.js` around
lines 300 - 357, The for...in generated for the req.setHeaders transform
currently iterates inherited enumerable properties; update the transformation
that builds the forInStatement (variables headersVar `_headers` and keyVar
`key`) to guard the loop body with an own-property check (e.g., if
(Object.prototype.hasOwnProperty.call(_headers, key)) { ... }) before calling
pm.request.headers.upsert, or alternatively replace the for-in with an iteration
over Object.keys(_headers) so only own keys are processed; adjust the
j.blockStatement used as the loop body in the j.forInStatement (or replace the
loop node) to include this guard so inherited properties are not upserted.
…#6981) - Added translations for req.method, req.headers, and req.body to their Postman equivalents. - Implemented handling for req.setUrl, req.setMethod, req.setBody, and req.setHeaders, converting them to appropriate Postman assignments and method calls. - Introduced support for translating res.responseTime and res.headers properties. - Updated tests to cover new translations and ensure accuracy in request and response handling.
Description
Contribution Checklist:
Note: Keeping the PR small and focused helps make it easier to review and merge. If you have multiple changes you want to make, please consider submitting them as separate pull requests.
Publishing to New Package Managers
Please see here for more information.
Summary by CodeRabbit
New Features
Documentation
Tests
✏️ Tip: You can customize this high-level summary in your review settings.