From 268a589f730d9168b25920b3dd3c6c51aaaa1255 Mon Sep 17 00:00:00 2001 From: kkrazynski-szczypala Date: Wed, 14 May 2025 18:53:34 +0200 Subject: [PATCH] Allow setting url variables from config --- README.md | 16 ++++++++++++++ config/swagger-ui.php | 5 +++++ .../Controllers/OpenApiJsonController.php | 5 ++++- tests/OpenApiRouteTest.php | 21 +++++++++++++++++++ 4 files changed, 46 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 0c4b7f1..4ea72fa 100644 --- a/README.md +++ b/README.md @@ -76,6 +76,22 @@ return [ ]; ``` +You can also set what variables that should be customizable for the server url in Swagger UI. Variable must be present with the same key (case-sensitive) in the url surrounded by curly brackets. + +```php +// in config/swagger-ui.php + +return [ + // ... + + 'server_url' => 'http://foo.bar/{Language}/api', + + 'server_variables' => ['Language' => ['default' => 'en']], + + // ... +] +``` + You can also set an oauth client ID and client secret. These values will be automatically prefilled in the authentication view in Swagger UI. ```php diff --git a/config/swagger-ui.php b/config/swagger-ui.php index acbe26b..ff702ec 100644 --- a/config/swagger-ui.php +++ b/config/swagger-ui.php @@ -50,6 +50,11 @@ */ 'server_url' => env('APP_URL'), + /* + * Server URL variables for the swagger ui. + */ + 'server_variables' => [], + /* * The oauth configuration for the swagger file. */ diff --git a/src/Http/Controllers/OpenApiJsonController.php b/src/Http/Controllers/OpenApiJsonController.php index 0dbdddc..0841d03 100644 --- a/src/Http/Controllers/OpenApiJsonController.php +++ b/src/Http/Controllers/OpenApiJsonController.php @@ -57,7 +57,10 @@ protected function configureServer(array $file, array $json) : array } $json['servers'] = [ - ['url' => $file['server_url'] ?? config('app.url')], + [ + 'url' => $file['server_url'] ?? config('app.url'), + 'variables' => $file['server_variables'] ?? [], + ], ]; return $json; diff --git a/tests/OpenApiRouteTest.php b/tests/OpenApiRouteTest.php index 62ef8ae..8728d5d 100644 --- a/tests/OpenApiRouteTest.php +++ b/tests/OpenApiRouteTest.php @@ -66,6 +66,27 @@ public function it_uses_a_custom_server_url_if_defined_in_config($openApiFile) ->assertJsonPath('servers.0.url', 'http://foo.bar/api'); } + /** + * @test + * + * @dataProvider openApiFileProvider + */ + public function is_uses_custom_variables_if_defined_in_config($openApiFile) + { + config()->set('app.url', 'http://foo.bar/{Language}'); + config()->set('swagger-ui.server_variables', ['Language' => ['default' => 'en']]); + 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_variables', ['Language' => ['default' => 'en']]); + + $this->get('swagger/v1') + ->assertStatus(200) + ->assertJsonCount(1, 'servers') + ->assertJsonPath('servers.0.url', 'http://foo.bar/{Language}') + ->assertJsonCount(1, 'servers.0.variables') + ->assertJsonPath('servers.0.variables.Language.default', 'en'); + } + /** * @test *