Open api json generator from typed classes

v2.0.0 2022-01-05 08:43 UTC

This package is auto-updated.

Last update: 2023-11-05 13:01:29 UTC


Framework agnostic Open api generator from phpdoc and php type hints

Latest Stable Version build codecov PHPStan License


$ composer require wedo/openapi-generator


see src/Config.php


Check tests folder, there's full working minimal API there:

this is minimal config required to run, to check all options see src/Config.php

//minimal config
$config = new Config();
$config->baseRequest = BaseRequest::class;
$config->serverUrl = '';
$config->baseEnum = BaseEnum::class; // use with @see annotation if U want to provide user enum options
$config->requiredAnnotation = '@required'; //what annotation should be used on requests to confirm that parameter is required
$config->namespace = 'App\Api\Controllers\\';
$config->path = __DIR__ . 'Api/Controllers';

$generator = new Generator($config);

//add standard error response to ref list
$this->generator->onBeforeGenerate[] = function () {

$this->generator->getClassProcessor()->getMethodProcessor()->onProcess[] = function() {
        // set some standard error responses for each endpoint
        $methodProcessor = $this->generator->getClassProcessor()->getMethodProcessor();
        $path->responses[400] = $methodProcessor->createResponse('Bad request error response', 'ErrorResponse');
        $annotations = $method->getAnnotations();
        if (!empty($annotations['throws'])) {
            // add your own error responses classes for some specific exceptions

//set some title
$this->generator->getJson()->info->title = 'My api';

//add your security schemes if needed
$this->generator->getJson()->components->securitySchemes = [
    'APIKeyHeader' => [
            'type' => 'apiKey',
            'in' => 'header',
            'name' => 'api-key',

$json = $this->generator->generate();
file_put_contents('open-api.json', $json);