Extending

Passes & middlewares

Change the whole document with a pass, and tell the generator what a middleware of your application means.

Passes

A pass receives the whole document once every operation is described, and returns it changed. The built-in passes add the security, the error responses, the tags and the components; yours run after them, in their order, before the unused components are removed and the document is cleaned up.

This one gives every operation the version of the API it belongs to:

app/Docs/ApiVersionPass.php
namespace App\Docs;

use Ark4ne\OpenApi\Context\GenerationContext;
use Ark4ne\OpenApi\Description\DocumentDescription;
use Ark4ne\OpenApi\Description\OperationDescription;
use Ark4ne\OpenApi\Generation\Pass;

final class ApiVersionPass implements Pass
{
    public function process(
        DocumentDescription $document,
        GenerationContext $context,
    ): DocumentDescription {
        return $document->operations(...array_map(
            static fn(OperationDescription $operation): OperationDescription
                => $operation->x('api-version', '2024-06'),
            $document->operations,
        ));
    }
}
paths./api/books.post
{
  "summary": "Add a book.",
  "operationId": "POST:books.store",
  "responses": { "…": "…" },
  "x-api-version": "2024-06"
}
config/openapi.php
'extensions' => [
    'passes' => [App\Docs\ApiVersionPass::class],
],

DocumentDescription holds the operations, the webhooks, the tags, the component schemas, the shared responses, the securitySchemes, the default security and the notes appended to info.description. Each has a replacing method and an appender: operations() and addOperations(), schema($name, $schema), response($name, $response), securityScheme($name, $scheme), addNotes()…

$context->document is the configuration of the document being generated (its name, its version), and $context->locale its language: a pass can behave differently per document.

Middleware mappers

A middleware mapper tells what a middleware adds to the operations that use it: a security requirement, a header, an error response. The built-in ones cover auth, auth.basic, can, signed, throttle, the CSRF middleware and the abilities and scopes of Sanctum and Passport; see Security & errors.

For a tenant middleware of your application that reads an X-Tenant header and answers 400 on an unknown tenant:

app/Docs/TenantMiddlewareMapper.php
namespace App\Docs;

use Ark4ne\OpenApi\Context\GenerationContext;
use Ark4ne\OpenApi\Description\DocumentDescription;
use Ark4ne\OpenApi\Description\OperationDescription;
use Ark4ne\OpenApi\Description\ParameterDescription;
use Ark4ne\OpenApi\Description\ResponseDescription;
use Ark4ne\OpenApi\Generation\Middleware;
use Ark4ne\OpenApi\Generation\MiddlewareMapper;
use Ark4ne\OpenApiWriter\Objects\Schema;

final class TenantMiddlewareMapper implements MiddlewareMapper
{
    public function handles(): array
    {
        return ['tenant'];
    }

    public function map(
        Middleware $middleware,
        OperationDescription $operation,
        DocumentDescription $document,
        GenerationContext $context,
    ): array {
        $operation = $operation
            ->request($operation->request->addParameters(
                ParameterDescription::header(
                    'X-Tenant',
                    Schema::string()->format('uuid'),
                )->required(),
            ))
            ->addResponses(
                ResponseDescription::create(400)->description('Unknown tenant'),
            );

        return [$operation, $document];
    }
}
config/openapi.php
'middlewares' => [
    'tenant' => [App\Docs\TenantMiddlewareMapper::class],
],

The key is the middleware as routes declare it: its alias (tenant) or its class. A middleware class of Laravel, Sanctum or Passport is read as its alias (ThrottleRequests as throttle). The mappers listed for a middleware replace the built-in ones of that middleware.

Middleware gives the name (the alias) and the parameters: throttle:60,1 gives throttle and ['60', '1']. The mapper returns the operation and the document: add shared components (security schemes, responses) to the document, and reference them from the operation.