Skip to content
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
14 changes: 14 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,24 @@
return [
'servers' => [
'v1' => [
/*
* Info section: title and version are required, everything else can be commented out if not needed.
* See https://swagger.io/specification/#info-object for more information.
*/
'info' => [
'title' => 'My JSON:API',
'description' => 'JSON:API built using Laravel',
'version' => '1.0.0',
'termsOfService' => 'https://example.com/terms-of-service',
'license' => [
'name' => 'MIT',
'url' => 'https://opensource.org/licenses/MIT',
],
'contact' => [
'name' => 'API Support',
'url' => 'https://www.example.com/support',
'email' => 'support@example.com',
],
],
],
],
Expand Down
38 changes: 26 additions & 12 deletions src/Descriptors/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,36 @@
namespace LaravelJsonApi\OpenApiSpec\Descriptors;

use GoldSpecDigital\ObjectOrientedOAS\Objects;
use Illuminate\Support\Arr;
use LaravelJsonApi\OpenApiSpec\Descriptors\Descriptor as BaseDescriptor;

class Server extends BaseDescriptor
{
/**
* @return \GoldSpecDigital\ObjectOrientedOAS\Objects\Info
*
* @todo Add contact
* @todo Add TOS
* @todo Add License
*/
public function info(): Objects\Info
{
return Objects\Info::create()
->title(config("openapi.servers.{$this->generator->key()}.info.title"))
->description(config("openapi.servers.{$this->generator->key()}.info.description"))
->version(config("openapi.servers.{$this->generator->key()}.info.version"));
->title(config("openapi.servers.{$this->generator->key()}.info.title"))
->description(config("openapi.servers.{$this->generator->key()}.info.description"))
->version(config("openapi.servers.{$this->generator->key()}.info.version"))
->termsOfService(config("openapi.servers.{$this->generator->key()}.info.termsOfService"))
->license(
Arr::has(config("openapi.servers.{$this->generator->key()}.info"), 'license')
? Objects\License::create()
->name(config("openapi.servers.{$this->generator->key()}.info.license.name"))
->url(config("openapi.servers.{$this->generator->key()}.info.license.url"))
: null
)
->contact(
Arr::has(config("openapi.servers.{$this->generator->key()}.info"), 'contact')
? Objects\Contact::create()
->name(config("openapi.servers.{$this->generator->key()}.info.contact.name"))
->email(config("openapi.servers.{$this->generator->key()}.info.contact.email"))
->url(config("openapi.servers.{$this->generator->key()}.info.contact.url"))
: null
);
}

/**
Expand All @@ -32,11 +45,12 @@ public function info(): Objects\Info
public function servers(): array
{
return [
Objects\Server::create()
->url('{serverUrl}')
->variables(Objects\ServerVariable::create('serverUrl')
->default($this->generator->server()->url())
),
Objects\Server::create()
->url('{serverUrl}')
->variables(
Objects\ServerVariable::create('serverUrl')
->default($this->generator->server()->url())
),
];
}
}
13 changes: 13 additions & 0 deletions tests/Feature/OpenApiSchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,17 @@ public function testItCreatesAnEmptyDescriptionIfASchemaDoesNotImplementTheDescr
{
$this->assertEquals('', $this->spec['paths']['/videos']['get']['description']);
}

public function testItUsesInfoConfig()
{
$this->assertEquals('My JSON:API', $this->spec['info']['title']);
$this->assertEquals('JSON:API built using Laravel', $this->spec['info']['description']);
$this->assertEquals('1.0.0', $this->spec['info']['version']);
$this->assertEquals('https://example.com/terms-of-service', $this->spec['info']['termsOfService']);
$this->assertEquals('MIT', $this->spec['info']['license']['name']);
$this->assertEquals('https://opensource.org/licenses/MIT', $this->spec['info']['license']['url']);
$this->assertEquals('API Support', $this->spec['info']['contact']['name']);
$this->assertEquals('https://www.example.com/support', $this->spec['info']['contact']['url']);
$this->assertEquals('support@example.com', $this->spec['info']['contact']['email']);
}
}