OpenAPI Exporter
BetterRoute\OpenApi\OpenApiExporter::export(array $contracts, array $options = []): array
Minimal example
use BetterRoute\OpenApi\OpenApiExporter;
$contracts = array_merge(
$router->contracts(true),
$resource->contracts(true)
);
$document = (new OpenApiExporter())->export($contracts, [
'title' => 'better-route API',
'version' => 'v0.4.0',
'description' => 'Contract-first API',
'serverUrl' => '/wp-json',
'openapiVersion' => '3.1.0',
'strictSchemas' => true, // v0.3.0; throws on unknown $ref components
'components' => [
'schemas' => [
'Article' => ['type' => 'object'],
],
],
]);
Export options
titleversiondescriptionserverUrlopenapiVersionincludeExcludedcomponentsstrictSchemas(v0.3.0) — whentrue, missing component references throwInvalidArgumentExceptioninstead of being substituted with{ type: 'object', additionalProperties: true }
Important behavior
- Path regex placeholders
(?P<id>...)convert to{id} - Path params are forced
required=true - Unsupported HTTP methods are skipped
defaultresponse references#/components/responses/ErrorResponse
Common mistakes
- forgetting
openapi.include=falsefor internal ops - using invalid
parametersstructure - referencing non-existent schemas
Validation checklist
- generated document has
openapi,info,servers,paths,components - expected operations include
x-scopesandx-better-routeextensions - POST routes emit
201success response