Skip to content

Commit c97db6b

Browse files
ViPErCZsoyuka
andauthored
feat: swagger ui persist authorization option (#6877)
Co-authored-by: Antoine Bluchet <[email protected]>
1 parent a44c38c commit c97db6b

File tree

12 files changed

+42
-5
lines changed

12 files changed

+42
-5
lines changed

src/Laravel/ApiPlatformProvider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,7 @@ public function register(): void
777777
contactEmail: $config->get('api-platform.swagger_ui.contact.email', ''),
778778
licenseName: $config->get('api-platform.swagger_ui.license.name', ''),
779779
licenseUrl: $config->get('api-platform.swagger_ui.license.url', ''),
780+
persistAuthorization: $config->get('api-platform.swagger_ui.persist_authorization', false),
780781
httpAuth: $config->get('api-platform.swagger_ui.http_auth', []),
781782
);
782783
});

src/Laravel/public/init-swagger-ui.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ window.onload = function() {
4545
spec: data.spec,
4646
dom_id: '#swagger-ui',
4747
validatorUrl: null,
48+
persistAuthorization: data.persistAuthorization,
4849
oauth2RedirectUrl: data.oauth.redirectUrl,
4950
presets: [
5051
SwaggerUIBundle.presets.apis,

src/OpenApi/Options.php

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,30 @@
1313

1414
namespace ApiPlatform\OpenApi;
1515

16-
final class Options
16+
final readonly class Options
1717
{
18-
public function __construct(private readonly string $title, private readonly string $description = '', private readonly string $version = '', private readonly bool $oAuthEnabled = false, private readonly ?string $oAuthType = null, private readonly ?string $oAuthFlow = null, private readonly ?string $oAuthTokenUrl = null, private readonly ?string $oAuthAuthorizationUrl = null, private readonly ?string $oAuthRefreshUrl = null, private readonly array $oAuthScopes = [], private readonly array $apiKeys = [], private readonly ?string $contactName = null, private readonly ?string $contactUrl = null, private readonly ?string $contactEmail = null, private readonly ?string $termsOfService = null, private readonly ?string $licenseName = null, private readonly ?string $licenseUrl = null, private bool $overrideResponses = true, private readonly array $httpAuth = [])
19-
{
18+
public function __construct(
19+
private string $title,
20+
private string $description = '',
21+
private string $version = '',
22+
private bool $oAuthEnabled = false,
23+
private ?string $oAuthType = null,
24+
private ?string $oAuthFlow = null,
25+
private ?string $oAuthTokenUrl = null,
26+
private ?string $oAuthAuthorizationUrl = null,
27+
private ?string $oAuthRefreshUrl = null,
28+
private array $oAuthScopes = [],
29+
private array $apiKeys = [],
30+
private ?string $contactName = null,
31+
private ?string $contactUrl = null,
32+
private ?string $contactEmail = null,
33+
private ?string $termsOfService = null,
34+
private ?string $licenseName = null,
35+
private ?string $licenseUrl = null,
36+
private bool $overrideResponses = true,
37+
private bool $persistAuthorization = false,
38+
private array $httpAuth = [],
39+
) {
2040
}
2141

2242
public function getTitle(): string
@@ -113,4 +133,9 @@ public function getOverrideResponses(): bool
113133
{
114134
return $this->overrideResponses;
115135
}
136+
137+
public function hasPersistAuthorization(): bool
138+
{
139+
return $this->persistAuthorization;
140+
}
116141
}

src/OpenApi/Tests/Factory/OpenApiFactoryTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ public function testInvoke(): void
533533
'type' => 'query',
534534
'name' => 'key',
535535
],
536-
], null, null, null, null, null, null, true, [
536+
], null, null, null, null, null, null, true, true, [
537537
'bearer' => [
538538
'scheme' => 'bearer',
539539
'bearerFormat' => 'JWT',

src/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,7 @@ private function registerSwaggerConfiguration(ContainerBuilder $container, array
500500
$container->setParameter('api_platform.enable_swagger_ui', $config['enable_swagger_ui']);
501501
$container->setParameter('api_platform.enable_re_doc', $config['enable_re_doc']);
502502
$container->setParameter('api_platform.swagger.api_keys', $config['swagger']['api_keys']);
503+
$container->setParameter('api_platform.swagger.persist_authorization', $config['swagger']['persist_authorization']);
503504
$container->setParameter('api_platform.swagger.http_auth', $config['swagger']['http_auth']);
504505
if ($config['openapi']['swagger_ui_extra_configuration'] && $config['swagger']['swagger_ui_extra_configuration']) {
505506
throw new RuntimeException('You can not set "swagger_ui_extra_configuration" twice - in "openapi" and "swagger" section.');

src/Symfony/Bundle/DependencyInjection/Configuration.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ private function addSwaggerSection(ArrayNodeDefinition $rootNode): void
293293
->arrayNode('swagger')
294294
->addDefaultsIfNotSet()
295295
->children()
296+
->booleanNode('persist_authorization')->defaultValue(false)->info('Persist the SwaggerUI Authorization in the localStorage.')->end()
296297
->arrayNode('versions')
297298
->info('The active versions of OpenAPI to be exported or used in Swagger UI. The first value is the default.')
298299
->defaultValue($supportedVersions)

src/Symfony/Bundle/Resources/config/openapi.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
<argument>%api_platform.openapi.license.name%</argument>
6060
<argument>%api_platform.openapi.license.url%</argument>
6161
<argument>%api_platform.openapi.overrideResponses%</argument>
62+
<argument>%api_platform.swagger.persist_authorization%</argument>
6263
<argument>%api_platform.swagger.http_auth%</argument>
6364
</service>
6465
<service id="ApiPlatform\OpenApi\Options" alias="api_platform.openapi.options" />

src/Symfony/Bundle/Resources/public/init-swagger-ui.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ window.onload = function() {
4545
spec: data.spec,
4646
dom_id: '#swagger-ui',
4747
validatorUrl: null,
48+
persistAuthorization: data.persistAuthorization,
4849
deepLinking: true,
4950
oauth2RedirectUrl: data.oauth.redirectUrl,
5051
presets: [

src/Symfony/Bundle/SwaggerUi/SwaggerUiAction.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ public function __invoke(Request $request): Response
7979
'pkce' => $this->oauthPkce,
8080
],
8181
'extraConfiguration' => $this->swaggerUiContext->getExtraConfiguration(),
82+
'persistAuthorization' => $this->openApiOptions->hasPersistAuthorization(),
8283
];
8384

8485
$originalRouteParams = $request->attributes->get('_api_original_route_params') ?? [];

src/Symfony/Bundle/SwaggerUi/SwaggerUiProcessor.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public function process(mixed $openApi, Operation $operation, array $uriVariable
6363
$swaggerData = [
6464
'url' => $this->urlGenerator->generate('api_doc', ['format' => 'json']),
6565
'spec' => $this->normalizer->normalize($openApi, 'json', []),
66+
'persistAuthorization' => $this->openApiOptions->hasPersistAuthorization(),
6667
'oauth' => [
6768
'enabled' => $this->openApiOptions->getOAuthEnabled(),
6869
'type' => $this->openApiOptions->getOAuthType(),

tests/Symfony/Bundle/DependencyInjection/ConfigurationTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ private function runDefaultConfigTests(array $doctrineIntegrationsToLoad = ['orm
153153
'api_keys' => [],
154154
'http_auth' => [],
155155
'swagger_ui_extra_configuration' => [],
156+
'persist_authorization' => false,
156157
],
157158
'eager_loading' => [
158159
'enabled' => true,

tests/Symfony/Bundle/SwaggerUi/SwaggerUiActionTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ public static function getInvokeParameters(): iterable
100100
'swagger_data' => [
101101
'url' => '/url',
102102
'spec' => self::SPEC,
103+
'persistAuthorization' => false,
103104
'oauth' => [
104105
'enabled' => false,
105106
'clientId' => '',
@@ -142,6 +143,7 @@ public static function getInvokeParameters(): iterable
142143
'swagger_data' => [
143144
'url' => '/url',
144145
'spec' => self::SPEC,
146+
'persistAuthorization' => false,
145147
'oauth' => [
146148
'enabled' => false,
147149
'clientId' => null,
@@ -201,6 +203,7 @@ public function testDoNotRunCurrentRequest(Request $request): void
201203
'swagger_data' => [
202204
'url' => '/url',
203205
'spec' => self::SPEC,
206+
'persistAuthorization' => true,
204207
'oauth' => [
205208
'enabled' => false,
206209
'clientId' => '',
@@ -228,7 +231,7 @@ public function testDoNotRunCurrentRequest(Request $request): void
228231
$urlGeneratorProphecy->reveal(),
229232
$normalizerProphecy->reveal(),
230233
$openApiFactoryProphecy->reveal(),
231-
new Options('title', '', '1.0.0'),
234+
new Options(title: 'title', description: '', version: '1.0.0', persistAuthorization: true),
232235
new SwaggerUiContext(),
233236
['jsonld' => ['application/ld+json']]
234237
);

0 commit comments

Comments
 (0)