diff --git a/src/Symfony/Action/DocumentationAction.php b/src/Symfony/Action/DocumentationAction.php index d2e1048eb3..fd5ea76b2d 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(); + } + 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 046bd59c2c..ad1a9a9e50 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 dcc22eba0c..32885d4714 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/Tests/Action/DocumentationActionTest.php b/src/Symfony/Tests/Action/DocumentationActionTest.php index 4328c93332..e98b7658ae 100644 --- a/src/Symfony/Tests/Action/DocumentationActionTest.php +++ b/src/Symfony/Tests/Action/DocumentationActionTest.php @@ -154,4 +154,26 @@ public function testHtmlFormatNotSupportedThrowsException(): void $documentation($request); } + + public function testHtmlFormatWhenDocsDisabledThrows404(): void + { + $this->expectException(NotFoundHttpException::class); + + $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); + } }