Repository navigation
fix(app): null-safe OAuth2 scope in OpenAPI export (BRU-3297) - #8086
bijin-bruno merged 3 commits into
Conversation
The OpenAPI exporter calls `.length` on `auth.oauth2.scope` without a null guard. When a user never fills the Scope field for an OAuth2 auth (grant types authorization_code, password, client_credentials), Bruno stores `scope` as `null`, causing the entire export to crash with `TypeError: Cannot read properties of null (reading 'length')`. Replace the unsafe `scope.length > 0` check with a truthy check that handles null, undefined, and empty string uniformly. Emit `scopes` as an empty object when no scope is set — OpenAPI 3.0 requires `scopes` to be present on every OAuth2 flow even when empty. Add 12 jest tests covering all 3 affected grant types. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Addresses Biome `noSwitchDeclarations` — `const` declarations inside a case clause without braces can leak across cases. Pure cosmetic; no behavior change. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
💤 Files with no reviewable changes (1)
WalkthroughThis PR refactors OAuth2 scope handling in the OpenAPI exporter to eliminate repeated inline logic by precomputing a ChangesOAuth2 Scope Refactoring
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
packages/bruno-app/src/utils/exporters/openapi-spec.js (1)
395-408:⚠️ Potential issue | 🟡 Minor | ⚡ Quick winPre-existing bug:
client_credentialsuses incorrect flow key.Line 399 uses
password:as the flow key, but the OpenAPI 3.0 spec expectsclientCredentials:for the client_credentials grant type. This bug predates BRU-3297 and is acknowledged in the test comments (line 521 of the spec file), but should be tracked separately.🔧 Proposed fix
case 'client_credentials': components.securitySchemes[securitySchemaId] = { type: 'oauth2', flows: { - password: { + clientCredentials: { tokenUrl: accessTokenUrl, scopes } } };🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/bruno-app/src/utils/exporters/openapi-spec.js` around lines 395 - 408, The client_credentials branch in the switch currently sets the OAuth flow under the wrong key ("password"); update the flow key to "clientCredentials" so components.securitySchemes[securitySchemaId] uses flows.clientCredentials with tokenUrl: accessTokenUrl and scopes, leaving the surrounding structure (pathBody['security'] assignment, securitySchemaId variable) unchanged; ensure this change is made in the case 'client_credentials' block in openapi-spec.js.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Outside diff comments:
In `@packages/bruno-app/src/utils/exporters/openapi-spec.js`:
- Around line 395-408: The client_credentials branch in the switch currently
sets the OAuth flow under the wrong key ("password"); update the flow key to
"clientCredentials" so components.securitySchemes[securitySchemaId] uses
flows.clientCredentials with tokenUrl: accessTokenUrl and scopes, leaving the
surrounding structure (pathBody['security'] assignment, securitySchemaId
variable) unchanged; ensure this change is made in the case 'client_credentials'
block in openapi-spec.js.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 0e33e39d-a73f-401c-85bc-4b695eeb2d1a
📒 Files selected for processing (2)
packages/bruno-app/src/utils/exporters/openapi-spec.jspackages/bruno-app/src/utils/exporters/openapi-spec.spec.js
Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
…no#8086) * fix(app): null-safe OAuth2 scope in OpenAPI export (BRU-3297) The OpenAPI exporter calls `.length` on `auth.oauth2.scope` without a null guard. When a user never fills the Scope field for an OAuth2 auth (grant types authorization_code, password, client_credentials), Bruno stores `scope` as `null`, causing the entire export to crash with `TypeError: Cannot read properties of null (reading 'length')`. Replace the unsafe `scope.length > 0` check with a truthy check that handles null, undefined, and empty string uniformly. Emit `scopes` as an empty object when no scope is set — OpenAPI 3.0 requires `scopes` to be present on every OAuth2 flow even when empty. Add 12 jest tests covering all 3 affected grant types. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> * style: wrap oauth2 case in block to scope local declarations Addresses Biome `noSwitchDeclarations` — `const` declarations inside a case clause without braces can leak across cases. Pure cosmetic; no behavior change. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> * chore: drop internal scope-discussion comment in openapi-spec test Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> --------- Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
…no#8086) * fix(app): null-safe OAuth2 scope in OpenAPI export (BRU-3297) The OpenAPI exporter calls `.length` on `auth.oauth2.scope` without a null guard. When a user never fills the Scope field for an OAuth2 auth (grant types authorization_code, password, client_credentials), Bruno stores `scope` as `null`, causing the entire export to crash with `TypeError: Cannot read properties of null (reading 'length')`. Replace the unsafe `scope.length > 0` check with a truthy check that handles null, undefined, and empty string uniformly. Emit `scopes` as an empty object when no scope is set — OpenAPI 3.0 requires `scopes` to be present on every OAuth2 flow even when empty. Add 12 jest tests covering all 3 affected grant types. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> * style: wrap oauth2 case in block to scope local declarations Addresses Biome `noSwitchDeclarations` — `const` declarations inside a case clause without braces can leak across cases. Pure cosmetic; no behavior change. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> * chore: drop internal scope-discussion comment in openapi-spec test Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> --------- Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
Description
The OpenAPI exporter (
packages/bruno-app/src/utils/exporters/openapi-spec.js) calls.lengthonauth.oauth2.scopewithout a null guard. When a user never fills the Scope field for an OAuth2 auth, Bruno storesscopeasnull, causing the entire export to crash with:Affected grant types:
authorization_code,password,client_credentials. A single offending request aborts the entire export — users with OAuth2-secured APIs can't get an OpenAPI export at all.Linked issue: #7365.
JIRA : https://usebruno.atlassian.net/browse/BRU-3297
Changes
scope.length > 0 ? { scopes: { [scope]: '' } } : {}ternary in all 3 grant cases with a single hoistedconst scopes = scope ? { [scope]: '' } : {}. Truthy check handlesnull,undefined, and""uniformly.scopesunconditionally on each flow. OpenAPI 3.0 requiresscopesto be present on every OAuth2 flow even when empty.case 'oauth2'in a block to scope the newconstdeclarations (resolves BiomenoSwitchDeclarations).Test plan
npm run test --workspace=packages/bruno-app -- openapi-spec— 29/29 pass (12 new + 17 existing)TypeError: Cannot read properties of null (reading 'length')atArray.map)Contribution Checklist:
Screenshot
Production crash on installed Bruno 3.1.x exporting a collection with OAuth2 auth + blank scope:
Stack
Array.mapcorresponds toitems.map(...)atopenapi-spec.js:133, inside which thescope.lengthruns in the OAuth2 case.🤖 Generated with Claude Code
Summary by CodeRabbit
Bug Fixes
Tests