Skip to content

Commit

Permalink
Merge pull request #1035 from nelmio/basepath
Browse files Browse the repository at this point in the history
Use request base url
  • Loading branch information
GuilhemN authored Jul 25, 2017
2 parents ad40790 + 0b05a23 commit bb14c85
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 21 deletions.
10 changes: 8 additions & 2 deletions Controller/DocumentationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Nelmio\ApiDocBundle\ApiDocGenerator;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;

final class DocumentationController
{
Expand All @@ -23,8 +24,13 @@ public function __construct(ApiDocGenerator $apiDocGenerator)
$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);
}
}
10 changes: 8 additions & 2 deletions Controller/SwaggerUiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -25,10 +26,15 @@ public function __construct(ApiDocGenerator $apiDocGenerator, \Twig_Environment
$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']
);
Expand Down
31 changes: 19 additions & 12 deletions Describer/ApiPlatformDescriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,32 @@

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) {
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);
// 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;
unset($documentation['basePath']);

foreach ($documentation['paths'] as $path => $value) {
$paths['/'.ltrim($basePath.'/'.ltrim($path, '/'), '/')] = $value;
}

$documentation['paths'] = $paths;

This comment has been minimized.

Copy link
@kobyzskyi

kobyzskyi Jul 27, 2017

undefined variable $path


return $documentation;
}, $overwrite);
});
}
}
1 change: 1 addition & 0 deletions Resources/config/api_platform.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
20 changes: 15 additions & 5 deletions Tests/Functional/SwaggerUiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,38 @@

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));
}
}

0 comments on commit bb14c85

Please sign in to comment.