From 721fde3e79c8fdb5f3adf9e8cbd6313a2c90d18d Mon Sep 17 00:00:00 2001 From: Aleksey Polyvanyi Date: Wed, 8 Oct 2025 14:24:57 +0200 Subject: [PATCH 1/4] feature/do-not-load-docs-routes-if-docs-disabled --- src/Symfony/Routing/ApiLoader.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Routing/ApiLoader.php b/src/Symfony/Routing/ApiLoader.php index f3abea05d35..de695329095 100644 --- a/src/Symfony/Routing/ApiLoader.php +++ b/src/Symfony/Routing/ApiLoader.php @@ -127,7 +127,10 @@ public function supports(mixed $resource, ?string $type = null): bool */ private function loadExternalFiles(RouteCollection $routeCollection): void { - $routeCollection->addCollection($this->fileLoader->load('docs.php')); + if ($this->docsEnabled || isset($this->formats['jsonld'])) { + $routeCollection->addCollection($this->fileLoader->load('docs.php')); + } + $routeCollection->addCollection($this->fileLoader->load('genid.php')); $routeCollection->addCollection($this->fileLoader->load('errors.php')); From 06a45aa02c9e7e0a458474a5d90fa1e996fed666 Mon Sep 17 00:00:00 2001 From: soyuka Date: Thu, 11 Dec 2025 10:37:25 +0100 Subject: [PATCH 2/4] review --- src/Symfony/Action/DocumentationAction.php | 5 ++++ .../Resources/config/symfony/controller.php | 1 + .../Resources/config/symfony/events.php | 1 + src/Symfony/Routing/ApiLoader.php | 5 +--- .../Tests/Action/DocumentationActionTest.php | 23 +++++++++++++++++++ 5 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/Symfony/Action/DocumentationAction.php b/src/Symfony/Action/DocumentationAction.php index d2e1048eb34..3eeb2215fbc 100644 --- a/src/Symfony/Action/DocumentationAction.php +++ b/src/Symfony/Action/DocumentationAction.php @@ -50,6 +50,7 @@ public function __construct( ?Negotiator $negotiator = null, private readonly array $documentationFormats = [OpenApiNormalizer::JSON_FORMAT => ['application/vnd.openapi+json'], OpenApiNormalizer::FORMAT => ['application/json']], private readonly bool $swaggerUiEnabled = true, + private readonly bool $docsEnabled = true, ) { $this->negotiator = $negotiator ?? new Negotiator(); } @@ -59,6 +60,10 @@ public function __construct( */ public function __invoke(?Request $request = null) { + if (false === $this->docsEnabled) { + throw new NotFoundHttpException('API documentation is disabled.'); + } + if (null === $request) { return new Documentation($this->resourceNameCollectionFactory->create(), $this->title, $this->description, $this->version); } diff --git a/src/Symfony/Bundle/Resources/config/symfony/controller.php b/src/Symfony/Bundle/Resources/config/symfony/controller.php index 046bd59c2c8..ad1a9a9e505 100644 --- a/src/Symfony/Bundle/Resources/config/symfony/controller.php +++ b/src/Symfony/Bundle/Resources/config/symfony/controller.php @@ -48,5 +48,6 @@ service('api_platform.negotiator')->nullOnInvalid(), '%api_platform.docs_formats%', '%api_platform.enable_swagger_ui%', + '%api_platform.enable_docs%', ]); }; diff --git a/src/Symfony/Bundle/Resources/config/symfony/events.php b/src/Symfony/Bundle/Resources/config/symfony/events.php index dcc22eba0cc..32885d47149 100644 --- a/src/Symfony/Bundle/Resources/config/symfony/events.php +++ b/src/Symfony/Bundle/Resources/config/symfony/events.php @@ -180,6 +180,7 @@ service('api_platform.negotiator')->nullOnInvalid(), '%api_platform.docs_formats%', '%api_platform.enable_swagger_ui%', + '%api_platform.enable_docs%', ]); $services->set('api_platform.action.placeholder', 'ApiPlatform\Symfony\Action\PlaceholderAction') diff --git a/src/Symfony/Routing/ApiLoader.php b/src/Symfony/Routing/ApiLoader.php index de695329095..f3abea05d35 100644 --- a/src/Symfony/Routing/ApiLoader.php +++ b/src/Symfony/Routing/ApiLoader.php @@ -127,10 +127,7 @@ public function supports(mixed $resource, ?string $type = null): bool */ private function loadExternalFiles(RouteCollection $routeCollection): void { - if ($this->docsEnabled || isset($this->formats['jsonld'])) { - $routeCollection->addCollection($this->fileLoader->load('docs.php')); - } - + $routeCollection->addCollection($this->fileLoader->load('docs.php')); $routeCollection->addCollection($this->fileLoader->load('genid.php')); $routeCollection->addCollection($this->fileLoader->load('errors.php')); diff --git a/src/Symfony/Tests/Action/DocumentationActionTest.php b/src/Symfony/Tests/Action/DocumentationActionTest.php index 4328c933329..c73a1271162 100644 --- a/src/Symfony/Tests/Action/DocumentationActionTest.php +++ b/src/Symfony/Tests/Action/DocumentationActionTest.php @@ -154,4 +154,27 @@ public function testHtmlFormatNotSupportedThrowsException(): void $documentation($request); } + + public function testHtmlFormatWhenDocsDisabledThrows404(): void + { + $this->expectException(NotFoundHttpException::class); + $this->expectExceptionMessage('API documentation is disabled.'); + + $request = new Request(); + $request->attributes->set('_format', 'html'); + + $openApiFactory = $this->createMock(OpenApiFactoryInterface::class); + $resourceNameCollectionFactory = $this->createMock(ResourceNameCollectionFactoryInterface::class); + + $documentation = new DocumentationAction( + $resourceNameCollectionFactory, + openApiFactory: $openApiFactory, + documentationFormats: [ + 'html' => ['text/html'], + ], + docsEnabled: false, + ); + + $documentation($request); + } } From 9e72d3653834a5563e9e9828a5ed1f4955750569 Mon Sep 17 00:00:00 2001 From: Antoine Bluchet Date: Thu, 11 Dec 2025 10:47:17 +0100 Subject: [PATCH 3/4] Update src/Symfony/Action/DocumentationAction.php --- src/Symfony/Action/DocumentationAction.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Action/DocumentationAction.php b/src/Symfony/Action/DocumentationAction.php index 3eeb2215fbc..fd5ea76b2db 100644 --- a/src/Symfony/Action/DocumentationAction.php +++ b/src/Symfony/Action/DocumentationAction.php @@ -61,7 +61,7 @@ public function __construct( public function __invoke(?Request $request = null) { if (false === $this->docsEnabled) { - throw new NotFoundHttpException('API documentation is disabled.'); + throw new NotFoundHttpException(); } if (null === $request) { From 66df74aa1c73c2d647d72a43cbc23a17e4efbe6f Mon Sep 17 00:00:00 2001 From: Antoine Bluchet Date: Thu, 11 Dec 2025 11:38:37 +0100 Subject: [PATCH 4/4] Update src/Symfony/Tests/Action/DocumentationActionTest.php --- src/Symfony/Tests/Action/DocumentationActionTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Symfony/Tests/Action/DocumentationActionTest.php b/src/Symfony/Tests/Action/DocumentationActionTest.php index c73a1271162..e98b7658aeb 100644 --- a/src/Symfony/Tests/Action/DocumentationActionTest.php +++ b/src/Symfony/Tests/Action/DocumentationActionTest.php @@ -158,7 +158,6 @@ public function testHtmlFormatNotSupportedThrowsException(): void public function testHtmlFormatWhenDocsDisabledThrows404(): void { $this->expectException(NotFoundHttpException::class); - $this->expectExceptionMessage('API documentation is disabled.'); $request = new Request(); $request->attributes->set('_format', 'html');