Skip to content

fix(api/swagger): send Authorization header from Swagger UI#29676

Open
abhay-codes07 wants to merge 2 commits into
calcom:mainfrom
abhay-codes07:fix/swagger-authorization-bearer
Open

fix(api/swagger): send Authorization header from Swagger UI#29676
abhay-codes07 wants to merge 2 commits into
calcom:mainfrom
abhay-codes07:fix/swagger-authorization-bearer

Conversation

@abhay-codes07

Copy link
Copy Markdown

What does this PR do?

Fixes Swagger UI silently dropping the Authorization header so "Try it out" requests no longer fail with 401/403.

API v2 endpoints document authentication with @ApiHeader({ name: "Authorization" }). The OpenAPI specification, however, mandates that a header parameter named Authorization be ignored, so Swagger UI renders the input field and accepts a value but never attaches the header to the outgoing request. Users fill in Bearer cal_xxx, hit Execute, and every authenticated call fails even though the UI suggested the token would be sent.

The fix, all contained in apps/api/v2/src/swagger/generate-swagger.ts:

  1. Registers a proper bearer security scheme on the OpenAPI document (addBearerAuth), which gives Swagger UI a working Authorize button.
  2. Adds applyAuthorizationSecurity(), a post-processing step that, for every operation which declared the (ignored) Authorization header, removes that parameter and attaches the bearer security requirement instead — so Swagger UI actually sends Authorization: Bearer <token>.

Operations that never declared an Authorization header (genuinely public endpoints) are left untouched, and any pre-existing per-operation security is preserved (the requirement is appended, not overwritten). Because the transform runs centrally, no per-controller changes are needed.

Visual Demo (For contributors especially)

N/A — this changes the generated OpenAPI document / Swagger UI behaviour. The resulting document now contains a components.securitySchemes.Authorization bearer scheme and a security requirement on the affected operations, which is exactly what makes Swagger UI attach the header. This is asserted by the unit tests below.

Mandatory Tasks (DO NOT REMOVE)

  • I have self-reviewed the code (A decent size PR without self-review might be rejected).
  • I have updated the developer docs if this PR makes changes that would require a documentation change. If N/A, write N/A here and check the checkbox. — N/A
  • I confirm automated tests are in place that prove my fix is effective or that my feature works.

How should this be tested?

No environment variables or DB are required for the unit tests — applyAuthorizationSecurity is a pure function over an OpenAPI document.

cd apps/api/v2
yarn jest src/swagger/generate-swagger.spec.ts

Expected: 5 tests pass, covering:

  • An operation with an Authorization header → the header parameter is removed and a { Authorization: [] } security requirement is added.
  • A public operation (no Authorization header) → left untouched.
  • An operation with pre-existing security → the requirement is appended, not overwritten.
  • $ref parameters → handled without throwing.
  • A document with no paths → returned unchanged.

Manual verification in Swagger UI: open /docs, click Authorize, enter an API key / access token, then Execute any authenticated endpoint and confirm the generated curl now includes -H 'Authorization: Bearer <token>'.

Checklist

  • I have read the contributing guide.
  • My code follows the style guidelines of this project.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have checked that my changes generate no new warnings.
  • My PR is small (1 source file + 1 test file).

Endpoints document their auth with `@ApiHeader({ name: "Authorization" })`,
but the OpenAPI spec mandates that a header parameter named "Authorization"
be ignored, so Swagger UI shows the field yet never sends the header. Every
"try it out" request then fails with 401/403 even though the user filled it
in.

Register a proper bearer security scheme on the document and, for each
operation that declared the ignored Authorization header, replace that
parameter with the bearer security requirement. The "Authorize" button now
attaches `Authorization: Bearer <token>` to requests. Genuinely public
endpoints (no Authorization header) are left untouched.

The replacement runs centrally in generate-swagger.ts, so no per-controller
changes are needed. Adds unit tests for the applyAuthorizationSecurity
post-processing.

