diff --git a/config/swagger-ui.php b/config/swagger-ui.php index 60ca75e..acbe26b 100644 --- a/config/swagger-ui.php +++ b/config/swagger-ui.php @@ -45,6 +45,11 @@ */ 'modify_file' => true, + /* + * The server URL configuration for the swagger file. + */ + 'server_url' => env('APP_URL'), + /* * The oauth configuration for the swagger file. */ diff --git a/src/Http/Controllers/OpenApiJsonController.php b/src/Http/Controllers/OpenApiJsonController.php index 5f68229..0dbdddc 100644 --- a/src/Http/Controllers/OpenApiJsonController.php +++ b/src/Http/Controllers/OpenApiJsonController.php @@ -57,7 +57,7 @@ protected function configureServer(array $file, array $json) : array } $json['servers'] = [ - ['url' => config('app.url')], + ['url' => $file['server_url'] ?? config('app.url')], ]; return $json; diff --git a/tests/OpenApiRouteTest.php b/tests/OpenApiRouteTest.php index 7e45ee3..62ef8ae 100644 --- a/tests/OpenApiRouteTest.php +++ b/tests/OpenApiRouteTest.php @@ -39,6 +39,7 @@ public function it_sets_server_to_current_app_url_if_modify_file_is_enabled($ope { config()->set('swagger-ui.files.0.versions', ['v1' => $openApiFile]); config()->set('swagger-ui.files.0.modify_file', true); + config()->set('swagger-ui.files.0.server_url', null); config()->set('app.url', 'http://foo.bar'); $this->get('swagger/v1') @@ -47,6 +48,24 @@ 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_uses_a_custom_server_url_if_defined_in_config($openApiFile) + { + config()->set('app.url', 'http://foo.bar'); + config()->set('swagger-ui.files.0.versions', ['v1' => $openApiFile]); + config()->set('swagger-ui.files.0.modify_file', true); + config()->set('swagger-ui.files.0.server_url', 'http://foo.bar/api'); + + $this->get('swagger/v1') + ->assertStatus(200) + ->assertJsonCount(1, 'servers') + ->assertJsonPath('servers.0.url', 'http://foo.bar/api'); + } + /** * @test *