Skip to content

fix(app): null-safe OAuth2 scope in OpenAPI export (BRU-3297) - #8086

Merged
bijin-bruno merged 3 commits into
usebruno:mainfrom
sundram-bruno:fix/bru-3297-openapi-oauth2-null-scope
May 26, 2026
Merged

bijin-bruno merged 3 commits into
usebruno:mainfrom
sundram-bruno:fix/bru-3297-openapi-oauth2-null-scope

Conversation

@sundram-bruno

@sundram-bruno sundram-bruno commented May 25, 2026 •

Copy link
Copy Markdown
Contributor

Description

The OpenAPI exporter (packages/bruno-app/src/utils/exporters/openapi-spec.js) calls .length on auth.oauth2.scope without a null guard. When a user never fills the Scope field for an OAuth2 auth, Bruno stores scope as null, causing the entire export to crash with:

TypeError: Cannot read properties of null (reading 'length')

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

  • Replace scope.length > 0 ? { scopes: { [scope]: '' } } : {} ternary in all 3 grant cases with a single hoisted const scopes = scope ? { [scope]: '' } : {}. Truthy check handles null, undefined, and "" uniformly.
  • Emit scopes unconditionally on each flow. OpenAPI 3.0 requires scopes to be present on every OAuth2 flow even when empty.
  • Wrap case 'oauth2' in a block to scope the new const declarations (resolves Biome noSwitchDeclarations).
  • Add 12 jest tests covering all 3 affected grant types.

Test plan

  • npm run test --workspace=packages/bruno-app -- openapi-spec — 29/29 pass (12 new + 17 existing)
  • Bug reproduced in installed Bruno 3.1.x via Share → OpenAPI Specification — identical stack trace confirmed (TypeError: Cannot read properties of null (reading 'length') at Array.map)

Contribution Checklist:

Screenshot

Production crash on installed Bruno 3.1.x exporting a collection with OAuth2 auth + blank scope:

TypeError: Cannot read properties of null (reading 'length')
  at file:///Applications/Bruno.app/Contents/Resources/app.asar/web/static/js/index.a442c869.js:45:21358
  at Array.map (<anonymous>)
  ...

Stack Array.map corresponds to items.map(...) at openapi-spec.js:133, inside which the scope.length runs in the OAuth2 case.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes

    • Improved stability when exporting API specifications using OAuth2 so exports no longer fail when scope is missing, null, or empty; exported specs include an explicit empty scopes object where appropriate.
  • Tests

    • Added tests covering OAuth2 scope scenarios (null, empty, and non-empty) to ensure consistent export behavior.

Review Change Stack

sundram-bruno and others added 2 commits May 25, 2026 02:27
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]>
@coderabbitai

coderabbitai Bot commented May 25, 2026 •

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: a8d56f01-521c-46fb-917f-5b0340a7be36

📥 Commits

Reviewing files that changed from the base of the PR and between 60beff1 and 4867f96.

📒 Files selected for processing (1)
  • packages/bruno-app/src/utils/exporters/openapi-spec.spec.js
💤 Files with no reviewable changes (1)
  • packages/bruno-app/src/utils/exporters/openapi-spec.spec.js

Walkthrough

This PR refactors OAuth2 scope handling in the OpenAPI exporter to eliminate repeated inline logic by precomputing a scopes object once, then adds comprehensive test coverage for edge cases where scope is null or undefined across three grant types.

Changes

OAuth2 Scope Refactoring

Layer / File(s) Summary
OAuth2 scope precomputation
packages/bruno-app/src/utils/exporters/openapi-spec.js
Wraps the oauth2 case in a block, precomputes scopes from auth.oauth2.scope (empty object when not provided), and reuses this value across authorization_code, password, and client_credentials flows, reducing code duplication.
OAuth2 scope handling test suite
packages/bruno-app/src/utils/exporters/openapi-spec.spec.js
Adds a test helper and test suite that validates exportApiSpec handles null and undefined OAuth2 scopes without throwing, and verifies the generated OpenAPI contains the expected flow keys and empty or populated scopes objects.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Suggested reviewers

  • helloanoop
  • lohit-bruno
  • naman-bruno
  • bijin-bruno
  • sid-bruno

Poem

✨ A single scopes born to end the repeat,
Tests march in checking nulls with steady feet,
Flows now consistent, no surprises to meet,
Exported spec hums tidy and neat—🔐

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main fix: making OAuth2 scope handling null-safe in OpenAPI export, with a specific issue reference (BRU-3297).
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot 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.

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 win

Pre-existing bug: client_credentials uses incorrect flow key.

Line 399 uses password: as the flow key, but the OpenAPI 3.0 spec expects clientCredentials: 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

📥 Commits

Reviewing files that changed from the base of the PR and between 9b09119 and 60beff1.

📒 Files selected for processing (2)
  • packages/bruno-app/src/utils/exporters/openapi-spec.js
  • packages/bruno-app/src/utils/exporters/openapi-spec.spec.js

@bijin-bruno
bijin-bruno merged commit 6b7e5f3 into usebruno:main May 26, 2026
27 of 29 checks passed
abhishekp-bruno pushed a commit to abhishekp-bruno/bruno that referenced this pull request May 29, 2026
…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]>
sharan-bruno pushed a commit to sharan-bruno/bruno that referenced this pull request Jun 4, 2026
…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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants