Componette

Componette

WEDOehf

WEDOehf / openapi-generator v2.0.0

Framework agnostic Open api generator from phpdoc and php type hints

download-cloud-line composer require wedo/openapi-generator

Open API Generator

Framework agnostic Open api generator from phpdoc and php type hints


Latest Stable Version build codecov PHPStan License

Installation

$ composer require wedo/openapi-generator

Configuration

see src/Config.php

Usage

Check tests folder, there's full working minimal API there: https://github.com/WEDOehf/openapi-generator/tree/master/tests

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 = 'https://www.mydomain.com/api/v1';
$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->getRefProcessor()->generateRef(ClassType::from(ErrorResponse::class));
};

$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);
  • v2.0.0 v2.0.0

    • support for php 8.x
    • dropped support for php 7.x
    • new coding standards
    • support for php8 attributes
    • BC drop nette\reflection (everything is done by pute reflection types this is BC )
  • v1.1.6 v1.1.6

    • fix duplicated parameters when parameter defined both in method and in annotation
  • v1.1.5 v1.1.5

    • typeReplacement
  • v1.1.2 v1.1.2

    • add support for PHP8
    • drop support for PHP7.1
  • v1.1.1 v1.1.1

    • with php 7.4 when item is array allways check phpdoc typehint for items type
  • v1.1.0 v1.1.0

    • support for php 7.4
    • generator is now first reading php type hint on properties if they're declared otherwise fallback to phpdoc
  • v1.0.0 v1.0.0

    • first release fully functional
Componette Componette felix@nette.org