Skip to content

Added tags #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: v0.7
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion config/laravel-swagger.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,21 @@

/*
|--------------------------------------------------------------------------
| Parse summary and descriptions
| Generate tags from controller name
|--------------------------------------------------------------------------
|
| If true creates a tag for an action from its controller name.
| (e.g. Methods in the UserController will be tagged with "User")
| This option does not overwrite any user specified tags on methods.
| Add "@tags tagName1 tagName2" to a methods phpDoc to use custom tags
|
*/

'autoTags' => false,

/*
|--------------------------------------------------------------------------
| Parse security
|--------------------------------------------------------------------------
|
| Tries to parse your middleware and defines the security definitions of
Expand Down
23 changes: 19 additions & 4 deletions src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Mtrajano\LaravelSwagger\Definitions\Security\Contracts\SecurityDefinitionsGenerator;
use Mtrajano\LaravelSwagger\Definitions\Security\SecurityDefinitionsFactory;
use Mtrajano\LaravelSwagger\Responses\ResponseGenerator;
use phpDocumentor\Reflection\DocBlock\Tags\Generic;
use phpDocumentor\Reflection\DocBlockFactory;
use ReflectionMethod;

Expand Down Expand Up @@ -175,14 +176,20 @@ protected function generatePath(): void
$actionInstance = $this->getActionClassInstance();
$docBlock = $actionInstance ? ($actionInstance->getDocComment() ?: '') : '';

[$isDeprecated, $summary, $description] = $this->parseActionDocBlock($docBlock);
[$isDeprecated, $summary, $description, $tags] = $this->parseActionDocBlock($docBlock);

if ($this->config['autoTags'] && empty($tags)) {
$className = $actionInstance->getDeclaringClass()->getShortName();
$tags = [substr($className, 0, strpos($className, 'Controller'))];
}

$path = $this->getRouteUri();

$this->docs['paths'][$path][$this->method] = [
'summary' => $summary,
'description' => $description,
'deprecated' => $isDeprecated,
'tags' => $tags,
];

$this->addActionDefinitions();
Expand Down Expand Up @@ -283,7 +290,7 @@ private function getActionClassInstance(): ?ReflectionMethod
private function parseActionDocBlock(string $docBlock): array
{
if (empty($docBlock) || !$this->config['parseDocBlock']) {
return [false, '', ''];
return [false, '', '', []];
}

try {
Expand All @@ -294,9 +301,17 @@ private function parseActionDocBlock(string $docBlock): array
$summary = $parsedComment->getSummary();
$description = (string) $parsedComment->getDescription();

return [$isDeprecated, $summary, $description];
/** @var Generic $actionTag */
$actionTag = collect($parsedComment->getTagsByName('tags'))->first();
if ($actionTag !== null && $actionTag->getDescription() !== null) {
$tags = explode(' ', $actionTag->getDescription()->getBodyTemplate());
} else {
$tags = [];
}

return [$isDeprecated, $summary, $description, $tags];
} catch (\Exception $e) {
return [false, '', ''];
return [false, '', '', []];
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/SwaggerDocsManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function getSwaggerFileUrl(string $version): string

$fileName = $this->generateSwaggerFileName($version, $format);

return sprintf("%s/%s", config('app.url'), $fileName);
return sprintf('%s/%s', config('app.url'), $fileName);
}

/**
Expand Down Expand Up @@ -158,6 +158,7 @@ private function _getFilledWithGlobalConfigs(array $version): array
$version['description'] = $this->_config['description'];
$version['schemes'] = $this->_config['schemes'];
$version['parseDocBlock'] = $this->_config['parseDocBlock'];
$version['autoTags'] = $this->_config['autoTags'];
$version['parseSecurity'] = $this->_config['parseSecurity'];
$version['generateExampleData'] = $this->_config['generateExampleData'];
$version['parseModelRelationships'] = $this->_config['parseModelRelationships'];
Expand Down
1 change: 1 addition & 0 deletions tests/Feature/SwaggerDocsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public function testGetSwaggerUiUsingDefaultVersion()
'host' => env('APP_URL'),
'schemes' => [],
'parseDocBlock' => true,
'autoTags' => false,
'parseSecurity' => true,
'generateExampleData' => true,
'parseModelRelationships' => true,
Expand Down
1 change: 1 addition & 0 deletions tests/GeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ public function testRouteData($paths)
$this->assertEquals('', $paths['/users/{id}']['get']['summary']);
$this->assertEquals(false, $paths['/users/{id}']['get']['deprecated']);
$this->assertEquals('', $paths['/users/{id}']['get']['description']);
$this->assertEquals(['user', 'show'], $paths['/users/{id}']['get']['tags']);

$this->assertEquals('', $paths['/users/details']['get']['summary']);
$this->assertEquals(true, $paths['/users/details']['get']['deprecated']);
Expand Down
3 changes: 3 additions & 0 deletions tests/Stubs/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ public function index()
return json_encode([['first_name' => 'John'], ['first_name' => 'Jack']]);
}

/**
* @tags user show
*/
public function show(UserShowRequest $request, $id)
{
return json_encode(['first_name' => 'John']);
Expand Down
5 changes: 4 additions & 1 deletion tests/SwaggerDocsManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ protected function setUp(): void
'description' => '',
'schemes' => [],
'parseDocBlock' => true,
'autoTags' => false,
'parseSecurity' => true,
'generateExampleData' => true,
'parseModelRelationships' => true,
Expand Down Expand Up @@ -68,6 +69,7 @@ protected function setUp(): void
'title' => '',
'description' => '',
'parseDocBlock' => true,
'autoTags' => false,
'parseSecurity' => true,
'generateExampleData' => true,
'parseModelRelationships' => true,
Expand Down Expand Up @@ -151,7 +153,8 @@ public function provideInvalidFileNames(): array
/**
* @dataProvider provideInvalidFileNames
*/
public function testChangeFileNameGeneratorReturningInvalidFileName($invalidFileName): void {
public function testChangeFileNameGeneratorReturningInvalidFileName($invalidFileName): void
{
$this->expectException(RuntimeException::class);

SwaggerDocsManager::setFileNameGenerator(
Expand Down