Use request base url

This commit is contained in:
Guilhem Niot 2017-07-05 15:41:53 +02:00
parent 7690f6cfb5
commit 0b05a23625
5 changed files with 52 additions and 22 deletions

View File

@ -13,6 +13,7 @@ namespace Nelmio\ApiDocBundle\Controller;
use Nelmio\ApiDocBundle\ApiDocGenerator;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
final class DocumentationController
{
@ -23,8 +24,13 @@ final class DocumentationController
$this->apiDocGenerator = $apiDocGenerator;
}
public function __invoke()
public function __invoke(Request $request)
{
return new JsonResponse($this->apiDocGenerator->generate()->toArray());
$spec = $this->apiDocGenerator->generate()->toArray();
if ('' !== $request->getBaseUrl()) {
$spec['basePath'] = $request->getBaseUrl();
}
return new JsonResponse($spec);
}
}

View File

@ -12,6 +12,7 @@
namespace Nelmio\ApiDocBundle\Controller;
use Nelmio\ApiDocBundle\ApiDocGenerator;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
final class SwaggerUiController
@ -25,10 +26,15 @@ final class SwaggerUiController
$this->twig = $twig;
}
public function __invoke()
public function __invoke(Request $request)
{
$spec = $this->apiDocGenerator->generate()->toArray();
if ('' !== $request->getBaseUrl()) {
$spec['basePath'] = $request->getBaseUrl();
}
return new Response(
$this->twig->render('@NelmioApiDoc/SwaggerUi/index.html.twig', ['swagger_data' => ['spec' => $this->apiDocGenerator->generate()->toArray()]]),
$this->twig->render('@NelmioApiDoc/SwaggerUi/index.html.twig', ['swagger_data' => ['spec' => $spec]]),
Response::HTTP_OK,
['Content-Type' => 'text/html']
);

View File

@ -13,25 +13,32 @@ namespace Nelmio\ApiDocBundle\Describer;
use ApiPlatform\Core\Documentation\Documentation;
use ApiPlatform\Core\Swagger\Serializer\DocumentationNormalizer;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\RequestContext;
final class ApiPlatformDescriber extends ExternalDocDescriber
{
public function __construct(Documentation $documentation, DocumentationNormalizer $normalizer, bool $overwrite = false)
public function __construct(Documentation $documentation, DocumentationNormalizer $normalizer, UrlGeneratorInterface $urlGenerator)
{
parent::__construct(function () use ($documentation, $normalizer) {
$documentation = (array) $normalizer->normalize($documentation);
// Remove base path
if (isset($documentation['basePath'])) {
$paths = [];
foreach ($documentation['paths'] as $path => $value) {
$paths['/'.ltrim($documentation['basePath'].'/'.ltrim($path, '/'), '/')] = $value;
}
unset($documentation['basePath']);
$documentation['paths'] = $paths;
parent::__construct(function () use ($documentation, $normalizer, $urlGenerator) {
$baseContext = $urlGenerator->getContext();
$urlGenerator->setContext(new RequestContext());
try {
$basePath = $urlGenerator->generate('api_entrypoint');
} finally {
$urlGenerator->setContext($baseContext);
}
$documentation = (array) $normalizer->normalize($documentation);
unset($documentation['basePath']);
foreach ($documentation['paths'] as $path => $value) {
$paths['/'.ltrim($basePath.'/'.ltrim($path, '/'), '/')] = $value;
}
$documentation['paths'] = $paths;
return $documentation;
}, $overwrite);
});
}
}

View File

@ -7,6 +7,7 @@
<service id="nelmio_api_doc.describers.api_platform" class="Nelmio\ApiDocBundle\Describer\ApiPlatformDescriber" public="false">
<argument type="service" id="nelmio_api_doc.describers.api_platform.documentation" />
<argument type="service" id="api_platform.swagger.normalizer.documentation" />
<argument type="service" id="router" />
<tag name="nelmio_api_doc.describer" priority="-100" />
</service>

View File

@ -13,28 +13,38 @@ namespace Nelmio\ApiDocBundle\Tests\Functional;
class SwaggerUiTest extends WebTestCase
{
protected static function createClient(array $options = [], array $server = [])
{
return parent::createClient([], ['PHP_SELF' => '/app_dev.php/docs', 'SCRIPT_FILENAME' => '/var/www/app/web/app_dev.php']);
}
public function testSwaggerUi()
{
$client = self::createClient();
$crawler = $client->request('GET', '/docs/');
$crawler = $client->request('GET', '/app_dev.php/docs/');
$response = $client->getResponse();
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals('text/html; charset=UTF-8', $response->headers->get('Content-Type'));
$swaggerUiSpec = json_decode($crawler->filterXPath('//script[@id="swagger-data"]')->text(), true);
$this->assertEquals($this->getSwaggerDefinition()->toArray(), $swaggerUiSpec['spec']);
$expected = $this->getSwaggerDefinition()->toArray();
$expected['basePath'] = '/app_dev.php';
$this->assertEquals($expected, json_decode($crawler->filterXPath('//script[@id="swagger-data"]')->text(), true)['spec']);
}
public function testJsonDocs()
{
$client = self::createClient();
$crawler = $client->request('GET', '/docs.json');
$crawler = $client->request('GET', '/app_dev.php/docs.json');
$response = $client->getResponse();
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals('application/json', $response->headers->get('Content-Type'));
$this->assertEquals($this->getSwaggerDefinition()->toArray(), json_decode($response->getContent(), true));
$expected = $this->getSwaggerDefinition()->toArray();
$expected['basePath'] = '/app_dev.php';
$this->assertEquals($expected, json_decode($response->getContent(), true));
}
}