Skip to content

feat(symfony,laravel): withCredentials option to Swagger UI#8197

Merged
soyuka merged 1 commit into
api-platform:mainfrom
cay89:feat/swagger-ui-with-credentials
May 22, 2026
Merged

feat(symfony,laravel): withCredentials option to Swagger UI#8197
soyuka merged 1 commit into
api-platform:mainfrom
cay89:feat/swagger-ui-with-credentials

Conversation

@cay89
Copy link
Copy Markdown
Contributor

@cay89 cay89 commented May 22, 2026

Q A
Branch? main
Tickets
License MIT
Doc PR api-platform/docs#2285

Problem

When an API Platform Laravel application is deployed behind Cloudflare Access, the Swagger UI's token and CORS requests are intercepted and rejected with a 401 response. This happens because the Cloudflare Access authentication cookie is not forwarded with these requests — browsers only include cookies in cross-origin requests when credentials: 'include' is explicitly set.

Solution

This PR adds a withCredentials boolean option to OpenApi\Options. When enabled, a requestInterceptor is injected into the SwaggerUIBundle configuration that sets credentials: 'include' on every outgoing request, ensuring the Cloudflare Access cookie is forwarded.

The option is disabled by default to preserve existing behaviour.

Changes

  • src/OpenApi/Options.php — new withCredentials constructor parameter (default false) and getWithCredentials() getter
  • src/Laravel/ApiPlatformProvider.php — reads swagger_ui.with_credentials from config and passes it to Options
  • src/Laravel/State/SwaggerUiProcessor.php — includes withCredentials in the swagger-data JSON payload
  • src/Laravel/public/init-swagger-ui.js — conditionally adds a requestInterceptor that sets req.credentials = 'include'
  • src/Laravel/config/api-platform.php — documents the new with_credentials config key (disabled by default)

Configuration

// config/api-platform.php
'swagger_ui' => [
    'with_credentials' => true,
],

Testing

  • Added Tests/DocsWithCredentialsTest.php — integration test verifying that "withCredentials":true is present in the rendered swagger-data JSON when the option is enabled, and absent by default.

@soyuka soyuka changed the title Add withCredentials option to Swagger UI fix(laravel): add withCredentials option to Swagger UI May 22, 2026
@soyuka soyuka changed the base branch from main to 4.3 May 22, 2026 11:52
@soyuka soyuka force-pushed the feat/swagger-ui-with-credentials branch 2 times, most recently from 0be4610 to f482924 Compare May 22, 2026 12:02
@soyuka soyuka changed the title fix(laravel): add withCredentials option to Swagger UI feat(symfony,laravel): add withCredentials option to Swagger UI May 22, 2026
@soyuka soyuka changed the base branch from 4.3 to main May 22, 2026 12:02
Adds a withCredentials boolean to OpenApi\Options. When enabled, a Swagger UI
requestInterceptor sets `credentials: 'include'` on every outgoing request so
authentication cookies (e.g. Cloudflare Access) are forwarded on token and
CORS calls. Disabled by default.

- Symfony: new `api_platform.swagger.with_credentials` config key, parameter
  wired into the Options service, exposed in SwaggerUiProcessor + init-swagger-ui.js
- Laravel: new `swagger_ui.with_credentials` config key wired through
  ApiPlatformProvider, SwaggerUiProcessor and init-swagger-ui.js
@soyuka soyuka force-pushed the feat/swagger-ui-with-credentials branch from f482924 to 0b5ba20 Compare May 22, 2026 12:57
@soyuka soyuka changed the title feat(symfony,laravel): add withCredentials option to Swagger UI feat(symfony,laravel): withCredentials option to Swagger UI May 22, 2026
@soyuka soyuka merged commit 2ff386b into api-platform:main May 22, 2026
111 of 116 checks passed
@soyuka
Copy link
Copy Markdown
Member

soyuka commented May 22, 2026

thanks!

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