Provide default host to generated .json documentation (#1492)

* Add default host to the current request host to keep the same behaviour as js does

* Add host to test case

* Revert deleted code

* Revert not needed update on expected result
This commit is contained in:
Ayrton Ricardo 2019-04-10 20:55:04 +02:00 committed by Guilhem N
parent 0bd54ea99a
commit 253e71a46e
2 changed files with 7 additions and 1 deletions

View File

@ -48,10 +48,15 @@ final class DocumentationController
} }
$spec = $this->generatorLocator->get($area)->generate()->toArray(); $spec = $this->generatorLocator->get($area)->generate()->toArray();
if ('' !== $request->getBaseUrl()) { if ('' !== $request->getBaseUrl()) {
$spec['basePath'] = $request->getBaseUrl(); $spec['basePath'] = $request->getBaseUrl();
} }
if (empty($spec['host'])) {
$spec['host'] = $request->getHost();
}
return new JsonResponse($spec); return new JsonResponse($spec);
} }
} }

View File

@ -58,7 +58,7 @@ class SwaggerUiTest extends WebTestCase
public function testJsonDocs() public function testJsonDocs()
{ {
$client = self::createClient(); $client = self::createClient();
$crawler = $client->request('GET', '/app_dev.php/docs.json'); $client->request('GET', '/app_dev.php/docs.json');
$response = $client->getResponse(); $response = $client->getResponse();
$this->assertEquals(200, $response->getStatusCode()); $this->assertEquals(200, $response->getStatusCode());
@ -66,6 +66,7 @@ class SwaggerUiTest extends WebTestCase
$expected = $this->getSwaggerDefinition()->toArray(); $expected = $this->getSwaggerDefinition()->toArray();
$expected['basePath'] = '/app_dev.php'; $expected['basePath'] = '/app_dev.php';
$expected['host'] = 'api.example.com';
$this->assertEquals($expected, json_decode($response->getContent(), true)); $this->assertEquals($expected, json_decode($response->getContent(), true));
} }