Skip to content

Commit

Permalink
Add option to append version to the end of server url
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelhgf committed Jan 7, 2025
1 parent d220610 commit eb79b0a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
5 changes: 5 additions & 0 deletions config/swagger-ui.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@
*/
'server_url' => env('APP_URL'),

/*
* Append the version to the end of server url.
*/
'append_version' => false,

/*
* The oauth configuration for the swagger file.
*/
Expand Down
12 changes: 11 additions & 1 deletion src/Http/Controllers/OpenApiJsonController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@

class OpenApiJsonController
{
private string $versionPath;

public function __invoke(Request $request, string $filename) : JsonResponse
{
$this->versionPath = $filename;

$path = implode('/', array_slice($request->segments(), 0, -1));

try {
Expand Down Expand Up @@ -56,8 +60,14 @@ protected function configureServer(array $file, array $json) : array
return $json;
}

$url = $file['server_url'] ?? config('app.url');

if ($file['append_version']) {
$url = Str::finish($url, '/') . ltrim($this->versionPath, '/');
}

$json['servers'] = [
['url' => $file['server_url'] ?? config('app.url')],
['url' => $url],
];

return $json;
Expand Down
19 changes: 19 additions & 0 deletions tests/OpenApiRouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,25 @@ public function it_sets_server_to_current_app_url_if_modify_file_is_enabled($ope
->assertJsonPath('servers.0.url', 'http://foo.bar');
}

/**
* @test
*
* @dataProvider openApiFileProvider
*/
public function it_appends_the_version_to_the_end_of_server_url_if_append_version_is_enabled($openApiFile)
{
config()->set('swagger-ui.files.0.versions', ['v1' => $openApiFile]);
config()->set('swagger-ui.files.0.modify_file', true);
config()->set('swagger-ui.files.0.append_version', true);
config()->set('swagger-ui.files.0.server_url', null);
config()->set('app.url', 'http://foo.bar');

$this->get('swagger/v1')
->assertStatus(200)
->assertJsonCount(1, 'servers')
->assertJsonPath('servers.0.url', 'http://foo.bar/v1');
}

/**
* @test
*
Expand Down

0 comments on commit eb79b0a

Please sign in to comment.