Skip to content

Commit

Permalink
toJson() was added; full support for route properties
Browse files Browse the repository at this point in the history
  • Loading branch information
fenric committed Apr 20, 2020
1 parent d264d9c commit 1a8429e
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/OpenApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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) {
Expand Down
50 changes: 50 additions & 0 deletions tests/OpenApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

0 comments on commit 1a8429e

Please sign in to comment.