wedo / openapi-generator
从类型化类生成Open api json
v2.0.0
2022-01-05 08:43 UTC
Requires
- php: ^8.0
- nette/finder: ^2.4
- nette/utils: ^2.4|^3.0
Requires (Dev)
- contributte/qa: ^0.1
- phpstan/phpstan: ^0.12.6
- phpstan/phpstan-deprecation-rules: ^0.12
- phpstan/phpstan-nette: ^0.12
- phpstan/phpstan-strict-rules: ^0.12
- phpunit/phpunit: ^7.0|^8.0|^9.0
README
从phpdoc和php类型提示生成框架无关的Open api生成器
安装
$ composer require wedo/openapi-generator
配置
请参阅src/Config.php
使用方法
检查测试文件夹,其中包含完整的最小API: https://github.com/WEDOehf/openapi-generator/tree/master/tests
这是运行所需的最小配置,要查看所有选项,请参阅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);