Overview
When the generator does not know something your application does (a custom validation rule, a return type, a middleware, a convention of your API), extend it rather than declaring the same attribute everywhere. Each extension point is an interface; your class joins a registry of built-in ones.
Extension points
| You want to describe | Implement | Page |
|---|---|---|
a validation rule registered by name (phone) | Extraction\Rules\RuleMapper | Rule mappers |
| a rule object of your application | Extraction\Rules\DescribesRule on the rule | Requests |
| a return type the package does not know | Extraction\Responses\ResponseExtractor | Extractors |
| another source of parameters or bodies | Extraction\Requests\RequestExtractor | Extractors |
| other metadata of the operations | Extraction\Operations\OperationExtractor | Operations & strategies |
| your own naming of summaries, tags, operation ids, query parameters | a strategy of Extraction\Strategies | Operations & strategies |
| how the samples of a model are built | Sampling\Sampler | Samplers |
| a change over the whole document | Generation\Pass | Passes & middlewares |
| what a middleware means | Generation\MiddlewareMapper | Passes & middlewares |
All namespaces are under Ark4ne\OpenApi\.
Register an extension
An application lists its classes in extensions of config/openapi.php:
'extensions' => [
'operation_extractors' => [],
'request_extractors' => [],
'rule_mappers' => [App\Docs\PhoneRuleMapper::class],
'response_extractors' => [],
'samplers' => [],
'passes' => [App\Docs\ApiVersionPass::class],
],
'middlewares' => [
'tenant' => [App\Docs\TenantMiddlewareMapper::class],
],
A package tags its classes in the container, in its service provider:
public function register(): void
{
$this->app->tag([PhoneRuleMapper::class], 'openapi.rule-mappers');
$this->app->tag([ApiVersionPass::class], 'openapi.passes');
}
| Configuration key | Container tag |
|---|---|
operation_extractors | openapi.operation-extractors |
request_extractors | openapi.request-extractors |
rule_mappers | openapi.rule-mappers |
response_extractors | openapi.response-extractors |
samplers | openapi.samplers |
passes | openapi.passes |
Every extension is built through the container: its constructor dependencies are injected. A class of the wrong kind fails the command with its key or its tag.
Priorities
Extractors and samplers declare their priority with priority(), by convention returning a public PRIORITY
constant:
| Registry | Order |
|---|---|
| Operation and request extractors | every supporting extractor runs, by increasing priority: each one refines what the previous ones gave |
| Response extractors and samplers | the first supporting one, by decreasing priority, is used |
| Rule mappers | one per rule name: yours replaces the built-in mapper of its rules |
| Passes | in their order, after the built-in passes, before the removal of unused components and the cleanup |
The priorities of the built-in classes are in their PRIORITY constants; for example, the attributes
(AttributeOperationExtractor, 200) run after the PHPDoc (PhpDocOperationExtractor, 100), and translation runs last
(1000).
Rules every extension follows
- Descriptions are immutable.
OperationDescription,RequestDescription,FieldDescriptionand the others return a copy from every method: always use the return value ($field = $field->schema(…)). - Schemas are writer schemas. They are
Ark4ne\OpenApiWriter\Objects\Schemaobjects, written in the version of the document by OpenAPI Writer: describe the latest model (Schema::string()->nullable()), never a version. - Never throw on application code. An extension that does not understand what it reads adds a diagnostic and
returns what it was given:
use Ark4ne\OpenApi\Context\Stage; $context->diagnostics->warning( Stage::Request, "the phone rule of {$rule->field} has no region", $operation->route->label(), ); - No state between documents. Each document is generated with its own
GenerationContext; keep what you learn in it, not in a static property.