From 8035bcb30ed7f78f0455e7b45d246b0d5d1287f7 Mon Sep 17 00:00:00 2001 From: Paul Johnston Date: Wed, 28 Aug 2024 14:26:38 +1000 Subject: [PATCH] Add wildcard argument to AugmentTags whitelist --- src/Processors/AugmentTags.php | 25 ++++++++++++++++++------- tests/Processors/AugmentTagsTest.php | 21 ++++++++++++++++++++- 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/src/Processors/AugmentTags.php b/src/Processors/AugmentTags.php index a19633e4..25290081 100644 --- a/src/Processors/AugmentTags.php +++ b/src/Processors/AugmentTags.php @@ -17,16 +17,19 @@ class AugmentTags implements ProcessorInterface { /** @var array */ - protected $unusedTagsToKeepWhitelist = []; + protected $whitelist; - public function __construct(array $unusedTagsToKeepWhitelist = []) + public function __construct(array $whitelist = []) { - $this->unusedTagsToKeepWhitelist = $unusedTagsToKeepWhitelist; + $this->whitelist = $whitelist; } - public function setUnusedTagsToKeepWhitelist(array $unusedTagsToKeepWhitelist): AugmentTags + /** + * Whitelist tags to keep even if not used. * may be used to keep all unused. + */ + public function setWhitelist(array $whitelist): AugmentTags { - $this->unusedTagsToKeepWhitelist = $unusedTagsToKeepWhitelist; + $this->whitelist = $whitelist; return $this; } @@ -65,8 +68,16 @@ 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); + $this->removeUnusedTags($usedTagNames, $declaredTags, $analysis); + } + + private function removeUnusedTags(array $usedTagNames, array $declaredTags, Analysis $analysis) + { + if (in_array('*', $this->whitelist)) { + return; + } + + $tagsToKeep = array_merge($usedTagNames, $this->whitelist); foreach ($declaredTags as $tag) { if (!in_array($tag->name, $tagsToKeep)) { if (false !== $index = array_search($tag, $analysis->openapi->tags, true)) { diff --git a/tests/Processors/AugmentTagsTest.php b/tests/Processors/AugmentTagsTest.php index b68ce587..2d049efe 100644 --- a/tests/Processors/AugmentTagsTest.php +++ b/tests/Processors/AugmentTagsTest.php @@ -50,10 +50,29 @@ public function testAllowUnusedTags(): void static::processors(), null, [ - 'augmentTags' => ['unusedTagsToKeepWhitelist' => ['fancy']], + 'augmentTags' => ['whitelist' => ['fancy']], ] ); $this->assertCount(2, $analysis->openapi->tags, 'Expecting fancy tag to be preserved'); } + + /** + * @requires PHP 8.1 + */ + public function testAllowUnusedTagsWildcard(): void + { + $this->skipLegacy(); + + $analysis = $this->analysisFromFixtures( + ['UnusedTags.php'], + static::processors(), + null, + [ + 'augmentTags' => ['whitelist' => ['*']], + ] + ); + + $this->assertCount(3, $analysis->openapi->tags, 'Expecting all tags to be preserved'); + } }