Skip to content

Commit 26cce17

Browse files
authored
Fix nullable OpenApiFactoryInterface (#4856)
1 parent 190ff0a commit 26cce17

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

src/Documentation/Action/DocumentationAction.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use ApiPlatform\Documentation\DocumentationInterface;
1818
use ApiPlatform\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
1919
use ApiPlatform\OpenApi\Factory\OpenApiFactoryInterface;
20-
use ApiPlatform\Util\RequestAttributesExtractor;
2120
use Symfony\Component\HttpFoundation\Request;
2221

2322
/**
@@ -31,7 +30,7 @@ final class DocumentationAction
3130
private string $title;
3231
private string $description;
3332
private string $version;
34-
private OpenApiFactoryInterface $openApiFactory;
33+
private OpenApiFactoryInterface|null $openApiFactory;
3534

3635
public function __construct(ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory, string $title = '', string $description = '', string $version = '', OpenApiFactoryInterface $openApiFactory = null)
3736
{
@@ -50,8 +49,6 @@ public function __invoke(Request $request = null): DocumentationInterface
5049
$context['api_gateway'] = true;
5150
}
5251
$request->attributes->set('_api_normalization_context', $request->attributes->get('_api_normalization_context', []) + $context);
53-
54-
$attributes = RequestAttributesExtractor::extractAttributes($request);
5552
}
5653

5754
if ('json' === $request->getRequestFormat() && null !== $this->openApiFactory) {

src/Symfony/Bundle/Resources/config/api.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@
9191
<argument>%api_platform.title%</argument>
9292
<argument>%api_platform.description%</argument>
9393
<argument>%api_platform.version%</argument>
94-
<argument type="service" id="api_platform.openapi.factory" on-invalid="null"></argument>
94+
<argument type="service" id="api_platform.openapi.factory" on-invalid="null" />
9595
</service>
9696

9797
<service id="api_platform.action.exception" class="ApiPlatform\Action\ExceptionAction" public="true">

tests/Documentation/Action/DocumentationActionTest.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
namespace ApiPlatform\Tests\Documentation\Action;
1515

1616
use ApiPlatform\Documentation\Action\DocumentationAction;
17+
use ApiPlatform\Documentation\Documentation;
1718
use ApiPlatform\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
19+
use ApiPlatform\Metadata\Resource\ResourceNameCollection;
1820
use ApiPlatform\OpenApi\Factory\OpenApiFactoryInterface;
1921
use ApiPlatform\OpenApi\Model\Info;
2022
use ApiPlatform\OpenApi\Model\Paths;
@@ -46,11 +48,31 @@ public function testDocumentationAction(): void
4648
$requestProphecy->query = $queryProphecy->reveal();
4749
$requestProphecy->getBaseUrl()->willReturn('/api')->shouldBeCalledTimes(1);
4850
$queryProphecy->getBoolean('api_gateway')->willReturn(true)->shouldBeCalledTimes(1);
49-
$attributesProphecy->all()->willReturn(['_api_normalization_context' => ['foo' => 'bar', 'base_url' => '/api', 'api_gateway' => true]])->shouldBeCalledTimes(1);
5051
$attributesProphecy->get('_api_normalization_context', [])->willReturn(['foo' => 'bar'])->shouldBeCalledTimes(1);
5152
$attributesProphecy->set('_api_normalization_context', ['foo' => 'bar', 'base_url' => '/api', 'api_gateway' => true])->shouldBeCalledTimes(1);
5253

5354
$documentation = new DocumentationAction($this->prophesize(ResourceNameCollectionFactoryInterface::class)->reveal(), 'my api', '', '1.0.0', $openApiFactoryProphecy->reveal());
5455
$this->assertInstanceOf(OpenApi::class, $documentation($requestProphecy->reveal()));
5556
}
57+
58+
public function testDocumentationActionWithoutOpenApiFactory(): void
59+
{
60+
$openApiFactoryProphecy = $this->prophesize(OpenApiFactoryInterface::class);
61+
$openApiFactoryProphecy->__invoke(Argument::any())->shouldNotBeCalled();
62+
$requestProphecy = $this->prophesize(Request::class);
63+
$requestProphecy->getRequestFormat()->willReturn('json');
64+
$attributesProphecy = $this->prophesize(ParameterBagInterface::class);
65+
$queryProphecy = $this->prophesize(ParameterBag::class);
66+
$requestProphecy->attributes = $attributesProphecy->reveal();
67+
$requestProphecy->query = $queryProphecy->reveal();
68+
$requestProphecy->getBaseUrl()->willReturn('/api')->shouldBeCalledTimes(1);
69+
$queryProphecy->getBoolean('api_gateway')->willReturn(true)->shouldBeCalledTimes(1);
70+
$attributesProphecy->get('_api_normalization_context', [])->willReturn(['foo' => 'bar'])->shouldBeCalledTimes(1);
71+
$attributesProphecy->set('_api_normalization_context', ['foo' => 'bar', 'base_url' => '/api', 'api_gateway' => true])->shouldBeCalledTimes(1);
72+
$resourceNameCollectionFactoryProphecy = $this->prophesize(ResourceNameCollectionFactoryInterface::class);
73+
$resourceNameCollectionFactoryProphecy->create()->willReturn(new ResourceNameCollection(['dummies']))->shouldBeCalled();
74+
75+
$documentation = new DocumentationAction($resourceNameCollectionFactoryProphecy->reveal(), 'my api', '', '1.0.0');
76+
$this->assertInstanceOf(Documentation::class, $documentation($requestProphecy->reveal()));
77+
}
5678
}

0 commit comments

Comments
 (0)