Skip to content

Commit ff9332d

Browse files
committed
fix(swagger): disable Swagger UI and keep openapi.json
1 parent c857b52 commit ff9332d

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

src/Symfony/Action/DocumentationAction.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use Negotiation\Negotiator;
2929
use Symfony\Component\HttpFoundation\Request;
3030
use Symfony\Component\HttpFoundation\Response;
31+
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
3132

3233
/**
3334
* Generates the API documentation.
@@ -48,6 +49,7 @@ public function __construct(
4849
private readonly ?ProcessorInterface $processor = null,
4950
?Negotiator $negotiator = null,
5051
private readonly array $documentationFormats = [OpenApiNormalizer::JSON_FORMAT => ['application/vnd.openapi+json'], OpenApiNormalizer::FORMAT => ['application/json']],
52+
private readonly bool $swaggerUiEnabled = true,
5153
) {
5254
$this->negotiator = $negotiator ?? new Negotiator();
5355
}
@@ -83,6 +85,10 @@ public function __invoke(?Request $request = null)
8385
*/
8486
private function getOpenApiDocumentation(array $context, string $format, Request $request): OpenApi|Response
8587
{
88+
if ('html' === $format && !$this->swaggerUiEnabled) {
89+
throw new NotFoundHttpException('Swagger UI is disabled.');
90+
}
91+
8692
if ($this->provider && $this->processor) {
8793
$context['request'] = $request;
8894
$operation = new Get(
@@ -93,7 +99,7 @@ class: OpenApi::class,
9399
outputFormats: $this->documentationFormats
94100
);
95101

96-
if ('html' === $format) {
102+
if ('html' === $format && $this->swaggerUiEnabled) {
97103
$operation = $operation->withProcessor('api_platform.swagger_ui.processor')->withWrite(true);
98104
}
99105

src/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -586,13 +586,13 @@ private function registerSwaggerConfiguration(ContainerBuilder $container, array
586586
$loader->load('openapi/yaml.php');
587587
}
588588

589-
$loader->load('swagger_ui.php');
589+
if ($config['enable_swagger_ui']) {
590+
$loader->load('swagger_ui.php');
590591

591-
if ($config['use_symfony_listeners']) {
592-
$loader->load('symfony/swagger_ui.php');
593-
}
592+
if ($config['use_symfony_listeners']) {
593+
$loader->load('symfony/swagger_ui.php');
594+
}
594595

595-
if ($config['enable_swagger_ui']) {
596596
$loader->load('state/swagger_ui.php');
597597
}
598598

src/Symfony/Bundle/Resources/config/symfony/controller.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,6 @@
4747
service('api_platform.state_processor.main'),
4848
service('api_platform.negotiator')->nullOnInvalid(),
4949
'%api_platform.docs_formats%',
50+
'%api_platform.enable_swagger_ui%',
5051
]);
5152
};

0 commit comments

Comments
 (0)