Skip to content

Commit 31289b8

Browse files
soyukaclaude
andauthored
fix(symfony): make enable_docs a master switch for disabling documentation (#7806)
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com> Fixes #6282
1 parent cfdc22c commit 31289b8

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ public function load(array $configs, ContainerBuilder $container): void
130130
if (!isset($config['defaults']['hideHydraOperation']) && !isset($config['defaults']['hide_hydra_operation'])) {
131131
$config['defaults']['hideHydraOperation'] = true;
132132
}
133+
// Disabling docs is a master switch: also disable Swagger UI and ReDoc
134+
// to prevent HTML documentation from being served on resource endpoints.
135+
$config['enable_swagger_ui'] = false;
136+
$config['enable_re_doc'] = false;
133137
}
134138
$jsonSchemaFormats = $config['jsonschema_formats'];
135139

tests/Functional/DocumentationActionTest.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,18 @@ class DocumentationActionAppKernel extends \AppKernel
2424
{
2525
public static bool $swaggerUiEnabled = true;
2626
public static bool $reDocEnabled = true;
27+
public static bool $docsEnabled = true;
2728

2829
public function getCacheDir(): string
2930
{
30-
$suffix = (self::$swaggerUiEnabled ? 'ui_' : 'no_ui_').(self::$reDocEnabled ? 'redoc' : 'no_redoc');
31+
$suffix = (self::$swaggerUiEnabled ? 'ui_' : 'no_ui_').(self::$reDocEnabled ? 'redoc' : 'no_redoc').(self::$docsEnabled ? '' : '_no_docs');
3132

3233
return parent::getCacheDir().'/'.$suffix;
3334
}
3435

3536
public function getLogDir(): string
3637
{
37-
$suffix = (self::$swaggerUiEnabled ? 'ui_' : 'no_ui_').(self::$reDocEnabled ? 'redoc' : 'no_redoc');
38+
$suffix = (self::$swaggerUiEnabled ? 'ui_' : 'no_ui_').(self::$reDocEnabled ? 'redoc' : 'no_redoc').(self::$docsEnabled ? '' : '_no_docs');
3839

3940
return parent::getLogDir().'/'.$suffix;
4041
}
@@ -47,6 +48,7 @@ protected function configureContainer(ContainerBuilder $c, LoaderInterface $load
4748
$container->loadFromExtension('api_platform', [
4849
'enable_swagger_ui' => DocumentationActionAppKernel::$swaggerUiEnabled,
4950
'enable_re_doc' => DocumentationActionAppKernel::$reDocEnabled,
51+
'enable_docs' => DocumentationActionAppKernel::$docsEnabled,
5052
]);
5153
});
5254
}
@@ -158,4 +160,22 @@ public function testJsonDocumentationIsAccessibleWhenSwaggerUiIsEnabled(): void
158160
$this->assertJsonContains(['openapi' => '3.1.0']);
159161
$this->assertJsonContains(['info' => ['title' => 'My Dummy API']]);
160162
}
163+
164+
public function testEnableDocsFalseDisablesSwaggerUiAndReDoc(): void
165+
{
166+
DocumentationActionAppKernel::$swaggerUiEnabled = true;
167+
DocumentationActionAppKernel::$reDocEnabled = true;
168+
DocumentationActionAppKernel::$docsEnabled = false;
169+
170+
$client = self::createClient();
171+
172+
$container = static::getContainer();
173+
$this->assertFalse($container->getParameter('api_platform.enable_docs'));
174+
// enable_docs: false acts as a master switch, forcing these to false
175+
$this->assertFalse($container->getParameter('api_platform.enable_swagger_ui'));
176+
$this->assertFalse($container->getParameter('api_platform.enable_re_doc'));
177+
178+
$client->request('GET', '/docs', ['headers' => ['Accept' => 'text/html']]);
179+
$this->assertResponseStatusCodeSame(404);
180+
}
161181
}

0 commit comments

Comments
 (0)