Skip to content

feat: Hide authentication from unauthenticated openapi curl examples - #1397

Merged
andreaangiolillo merged 8 commits into
mainfrom
sage-bot/CLOUDP-428048/sage-CLOUDP-428048-1785162451972
Jul 28, 2026
Merged

feat: Hide authentication from unauthenticated openapi curl examples#1397
andreaangiolillo merged 8 commits into
mainfrom
sage-bot/CLOUDP-428048/sage-CLOUDP-428048-1785162451972

Conversation

@mongodb-sage-bot

@mongodb-sage-bot mongodb-sage-bot Bot commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

Proposed changes

Jira ticket: CLOUDP-428048

This pull request refactors how cURL code samples are generated for OpenAPI endpoints, improving maintainability and adding logic to distinguish between authenticated and unauthenticated endpoints. It introduces a helper function to centralize cURL command construction and updates the code to emit different code samples based on endpoint authentication requirements. Comprehensive tests are added to cover new behaviors.

Refactoring and Code Simplification:

  • Extracted common cURL command construction logic into a new helper function appendCurlMethodSource, reducing code duplication and simplifying the code sample generation process. [1] [2]
  • Refactored code sample generator methods to use the new helper and clarified their responsibilities: newCurlCodeSamplesForOperation for unauthenticated endpoints, newServiceAccountCurlCodeSamplesForOperation and newDigestCurlCodeSamplesForOperation for authenticated endpoints.

Authentication-based Code Sample Selection:

  • Added the isEndpointUnAuthenticated function to determine if an endpoint requires authentication, based on the absence of 401 and 403 responses.
  • Updated the main code sample inclusion logic to emit only a plain cURL sample for unauthenticated endpoints, and both service account and digest samples for authenticated endpoints.

Testing Enhancements:

  • Added new test cases to verify that code samples are correctly generated for both authenticated and unauthenticated endpoints, including preview and stable API versions.
  • Updated existing tests to ensure coverage of the new authentication logic and refactored code. [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14]

These changes improve the maintainability of the code sample generator and ensure that users receive appropriate example commands depending on endpoint authentication requirements.

Checklist

  • I have signed the MongoDB CLA
  • I have added tests that prove my fix is effective or that my feature works

…examples

## Proposed changes

Update the curl code sample generation in
`tools/foas/openapi/filter/code_sample.go` so that endpoints which do not
require authentication no longer expose authentication flags in their examples.

An operation is treated as unauthenticated when it overrides the global
security with an empty set (`security: []`). For those endpoints a single
`curl` sample is emitted without `--user "${PUBLIC_KEY}:${PRIVATE_KEY}" --digest`
(digest) and without `--header "Authorization: Bearer ${ACCESS_TOKEN}"`
(service account).

For authenticated operations the Service Account and Digest samples are now
gated on the security schemes the operation actually supports, so an endpoint
that only references one scheme gets only that sample. Operations that do not
override the global security keep emitting both samples as before.

The request-building portion shared by every curl variant was extracted into a
`curlRequestSuffix` helper; the generated output for the existing samples is
unchanged.

_Jira ticket:_ CLOUDP-428048

## Checklist

- [ ] I have signed the [MongoDB CLA](https://www.mongodb.com/legal/contributor-agreement)
- [x] I have added tests that prove my fix is effective or that my feature works

### Changes to Spectral
- [ ] I have read the [README](../tools/spectral/README.md) file for Spectral Updates
Comment thread tools/foas/openapi/filter/code_sample.go Outdated
@andreaangiolillo andreaangiolillo changed the title CLOUDP-428048: Hide authentication from unauthenticated openapi curl examples feat: Hide authentication from unauthenticated openapi curl examples Jul 28, 2026
@mongodb mongodb deleted a comment from mongodb-sage-bot Bot Jul 28, 2026
@andreaangiolillo
andreaangiolillo marked this pull request as ready for review July 28, 2026 12:11
@andreaangiolillo
andreaangiolillo requested a review from a team as a code owner July 28, 2026 12:11
Comment thread tools/foas/openapi/filter/code_sample.go Outdated
@andreaangiolillo
andreaangiolillo enabled auto-merge (squash) July 28, 2026 13:03

@lovisaberggren lovisaberggren left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ty!

@andreaangiolillo
andreaangiolillo merged commit bf857ca into main Jul 28, 2026
13 checks passed
@andreaangiolillo
andreaangiolillo deleted the sage-bot/CLOUDP-428048/sage-CLOUDP-428048-1785162451972 branch July 28, 2026 13:37
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.

2 participants