From b8b166e6774aa4f91850a60a289dc0af0d88c245 Mon Sep 17 00:00:00 2001 From: Blake Bengtson Date: Thu, 21 Nov 2024 12:51:25 -0600 Subject: [PATCH 1/3] Added server_url config setting Updated configureServer() to check for this setting first before falling back on config('app.url'). Added test to verify this works as expected. --- config/swagger-ui.php | 5 +++++ src/Http/Controllers/OpenApiJsonController.php | 2 +- tests/OpenApiRouteTest.php | 18 ++++++++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/config/swagger-ui.php b/config/swagger-ui.php index 60ca75e..cb277ba 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. Defaults to the app URL. + */ + 'server_url' => config('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..e07f69c 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' => config('swagger-ui.server_url', config('app.url'))], ]; return $json; diff --git a/tests/OpenApiRouteTest.php b/tests/OpenApiRouteTest.php index 7e45ee3..c022c8c 100644 --- a/tests/OpenApiRouteTest.php +++ b/tests/OpenApiRouteTest.php @@ -47,6 +47,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.server_url', 'http://foo.bar/api'); + + $this->get('swagger/v1') + ->assertStatus(200) + ->assertJsonCount(1, 'servers') + ->assertJsonPath('servers.0.url', 'http://foo.bar/api'); + } + /** * @test * From d7f3c517a46167bc9cfe47098cfd2049e8ee7769 Mon Sep 17 00:00:00 2001 From: Blake Bengtson Date: Wed, 27 Nov 2024 16:02:02 -0600 Subject: [PATCH 2/3] Fixed bug - server_url should work now Wasn't taking into account the 'files' array in the config. server_url was being set in the root, which didn't work. Updated tests as well. --- src/Http/Controllers/OpenApiJsonController.php | 2 +- tests/OpenApiRouteTest.php | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Http/Controllers/OpenApiJsonController.php b/src/Http/Controllers/OpenApiJsonController.php index e07f69c..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('swagger-ui.server_url', config('app.url'))], + ['url' => $file['server_url'] ?? config('app.url')], ]; return $json; diff --git a/tests/OpenApiRouteTest.php b/tests/OpenApiRouteTest.php index c022c8c..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') @@ -57,7 +58,7 @@ 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.server_url', 'http://foo.bar/api'); + config()->set('swagger-ui.files.0.server_url', 'http://foo.bar/api'); $this->get('swagger/v1') ->assertStatus(200) From 6282dab1685d6f4619c873ea96921b5abaea3ef6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gu=CC=88nther=20Debrauwer?= Date: Thu, 12 Dec 2024 20:16:08 +0100 Subject: [PATCH 3/3] Change config --- config/swagger-ui.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config/swagger-ui.php b/config/swagger-ui.php index cb277ba..acbe26b 100644 --- a/config/swagger-ui.php +++ b/config/swagger-ui.php @@ -46,9 +46,9 @@ 'modify_file' => true, /* - * The server URL configuration for the swagger file. Defaults to the app URL. + * The server URL configuration for the swagger file. */ - 'server_url' => config('app.url'), + 'server_url' => env('APP_URL'), /* * The oauth configuration for the swagger file.