Extending

Operations & strategies

Add metadata to the operations, and name summaries, tags, operation ids and query parameters your own way.

Operation extractors

An operation extractor reads the metadata of an operation: summary, description, tags, operation id, deprecation, extensions. Every supporting extractor runs, by increasing priority, refining what the previous ones gave:

Built-in extractorPriorityGives
NamingOperationExtractor0the naming strategies
PhpDocOperationExtractor100the PHPDoc
AttributeOperationExtractor200#[Operation], #[Tag], #[Security]
TranslationOperationExtractor1000the translations

This one marks the actions tagged @internal in their PHPDoc with an x-internal extension:

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

use Ark4ne\OpenApi\Context\GenerationContext;
use Ark4ne\OpenApi\Description\OperationDescription;
use Ark4ne\OpenApi\Extraction\OperationContext;
use Ark4ne\OpenApi\Extraction\Operations\OperationExtractor;

final class InternalOperationExtractor implements OperationExtractor
{
    public const PRIORITY = 250;

    public function priority(): int
    {
        return self::PRIORITY;
    }

    public function supports(OperationContext $operation): bool
    {
        return $operation->doc !== null
            && $operation->doc->node->getTagsByName('@internal') !== [];
    }

    public function extract(
        OperationContext $operation,
        OperationDescription $description,
        GenerationContext $context,
    ): OperationDescription {
        return $description->x('internal', true);
    }
}
paths./api/export.get
{
  "summary": "Export the orders.",
  "operationId": "GET:orders.export",
  "responses": { "…": "…" },
  "x-internal": true
}
config/openapi.php
'extensions' => [
    'operation_extractors' => [App\Docs\InternalOperationExtractor::class],
],

$operation->doc->node is the PHPDoc parsed by phpstan/phpdoc-parser. OperationDescription has a method per field: summary(), description(), operationId(), tags(), addTags(), deprecated(), externalDocs(), x($name, $value) (the x- prefix is added), exclude(). To leave an operation out of the document, return $description->exclude().

Naming strategies

strategies of a document chooses how the generator names things it cannot read elsewhere. Each strategy takes a built-in value in by, or a class implementing its interface of Ark4ne\OpenApi\Extraction\Strategies, built through the container:

StrategyInterfaceMethod
summarySummaryStrategysummary(OperationContext $operation): ?string
tagsTagStrategytags(OperationContext $operation): array (list of tag names)
groupsGroupStrategygroup(string $tag, OperationContext $operation): ?string (the parent tag)
operation_idOperationIdStrategyoperationId(OperationContext $operation): ?string
queryQueryStrategyparameters(array $fields, GenerationContext $context): array (FieldDescription → ParameterDescription)

A strategy that tags each operation by the first segment of its route name, in title case:

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

use Ark4ne\OpenApi\Extraction\OperationContext;
use Ark4ne\OpenApi\Extraction\Strategies\TagStrategy;
use Illuminate\Support\Str;

final class RouteNameTags implements TagStrategy
{
    public function tags(OperationContext $operation): array
    {
        $name = $operation->route->name;

        return $name === null ? [] : [Str::title(Str::before($name, '.'))];
    }
}
config/openapi.php
'documents' => [
    'v1' => [
        'strategies' => [
            'tags' => ['by' => App\Docs\RouteNameTags::class],
        ],
    ],
],

The PHPDoc and the attributes still win over a strategy. Operation ids are made unique after the strategy: a duplicate gets a _2 suffix and a warning.