Enable overriding server url for yaml and json export from console

This commit is contained in:
Zdeněk Drahoš 2021-07-29 12:22:54 +02:00
parent 181f4b2763
commit 1a21f1855e
3 changed files with 19 additions and 3 deletions

View File

@ -31,7 +31,6 @@ class DumpCommand extends Command
private $defaultHtmlConfig = [ private $defaultHtmlConfig = [
'assets_mode' => AssetsMode::CDN, 'assets_mode' => AssetsMode::CDN,
'swagger_ui_config' => [], 'swagger_ui_config' => [],
'server_url' => null,
]; ];
public function __construct(RenderOpenApi $renderOpenApi) public function __construct(RenderOpenApi $renderOpenApi)
@ -57,8 +56,9 @@ class DumpCommand extends Command
'Output format like: '.implode(', ', $availableFormats), 'Output format like: '.implode(', ', $availableFormats),
RenderOpenApi::JSON RenderOpenApi::JSON
) )
->addOption('server-url', '', InputOption::VALUE_REQUIRED, 'URL where live api doc is served')
->addOption('html-config', '', InputOption::VALUE_REQUIRED, '', json_encode($this->defaultHtmlConfig)) ->addOption('html-config', '', InputOption::VALUE_REQUIRED, '', json_encode($this->defaultHtmlConfig))
->addOption('no-pretty', '', InputOption::VALUE_NONE, 'Do not pretty format output') ->addOption('no-pretty', '', InputOption::VALUE_NONE, 'Do not pretty format JSON output')
; ;
} }
@ -80,6 +80,10 @@ class DumpCommand extends Command
]; ];
} }
if ($input->getOption('server-url')) {
$options['server_url'] = $input->getOption('server-url');
}
$docs = $this->renderOpenApi->render($format, $area, $options); $docs = $this->renderOpenApi->render($format, $area, $options);
$output->writeln($docs, OutputInterface::OUTPUT_RAW); $output->writeln($docs, OutputInterface::OUTPUT_RAW);

View File

@ -17,6 +17,12 @@ without whitespace, use the ``--no-pretty`` option.
$ php app/console api:doc:dump --format=json > json-pretty-formatted.json $ php app/console api:doc:dump --format=json > json-pretty-formatted.json
$ php app/console api:doc:dump --format=json --no-pretty > json-no-pretty.json $ php app/console api:doc:dump --format=json --no-pretty > json-no-pretty.json
Every format can override API url. Useful if static documentation is not hosted on API url:
.. code-block:: bash
$ php app/console api:doc:dump --format=yaml --server-url "http://example.com/api" > api.yaml
For example to generate a static version of your documentation you can use: For example to generate a static version of your documentation you can use:
.. code-block:: bash .. code-block:: bash

View File

@ -42,8 +42,14 @@ class DumpCommandTest extends WebTestCase
{ {
$output = $this->executeDumpCommand([ $output = $this->executeDumpCommand([
'--format' => 'yaml', '--format' => 'yaml',
'--server-url' => 'http://example.com/api',
]); ]);
self::assertStringContainsString($this->getOpenApiDefinition()->toYaml(), $output); $expectedYaml = <<<YAML
servers:
-
url: 'http://example.com/api'
YAML;
self::assertStringContainsString($expectedYaml, $output);
} }
/** @dataProvider provideAssetsMode */ /** @dataProvider provideAssetsMode */