Extending

Overview

The extension points of the generator, how to register an extension, and the rules every extension follows.

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 describeImplementPage
a validation rule registered by name (phone)Extraction\Rules\RuleMapperRule mappers
a rule object of your applicationExtraction\Rules\DescribesRule on the ruleRequests
a return type the package does not knowExtraction\Responses\ResponseExtractorExtractors
another source of parameters or bodiesExtraction\Requests\RequestExtractorExtractors
other metadata of the operationsExtraction\Operations\OperationExtractorOperations & strategies
your own naming of summaries, tags, operation ids, query parametersa strategy of Extraction\StrategiesOperations & strategies
how the samples of a model are builtSampling\SamplerSamplers
a change over the whole documentGeneration\PassPasses & middlewares
what a middleware meansGeneration\MiddlewareMapperPasses & middlewares

All namespaces are under Ark4ne\OpenApi\.

Register an extension

An application lists its classes in extensions of config/openapi.php:

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:

src/MyPackageServiceProvider.php
public function register(): void
{
    $this->app->tag([PhoneRuleMapper::class], 'openapi.rule-mappers');
    $this->app->tag([ApiVersionPass::class], 'openapi.passes');
}
Configuration keyContainer tag
operation_extractorsopenapi.operation-extractors
request_extractorsopenapi.request-extractors
rule_mappersopenapi.rule-mappers
response_extractorsopenapi.response-extractors
samplersopenapi.samplers
passesopenapi.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:

RegistryOrder
Operation and request extractorsevery supporting extractor runs, by increasing priority: each one refines what the previous ones gave
Response extractors and samplersthe first supporting one, by decreasing priority, is used
Rule mappersone per rule name: yours replaces the built-in mapper of its rules
Passesin 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, FieldDescription and 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\Schema objects, 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.