From d264d9cbe6ce87ec745dddff703b6c3fa5db5693 Mon Sep 17 00:00:00 2001 From: "@fenric" Date: Mon, 20 Apr 2020 07:21:16 +0300 Subject: [PATCH 1/2] the min version for the router was changed --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index a6dc72e..707d754 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,7 @@ "require": { "php": "^7.1", "doctrine/annotations": "^1.6", - "sunrise/http-router": "^2.1" + "sunrise/http-router": "^2.4" }, "require-dev": { "phpbench/phpbench": "0.16.10", From 1a8429e1688f52a841575ccc3e0f9f1d50c27d1d Mon Sep 17 00:00:00 2001 From: "@fenric" Date: Mon, 20 Apr 2020 07:22:19 +0300 Subject: [PATCH 2/2] toJson() was added; full support for route properties --- src/OpenApi.php | 32 +++++++++++++++++++++++++++ tests/OpenApiTest.php | 50 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) diff --git a/src/OpenApi.php b/src/OpenApi.php index edaf78c..c5af6de 100644 --- a/src/OpenApi.php +++ b/src/OpenApi.php @@ -31,6 +31,7 @@ */ use function Sunrise\Http\Router\path_parse; use function Sunrise\Http\Router\path_plain; +use function json_encode; use function strtolower; /** @@ -164,6 +165,16 @@ public function __construct(Info $info) $this->annotationReader->addNamespace(self::ANNOTATIONS_NAMESPACE); } + /** + * @param int $options + * + * @return string + */ + public function toJson(int $options = 0) : string + { + return json_encode($this->toArray(), $options); + } + /** * @param bool $value * @@ -290,6 +301,27 @@ private function fetchOperation(RouteInterface $route) : ?OperationAnnotation $operation->operationId = $route->getName(); } + if (null === $operation->tags) { + $routeTags = $route->getTags(); + if ([] !== $routeTags) { + $operation->tags = $routeTags; + } + } + + if (null === $operation->summary) { + $routeSummary = $route->getSummary(); + if ('' !== $routeSummary) { + $operation->summary = $routeSummary; + } + } + + if (null === $operation->description) { + $routeDescription = $route->getDescription(); + if ('' !== $routeDescription) { + $operation->description = $routeDescription; + } + } + $attributes = path_parse($route->getPath()); foreach ($attributes as $attribute) { diff --git a/tests/OpenApiTest.php b/tests/OpenApiTest.php index 2a96692..17cfcf8 100644 --- a/tests/OpenApiTest.php +++ b/tests/OpenApiTest.php @@ -345,4 +345,54 @@ public function testNotIncludeUndescribedRoutes() : void ], ], $object->toArray()); } + + /** + * @return void + */ + public function testRouteData() : void + { + $handler = $this->createMock(RequestHandlerInterface::class); + + $route = $this->createMock(RouteInterface::class); + $route->method('getRequestHandler')->willReturn($handler); + $route->method('getName')->willReturn('foo'); + $route->method('getMethods')->willReturn(['GET']); + $route->method('getPath')->willReturn('/foo'); + $route->method('getTags')->willReturn(['foo', 'bar']); + $route->method('getSummary')->willReturn('summary of the route'); + $route->method('getDescription')->willReturn('description of the route'); + + $object = new OpenApi(new Info('foo', 'bar')); + $object->addRoute($route); + + $this->assertSame([ + 'openapi' => '3.0.2', + 'info' => [ + 'title' => 'foo', + 'version' => 'bar', + ], + 'paths' => [ + '/foo' => [ + 'get' => [ + 'operationId' => 'foo', + 'tags' => ['foo', 'bar'], + 'summary' => 'summary of the route', + 'description' => 'description of the route', + ], + ], + ], + ], $object->toArray()); + } + + /** + * @return void + */ + public function testJson() : void + { + $object = new OpenApi(new Info('foo', 'bar')); + + $expected = json_encode($object->toArray(), \JSON_PRETTY_PRINT); + + $this->assertSame($expected, $object->toJson(\JSON_PRETTY_PRINT)); + } }