Enable dumping docs to yaml

This commit is contained in:
Zdeněk Drahoš 2021-07-29 11:57:00 +02:00
parent 2c890ff93b
commit 1b9be28ad6
7 changed files with 72 additions and 21 deletions

View File

@ -48,7 +48,7 @@ class DumpCommand extends Command
{
$availableFormats = $this->renderOpenApi->getAvailableFormats();
$this
->setDescription('Dumps documentation in OpenAPI JSON format or HTML')
->setDescription('Dumps documentation in OpenAPI format to: '.implode(', ', $availableFormats))
->addOption('area', '', InputOption::VALUE_OPTIONAL, '', 'default')
->addOption(
'format',

View File

@ -11,37 +11,38 @@
namespace Nelmio\ApiDocBundle\Controller;
use OpenApi\Annotations\OpenApi;
use OpenApi\Annotations\Server;
use Psr\Container\ContainerInterface;
use InvalidArgumentException;
use Nelmio\ApiDocBundle\Render\RenderOpenApi;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
final class YamlDocumentationController
{
private $generatorLocator;
/**
* @var RenderOpenApi
*/
private $renderOpenApi;
public function __construct(ContainerInterface $generatorLocator)
public function __construct(RenderOpenApi $renderOpenApi)
{
$this->generatorLocator = $generatorLocator;
$this->renderOpenApi = $renderOpenApi;
}
public function __invoke(Request $request, $area = 'default')
{
if (!$this->generatorLocator->has($area)) {
try {
$response = new Response(
$this->renderOpenApi->render(RenderOpenApi::YAML, $area, [
'server_url' => '' !== $request->getBaseUrl() ? $request->getSchemeAndHttpHost().$request->getBaseUrl() : null,
]),
Response::HTTP_OK,
['Content-Type' => 'text/x-yaml']
);
return $response->setCharset('UTF-8');
} catch (InvalidArgumentException $e) {
throw new BadRequestHttpException(sprintf('Area "%s" is not supported as it isn\'t defined in config.', $area));
}
/** @var OpenApi $spec */
$spec = $this->generatorLocator->get($area)->generate();
if ('' !== $request->getBaseUrl()) {
$spec->servers = [new Server(['url' => $request->getSchemeAndHttpHost().$request->getBaseUrl()])];
}
return new Response($spec->toYaml(), 200, [
'Content-Type' => 'text/x-yaml',
]);
}
}

View File

@ -19,6 +19,7 @@ class RenderOpenApi
{
public const HTML = 'html';
public const JSON = 'json';
public const YAML = 'yaml';
/** @var ContainerInterface */
private $generatorLocator;

View File

@ -0,0 +1,38 @@
<?php
/*
* This file is part of the NelmioApiDocBundle package.
*
* (c) Nelmio
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Nelmio\ApiDocBundle\Render\Yaml;
use Nelmio\ApiDocBundle\Render\OpenApiRenderer;
use Nelmio\ApiDocBundle\Render\RenderOpenApi;
use OpenApi\Annotations\OpenApi;
use OpenApi\Annotations\Server;
class YamlOpenApiRenderer implements OpenApiRenderer
{
public function getFormat(): string
{
return RenderOpenApi::YAML;
}
public function render(OpenApi $spec, array $options = []): string
{
$options += [
'server_url' => null,
];
if ($options['server_url']) {
$spec->servers = [new Server(['url' => $options['server_url']])];
}
return $spec->toYaml();
}
}

View File

@ -22,7 +22,7 @@
</service>
<service id="nelmio_api_doc.controller.swagger_yaml" class="Nelmio\ApiDocBundle\Controller\YamlDocumentationController" public="true">
<argument type="service" id="nelmio_api_doc.generator_locator" />
<argument type="service" id="nelmio_api_doc.render_docs" />
</service>
<!-- Render -->
@ -30,6 +30,7 @@
<argument type="service" id="nelmio_api_doc.generator_locator" />
<argument type="service" id="nelmio_api_doc.render_docs.html" />
<argument type="service" id="nelmio_api_doc.render_docs.json" />
<argument type="service" id="nelmio_api_doc.render_docs.yaml" />
</service>
<service id="nelmio_api_doc.render_docs.html" class="Nelmio\ApiDocBundle\Render\Html\HtmlOpenApiRenderer" public="false">
<argument type="service" id="twig" />
@ -40,6 +41,8 @@
</service>
<service id="nelmio_api_doc.render_docs.json" class="Nelmio\ApiDocBundle\Render\Json\JsonOpenApiRenderer" public="false">
</service>
<service id="nelmio_api_doc.render_docs.yaml" class="Nelmio\ApiDocBundle\Render\Yaml\YamlOpenApiRenderer" public="false">
</service>
<!-- Swagger Spec Generator -->
<service id="nelmio_api_doc.generator" alias="nelmio_api_doc.generator.default" public="true" />

View File

@ -1,7 +1,7 @@
Commands
========
A command is provided in order to dump the documentation in ``json`` or ``html``.
A command is provided in order to dump the documentation in ``json``, ``yaml`` or ``html``.
.. code-block:: bash

View File

@ -38,6 +38,14 @@ class DumpCommandTest extends WebTestCase
];
}
public function testYaml()
{
$output = $this->executeDumpCommand([
'--format' => 'yaml',
]);
self::assertStringContainsString($this->getOpenApiDefinition()->toYaml(), $output);
}
/** @dataProvider provideAssetsMode */
public function testHtml($htmlConfig, string $expectedHtml)
{