Skip to content

Support YAML output from SwaggerCommand with --yaml #1695

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,6 @@ script:
- if [[ $APP_ENV != 'postgres' ]]; then vendor/bin/behat --format=progress; fi
- if [[ $APP_ENV = 'postgres' ]]; then vendor/bin/behat --tags='~@sqlite' --format=progress; fi
- tests/Fixtures/app/console api:swagger:export > swagger.json && swagger-cli validate swagger.json && rm swagger.json
- tests/Fixtures/app/console api:swagger:export --yaml > swagger.yaml && swagger-cli validate --no-schema swagger.yaml && rm swagger.yaml
- if [[ $lint = 1 ]]; then php php-cs-fixer.phar fix --dry-run --diff --no-ansi; fi
- if [[ $lint = 1 ]]; then phpstan analyse -c phpstan.neon -l5 --ansi src tests; fi
7 changes: 5 additions & 2 deletions src/Bridge/Symfony/Bundle/Command/SwaggerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Yaml\Yaml;

/**
* Console command to dump Swagger API documentations.
Expand Down Expand Up @@ -53,7 +55,8 @@ protected function configure()
{
$this
->setName('api:swagger:export')
->setDescription('Dump the Swagger 2.0 (OpenAPI) documentation');
->setDescription('Dump the Swagger 2.0 (OpenAPI) documentation')
->addOption('yaml', 'y', InputOption::VALUE_NONE, 'Dump the documentation in YAML');
}

/**
Expand All @@ -63,7 +66,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
$documentation = new Documentation($this->resourceNameCollectionFactory->create(), $this->apiTitle, $this->apiDescription, $this->apiVersion, $this->apiFormats);
$data = $this->documentationNormalizer->normalize($documentation);
$content = json_encode($data, JSON_PRETTY_PRINT);
$content = $input->getOption('yaml') ? Yaml::dump($data) : json_encode($data, JSON_PRETTY_PRINT);
$output->writeln($content);
}
}
39 changes: 35 additions & 4 deletions tests/Bridge/Symfony/Bundle/Command/SwaggerCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,54 @@
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Console\Tester\ApplicationTester;
use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Yaml;

/**
* @author Amrouche Hamza <[email protected]>
*/
class SwaggerCommandTest extends KernelTestCase
{
public function testExecute()
/**
* @var ApplicationTester
*/
private $tester;

protected function setUp()
{
self::bootKernel();

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

$tester = new ApplicationTester($application);
$tester->run(['command' => 'api:swagger:export']);
$this->tester = new ApplicationTester($application);
}

$this->assertJson($tester->getDisplay());
public function testExecute()
{
$this->tester->run(['command' => 'api:swagger:export']);

$this->assertJson($this->tester->getDisplay());
}

public function testExecuteWithYaml()
{
$this->tester->run(['command' => 'api:swagger:export', '--yaml' => true]);

$this->assertYaml($this->tester->getDisplay());
}

/**
* @param string $data
*/
private function assertYaml($data)
{
try {
Yaml::parse($data);
} catch (ParseException $exception) {
$this->fail('Is not valid YAML: '.$exception->getMessage());
}
$this->addToAssertionCount(1);
}
}