diff --git a/Command/DumpCommand.php b/Command/DumpCommand.php
index 481c682..47f80be 100644
--- a/Command/DumpCommand.php
+++ b/Command/DumpCommand.php
@@ -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',
diff --git a/Controller/YamlDocumentationController.php b/Controller/YamlDocumentationController.php
index 7557fc8..ef9bc36 100644
--- a/Controller/YamlDocumentationController.php
+++ b/Controller/YamlDocumentationController.php
@@ -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',
- ]);
}
}
diff --git a/Render/RenderOpenApi.php b/Render/RenderOpenApi.php
index 1407b67..8076d49 100644
--- a/Render/RenderOpenApi.php
+++ b/Render/RenderOpenApi.php
@@ -19,6 +19,7 @@ class RenderOpenApi
{
public const HTML = 'html';
public const JSON = 'json';
+ public const YAML = 'yaml';
/** @var ContainerInterface */
private $generatorLocator;
diff --git a/Render/Yaml/YamlOpenApiRenderer.php b/Render/Yaml/YamlOpenApiRenderer.php
new file mode 100644
index 0000000..025216c
--- /dev/null
+++ b/Render/Yaml/YamlOpenApiRenderer.php
@@ -0,0 +1,38 @@
+ null,
+ ];
+
+ if ($options['server_url']) {
+ $spec->servers = [new Server(['url' => $options['server_url']])];
+ }
+
+ return $spec->toYaml();
+ }
+}
diff --git a/Resources/config/services.xml b/Resources/config/services.xml
index d125a10..d452034 100644
--- a/Resources/config/services.xml
+++ b/Resources/config/services.xml
@@ -22,7 +22,7 @@
-
+
@@ -30,6 +30,7 @@
+
@@ -40,6 +41,8 @@
+
+
diff --git a/Resources/doc/commands.rst b/Resources/doc/commands.rst
index fe4df38..93bb6e1 100644
--- a/Resources/doc/commands.rst
+++ b/Resources/doc/commands.rst
@@ -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
diff --git a/Tests/Command/DumpCommandTest.php b/Tests/Command/DumpCommandTest.php
index 23e6f3a..eb2bfbe 100644
--- a/Tests/Command/DumpCommandTest.php
+++ b/Tests/Command/DumpCommandTest.php
@@ -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)
{