Skip to content

Fix OAuth2 scope deduplication in OpenAPI spec generation #7771

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jun 27, 2025

When OAuth2 authentication schemes have multiple flows with the same scopes, the generated OpenAPI spec contained duplicate scope entries in the security section.

For example, with this TypeSpec definition:

model oauth<Scopes extends string[]>
  is OAuth2Auth<
    [
      {
        type: OAuth2FlowType.authorizationCode;
        authorizationUrl: "https://example.org/oauth2/v2.0/authorize";
        tokenUrl: "https://example.org/oauth2/v2.0/token";
        refreshUrl: "https://example.org/oauth2/v2.0/token";
      },
      {
        type: OAuth2FlowType.clientCredentials;
        tokenUrl: "https://example.org/oauth2/v2.0/token";
      }
    ],
    Scopes
  >;

@useAuth(oauth<["api:read"]>)
op Get(): void;

The generated OpenAPI security section would incorrectly show:

security:
  - oauth:
    - api:read
    - api:read  # duplicate

Root Cause:
The makeHttpAuthRef function in packages/http/src/auth.ts was collecting scopes from all flows without deduplication, causing the same scope to appear multiple times when shared across flows.

Fix:
Added Array.from(new Set(scopes)) to deduplicate the scopes array before creating the OAuth2HttpAuthRef object. This follows the same pattern already used in the mergeOAuthScopes function.

Testing:

  • Added a comprehensive test case that reproduces the exact issue
  • All existing security tests continue to pass (38 tests)
  • All http package tests continue to pass (377 tests)

Fixes #7685.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI and others added 2 commits June 27, 2025 19:56
Co-authored-by: chrisradek <14189820+chrisradek@users.noreply.github.com>
Co-authored-by: chrisradek <14189820+chrisradek@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] Deduplicate scope names in openapi spec generation Fix OAuth2 scope deduplication in OpenAPI spec generation Jun 27, 2025
@Copilot Copilot AI requested a review from chrisradek June 27, 2025 20:00
Copilot finished work on behalf of chrisradek June 27, 2025 20:00
@chrisradek
Copy link
Member

/azp run typespec - pr tools

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@azure-sdk
Copy link
Collaborator

You can try these changes here

🛝 Playground 🌐 Website 🛝 VSCode Extension

@chrisradek
Copy link
Member

@copilot This needs a changelog added for the @typespec/http package.

Co-authored-by: chrisradek <14189820+chrisradek@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jun 27, 2025

@copilot This needs a changelog added for the @typespec/http package.

Added changelog for @typespec/http package in commit e7a6b7c.

@Copilot Copilot AI requested a review from chrisradek June 27, 2025 21:01
Copilot finished work on behalf of chrisradek June 27, 2025 21:01
Co-authored-by: chrisradek <14189820+chrisradek@users.noreply.github.com>
Copilot finished work on behalf of chrisradek June 27, 2025 21:09
Co-authored-by: chrisradek <14189820+chrisradek@users.noreply.github.com>
@Copilot Copilot AI requested a review from chrisradek June 27, 2025 21:40
Copilot finished work on behalf of chrisradek June 27, 2025 21:40
@chrisradek
Copy link
Member

@copilot Rerun formatter via pnpm format at the root of the repo.

Co-authored-by: chrisradek <14189820+chrisradek@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jun 28, 2025

@copilot Rerun formatter via pnpm format at the root of the repo.

Ran pnpm format at the root of the repo. Fixed trailing whitespace formatting in the test file. See commit d26a8c1.

Copilot finished work on behalf of chrisradek June 28, 2025 03:46
@chrisradek chrisradek marked this pull request as ready for review June 30, 2025 17:25
Co-authored-by: timotheeguerin <1031227+timotheeguerin@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jun 30, 2025

Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • telemetry.astro.build
    • Triggering command: node /home/REDACTED/work/typespec/typespec/website/node_modules/.bin/../astro/astro.js build (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot finished work on behalf of timotheeguerin June 30, 2025 17:44
@Copilot Copilot AI requested a review from timotheeguerin June 30, 2025 17:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Deduplicate scope names in openapi spec generation
4 participants