Skip to content

Commit 124c6b2

Browse files
committed
Update the export command
1 parent b56eef3 commit 124c6b2

File tree

4 files changed

+56
-18
lines changed

4 files changed

+56
-18
lines changed

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,5 @@ script:
5353
fi
5454
- tests/Fixtures/app/console api:swagger:export > swagger.json && npx swagger-cli validate swagger.json && rm swagger.json
5555
- tests/Fixtures/app/console api:swagger:export --yaml > swagger.yaml && npx swagger-cli validate swagger.yaml && rm swagger.yaml
56+
- tests/Fixtures/app/console api:openapi:export --spec-version 3 > swagger.json && npx swagger-cli validate swagger.json && rm swagger.json
57+
- tests/Fixtures/app/console api:openapi:export --spec-version 3 --yaml > swagger.yaml && npx swagger-cli validate swagger.yaml && rm swagger.yaml

src/Bridge/Symfony/Bundle/Command/SwaggerCommand.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use ApiPlatform\Core\Documentation\Documentation;
1717
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
1818
use Symfony\Component\Console\Command\Command;
19+
use Symfony\Component\Console\Exception\InvalidOptionException;
1920
use Symfony\Component\Console\Input\InputInterface;
2021
use Symfony\Component\Console\Input\InputOption;
2122
use Symfony\Component\Console\Output\OutputInterface;
@@ -29,18 +30,20 @@
2930
*/
3031
final class SwaggerCommand extends Command
3132
{
32-
private $documentationNormalizer;
33+
private $v2documentationNormalizer;
34+
private $v3documentationNormalizer;
3335
private $resourceNameCollectionFactory;
3436
private $apiTitle;
3537
private $apiDescription;
3638
private $apiVersion;
3739
private $apiFormats;
3840

39-
public function __construct(NormalizerInterface $documentationNormalizer, ResourceNameCollectionFactoryInterface $resourceNameCollection, string $apiTitle, string $apiDescription, string $apiVersion, array $apiFormats)
41+
public function __construct(NormalizerInterface $v2documentationNormalizer, NormalizerInterface $v3documentationNormalizer, ResourceNameCollectionFactoryInterface $resourceNameCollection, string $apiTitle, string $apiDescription, string $apiVersion, array $apiFormats)
4042
{
4143
parent::__construct();
4244

43-
$this->documentationNormalizer = $documentationNormalizer;
45+
$this->v2documentationNormalizer = $v2documentationNormalizer;
46+
$this->v3documentationNormalizer = $v3documentationNormalizer;
4447
$this->resourceNameCollectionFactory = $resourceNameCollection;
4548
$this->apiTitle = $apiTitle;
4649
$this->apiDescription = $apiDescription;
@@ -54,9 +57,11 @@ public function __construct(NormalizerInterface $documentationNormalizer, Resour
5457
protected function configure()
5558
{
5659
$this
57-
->setName('api:swagger:export')
58-
->setDescription('Dump the Swagger 2.0 (OpenAPI) documentation')
60+
->setName('api:openapi:export')
61+
->setAliases(['api:swagger:export'])
62+
->setDescription('Dump the OpenAPI documentation')
5963
->addOption('yaml', 'y', InputOption::VALUE_NONE, 'Dump the documentation in YAML')
64+
->addOption('spec-version', null, InputOption::VALUE_OPTIONAL, 'OpenAPI version to use ("2" or "3")', '2')
6065
->addOption('output', 'o', InputOption::VALUE_OPTIONAL, 'Write output to file');
6166
}
6267

@@ -65,9 +70,15 @@ protected function configure()
6570
*/
6671
protected function execute(InputInterface $input, OutputInterface $output)
6772
{
73+
/** @var string $version */
74+
$version = $input->getOption('spec-version');
75+
if (!\in_array($version, ['2', '3'], true)) {
76+
throw new InvalidOptionException(sprintf('This tool only support version 2 and 3 of the OpenAPI specification ("%s" given).', $version));
77+
}
78+
$documentationNormalizer = sprintf('v%ddocumentationNormalizer', $version);
6879
$documentation = new Documentation($this->resourceNameCollectionFactory->create(), $this->apiTitle, $this->apiDescription, $this->apiVersion, $this->apiFormats);
69-
$data = $this->documentationNormalizer->normalize($documentation);
70-
$content = $input->getOption('yaml') ? Yaml::dump($data, 6, 4, Yaml::DUMP_OBJECT_AS_MAP | Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE) : (json_encode($data, JSON_PRETTY_PRINT) ?: '');
80+
$data = $this->$documentationNormalizer->normalize($documentation);
81+
$content = $input->getOption('yaml') ? Yaml::dump($data, 10, 2, Yaml::DUMP_OBJECT_AS_MAP | Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE) : (json_encode($data, JSON_PRETTY_PRINT) ?: '');
7182
if (!empty($filename = $input->getOption('output')) && \is_string($filename)) {
7283
file_put_contents($filename, $content);
7384
$output->writeln(

src/Bridge/Symfony/Bundle/Resources/config/swagger.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,12 @@
6767

6868
<service id="api_platform.swagger.command.swagger_command" class="ApiPlatform\Core\Bridge\Symfony\Bundle\Command\SwaggerCommand">
6969
<argument type="service" id="api_platform.swagger.normalizer.documentation" />
70+
<argument type="service" id="api_platform.openapi.normalizer.documentation" />
7071
<argument type="service" id="api_platform.metadata.resource.name_collection_factory" />
7172
<argument>%api_platform.title%</argument>
7273
<argument>%api_platform.description%</argument>
7374
<argument>%api_platform.version%</argument>
7475
<argument>%api_platform.formats%</argument>
75-
7676
<tag name="console.command" />
7777
</service>
7878

tests/Bridge/Symfony/Bundle/Command/SwaggerCommandTest.php

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use Symfony\Bundle\FrameworkBundle\Console\Application;
1717
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
18+
use Symfony\Component\Console\Exception\InvalidOptionException;
1819
use Symfony\Component\Console\Tester\ApplicationTester;
1920
use Symfony\Component\Yaml\Exception\ParseException;
2021
use Symfony\Component\Yaml\Yaml;
@@ -40,13 +41,20 @@ protected function setUp()
4041
$this->tester = new ApplicationTester($application);
4142
}
4243

43-
public function testExecute()
44+
public function testExecuteWithAlias()
4445
{
4546
$this->tester->run(['command' => 'api:swagger:export']);
4647

4748
$this->assertJson($this->tester->getDisplay());
4849
}
4950

51+
public function testExecuteOpenApiV3()
52+
{
53+
$this->tester->run(['command' => 'api:openapi:export', '--spec-version' => '3']);
54+
55+
$this->assertJson($this->tester->getDisplay());
56+
}
57+
5058
public function testExecuteWithYaml()
5159
{
5260
$this->tester->run(['command' => 'api:swagger:export', '--yaml' => true]);
@@ -55,23 +63,40 @@ public function testExecuteWithYaml()
5563
$this->assertYaml($result);
5664

5765
$expected = <<<YAML
58-
/dummy_cars:
59-
get:
60-
tags:
61-
- DummyCar
62-
operationId: getDummyCarCollection
66+
/dummy_cars:
67+
get:
68+
tags:
69+
- DummyCar
70+
operationId: getDummyCarCollection
6371
YAML;
6472

6573
$this->assertContains($expected, $result, 'nested object should be present.');
6674

6775
$expected = <<<YAML
68-
'/dummy_cars/{id}':
69-
get:
70-
tags: []
71-
operationId: getDummyCarItem
76+
'/dummy_cars/{id}':
77+
get:
78+
tags: []
79+
operationId: getDummyCarItem
7280
YAML;
7381

7482
$this->assertContains($expected, $result, 'arrays should be correctly formatted.');
83+
$this->assertContains("swagger: '2.0'", $result);
84+
}
85+
86+
public function testExecuteOpenApiV3WithYaml()
87+
{
88+
$this->tester->run(['command' => 'api:openapi:export', '--spec-version' => '3', '--yaml' => true]);
89+
90+
$result = $this->tester->getDisplay();
91+
$this->assertYaml($result);
92+
$this->assertContains('openapi: 3.0.2', $result);
93+
}
94+
95+
public function testExecuteWithBadArguments()
96+
{
97+
$this->expectException(InvalidOptionException::class);
98+
$this->expectExceptionMessage('This tool only support version 2 and 3 of the OpenAPI specification ("" given).');
99+
$this->tester->run(['command' => 'api:openapi:export', '--spec-version' => '', '--yaml' => true]);
75100
}
76101

77102
public function testWriteToFile()

0 commit comments

Comments
 (0)