mirror of
https://github.com/retailcrm/NelmioApiDocBundle.git
synced 2025-02-09 19:19:29 +03:00
Fix #1462 by allowing all supported implementations of NormalizerInterface in ApiPlatformDescriber
> add test
This commit is contained in:
parent
7848e00d41
commit
f1bb40c6e4
@ -12,19 +12,17 @@
|
|||||||
namespace Nelmio\ApiDocBundle\Describer;
|
namespace Nelmio\ApiDocBundle\Describer;
|
||||||
|
|
||||||
use ApiPlatform\Core\Documentation\Documentation;
|
use ApiPlatform\Core\Documentation\Documentation;
|
||||||
use ApiPlatform\Core\Swagger\Serializer\ApiGatewayNormalizer;
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
||||||
use ApiPlatform\Core\Swagger\Serializer\DocumentationNormalizer;
|
|
||||||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
|
||||||
|
|
||||||
final class ApiPlatformDescriber extends ExternalDocDescriber
|
final class ApiPlatformDescriber extends ExternalDocDescriber
|
||||||
{
|
{
|
||||||
public function __construct(Documentation $documentation, $normalizer, UrlGeneratorInterface $urlGenerator)
|
public function __construct(Documentation $documentation, NormalizerInterface $normalizer)
|
||||||
{
|
{
|
||||||
if (!$normalizer instanceof ApiGatewayNormalizer && !$normalizer instanceof DocumentationNormalizer) {
|
if (!$normalizer->supportsNormalization($documentation, 'json')) {
|
||||||
throw new \InvalidArgumentException(sprintf('Argument 2 passed to %s() must be an instance of %s or %s, %s given.', __METHOD__, ApiGatewayNormalizer::class, DocumentationNormalizer::class, get_class($normalizer)));
|
throw new \InvalidArgumentException(sprintf('Argument 2 passed to %s() must implement %s and support normalization of %s, %s given.', __METHOD__, NormalizerInterface::class, Documentation::class, get_class($normalizer)));
|
||||||
}
|
}
|
||||||
|
|
||||||
parent::__construct(function () use ($documentation, $normalizer, $urlGenerator) {
|
parent::__construct(function () use ($documentation, $normalizer) {
|
||||||
$documentation = (array) $normalizer->normalize($documentation);
|
$documentation = (array) $normalizer->normalize($documentation);
|
||||||
unset($documentation['basePath']);
|
unset($documentation['basePath']);
|
||||||
|
|
||||||
|
@ -7,7 +7,6 @@
|
|||||||
<service id="nelmio_api_doc.describers.api_platform" class="Nelmio\ApiDocBundle\Describer\ApiPlatformDescriber" public="false">
|
<service id="nelmio_api_doc.describers.api_platform" class="Nelmio\ApiDocBundle\Describer\ApiPlatformDescriber" public="false">
|
||||||
<argument type="service" id="nelmio_api_doc.describers.api_platform.documentation" />
|
<argument type="service" id="nelmio_api_doc.describers.api_platform.documentation" />
|
||||||
<argument type="service" id="api_platform.swagger.normalizer.documentation" />
|
<argument type="service" id="api_platform.swagger.normalizer.documentation" />
|
||||||
<argument type="service" id="router" />
|
|
||||||
|
|
||||||
<tag name="nelmio_api_doc.describer" priority="-100" />
|
<tag name="nelmio_api_doc.describer" priority="-100" />
|
||||||
</service>
|
</service>
|
||||||
|
59
Tests/Describer/ApiPlatformDescriberTest.php
Normal file
59
Tests/Describer/ApiPlatformDescriberTest.php
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the NelmioApiDocBundle package.
|
||||||
|
*
|
||||||
|
* (c) Nelmio
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Nelmio\ApiDocBundle\Tests\Describer;
|
||||||
|
|
||||||
|
use ApiPlatform\Core\Documentation\Documentation;
|
||||||
|
use ApiPlatform\Core\Metadata\Resource\ResourceNameCollection;
|
||||||
|
use EXSyst\Component\Swagger\Swagger;
|
||||||
|
use Nelmio\ApiDocBundle\Describer\ApiPlatformDescriber;
|
||||||
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
||||||
|
|
||||||
|
class ApiPlatformDescriberTest extends AbstractDescriberTest
|
||||||
|
{
|
||||||
|
private $documentation;
|
||||||
|
|
||||||
|
private $normalizer;
|
||||||
|
|
||||||
|
public function testDescribe()
|
||||||
|
{
|
||||||
|
$this->normalizer->expects($this->once())
|
||||||
|
->method('normalize')
|
||||||
|
->with($this->documentation)
|
||||||
|
->willReturn(['info' => ['title' => 'My Test App']]);
|
||||||
|
|
||||||
|
$expectedApi = new Swagger(['info' => ['title' => 'My Test App']]);
|
||||||
|
$this->assertEquals($expectedApi->toArray(), $this->getSwaggerDoc()->toArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testDescribeRemovesBasePathAfterNormalization()
|
||||||
|
{
|
||||||
|
$this->normalizer->expects($this->once())
|
||||||
|
->method('normalize')
|
||||||
|
->with($this->documentation)
|
||||||
|
->willReturn(['info' => ['title' => 'My Test App'], 'basePath' => '/foo']);
|
||||||
|
|
||||||
|
$expectedApi = new Swagger(['info' => ['title' => 'My Test App']]);
|
||||||
|
$this->assertEquals($expectedApi->toArray(), $this->getSwaggerDoc()->toArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function setUp()
|
||||||
|
{
|
||||||
|
$this->documentation = new Documentation(new ResourceNameCollection(['dummy' => 'dummy']));
|
||||||
|
|
||||||
|
$this->normalizer = $this->createMock(NormalizerInterface::class);
|
||||||
|
$this->normalizer->expects($this->once())
|
||||||
|
->method('supportsNormalization')
|
||||||
|
->willReturn(true);
|
||||||
|
|
||||||
|
$this->describer = new ApiPlatformDescriber($this->documentation, $this->normalizer);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user