Components and references
Declare each reusable piece once in Components, under a name, then refer to it with ::ref() wherever the
specification accepts a reference.
Declaring components
Every section of Components is a map: the objectId of each entry is the component name.
use Ark4ne\OpenApiWriter\Objects\Components;
use Ark4ne\OpenApiWriter\Objects\MediaType;
use Ark4ne\OpenApiWriter\Objects\Parameter;
use Ark4ne\OpenApiWriter\Objects\RequestBody;
use Ark4ne\OpenApiWriter\Objects\Response;
use Ark4ne\OpenApiWriter\Objects\Schema;
$error = MediaType::json()->schema(Schema::ref('Error'));
$components = Components::create()
->schemas(
Schema::object('Book')
->properties(
Schema::integer('id')->format('int64')->readOnly(),
Schema::string('title')->minLength(1),
)
->required('id', 'title'),
Schema::object('Error')
->properties(Schema::string('message'))
->required('message'),
)
->parameters(
Parameter::path('book', 'BookId')
->schema(Schema::integer()->format('int64')),
)
->requestBodies(
RequestBody::create('Book')
->required()
->content(MediaType::json()->schema(Schema::ref('Book'))),
)
->responses(
Response::notFound()->objectId('NotFound')->content($error),
);
$document = $document->components($components);
- A schema's name is its first argument:
Schema::object('Book'). - A parameter's first argument is its
name; the second is the component name:Parameter::path('book', 'BookId'). - A response shortcut is keyed by its status: rename it with
objectId('NotFound').
The sections are schemas, responses, parameters, examples, requestBodies, headers, securitySchemes,
links, callbacks, pathItems (3.1+) and mediaTypes (3.2), each with its add method.
Referring to a component
Each ::ref() builds the path of its section:
| Call | Writes |
|---|---|
Schema::ref('Book') | #/components/schemas/Book |
Response::ref('NotFound') | #/components/responses/NotFound |
Parameter::ref('BookId') | #/components/parameters/BookId |
RequestBody::ref('Book') | #/components/requestBodies/Book |
Header::ref(), Example::ref(), Link::ref(), Callback::ref(), SecurityScheme::ref() | their section |
MediaType::ref('BookJson', 'application/json') | #/components/mediaTypes/BookJson (3.2) |
A value containing # or / is written as is, for an external file: Schema::ref('common.yaml#/Error').
When the reference sits in a map, its key is the second argument. A referenced response needs its status code:
Operation::get()->responses(
Response::ok()->content(
MediaType::json()->schema(Schema::ref('Book')),
),
Response::ref('NotFound', '404'),
);
{
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": { "$ref": "#/components/schemas/Book" }
}
}
},
"404": { "$ref": "#/components/responses/NotFound" }
}
}
The same goes for a referenced schema used as a property: Schema::ref('User', 'owner') is the property owner.
Schema references stay schemas
Schema::ref() returns a Schema, so you can chain what a referencing schema may carry: description(),
readOnly(), nullable()…
Schema::ref('User', 'owner')
->description('Owner of the book');
{ "$ref": "#/components/schemas/User", "description": "Owner of the book" }
{ "description": "Owner of the book", "allOf": [{ "$ref": "#/components/schemas/User" }] }
OpenAPI 3.0 ignores the siblings of a $ref, so the library wraps it in allOf there.
The other ::ref() return a Reference, which takes summary() and description() from OpenAPI 3.1.
Reference objects
Reference::to() takes any reference; Reference::for() builds one from a component object, its class and objectId:
use Ark4ne\OpenApiWriter\Objects\Reference;
$notFound = Response::notFound()->objectId('NotFound');
// Both write #/components/responses/NotFound
Reference::for($notFound);
Reference::to('#/components/responses/NotFound', '404');
The validator checks that every local reference resolves (LocalReferencesRule); see
Validation rules.