Skip to content

Allow setting url variables from config #48

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
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'),

/*
* Server URL variables for the swagger ui.
*/
'server_variables' => [],

/*
* The oauth configuration for the swagger file.
*/
Expand Down
5 changes: 4 additions & 1 deletion src/Http/Controllers/OpenApiJsonController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
21 changes: 21 additions & 0 deletions tests/OpenApiRouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down