-
Notifications
You must be signed in to change notification settings - Fork 2
SpecGenerator
Viames Marino edited this page Feb 23, 2026
·
1 revision
Pair\Api\OpenApi\SpecGenerator builds an OpenAPI 3.1 specification from Pair CRUD resources plus optional custom endpoints.
Use SpecGenerator when you need machine-readable API docs for SDK generation, contract review, and client integration.
$spec = new \Pair\Api\OpenApi\SpecGenerator('My API', '1.0.0');setDescription(string $description): voidaddContact(string $name, ?string $email = null, ?string $url = null): voidaddLicense(string $name, ?string $url = null): void
addServer(string $url, string $description = ''): voidaddTag(string $name, string $description = ''): voidaddSecurityScheme(string $name, string $type, array $config = []): void
addPath(string $path, string $method, array $operation): voidaddCrudResources(CrudController $controller, string $basePath = '/api'): void
build(): arraytoJson(bool $pretty = true): stringtoYaml(): string
use Pair\Api\OpenApi\SpecGenerator;
$spec = new SpecGenerator('Pair App API', '1.0.0');
$spec->setDescription('Public REST API');
$spec->addServer('https://api.example.com', 'Production');
$spec->addContact('API Team', 'api@example.com');
$spec->addLicense('MIT', 'https://opensource.org/licenses/MIT');
$spec->addSecurityScheme('bearerAuth', 'http', ['scheme' => 'bearer']);
// from CrudController instance
$spec->addCrudResources($apiController, '/api');
// custom endpoint
$spec->addPath('/api/auth/login', 'post', [
'summary' => 'Login',
'responses' => [
'200' => ['description' => 'Success'],
'401' => ['description' => 'Unauthorized'],
]
]);
$json = $spec->toJson();- CRUD paths are auto-generated per registered slug.
- Components include schemas from
SchemaGenerator. - Security schemes are added under
components.securitySchemes.
$spec->addServer('https://api.example.com', 'Production');
$spec->addServer('https://staging-api.example.com', 'Staging');$spec->addSecurityScheme('bearerAuth', 'http', [
'scheme' => 'bearer',
'bearerFormat' => 'JWT'
]);file_put_contents(APP_PATH . '/public/openapi.json', $spec->toJson(true));
file_put_contents(APP_PATH . '/public/openapi.yaml', $spec->toYaml());- Forgetting to include custom non-CRUD endpoints.
- Drift between validation rules and documented request schemas.
- Publishing specs without environment-appropriate server URLs.
See also: SchemaGenerator, CrudController, API.