http router openapi
v2.2.0
composer require 'sunrise/http-router-openapi:^2.1'use PsrSimpleCacheCacheInterface;
use SunriseHttpRouterOpenApiObjectInfo;
use SunriseHttpRouterOpenApiOpenApi;
use SunriseHttpRouterOpenApiRouteInterface;
$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 SunriseHttpRouterRouter $router */
$openapi->addRoute(...$router->getRoutes());// 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();Converts an operation part to JSON Schema.
$openapi->getRequestCookieJsonSchema();
$openapi->getRequestHeaderJsonSchema();
$openapi->getRequestQueryJsonSchema();
$openapi->getRequestBodyJsonSchema();
$openapi->getResponseBodyJsonSchema();Validates a request using a route description.
use SunriseHttpRouterOpenApiMiddlewareRequestValidationMiddleware;
use SunriseHttpRouterOpenApiOpenApi;
/** @var OpenApi $openapi */
$middleware = new RequestValidationMiddleware($openapi);Generates OpenAPI document.
use SunriseHttpRouterOpenApiCommandGenerateOpenapiDocumentCommand;
use SunriseHttpRouterOpenApiOpenApi;
/** @var OpenApi $openapi */
$command = new GenerateOpenapiDocumentCommand($openapi);php bin/app router:generate-openapi-document --helpGenerates an operation part to JSON Schema.
use SunriseHttpRouterOpenApiCommandGenerateJsonSchemaCommand;
use SunriseHttpRouterOpenApiOpenApi;
/** @var OpenApi $openapi */
$command = new GenerateJsonSchemaCommand($openapi);php bin/app router:generate-json-schema --helpThe assertion fails if the given response body doesn't match a description of the operation identified by the given ID.
use PHPUnitFrameworkTestCase;
use PsrHttpMessageResponseInterface;
use SunriseHttpRouterOpenApiTestOpenapiTestKit;
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
{
/**
* @OpenApiOperation(
* requestBody=@OpenApiRequestBody(
* content={
* "application/json": @OpenApiMediaType(
* schema=@OpenApiSchema(
* type="object",
* properties={
* "foo": @OpenApiSchema(
* type="string",
* ),
* },
* ),
* ),
* },
* ),
* responses={
* 200: @OpenApiResponse(
* description="Ok",
* ),
* },
* )
*/
public function someAction()
{
}
}Look for more examples here: Some App