Building documents

Requests and responses

Request bodies, media types, file uploads, responses, headers, examples and links.

A request body and a response both carry content: a map of media types, each with its schema. Responses add headers and links.

Request bodies

use Ark4ne\OpenApiWriter\Objects\MediaType;
use Ark4ne\OpenApiWriter\Objects\Operation;
use Ark4ne\OpenApiWriter\Objects\RequestBody;
use Ark4ne\OpenApiWriter\Objects\Response;
use Ark4ne\OpenApiWriter\Objects\Schema;

$store = Operation::post()
    ->operationId('pets.store')
    ->requestBody(
        RequestBody::create()
            ->required()
            ->content(
                MediaType::json()->schema(Schema::ref('NewPet')),
            ),
    )
    ->responses(Response::created());

The media type shortcuts are MediaType::json(), xml(), formUrlEncoded() and multipart() (multipart/form-data); MediaType::of('text/csv') takes any other.

File uploads

A file is a string with a contentMediaType; Encoding (keyed by the property name) gives the content type of the part in a multipart body.

use Ark4ne\OpenApiWriter\Objects\Encoding;

$upload = RequestBody::create()
    ->required()
    ->content(
        MediaType::multipart()
            ->schema(
                Schema::object()
                    ->properties(
                        Schema::string('name'),
                        Schema::string('file')
                            ->contentMediaType('application/octet-stream'),
                    )
                    ->required('file'),
            )
            ->encoding(
                Encoding::create('file')
                    ->contentType('image/png, image/jpeg'),
            ),
    );

For OpenAPI 3.0, contentMediaType is written as format: binary; see Conversions.

Responses

The status shortcuts set the status code and its reason phrase as description:

ShortcutStatus
ok(), created(), accepted(), noContent()200, 201, 202, 204
movedPermanently(), found(), notModified()301, 302, 304
badRequest(), unauthorized(), forbidden(), notFound()400, 401, 403, 404
methodNotAllowed(), conflict(), unprocessableEntity(), tooManyRequests()405, 409, 422, 429
internalServerError(), serviceUnavailable()500, 503

Any other status uses Response::status(), which sets no description:

$teapot = Response::status(418)->description('I am a teapot');
$clientErrors = Response::status('4XX')->description('Client error');
$fallback = Response::create('default')->description('Unexpected error');

A response whose key is default goes to the default field of the operation's responses.

Operation
Operation::get()->responses(
    Response::ok()->content(
        MediaType::json()->schema(Schema::ref('Pet')),
    ),
    Response::create('default')
        ->description('Unexpected error'),
);
toArray()
{
  "responses": {
    "default": { "description": "Unexpected error" },
    "200": {
      "description": "OK",
      "content": {
        "application/json": {
          "schema": { "$ref": "#/components/schemas/Pet" }
        }
      }
    }
  }
}

A response without description is written with "description": "", since the field is required.

use Ark4ne\OpenApiWriter\Objects\Header;
use Ark4ne\OpenApiWriter\Objects\Link;

$created = Response::created()
    ->headers(
        Header::create('Location')
            ->required()
            ->schema(Schema::string()->format('uri')),
        Header::create('X-RateLimit-Remaining')
            ->schema(Schema::integer())
            ->example(59),
    )
    ->content(MediaType::json()->schema(Schema::ref('Pet')))
    ->links(
        Link::create('GetPet')
            ->operationId('pets.show')
            ->parameters(['id' => '$response.body#/id']),
    );

The objectId of a header is its name; that of a link is the link name. Link::parameters() takes an array of runtime expressions. operationId() and operationRef() are exclusive.

Examples

MediaType, Parameter and Header take a single example() or named examples(), never both:

use Ark4ne\OpenApiWriter\Objects\Example;

$json = MediaType::json()
    ->schema(Schema::ref('Pet'))
    ->examples(
        Example::create('minimal')
            ->summary('Name only')
            ->value(['name' => 'Rex']),
        Example::create('full')
            ->value(['name' => 'Rex', 'tag' => 'dog']),
    );

Example::value() excludes externalValue(). A schema's own examples() is different: it takes plain values, see Types and constraints.