Building documents

Document

The root object - info, servers, tags, external documentation and the default version.

OpenApi is the root of the document. It holds the description of the API (info), where it is served (servers), how its operations are grouped (tags), and the paths, components and security described in the next pages.

Info

info is required, and so are its title and version (the version of your API, not of OpenAPI).

use Ark4ne\OpenApiWriter\Objects\Contact;
use Ark4ne\OpenApiWriter\Objects\Info;
use Ark4ne\OpenApiWriter\Objects\License;
use Ark4ne\OpenApiWriter\Objects\OpenApi;

$document = OpenApi::create()
    ->info(
        Info::create()
            ->title('Pet Store')
            ->version('2.1.0')
            ->description('Pets and their owners.')
            ->termsOfService('https://example.com/terms')
            ->contact(
                Contact::create()
                    ->name('API team')
                    ->email('api@example.com'),
            )
            ->license(
                License::create()
                    ->name('MIT')
                    ->url('https://opensource.org/license/mit'),
            ),
    );

Info::summary() and License::identifier() (an SPDX identifier, exclusive with url) exist from OpenAPI 3.1.

Servers

A server URL may hold variables, declared with ServerVariable; the objectId is the variable name.

use Ark4ne\OpenApiWriter\Objects\Server;
use Ark4ne\OpenApiWriter\Objects\ServerVariable;

$document = $document->servers(
    Server::create()
        ->url('https://{region}.example.com/v2')
        ->description('Production')
        ->variables(
            ServerVariable::create('region')
                ->enum('eu', 'us')
                ->default('eu'),
        ),
    Server::create()->url('http://localhost:8000'),
);

Path items and operations accept servers() too, to override the document's.

Tags

Tags group operations. Declare at the root every tag your operations use, with its name:

use Ark4ne\OpenApiWriter\Objects\ExternalDocumentation;
use Ark4ne\OpenApiWriter\Objects\Tag;

$document = $document->tags(
    Tag::create()
        ->name('pets')
        ->description('Everything about pets')
        ->externalDocs(
            ExternalDocumentation::create()
                ->url('https://docs.example.com/pets'),
        ),
    Tag::create()->name('owners'),
);

OpenAPI 3.2 adds a hierarchy and a kind to tags (parent(), kind(), summary()); see OpenAPI 3.2.

External documentation

$document = $document->externalDocs(
    ExternalDocumentation::create()
        ->url('https://docs.example.com')
        ->description('Guides and tutorials'),
);

Default version

openapi() sets the version the document is written and validated for when none is given:

use Ark4ne\OpenApiWriter\Enums\Version;

$document = $document->openapi(Version::V3_0_4);

$document->toJson();   // written for 3.0.4

Without it, the default is 3.1.2. See Choosing a version.

Other root fields

MethodField
paths(PathItem ...)Paths and operations
components(?Components)Components and references
security(SecurityRequirement ...)Security
webhooks(PathItemContract ...)Webhooks and callbacks (3.1+)
jsonSchemaDialect(?string)the default $schema of the schemas (3.1+)
self(?string)the $self URI of the document (3.2)