Merge pull request #1463 from MyHammer/fix-api-platform-documentation-decoration

Fix documentation decoration with API Platform
This commit is contained in:
Guilhem N 2019-01-23 13:42:03 +01:00 committed by GitHub
commit edb54a1551
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 64 additions and 8 deletions

View File

@ -12,19 +12,17 @@
namespace Nelmio\ApiDocBundle\Describer;
use ApiPlatform\Core\Documentation\Documentation;
use ApiPlatform\Core\Swagger\Serializer\ApiGatewayNormalizer;
use ApiPlatform\Core\Swagger\Serializer\DocumentationNormalizer;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
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) {
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)));
if (!$normalizer->supportsNormalization($documentation, 'json')) {
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);
unset($documentation['basePath']);

View File

@ -7,7 +7,6 @@
<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="api_platform.swagger.normalizer.documentation" />
<argument type="service" id="router" />
<tag name="nelmio_api_doc.describer" priority="-100" />
</service>

View 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);
}
}