From f1bb40c6e469d16f5623b6e4dcf9dd5f6c251c5f Mon Sep 17 00:00:00 2001 From: Alexander Wenzel Date: Fri, 11 Jan 2019 16:54:58 +0100 Subject: [PATCH] Fix #1462 by allowing all supported implementations of NormalizerInterface in ApiPlatformDescriber > add test --- Describer/ApiPlatformDescriber.php | 12 ++-- Resources/config/api_platform.xml | 1 - Tests/Describer/ApiPlatformDescriberTest.php | 59 ++++++++++++++++++++ 3 files changed, 64 insertions(+), 8 deletions(-) create mode 100644 Tests/Describer/ApiPlatformDescriberTest.php diff --git a/Describer/ApiPlatformDescriber.php b/Describer/ApiPlatformDescriber.php index 2a77f4c..0c28b29 100644 --- a/Describer/ApiPlatformDescriber.php +++ b/Describer/ApiPlatformDescriber.php @@ -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']); diff --git a/Resources/config/api_platform.xml b/Resources/config/api_platform.xml index 4fb4399..24d747c 100644 --- a/Resources/config/api_platform.xml +++ b/Resources/config/api_platform.xml @@ -7,7 +7,6 @@ - diff --git a/Tests/Describer/ApiPlatformDescriberTest.php b/Tests/Describer/ApiPlatformDescriberTest.php new file mode 100644 index 0000000..5e83a03 --- /dev/null +++ b/Tests/Describer/ApiPlatformDescriberTest.php @@ -0,0 +1,59 @@ +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); + } +}