Skip to content

Commit

Permalink
Merge pull request #2470 from vincentchalamon/issue/api-gateway-norma…
Browse files Browse the repository at this point in the history
…lizer

Decrease ApiGatewayNormalizer priority
  • Loading branch information
vincentchalamon committed Jan 24, 2019
2 parents 5c8820d + a7eb382 commit a9bc507
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 3 deletions.
14 changes: 14 additions & 0 deletions features/swagger/docs.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
6 changes: 4 additions & 2 deletions src/Bridge/Symfony/Bundle/Command/SwaggerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
}

/**
Expand All @@ -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)) {
Expand Down
2 changes: 1 addition & 1 deletion src/Bridge/Symfony/Bundle/Resources/config/swagger.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<tag name="serializer.normalizer" priority="16" />
</service>

<service id="api_platform.swagger.normalizer.api_gateway" class="ApiPlatform\Core\Swagger\Serializer\ApiGatewayNormalizer" public="false" decorates="api_platform.swagger.normalizer.documentation">
<service id="api_platform.swagger.normalizer.api_gateway" class="ApiPlatform\Core\Swagger\Serializer\ApiGatewayNormalizer" public="false" decorates="api_platform.swagger.normalizer.documentation" decoration-priority="-1">
<argument type="service" id="api_platform.swagger.normalizer.api_gateway.inner" />
<tag name="serializer.normalizer" priority="17" />
</service>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* 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 <vincentchalamon@gmail.com>
*/
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);
}
}
6 changes: 6 additions & 0 deletions tests/Fixtures/app/config/config_common.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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']

0 comments on commit a9bc507

Please sign in to comment.