mirror of
https://github.com/retailcrm/NelmioApiDocBundle.git
synced 2025-02-15 14:03:15 +03:00
parent
306aba97a4
commit
46ef005787
@ -18,7 +18,7 @@ use Symfony\Component\HttpFoundation\JsonResponse;
|
|||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
||||||
|
|
||||||
final class DocumentationController
|
final class JsonDocumentationController
|
||||||
{
|
{
|
||||||
private $generatorLocator;
|
private $generatorLocator;
|
||||||
|
|
48
Controller/YamlDocumentationController.php
Normal file
48
Controller/YamlDocumentationController.php
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
<?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\Controller;
|
||||||
|
|
||||||
|
use OpenApi\Annotations\OpenApi;
|
||||||
|
use OpenApi\Annotations\Server;
|
||||||
|
use Psr\Container\ContainerInterface;
|
||||||
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
||||||
|
use Symfony\Component\Yaml\Yaml;
|
||||||
|
|
||||||
|
final class YamlDocumentationController
|
||||||
|
{
|
||||||
|
private $generatorLocator;
|
||||||
|
|
||||||
|
public function __construct(ContainerInterface $generatorLocator)
|
||||||
|
{
|
||||||
|
$this->generatorLocator = $generatorLocator;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function __invoke(Request $request, $area = 'default')
|
||||||
|
{
|
||||||
|
if (!$this->generatorLocator->has($area)) {
|
||||||
|
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'
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
@ -16,7 +16,13 @@
|
|||||||
<argument type="service" id="twig" />
|
<argument type="service" id="twig" />
|
||||||
</service>
|
</service>
|
||||||
|
|
||||||
<service id="nelmio_api_doc.controller.swagger" class="Nelmio\ApiDocBundle\Controller\DocumentationController" public="true">
|
<service id="nelmio_api_doc.controller.swagger" alias="nelmio_api_doc.controller.swagger_json" public="true" />
|
||||||
|
|
||||||
|
<service id="nelmio_api_doc.controller.swagger_json" class="Nelmio\ApiDocBundle\Controller\JsonDocumentationController" public="true">
|
||||||
|
<argument type="service" id="nelmio_api_doc.generator_locator" />
|
||||||
|
</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.generator_locator" />
|
||||||
</service>
|
</service>
|
||||||
|
|
||||||
|
@ -11,7 +11,9 @@
|
|||||||
|
|
||||||
namespace Nelmio\ApiDocBundle\Tests\Functional;
|
namespace Nelmio\ApiDocBundle\Tests\Functional;
|
||||||
|
|
||||||
|
use OpenApi\Annotations\Server;
|
||||||
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
|
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
|
||||||
|
use Symfony\Component\Yaml\Yaml;
|
||||||
|
|
||||||
class SwaggerUiTest extends WebTestCase
|
class SwaggerUiTest extends WebTestCase
|
||||||
{
|
{
|
||||||
@ -72,4 +74,19 @@ class SwaggerUiTest extends WebTestCase
|
|||||||
|
|
||||||
$this->assertEquals($expected, json_decode($response->getContent(), true));
|
$this->assertEquals($expected, json_decode($response->getContent(), true));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testYamlDocs()
|
||||||
|
{
|
||||||
|
$this->client->request('GET', '/app_dev.php/docs.yaml');
|
||||||
|
|
||||||
|
$response = $this->client->getResponse();
|
||||||
|
$this->assertEquals(200, $response->getStatusCode());
|
||||||
|
$this->assertEquals('text/x-yaml; charset=UTF-8', $response->headers->get('Content-Type'));
|
||||||
|
|
||||||
|
$spec = $this->getOpenApiDefinition();
|
||||||
|
$spec->servers = [new Server(['url' => 'http://api.example.com/app_dev.php'])];
|
||||||
|
$expected = $spec->toYaml();
|
||||||
|
|
||||||
|
$this->assertEquals($expected, $response->getContent());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -87,7 +87,8 @@ class TestKernel extends Kernel
|
|||||||
$routes->import(__DIR__.'/Controller/InvokableController.php', '/', 'annotation');
|
$routes->import(__DIR__.'/Controller/InvokableController.php', '/', 'annotation');
|
||||||
$routes->import('', '/api', 'api_platform');
|
$routes->import('', '/api', 'api_platform');
|
||||||
$routes->add('/docs/{area}', 'nelmio_api_doc.controller.swagger_ui')->setDefault('area', 'default');
|
$routes->add('/docs/{area}', 'nelmio_api_doc.controller.swagger_ui')->setDefault('area', 'default');
|
||||||
$routes->add('/docs.json', 'nelmio_api_doc.controller.swagger');
|
$routes->add('/docs.json', 'nelmio_api_doc.controller.swagger_json');
|
||||||
|
$routes->add('/docs.yaml', 'nelmio_api_doc.controller.swagger_yaml');
|
||||||
$routes->import(__DIR__.'/Controller/FOSRestController.php', '/', 'annotation');
|
$routes->import(__DIR__.'/Controller/FOSRestController.php', '/', 'annotation');
|
||||||
|
|
||||||
if (class_exists(SerializedName::class)) {
|
if (class_exists(SerializedName::class)) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user