Operations & strategies
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 extractor | Priority | Gives |
|---|---|---|
NamingOperationExtractor | 0 | the naming strategies |
PhpDocOperationExtractor | 100 | the PHPDoc |
AttributeOperationExtractor | 200 | #[Operation], #[Tag], #[Security] |
TranslationOperationExtractor | 1000 | the translations |
This one marks the actions tagged @internal in their PHPDoc with an x-internal extension:
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);
}
}
{
"summary": "Export the orders.",
"operationId": "GET:orders.export",
"responses": { "…": "…" },
"x-internal": true
}
'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:
| Strategy | Interface | Method |
|---|---|---|
summary | SummaryStrategy | summary(OperationContext $operation): ?string |
tags | TagStrategy | tags(OperationContext $operation): array (list of tag names) |
groups | GroupStrategy | group(string $tag, OperationContext $operation): ?string (the parent tag) |
operation_id | OperationIdStrategy | operationId(OperationContext $operation): ?string |
query | QueryStrategy | parameters(array $fields, GenerationContext $context): array (FieldDescription → ParameterDescription) |
A strategy that tags each operation by the first segment of its route name, in title case:
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, '.'))];
}
}
'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.