sunrise / http-router-openapi
Sunrise // OpenApi (Swagger) 规范支持 Sunrise 路由
v2.2.0
2021-11-01 08:32 UTC
Requires
- php: ^7.1|^8.0
- doctrine/annotations: ^1.6
Requires (Dev)
- justinrainbow/json-schema: 5.2.10
- phpunit/phpunit: 7.5.20|9.5.0
- sunrise/coding-standard: 1.0.0
- sunrise/http-factory: 1.1.0
- sunrise/http-router: ^2.11
- symfony/console: ^4.4
- dev-master
- v2.2.0
- v2.1.0
- v2.0.0
- v1.10.0
- v1.9.0
- v1.8.0
- v1.7.0
- v1.6.0
- v1.5.1
- v1.5.0
- v1.4.1
- v1.4.0
- v1.3.0
- v1.2.0
- v1.1.1
- v1.1.0
- v1.0.2
- v1.0.1
- v1.0.0
- dev-renovate/phpunit-phpunit-11.x
- dev-renovate/phpunit-phpunit-9.x
- dev-renovate/justinrainbow-json-schema-6.x
- dev-renovate/justinrainbow-json-schema-5.x
- dev-renovate/major-symfony
- dev-renovate/sunrise-http-factory-2.x
- dev-renovate/doctrine-annotations-2.x
- dev-renovate/circleci-php-8.x
- dev-renovate/circleci-php-7.x
- dev-renovate/sunrise-http-factory-1.x
- dev-release/v2.2.0
- dev-release/v2.1.0
- dev-release/v2.0.0
- dev-release/v1.10.0
- dev-release/v1.9.0
- dev-release/v1.8.0
This package is auto-updated.
Last update: 2024-09-19 13:57:43 UTC
README
理解重要事项
安装
composer require 'sunrise/http-router-openapi:^2.1'
快速入门
use Psr\SimpleCache\CacheInterface; use Sunrise\Http\Router\OpenApi\Object\Info; use Sunrise\Http\Router\OpenApi\OpenApi; use Sunrise\Http\Router\OpenApi\RouteInterface; $openapi = new OpenApi(new Info('Acme', '1.0.0')); // Passing PSR-16 cache to the openapi object: /** @var CacheInterface $cache */ $openapi->setCache($cache); // Passing all routes to the openapi object: /** @var RouteInterface[] $routes */ $openapi->addRoute(...$routes); // When using Sunrise Router: /** @var \Sunrise\Http\Router\Router $router */ $openapi->addRoute(...$router->getRoutes());
构建 OpenAPI 文档
// Converting the openapi object to JSON document: $openapi->toJson(); // Converting the openapi object to YAML document: $openapi->toYaml(); // Converting the openapi object to an array: $openapi->toArray();
构建 JSON 架构
将操作部分转换为 JSON Schema。
$openapi->getRequestCookieJsonSchema(); $openapi->getRequestHeaderJsonSchema(); $openapi->getRequestQueryJsonSchema(); $openapi->getRequestBodyJsonSchema(); $openapi->getResponseBodyJsonSchema();
PSR-15 中间件
RequestValidationMiddleware
使用路由描述验证请求。
use Sunrise\Http\Router\OpenApi\Middleware\RequestValidationMiddleware; use Sunrise\Http\Router\OpenApi\OpenApi; /** @var OpenApi $openapi */ $middleware = new RequestValidationMiddleware($openapi);
Symfony 命令
GenerateOpenapiDocumentCommand
生成 OpenAPI 文档。
use Sunrise\Http\Router\OpenApi\Command\GenerateOpenapiDocumentCommand; use Sunrise\Http\Router\OpenApi\OpenApi; /** @var OpenApi $openapi */ $command = new GenerateOpenapiDocumentCommand($openapi);
php bin/app router:generate-openapi-document --help
GenerateJsonSchemaCommand
生成操作部分到 JSON Schema。
use Sunrise\Http\Router\OpenApi\Command\GenerateJsonSchemaCommand; use Sunrise\Http\Router\OpenApi\OpenApi; /** @var OpenApi $openapi */ $command = new GenerateJsonSchemaCommand($openapi);
php bin/app router:generate-json-schema --help
测试套件
assertResponseBodyMatchesDescription
如果给定的响应体与通过给定 ID 识别的操作描述不匹配,则断言失败。
use PHPUnit\Framework\TestCase; use Psr\Http\Message\ResponseInterface; use Sunrise\Http\Router\OpenApi\Test\OpenapiTestKit; class SomeTest extends TestCase { use OpenapiTestKit; public function testResponseBodyMatchesDescription() : void { // some logic to run a route... /** @var ResponseInterface $response */ $this->assertResponseBodyMatchesDescription('route.name', $response); } }
简单路由描述
class SomeController { /** * @OpenApi\Operation( * requestBody=@OpenApi\RequestBody( * content={ * "application/json": @OpenApi\MediaType( * schema=@OpenApi\Schema( * type="object", * properties={ * "foo": @OpenApi\Schema( * type="string", * ), * }, * ), * ), * }, * ), * responses={ * 200: @OpenApi\Response( * description="Ok", * ), * }, * ) */ public function someAction() { } }
在此处查找更多示例: Some App