Fixes calcom#29564
Copilot AI review requested due to automatic review settings June 29, 2026 21:54

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.

@github-actions github-actions Bot added the 🐛 bug Something isn't working label Jun 29, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Welcome to Cal.diy, @abhay-codes07! Thanks for opening this pull request.

A few things to keep in mind:

  • This is Cal.diy, not Cal.com. Cal.diy is a community-driven, fully open-source fork of Cal.com licensed under MIT. Your changes here will be part of Cal.diy — they will not be deployed to the Cal.com production app.
  • Please review our Contributing Guidelines if you haven't already.
  • Make sure your PR title follows the Conventional Commits format.

A maintainer will review your PR soon. Thanks for contributing!

@coderabbitai

coderabbitai Bot commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 10332603-d920-4344-a8e9-f448de0efcc4

📥 Commits

Reviewing files that changed from the base of the PR and between 44d0791 and 8f9aa08.

📒 Files selected for processing (2)
  • apps/api/v2/src/swagger/generate-swagger.spec.ts
  • apps/api/v2/src/swagger/generate-swagger.ts
🚧 Files skipped from review as they are similar to previous changes (2)
  • apps/api/v2/src/swagger/generate-swagger.spec.ts
  • apps/api/v2/src/swagger/generate-swagger.ts

📝 Walkthrough

Walkthrough

generate-swagger.ts adds AUTHORIZATION_SECURITY_SCHEME = "Authorization" and applyAuthorizationSecurity. The function scans OpenAPI paths and operations, removes Authorization header parameters, and adds bearer security entries. generateSwaggerForApp now configures Swagger with bearer auth and applies this post-processing before returning the document. The Jest spec covers the transformation cases, existing security, path-level parameters, $ref parameters, and missing paths.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 20.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title is concise and accurately describes the Swagger UI Authorization header fix.
Description check ✅ Passed The description is clearly about the Swagger UI Authorization header fix and matches the changes.
Linked Issues check ✅ Passed The changes add bearer auth and rewrite Authorization header parameters as security requirements, matching #29564.
Out of Scope Changes check ✅ Passed The extra path-level Authorization handling is relevant to the same fix and no unrelated changes appear.
✨ 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.

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

Actionable comments posted: 1

🤖 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.

Inline comments:
In `@apps/api/v2/src/swagger/generate-swagger.ts`:
- Around line 84-101: The swagger generation logic in generate-swagger.ts only
inspects operation.parameters, so shared pathItem.parameters with an
Authorization header are missed. Update the method loop that processes each path
item to also consider path-level parameters when checking for the Authorization
header, and make sure the bearer security requirement is added whenever either
the operation or the path item declares that header. Keep the existing
isAuthorizationHeader helper and the operation.parameters filtering, but extend
the logic around HttpMethods.forEach and pathItem so the shared parameter is
handled too.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 5a62da29-9c21-45a2-890a-e060782e40cf

📥 Commits

Reviewing files that changed from the base of the PR and between 5d6d4d3 and 44d0791.

📒 Files selected for processing (2)
  • apps/api/v2/src/swagger/generate-swagger.spec.ts
  • apps/api/v2/src/swagger/generate-swagger.ts

Comment thread apps/api/v2/src/swagger/generate-swagger.ts
OpenAPI allows header parameters to be declared at the path level, where
they are inherited by every operation in the path. Extend
applyAuthorizationSecurity to detect an inherited path-level Authorization
header, apply the bearer security requirement to each operation in the path,
and drop the path-level parameter. Adds a unit test for the path-level case.
@github-actions

github-actions Bot commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

This PR has been marked as stale due to inactivity. If you're still working on it or need any help, please let us know or update the PR to keep it active.

@github-actions github-actions Bot added the Stale label Jul 7, 2026
@CLAassistant

CLAassistant commented Jul 23, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

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

Labels

🐛 bug Something isn't working size/L Stale

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Swagger UI silently drops Authorization header defined with @ApiHeader

3 participants