Skip to content

Commit

Permalink
Merge pull request #47 from cnizzardini/enhancement/42-custom-operati…
Browse files Browse the repository at this point in the history
…on-tags

Override default tag attribute #42
  • Loading branch information
cnizzardini committed May 28, 2020
2 parents 6944f86 + ee00192 commit ab14909
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 7 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,11 +234,12 @@ public function index() {}
```

#### `@Swag\SwagOperation`
Method level annotation for hiding a controller action from swagger.
Method level annotation for OpenApi Operations. Toggle visibility with isVisible and customize tag names which default
to the controllers name.

```php
/**
* @SwagOperation(isVisible=false)
* @SwagOperation(isVisible=false, tagNames={"MyTag","AnotherTag"})
*/
public function index() {}
```
Expand Down
7 changes: 6 additions & 1 deletion src/Lib/Annotation/SwagOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,21 @@
* @Target({"METHOD"})
* @Attributes({
* @Attribute("isVisible", type="bool"),
* @Attribute("tagNames", type="array"),
* })
*/
class SwagOperation
{
/** @var bool */
public $isVisible;

/** @var string[] */
public $tagNames;

public function __construct(array $values)
{
$values = array_merge(['isVisible' => true], $values);
$values = array_merge(['isVisible' => true, 'tagNames' => []], $values);
$this->isVisible = (bool) $values['isVisible'];
$this->tagNames = $values['tagNames'];
}
}
30 changes: 26 additions & 4 deletions src/Lib/Operation/OperationFromRouteFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,9 @@ public function create(RouteDecorator $route, string $httpMethod, ?Schema $schem
->setSummary($docBlock->getSummary())
->setDescription($docBlock->getDescription())
->setHttpMethod(strtolower($httpMethod))
->setOperationId($route->getName())
->setTags([
Inflector::humanize(Inflector::underscore($route->getController()))
]);
->setOperationId($route->getName());

$operation = $this->getOperationWithTags($operation, $route, $annotations);

$args = [$config, $operation, $docBlock, $annotations, $route, $schema];

Expand Down Expand Up @@ -142,4 +141,27 @@ private function isVisible(array $annotations) : bool

return $swagOperation->isVisible === false ? false : true;
}

/**
* @param Operation $operation
* @param RouteDecorator $route
* @param array $annotations
* @return Operation]
*/
private function getOperationWithTags(Operation $operation, RouteDecorator $route, array $annotations) : Operation
{
$swagOperations = array_filter($annotations, function ($annotation) {
return $annotation instanceof SwagOperation;
});

$swagOperation = reset($swagOperations);

if (empty($swagOperation->tagNames)) {
return $operation->setTags([
Inflector::humanize(Inflector::underscore($route->getController()))
]);
}

return $operation->setTags($swagOperation->tagNames);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@ public function testCreate()
$route = reset($routes);

$operation = (new OperationFromRouteFactory($swagger))->create($route, 'GET', null);

$this->assertInstanceOf(Operation::class, $operation);
$this->assertEquals('GET', $operation->getHttpMethod());
$this->assertEquals('employees:index', $operation->getOperationId());
$this->assertEquals('CustomTag', $operation->getTags()[1]);
}
}
1 change: 1 addition & 0 deletions tests/test_app/src/Controller/EmployeesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function initialize() : void
/**
* Gets Employees
*
* @Swag\SwagOperation(tagNames={"Employees","CustomTag"})
* @return \Cake\Http\Response|null|void Renders view
*/
public function index()
Expand Down

0 comments on commit ab14909

Please sign in to comment.