-
Notifications
You must be signed in to change notification settings - Fork 938
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
allow unused tags to be kept #1647
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -15,6 +15,22 @@ | |||||||||||
*/ | ||||||||||||
class AugmentTags implements ProcessorInterface | ||||||||||||
{ | ||||||||||||
|
||||||||||||
/** @var array<string> */ | ||||||||||||
protected $unusedTagsToKeepWhitelist = []; | ||||||||||||
|
||||||||||||
public function __construct(array $unusedTagsToKeepWhitelist = []) | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
It think this should be meaningful enough? This class is all about tags, after all. |
||||||||||||
{ | ||||||||||||
$this->unusedTagsToKeepWhitelist = $unusedTagsToKeepWhitelist; | ||||||||||||
} | ||||||||||||
|
||||||||||||
public function setUnusedTagsToKeepWhitelist(array $unusedTagsToKeepWhitelist): AugmentTags | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
The docblock on the |
||||||||||||
{ | ||||||||||||
$this->unusedTagsToKeepWhitelist = $unusedTagsToKeepWhitelist; | ||||||||||||
|
||||||||||||
return $this; | ||||||||||||
} | ||||||||||||
|
||||||||||||
public function __invoke(Analysis $analysis) | ||||||||||||
{ | ||||||||||||
/** @var OA\Operation[] $operations */ | ||||||||||||
|
@@ -39,6 +55,7 @@ public function __invoke(Analysis $analysis) | |||||||||||
$analysis->openapi->tags = array_values($declaredTags); | ||||||||||||
} | ||||||||||||
|
||||||||||||
// Add a tag for each tag that is used in operations but not declared in the global tags | ||||||||||||
if ($usedTagNames) { | ||||||||||||
$declatedTagNames = array_keys($declaredTags); | ||||||||||||
foreach ($usedTagNames as $tagName) { | ||||||||||||
|
@@ -48,8 +65,10 @@ public function __invoke(Analysis $analysis) | |||||||||||
} | ||||||||||||
} | ||||||||||||
|
||||||||||||
// remove tags that we don't want to keep (defaults to all unused tags) | ||||||||||||
$tagsToKeep = array_merge($usedTagNames, $this->unusedTagsToKeepWhitelist); | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could there be a special case of |
||||||||||||
foreach ($declaredTags as $tag) { | ||||||||||||
if (!in_array($tag->name, $usedTagNames)) { | ||||||||||||
if (!in_array($tag->name, $tagsToKeep)) { | ||||||||||||
if (false !== $index = array_search($tag, $analysis->openapi->tags, true)) { | ||||||||||||
$analysis->annotations->detach($tag); | ||||||||||||
unset($analysis->openapi->tags[$index]); | ||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php declare(strict_types=1); | ||
|
||
/** | ||
* @license Apache 2.0 | ||
*/ | ||
|
||
namespace OpenApi\Tests\Fixtures; | ||
|
||
use OpenApi\Attributes as OAT; | ||
|
||
#[OAT\OpenApi( | ||
info: new OAT\Info( | ||
title: 'test', | ||
description: 'test', | ||
version: '2.0.0' | ||
), | ||
tags: [ | ||
// definding tag 'other' globally with nice description | ||
new OAT\Tag('other', 'Other description'), | ||
// making a tag that has no operations referencing it, but we wish to keep it | ||
new OAT\Tag('fancy', 'Fancy description'), | ||
// making a tag that it not used, but we do not wish to keep | ||
new OAT\Tag('notused', 'remove this one'), | ||
] | ||
)] | ||
class UnusedTags | ||
{ | ||
#[OAT\Get(path: '/other/', tags: ['other'], responses: [new OAT\Response(response: '200', description: 'success')])] | ||
public function hello(): void | ||
{ | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the other processors only intialize in the constructor and that should be enough.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
based it off CleanUnusedComponents, makes sense to only do it in one spot though.