Skip to content

Commit 4de43ba

Browse files
authored
fix: swagger ui provider accept html (#6449)
1 parent 7926dc6 commit 4de43ba

File tree

3 files changed

+63
-3
lines changed

3 files changed

+63
-3
lines changed

src/Symfony/Bundle/SwaggerUi/SwaggerUiProvider.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace ApiPlatform\Symfony\Bundle\SwaggerUi;
1515

1616
use ApiPlatform\Documentation\Documentation;
17+
use ApiPlatform\Documentation\Entrypoint;
1718
use ApiPlatform\Metadata\Error;
1819
use ApiPlatform\Metadata\Get;
1920
use ApiPlatform\Metadata\HttpOperation;
@@ -44,6 +45,7 @@ public function provide(Operation $operation, array $uriVariables = [], array $c
4445
!($operation instanceof HttpOperation)
4546
|| !($request = $context['request'] ?? null)
4647
|| 'html' !== $request->getRequestFormat()
48+
|| true === ($operation->getExtraProperties()['_api_disable_swagger_provider'] ?? false)
4749
) {
4850
return $this->decorated->provide($operation, $uriVariables, $context);
4951
}
@@ -55,11 +57,12 @@ public function provide(Operation $operation, array $uriVariables = [], array $c
5557
// We need to call our operation provider just in case it fails
5658
// when it fails we'll get an Error and we'll fix the status accordingly
5759
// @see features/main/content_negotiation.feature:119
58-
// DocumentationAction has no content negotation as well we want HTML so render swagger ui
59-
if (!$operation instanceof Error && Documentation::class !== $operation->getClass()) {
60+
// When requesting DocumentationAction or EntrypointAction with Accept: text/html we render SwaggerUi
61+
if (!$operation instanceof Error && !\in_array($operation->getClass(), [Documentation::class, Entrypoint::class], true)) {
6062
$this->decorated->provide($operation, $uriVariables, $context);
6163
}
6264

65+
// This should render only when an error occured
6366
$swaggerUiOperation = new Get(
6467
class: OpenApi::class,
6568
processor: 'api_platform.swagger_ui.processor',
@@ -71,7 +74,6 @@ class: OpenApi::class,
7174

7275
// save our operation
7376
$request->attributes->set('_api_operation', $swaggerUiOperation);
74-
7577
$data = $this->openApiFactory->__invoke(['base_url' => $request->getBaseUrl() ?: '/']);
7678
$request->attributes->set('data', $data);
7779

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue6384;
15+
16+
use ApiPlatform\Metadata\Get;
17+
use Symfony\Component\HttpFoundation\Response;
18+
19+
#[Get(
20+
uriTemplate: 'accept_html',
21+
provider: [self::class, 'provide'],
22+
outputFormats: ['html' => ['text/html']],
23+
formats: ['html' => ['text/html']],
24+
extraProperties: ['_api_disable_swagger_provider' => true]
25+
)]
26+
class AcceptHtml
27+
{
28+
public static function provide(): Response
29+
{
30+
return new Response('<h1>hello</h1>');
31+
}
32+
}

tests/Functional/FormatTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Tests\Functional;
15+
16+
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
17+
18+
final class FormatTest extends ApiTestCase
19+
{
20+
public function testShouldReturnHtml(): void
21+
{
22+
$r = self::createClient()->request('GET', '/accept_html', ['headers' => ['Accept' => 'text/html']]);
23+
$this->assertResponseIsSuccessful();
24+
$this->assertEquals($r->getContent(), '<h1>hello</h1>');
25+
}
26+
}

0 commit comments

Comments
 (0)