Paths and operations
A path item is a URL template, /pets/{id}; its operations are the HTTP methods it answers. Parameters can be declared
on the path item (shared by all its operations) or on each operation.
Paths and operations
use Ark4ne\OpenApiWriter\Objects\Operation;
use Ark4ne\OpenApiWriter\Objects\PathItem;
use Ark4ne\OpenApiWriter\Objects\Response;
$pets = PathItem::create('/pets')
->summary('Pets')
->operations(
Operation::get()
->operationId('pets.index')
->tags('pets')
->summary('List the pets')
->responses(Response::ok()),
Operation::post()
->operationId('pets.store')
->tags('pets')
->responses(Response::created()),
);
$document = $document->paths($pets);
operations() puts each operation under its method. The method shortcuts are get(), put(), post(),
delete(), options(), head(), patch() and trace(); PathItem also has one setter per method:
->get(Operation::get()->…).
An operation without responses() is invalid in OpenAPI 3.0: give every operation at least one response.
| Operation method | Notes |
|---|---|
operationId(?string) | Unique in the document; the validator checks it |
tags(string ...) | Tag names; declare them in OpenApi::tags() |
summary(?string), description(?string) | |
deprecated(?bool = true) | |
parameters(ParameterContract ...) | See below |
requestBody(?RequestBodyContract) | See Requests and responses |
responses(Responses|ResponseContract ...) | Responses keyed by their status code |
security(SecurityRequirement ...) | See Security |
callbacks(CallbackContract ...) | See Webhooks and callbacks |
servers(Server ...), externalDocs(?ExternalDocumentation) |
Parameters
One shortcut per location; the first argument is the parameter name.
use Ark4ne\OpenApiWriter\Objects\Parameter;
use Ark4ne\OpenApiWriter\Objects\Schema;
$id = Parameter::path('id')
->schema(Schema::integer()->format('int64'));
$page = Parameter::query('page')
->description('Page number')
->schema(Schema::integer()->minimum(1)->default(1));
$tenant = Parameter::header('X-Tenant')
->required()
->schema(Schema::string());
$session = Parameter::cookie('session')
->schema(Schema::string());
Parameter::path() sets required: true, which the specification demands for path parameters. Every {name} of a
path needs its path parameter, on the path item or on each operation:
$document = $document->addPaths(
PathItem::create('/pets/{id}')
->parameters($id)
->operations(
Operation::get()
->operationId('pets.show')
->parameters($page)
->responses(Response::ok(), Response::notFound()),
Operation::delete()
->operationId('pets.destroy')
->deprecated()
->responses(Response::noContent()),
),
);
Arrays and objects in parameters
style() and explode() describe how a value is serialized; both take the enum or its string value:
use Ark4ne\OpenApiWriter\Enums\ParameterStyle;
Parameter::query('tags')
->style(ParameterStyle::Form)
->explode(false)
->schema(
Schema::array()->items(Schema::string()),
);
{
"name": "tags",
"in": "query",
"style": "form",
"explode": false,
"schema": {
"type": "array",
"items": { "type": "string" }
}
}
For a complex value, give content() a single media type instead of schema(). Examples go in example() or
examples(Example ...), never both.
Adding paths in a loop
A path can be added only once (a second addPaths() with the same path throws DuplicateObjectIdException). When
building from a list of routes, group the operations by path first, and reassign the document on each step:
$routes = [
['GET', '/health', 'health.show'],
['GET', '/status', 'status.show'],
['POST', '/status', 'status.update'],
];
$operationsByPath = [];
foreach ($routes as [$method, $uri, $name]) {
$operationsByPath[$uri][] = Operation::create(strtolower($method))
->operationId($name)
->responses(Response::noContent());
}
foreach ($operationsByPath as $uri => $operations) {
$document = $document->addPaths(
PathItem::create($uri)->operations(...$operations),
);
}
Shared path items
PathItem::ref() points a path to a path item defined elsewhere, and Components::pathItems() (3.1+) stores reusable
path items.
OpenAPI 3.2 adds the QUERY method and additionalOperations for any other method; see
OpenAPI 3.2.