diff --git a/src/Processors/AugmentTags.php b/src/Processors/AugmentTags.php index 307dde2d..a8f63a4c 100644 --- a/src/Processors/AugmentTags.php +++ b/src/Processors/AugmentTags.php @@ -20,24 +20,36 @@ public function __invoke(Analysis $analysis) /** @var OA\Operation[] $operations */ $operations = $analysis->getAnnotationsOfType(OA\Operation::class); - $usedTags = []; + $usedTagNames = []; foreach ($operations as $operation) { if (!Generator::isDefault($operation->tags)) { - $usedTags = array_merge($usedTags, $operation->tags); + $usedTagNames = array_merge($usedTagNames, $operation->tags); } } + $usedTagNames = array_unique($usedTagNames); - if ($usedTags) { - $usedTags = array_unique($usedTags); - $declaredTags = []; - if (!Generator::isDefault($analysis->openapi->tags)) { - foreach ($analysis->openapi->tags as $tag) { - $declaredTags[] = $tag->name; + $declaredTags = []; + if (!Generator::isDefault($analysis->openapi->tags)) { + foreach ($analysis->openapi->tags as $tag) { + $declaredTags[$tag->name] = $tag; + } + } + + if ($usedTagNames) { + $declatedTagNames = array_keys($declaredTags); + foreach ($usedTagNames as $tagName) { + if (!in_array($tagName, $declatedTagNames)) { + $analysis->openapi->merge([new OA\Tag(['name' => $tagName, 'description' => $tagName])]); } } - foreach ($usedTags as $tag) { - if (!in_array($tag, $declaredTags)) { - $analysis->openapi->merge([new OA\Tag(['name' => $tag, 'description' => $tag])]); + } + + foreach ($declaredTags as $tag) { + if (!in_array($tag->name, $usedTagNames)) { + if (false !== $index = array_search($tag, $analysis->openapi->tags)) { + $analysis->annotations->detach($tag); + unset($analysis->openapi->tags[$index]); + $analysis->openapi->tags = array_values($analysis->openapi->tags); } } } diff --git a/tests/Fixtures/Scratch/Tags.php b/tests/Fixtures/Scratch/Tags.php index 09f15c05..59127b67 100644 --- a/tests/Fixtures/Scratch/Tags.php +++ b/tests/Fixtures/Scratch/Tags.php @@ -8,6 +8,8 @@ use OpenApi\Attributes as OAT; +#[OAT\Tag(name: 'sandbox', description: 'Sandbox tag')] +#[OAT\Tag(name: 'unused', description: 'Not used')] #[OAT\Info( title: 'Tags', description: 'Tag Scratch', @@ -18,7 +20,7 @@ #[OAT\Get( path: '/endpoint', description: 'Sandbox endpoint', - tags: ['sandbox'], + tags: ['sandbox', 'other'], responses: [ new OAT\Response( response: 200, diff --git a/tests/Fixtures/Scratch/Tags3.0.0.yaml b/tests/Fixtures/Scratch/Tags3.0.0.yaml index 0d6caf6f..ba8880e4 100644 --- a/tests/Fixtures/Scratch/Tags3.0.0.yaml +++ b/tests/Fixtures/Scratch/Tags3.0.0.yaml @@ -11,6 +11,7 @@ paths: get: tags: - sandbox + - other description: 'Sandbox endpoint' operationId: f1c7f5a59b9708596c2bc955017002e6 responses: @@ -19,4 +20,7 @@ paths: tags: - name: sandbox - description: sandbox + description: 'Sandbox tag' + - + name: other + description: other diff --git a/tests/Fixtures/Scratch/Tags3.1.0.yaml b/tests/Fixtures/Scratch/Tags3.1.0.yaml index 013508b9..dc063dff 100644 --- a/tests/Fixtures/Scratch/Tags3.1.0.yaml +++ b/tests/Fixtures/Scratch/Tags3.1.0.yaml @@ -11,6 +11,7 @@ paths: get: tags: - sandbox + - other description: 'Sandbox endpoint' operationId: f1c7f5a59b9708596c2bc955017002e6 responses: @@ -19,4 +20,7 @@ paths: tags: - name: sandbox - description: sandbox + description: 'Sandbox tag' + - + name: other + description: other