diff --git a/config/config.php b/config/config.php index fac19ff..9f99b80 100644 --- a/config/config.php +++ b/config/config.php @@ -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', + ], ], ], ], diff --git a/src/Descriptors/Server.php b/src/Descriptors/Server.php index d934311..e39dd6e 100644 --- a/src/Descriptors/Server.php +++ b/src/Descriptors/Server.php @@ -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 + ); } /** @@ -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()) + ), ]; } } diff --git a/tests/Feature/OpenApiSchemaTest.php b/tests/Feature/OpenApiSchemaTest.php index 69c269c..95e664d 100644 --- a/tests/Feature/OpenApiSchemaTest.php +++ b/tests/Feature/OpenApiSchemaTest.php @@ -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']); + } }