Skip to content

Commit 563222f

Browse files
committed
Added documentation, tests and some cleanup
1 parent 600cc44 commit 563222f

File tree

4 files changed

+84
-30
lines changed

4 files changed

+84
-30
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,13 @@ hb_swagger_ui:
129129
- "my_other_swagger_spec.json"
130130
```
131131

132+
Optional: If you want to have a default configuration in place for Swagger UI, place a `config.json` in the same directory as the swagger files, and add it to the configuration. It will be loaded automatically by appending it as the query parameter `configUrl`.
133+
134+
```yaml
135+
hb_swagger_ui:
136+
configFile: "config.json"
137+
```
138+
132139
Optional: If you serve your project from a different directory than the vhost root, you can overwrite the asset URL path with the `assetUrlPath` configuration. Be sure to add a leading and trailing slash.
133140

134141
```yaml

src/Controller/DocsController.php

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@
1313

1414
class DocsController extends AbstractController
1515
{
16-
/** @var string */
17-
private $projectDir;
18-
1916
/** @var array */
2017
private $swaggerFiles;
2118

@@ -25,12 +22,11 @@ class DocsController extends AbstractController
2522
/** @var string */
2623
private $assetUrlPath;
2724

28-
/** @var string */
25+
/** @var string|null */
2926
private $configFile;
3027

31-
public function __construct($projectDir, $swaggerFiles, $directory, $assetUrlPath, $configFile)
28+
public function __construct($swaggerFiles, $directory, $assetUrlPath, $configFile)
3229
{
33-
$this->projectDir = $projectDir;
3430
$this->swaggerFiles = $swaggerFiles;
3531
$this->directory = $directory;
3632
$this->assetUrlPath = $assetUrlPath;
@@ -44,15 +40,18 @@ public function __construct($projectDir, $swaggerFiles, $directory, $assetUrlPat
4440
*/
4541
public function indexAction(Request $request)
4642
{
47-
if (!$request->get('url') || (!$request->get('configUrl') && $this->configFile)) {
43+
if (!$request->get('url')) {
4844
// if there is no ?url=... parameter, redirect to the default one
49-
$specFile = reset($this->swaggerFiles);
45+
$defaultSwaggerFile = reset($this->swaggerFiles);
5046

51-
return $this->redirect($this->getRedirectUrlToSpec($specFile, $request->get('url')));
47+
return $this->redirect($this->getRedirectUrlToSpec($defaultSwaggerFile));
5248
}
5349
$contents = @file_get_contents(__DIR__ . '/../Resources/public/index.html');
5450
if ($contents === false) {
55-
throw new \RuntimeException('Unable to load [Resources/public/index.html]. Did [ScriptHandler::linkAssets] run correctly?');
51+
return new Response(
52+
'Unable to load [Resources/public/index.html]. Did [ScriptHandler::linkAssets] run correctly?',
53+
Response::HTTP_INTERNAL_SERVER_ERROR
54+
);
5655
}
5756

5857
return new Response($contents);
@@ -110,10 +109,12 @@ public function swaggerFileAction($fileName)
110109
*/
111110
private function getFilePath($fileName = '')
112111
{
113-
if ($fileName !== '' && !in_array($fileName, $this->swaggerFiles)) {
114-
throw new \RuntimeException(
115-
sprintf('File [%s] not defined under [hb_swagger_ui.files] in config.yml.', $fileName)
116-
);
112+
if ($this->configFile !== $fileName) {
113+
if ($fileName !== '' && !in_array($fileName, $this->swaggerFiles)) {
114+
throw new \RuntimeException(
115+
sprintf('File [%s] not defined under [hb_swagger_ui.files] in config.yml.', $fileName)
116+
);
117+
}
117118
}
118119

119120
if ($this->directory === '') {
@@ -135,27 +136,26 @@ private function getFilePath($fileName = '')
135136
*
136137
* @return string
137138
*/
138-
private function getRedirectUrlToSpec($fileName, $url = null)
139+
private function getRedirectUrlToSpec($fileName)
139140
{
140-
if ($url) {
141-
$specUrl = $url;
141+
if (strpos($fileName, '/') === 0 || preg_match('#http[s]?://#', $fileName)) {
142+
// if absolute path or URL use it raw
143+
$specUrl = $fileName;
142144
} else {
143-
if (strpos($fileName, '/') === 0 || preg_match('#http[s]?://#', $fileName)) {
144-
// if absolute path or URL use it raw
145-
$specUrl = $fileName;
146-
} else {
147-
$specUrl = $this->generateUrl(
148-
'hb_swagger_ui_swagger_file',
149-
['fileName' => $fileName],
150-
UrlGeneratorInterface::ABSOLUTE_PATH
151-
);
152-
}
145+
$specUrl = $this->generateUrl(
146+
'hb_swagger_ui_swagger_file',
147+
['fileName' => $fileName],
148+
UrlGeneratorInterface::ABSOLUTE_PATH
149+
);
153150
}
154151

155152
$parameters = ['url' => $specUrl];
156-
157153
if ($this->configFile) {
158-
$parameters['configUrl'] = $this->configFile;
154+
$parameters['configUrl'] = $this->generateUrl(
155+
'hb_swagger_ui_swagger_file',
156+
['fileName' => $this->configFile],
157+
UrlGeneratorInterface::ABSOLUTE_PATH
158+
);
159159
}
160160

161161
return $this->generateUrl('hb_swagger_ui_default', $parameters);

src/Resources/config/services.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ services:
55
public: true
66
tags: ['controller.service_arguments']
77
bind:
8-
$projectDir: '%kernel.project_dir%'
98
$swaggerFiles: '%hb_swagger_ui.files%'
109
$directory: '%hb_swagger_ui.directory%'
1110
$assetUrlPath: '%hb_swagger_ui.assetUrlPath%'

tests/Controller/DocsControllerTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace HarmBandstra\SwaggerUiBundle\Tests\Controller;
44

5+
use HarmBandstra\SwaggerUiBundle\Controller\DocsController;
56
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
67
use Symfony\Component\HttpFoundation\Response;
78
use Symfony\Component\Yaml\Yaml;
@@ -17,6 +18,26 @@ public function testIndex()
1718
$this->assertEquals(Response::HTTP_MOVED_PERMANENTLY, $response->getStatusCode());
1819
}
1920

21+
public function testIndexRedirectsProperly()
22+
{
23+
$client = static::createClient();
24+
$client->followRedirects(true);
25+
$client->request('GET', '/docs');
26+
27+
$uri = $client->getInternalRequest()->getUri();
28+
$parsedUrl = parse_url($uri);
29+
30+
$this->assertSame('/docs/', $parsedUrl['path']);
31+
$this->assertSame('url=/docs/file/petstore.json&configUrl=/docs/file/config.json', $parsedUrl['query']);
32+
33+
$response = $client->getResponse();
34+
$this->assertEquals(Response::HTTP_INTERNAL_SERVER_ERROR, $response->getStatusCode());
35+
$this->assertSame(
36+
'Unable to load [Resources/public/index.html]. Did [ScriptHandler::linkAssets] run correctly?',
37+
$response->getContent()
38+
);
39+
}
40+
2041
public function testIndexWithTrailingSlash()
2142
{
2243
$client = static::createClient();
@@ -94,4 +115,31 @@ public function testIfSwaggerFileReturnsErrorOnFileNotFound()
94115
$response->getContent()
95116
);
96117
}
118+
119+
public function testIfConfigFileCanBeLoaded()
120+
{
121+
$client = static::createClient();
122+
$client->request('GET', '/docs/file/config.json');
123+
124+
$response = $client->getResponse();
125+
$this->assertEquals(Response::HTTP_OK, $response->getStatusCode());
126+
}
127+
128+
public function testIfConfigFileReturnsFileNotFoundWhenNoConfigFileIsSet()
129+
{
130+
$docsController = new DocsController(
131+
["petstore.json", "petstore.yaml", "not_found.yaml"],
132+
__DIR__ . "../../tests/Resources/docs/",
133+
null,
134+
null
135+
);
136+
137+
$response = $docsController->swaggerFileAction('config.json');
138+
139+
$this->assertEquals(Response::HTTP_NOT_FOUND, $response->getStatusCode());
140+
$this->assertEquals(
141+
'"File [config.json] not defined under [hb_swagger_ui.files] in config.yml."',
142+
$response->getContent()
143+
);
144+
}
97145
}

0 commit comments

Comments
 (0)