Request & response extractors
Extractors read an operation. A response extractor describes the responses of one return type; a request
extractor refines what the operation receives. Both receive an OperationContext: the route, the HTTP method and path,
the reflected action, its PHPDoc and its return type.
Response extractors
The first response extractor that supports the return type, by decreasing priority, describes its responses. Add one for a type of your application:
namespace App\Docs;
use App\Exports\CsvExport;
use Ark4ne\OpenApi\Context\GenerationContext;
use Ark4ne\OpenApi\Description\ResponseDescription;
use Ark4ne\OpenApi\Extraction\OperationContext;
use Ark4ne\OpenApi\Extraction\Responses\ResponseExtractor;
use Ark4ne\OpenApi\Types\NamedType;
use Ark4ne\OpenApi\Types\Type;
use Ark4ne\OpenApiWriter\Objects\Schema;
final class CsvExportExtractor implements ResponseExtractor
{
public const PRIORITY = 500;
public function priority(): int
{
return self::PRIORITY;
}
public function supports(Type $type, OperationContext $operation): bool
{
return $type instanceof NamedType
&& is_a($type->name, CsvExport::class, true);
}
public function extract(
Type $type,
OperationContext $operation,
GenerationContext $context,
): array {
return [
ResponseDescription::ok()
->description('A CSV file')
->content('text/csv', Schema::string()),
];
}
}
{
"200": {
"description": "A CSV file",
"content": {
"text/csv": { "schema": { "type": "string" } }
}
}
}
'extensions' => [
'response_extractors' => [App\Docs\CsvExportExtractor::class],
],
Type is the parsed return type, PHPDoc included: a NamedType (a class or a built-in type, $type->name), a
GenericType (AnonymousResourceCollection<UserResource>), a UnionType, an ArrayShapeType, a ListType… in
Ark4ne\OpenApi\Types. A union is split before the extractors are asked: each member is extracted on its own.
The priorities of the built-in extractors go from 100 (EmptyResponseExtractor) to 320
(ResourceCollectionExtractor); a priority above them makes yours win for the types it supports.
ResponseDescription is built with create(int|string $status) or ok(), then description(), content($mediaType, $schema), headers(HeaderDescription …) and summary(). A response declared with #[Response] still replaces yours
for its status.
Request extractors
Every request extractor that supports the operation runs, by increasing priority, each refining the
RequestDescription the previous ones gave. The built-in ones run in this order: path parameters (0), form request
(100), DescribesRequest (200), attributes (300).
This one adds a header to every operation under /api/tenants/:
namespace App\Docs;
use Ark4ne\OpenApi\Context\GenerationContext;
use Ark4ne\OpenApi\Description\ParameterDescription;
use Ark4ne\OpenApi\Description\RequestDescription;
use Ark4ne\OpenApi\Extraction\OperationContext;
use Ark4ne\OpenApi\Extraction\Requests\RequestExtractor;
use Ark4ne\OpenApiWriter\Objects\Schema;
final class TenantHeaderExtractor implements RequestExtractor
{
public const PRIORITY = 150;
public function priority(): int
{
return self::PRIORITY;
}
public function supports(OperationContext $operation): bool
{
return str_starts_with($operation->path, '/api/tenants/');
}
public function extract(
OperationContext $operation,
RequestDescription $request,
GenerationContext $context,
): RequestDescription {
return $request->addParameters(
ParameterDescription::header(
'X-Tenant',
Schema::string()->format('uuid'),
)
->required()
->description('Tenant of the request'),
);
}
}
At 150, it runs after the form request and before the attributes: a #[Header('X-Tenant', …)] on an action still
completes it.
RequestDescription holds the parameters (ParameterDescription::path(), query(), header(), cookie()), the
fields of the rules, the body and two flags, validated (adds the 422) and authorized (adds the 403).
The operation context
| Member | Holds |
|---|---|
route | the RouteDefinition: name, domain, controller, action, middlewares, label() (GET /api/users) |
method, path | the HTTP method and the OpenAPI path of the operation |
action | the reflected action (ReflectionMethod, or ReflectionFunction for a closure) |
doc | the PHPDoc of the action: summary(), description(), isDeprecated(), and its parsed node |
returnType, requestType | the parsed return type and form request type |
attributes(Attr::class, $context) | the attributes of the class and of the action, the action's last |