Skip to content
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

Remove unused tags #1625

Merged
merged 1 commit into from
Jul 10, 2024
Merged
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
34 changes: 23 additions & 11 deletions src/Processors/AugmentTags.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion tests/Fixtures/Scratch/Tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -18,7 +20,7 @@
#[OAT\Get(
path: '/endpoint',
description: 'Sandbox endpoint',
tags: ['sandbox'],
tags: ['sandbox', 'other'],
responses: [
new OAT\Response(
response: 200,
Expand Down
6 changes: 5 additions & 1 deletion tests/Fixtures/Scratch/Tags3.0.0.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ paths:
get:
tags:
- sandbox
- other
description: 'Sandbox endpoint'
operationId: f1c7f5a59b9708596c2bc955017002e6
responses:
Expand All @@ -19,4 +20,7 @@ paths:
tags:
-
name: sandbox
description: sandbox
description: 'Sandbox tag'
-
name: other
description: other
6 changes: 5 additions & 1 deletion tests/Fixtures/Scratch/Tags3.1.0.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ paths:
get:
tags:
- sandbox
- other
description: 'Sandbox endpoint'
operationId: f1c7f5a59b9708596c2bc955017002e6
responses:
Expand All @@ -19,4 +20,7 @@ paths:
tags:
-
name: sandbox
description: sandbox
description: 'Sandbox tag'
-
name: other
description: other