Skip to content

Commit 6561eb1

Browse files
alexndlmsoyuka
andauthored
fix(symfony): do not load docs routes if docs disabled (#7448)
Co-authored-by: soyuka <[email protected]>
1 parent 8effdf7 commit 6561eb1

File tree

4 files changed

+29
-0
lines changed

4 files changed

+29
-0
lines changed

src/Symfony/Action/DocumentationAction.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public function __construct(
5050
?Negotiator $negotiator = null,
5151
private readonly array $documentationFormats = [OpenApiNormalizer::JSON_FORMAT => ['application/vnd.openapi+json'], OpenApiNormalizer::FORMAT => ['application/json']],
5252
private readonly bool $swaggerUiEnabled = true,
53+
private readonly bool $docsEnabled = true,
5354
) {
5455
$this->negotiator = $negotiator ?? new Negotiator();
5556
}
@@ -59,6 +60,10 @@ public function __construct(
5960
*/
6061
public function __invoke(?Request $request = null)
6162
{
63+
if (false === $this->docsEnabled) {
64+
throw new NotFoundHttpException();
65+
}
66+
6267
if (null === $request) {
6368
return new Documentation($this->resourceNameCollectionFactory->create(), $this->title, $this->description, $this->version);
6469
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,6 @@
4848
service('api_platform.negotiator')->nullOnInvalid(),
4949
'%api_platform.docs_formats%',
5050
'%api_platform.enable_swagger_ui%',
51+
'%api_platform.enable_docs%',
5152
]);
5253
};

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@
180180
service('api_platform.negotiator')->nullOnInvalid(),
181181
'%api_platform.docs_formats%',
182182
'%api_platform.enable_swagger_ui%',
183+
'%api_platform.enable_docs%',
183184
]);
184185

185186
$services->set('api_platform.action.placeholder', 'ApiPlatform\Symfony\Action\PlaceholderAction')

src/Symfony/Tests/Action/DocumentationActionTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,26 @@ public function testHtmlFormatNotSupportedThrowsException(): void
154154

155155
$documentation($request);
156156
}
157+
158+
public function testHtmlFormatWhenDocsDisabledThrows404(): void
159+
{
160+
$this->expectException(NotFoundHttpException::class);
161+
162+
$request = new Request();
163+
$request->attributes->set('_format', 'html');
164+
165+
$openApiFactory = $this->createMock(OpenApiFactoryInterface::class);
166+
$resourceNameCollectionFactory = $this->createMock(ResourceNameCollectionFactoryInterface::class);
167+
168+
$documentation = new DocumentationAction(
169+
$resourceNameCollectionFactory,
170+
openApiFactory: $openApiFactory,
171+
documentationFormats: [
172+
'html' => ['text/html'],
173+
],
174+
docsEnabled: false,
175+
);
176+
177+
$documentation($request);
178+
}
157179
}

0 commit comments

Comments
 (0)