Skip to content

Commit e84528e

Browse files
committed
-refactoring
1 parent 721fde3 commit e84528e

File tree

6 files changed

+35
-8
lines changed

6 files changed

+35
-8
lines changed

behat.yml.dist

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ default:
1818
tags: '~@postgres&&~@mongodb&&~@elasticsearch&&~@controller&&~@mercure&&~@query_parameter_validator'
1919
extensions:
2020
'FriendsOfBehat\SymfonyExtension':
21-
bootstrap: 'tests/Fixtures/app/bootstrap.php'
21+
bootstrap: '/Users/alex/PhpstormProjects/api-platform-core/tests/Fixtures/app/bootstrap.php'
2222
kernel:
2323
environment: 'test'
2424
debug: true
@@ -203,7 +203,7 @@ legacy:
203203
environment: 'test'
204204
debug: true
205205
class: AppKernel
206-
path: 'tests/Fixtures/app/AppKernel.php'
206+
path: '/Users/alex/PhpstormProjects/api-platform-core/tests/Fixtures/app/AppKernel.php'
207207
'Behat\MinkExtension':
208208
base_url: 'http://example.com/'
209209
files_path: 'features/files'
@@ -232,12 +232,12 @@ symfony_listeners:
232232
tags: '~@postgres&&~@mongodb&&~@elasticsearch&&~@mercure&&~@query_parameter_validator'
233233
extensions:
234234
'FriendsOfBehat\SymfonyExtension':
235-
bootstrap: 'tests/Fixtures/app/bootstrap.php'
235+
bootstrap: '/Users/alex/PhpstormProjects/api-platform-core/tests/Fixtures/app/bootstrap.php'
236236
kernel:
237237
environment: 'test'
238238
debug: true
239239
class: AppKernel
240-
path: 'tests/Fixtures/app/AppKernel.php'
240+
path: '/Users/alex/PhpstormProjects/api-platform-core/tests/Fixtures/app/AppKernel.php'
241241
'Behat\MinkExtension':
242242
base_url: 'http://example.com/'
243243
files_path: 'features/files'

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 ($this->docsEnabled === false) {
64+
throw new NotFoundHttpException('API documentation is disabled.');
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/Routing/ApiLoader.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,7 @@ public function supports(mixed $resource, ?string $type = null): bool
127127
*/
128128
private function loadExternalFiles(RouteCollection $routeCollection): void
129129
{
130-
if ($this->docsEnabled || isset($this->formats['jsonld'])) {
131-
$routeCollection->addCollection($this->fileLoader->load('docs.php'));
132-
}
133-
130+
$routeCollection->addCollection($this->fileLoader->load('docs.php'));
134131
$routeCollection->addCollection($this->fileLoader->load('genid.php'));
135132
$routeCollection->addCollection($this->fileLoader->load('errors.php'));
136133

src/Symfony/Tests/Action/DocumentationActionTest.php

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

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

0 commit comments

Comments
 (0)