diff --git a/features/swagger/docs.feature b/features/swagger/docs.feature index e10b4eaa109..e72e054dc6f 100644 --- a/features/swagger/docs.feature +++ b/features/swagger/docs.feature @@ -9,6 +9,10 @@ Feature: Documentation support Then the response status code should be 200 And the response should be in JSON And the header "Content-Type" should be equal to "application/json; charset=utf-8" + # OverrideDocumentationNormalizer + And the JSON node "definitions.RamseyUuidDummy.properties.id.description" should be equal to "The dummy id" + And the JSON node "definitions.RelatedDummy-barcelona" should exist + And the JSON node "definitions.RelatedDummybarcelona" should not exist # Context And the JSON node "swagger" should be equal to "2.0" # Root properties @@ -103,3 +107,13 @@ Feature: Documentation support And I should see text matching "My Dummy API" And I should see text matching "swagger" And I should see text matching "2.0" + + Scenario: Retrieve the Swagger/OpenAPI documentation with API Gateway compatibility + Given I send a "GET" request to "/docs.json?api_gateway=true" + Then the response status code should be 200 + And the response should be in JSON + And the header "Content-Type" should be equal to "application/json; charset=utf-8" + And the JSON node "basePath" should be equal to "/" + And the JSON node "definitions.RamseyUuidDummy.properties.id.description" should be equal to "The dummy id" + And the JSON node "definitions.RelatedDummy-barcelona" should not exist + And the JSON node "definitions.RelatedDummybarcelona" should exist diff --git a/src/Bridge/Symfony/Bundle/Command/SwaggerCommand.php b/src/Bridge/Symfony/Bundle/Command/SwaggerCommand.php index a4272bd779f..64555053c72 100644 --- a/src/Bridge/Symfony/Bundle/Command/SwaggerCommand.php +++ b/src/Bridge/Symfony/Bundle/Command/SwaggerCommand.php @@ -15,6 +15,7 @@ use ApiPlatform\Core\Documentation\Documentation; use ApiPlatform\Core\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface; +use ApiPlatform\Core\Swagger\Serializer\ApiGatewayNormalizer; use ApiPlatform\Core\Swagger\Serializer\DocumentationNormalizer; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Exception\InvalidOptionException; @@ -62,7 +63,8 @@ protected function configure() ->setDescription('Dump the OpenAPI documentation') ->addOption('yaml', 'y', InputOption::VALUE_NONE, 'Dump the documentation in YAML') ->addOption('spec-version', null, InputOption::VALUE_OPTIONAL, 'OpenAPI version to use ("2" or "3")', '2') - ->addOption('output', 'o', InputOption::VALUE_OPTIONAL, 'Write output to file'); + ->addOption('output', 'o', InputOption::VALUE_OPTIONAL, 'Write output to file') + ->addOption('api-gateway', null, InputOption::VALUE_NONE, 'API Gateway compatibility'); } /** @@ -79,7 +81,7 @@ protected function execute(InputInterface $input, OutputInterface $output) } $documentation = new Documentation($this->resourceNameCollectionFactory->create(), $this->apiTitle, $this->apiDescription, $this->apiVersion, $this->apiFormats); - $data = $this->normalizer->normalize($documentation, DocumentationNormalizer::FORMAT, ['spec_version' => (int) $version]); + $data = $this->normalizer->normalize($documentation, DocumentationNormalizer::FORMAT, ['spec_version' => (int) $version, ApiGatewayNormalizer::API_GATEWAY => $input->getOption('api-gateway')]); $content = $input->getOption('yaml') ? Yaml::dump($data, 10, 2, Yaml::DUMP_OBJECT_AS_MAP | Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE | Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK) : (json_encode($data, JSON_PRETTY_PRINT) ?: ''); if (!empty($filename = $input->getOption('output')) && \is_string($filename)) { diff --git a/src/Bridge/Symfony/Bundle/Resources/config/swagger.xml b/src/Bridge/Symfony/Bundle/Resources/config/swagger.xml index d980b529120..61244f4ec38 100644 --- a/src/Bridge/Symfony/Bundle/Resources/config/swagger.xml +++ b/src/Bridge/Symfony/Bundle/Resources/config/swagger.xml @@ -34,7 +34,7 @@ - + diff --git a/tests/Fixtures/TestBundle/Serializer/Normalizer/OverrideDocumentationNormalizer.php b/tests/Fixtures/TestBundle/Serializer/Normalizer/OverrideDocumentationNormalizer.php new file mode 100644 index 00000000000..a3c66d83a3b --- /dev/null +++ b/tests/Fixtures/TestBundle/Serializer/Normalizer/OverrideDocumentationNormalizer.php @@ -0,0 +1,46 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace ApiPlatform\Core\Tests\Fixtures\TestBundle\Serializer\Normalizer; + +use Symfony\Component\Serializer\Normalizer\NormalizerInterface; + +/** + * @author Vincent Chalamon + */ +final class OverrideDocumentationNormalizer implements NormalizerInterface +{ + private $documentationNormalizer; + + public function __construct(NormalizerInterface $documentationNormalizer) + { + $this->documentationNormalizer = $documentationNormalizer; + } + + public function normalize($object, $format = null, array $context = []) + { + $data = $this->documentationNormalizer->normalize($object, $format, $context); + if (isset($data['definitions'])) { + $data['definitions']['RamseyUuidDummy']['properties']['id']['description'] = 'The dummy id'; + } else { + $data['components']['schemas']['RamseyUuidDummy']['properties']['id']['description'] = 'The dummy id'; + } + + return $data; + } + + public function supportsNormalization($data, $format = null) + { + return $this->documentationNormalizer->supportsNormalization($data, $format); + } +} diff --git a/tests/Fixtures/app/config/config_common.yml b/tests/Fixtures/app/config/config_common.yml index c4767454ef3..456baa29f73 100644 --- a/tests/Fixtures/app/config/config_common.yml +++ b/tests/Fixtures/app/config/config_common.yml @@ -197,3 +197,9 @@ services: mercure.hub.default.publisher: class: ApiPlatform\Core\Tests\Fixtures\DummyMercurePublisher + + app.serializer.normalizer.override_documentation: + class: ApiPlatform\Core\Tests\Fixtures\TestBundle\Serializer\Normalizer\OverrideDocumentationNormalizer + decorates: api_platform.swagger.normalizer.documentation + public: false + arguments: ['@app.serializer.normalizer.override_documentation.inner']