Skip to content

Commit

Permalink
Merge pull request #12 from sunrise-php/release/v1.3.0
Browse files Browse the repository at this point in the history
Option to not include undescribed operations
  • Loading branch information
fenric authored Feb 8, 2020
2 parents 1041422 + 961fdab commit 4bf576e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
30 changes: 28 additions & 2 deletions src/OpenApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ class OpenApi extends AbstractObject
*/
private $annotationReader;

/**
* @var bool
*/
private $includeUndescribedOperations = true;

/**
* @param Info $info
*/
Expand All @@ -159,6 +164,16 @@ public function __construct(Info $info)
$this->annotationReader->addNamespace(self::ANNOTATIONS_NAMESPACE);
}

/**
* @param bool $value
*
* @return void
*/
public function includeUndescribedOperations(bool $value) : void
{
$this->includeUndescribedOperations = $value;
}

/**
* @param Server ...$servers
*
Expand All @@ -182,6 +197,10 @@ public function addRoute(RouteInterface ...$routes) : void
$path = path_plain($route->getPath());
$operation = $this->fetchOperation($route);

if (null === $operation) {
continue;
}

$this->addComponentObject(...$operation->getReferencedObjects($this->annotationReader));

foreach ($route->getMethods() as $method) {
Expand Down Expand Up @@ -245,18 +264,25 @@ public function setExternalDocs(ExternalDocumentation $externalDocs) : void
* This method always returns an instance of the Operation Object,
* even if the given route doesn't contain it.
*
* If you do not want to include undescribed operations,
* use the `$openapi->includeUndescribedOperations(false)` method.
*
* @param RouteInterface $route
*
* @return OperationAnnotation
* @return null|OperationAnnotation
*/
private function fetchOperation(RouteInterface $route) : OperationAnnotation
private function fetchOperation(RouteInterface $route) : ?OperationAnnotation
{
$operation = $this->annotationReader->getClassAnnotation(
new ReflectionClass($route->getRequestHandler()),
OperationAnnotation::class
);

if (null === $operation) {
if (false === $this->includeUndescribedOperations) {
return null;
}

$operation = new OperationAnnotation();
}

Expand Down
26 changes: 26 additions & 0 deletions tests/OpenApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -319,4 +319,30 @@ public function testAddRouteWithoutDescription() : void
],
], $object->toArray());
}

/**
* @return void
*/
public function testNotIncludeUndescribedRoutes() : 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');

$object = new OpenApi(new Info('foo', 'bar'));
$object->includeUndescribedOperations(false);
$object->addRoute($route);

$this->assertSame([
'openapi' => '3.0.2',
'info' => [
'title' => 'foo',
'version' => 'bar',
],
], $object->toArray());
}
}

0 comments on commit 4bf576e

Please sign in to comment.