Skip to content

Commit c8b45fa

Browse files
committed
Support YAML output from SwaggerCommand with --yaml
1 parent 34151e8 commit c8b45fa

File tree

2 files changed

+44
-6
lines changed

2 files changed

+44
-6
lines changed

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
1818
use Symfony\Component\Console\Command\Command;
1919
use Symfony\Component\Console\Input\InputInterface;
20+
use Symfony\Component\Console\Input\InputOption;
2021
use Symfony\Component\Console\Output\OutputInterface;
2122
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
23+
use Symfony\Component\Yaml\Yaml;
2224

2325
/**
2426
* Console command to dump Swagger API documentations.
@@ -53,7 +55,8 @@ protected function configure()
5355
{
5456
$this
5557
->setName('api:swagger:export')
56-
->setDescription('Dump the Swagger 2.0 (OpenAPI) documentation');
58+
->setDescription('Dump the Swagger 2.0 (OpenAPI) documentation')
59+
->addOption('yaml', 'y', InputOption::VALUE_NONE, 'Dump the documentation in YAML');
5760
}
5861

5962
/**
@@ -63,7 +66,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
6366
{
6467
$documentation = new Documentation($this->resourceNameCollectionFactory->create(), $this->apiTitle, $this->apiDescription, $this->apiVersion, $this->apiFormats);
6568
$data = $this->documentationNormalizer->normalize($documentation);
66-
$content = json_encode($data, JSON_PRETTY_PRINT);
69+
if ($input->getOption('yaml')) {
70+
$content = Yaml::dump($data);
71+
} else {
72+
$content = json_encode($data, JSON_PRETTY_PRINT);
73+
}
6774
$output->writeln($content);
6875
}
6976
}

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

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,54 @@
1616
use Symfony\Bundle\FrameworkBundle\Console\Application;
1717
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
1818
use Symfony\Component\Console\Tester\ApplicationTester;
19+
use Symfony\Component\Yaml\Exception\ParseException;
20+
use Symfony\Component\Yaml\Yaml;
1921

2022
/**
2123
* @author Amrouche Hamza <[email protected]>
2224
*/
2325
class SwaggerCommandTest extends KernelTestCase
2426
{
25-
public function testExecute()
27+
/**
28+
* @var ApplicationTester
29+
*/
30+
private $tester;
31+
32+
protected function setUp()
2633
{
2734
self::bootKernel();
2835

2936
$application = new Application(static::$kernel);
3037
$application->setCatchExceptions(false);
3138
$application->setAutoExit(false);
3239

33-
$tester = new ApplicationTester($application);
34-
$tester->run(['command' => 'api:swagger:export']);
40+
$this->tester = new ApplicationTester($application);
41+
}
3542

36-
$this->assertJson($tester->getDisplay());
43+
public function testExecute()
44+
{
45+
$this->tester->run(['command' => 'api:swagger:export']);
46+
47+
$this->assertJson($this->tester->getDisplay());
48+
}
49+
50+
public function testExecuteWithYaml()
51+
{
52+
$this->tester->run(['command' => 'api:swagger:export', '--yaml' => true]);
53+
54+
$this->assertYaml($this->tester->getDisplay());
55+
}
56+
57+
/**
58+
* @param string $data
59+
*/
60+
private function assertYaml($data)
61+
{
62+
try {
63+
Yaml::parse($data);
64+
} catch (ParseException $exception) {
65+
$this->fail('Is not valid YAML: '.$exception->getMessage());
66+
}
67+
$this->addToAssertionCount(1);
3768
}
3869
}

0 commit comments

Comments
 (0)