Skip to content

Commit

Permalink
Deduplicate declared tags (zircote#1634)
Browse files Browse the repository at this point in the history
  • Loading branch information
DerManoMann authored Jul 26, 2024
1 parent c469b10 commit e462ff5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/Processors/AugmentTags.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ public function __invoke(Analysis $analysis)
$declaredTags[$tag->name] = $tag;
}
}
if ($declaredTags) {
// last one wins
$analysis->openapi->tags = array_values($declaredTags);
}

if ($usedTagNames) {
$declatedTagNames = array_keys($declaredTags);
Expand Down
7 changes: 7 additions & 0 deletions tests/Fixtures/SurplusTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,18 @@
description: 'test',
version: '2.0.0'
),
tags: [
// definding tag 'other' globally with nice description
new OAT\Tag('other', 'Other description'),
]
)]
class SurplusTag
{
#[OAT\Get(path: '/world/', tags: ['tag world'], responses: [new OAT\Response(response: '200', description: 'success')])]
#[OAT\Get(path: '/hello/', tags: ['tag hello'], responses: [new OAT\Response(response: '200', description: 'success')])]
#[OAT\Get(path: '/other/', tags: ['other'], responses: [new OAT\Response(response: '200', description: 'success')])]
// also definding tag 'other' with another description
#[OAT\Tag('other', 'Another description')]
public function hello(): void
{
}
Expand Down
15 changes: 14 additions & 1 deletion tests/Processors/AugmentTagsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class AugmentTagsTest extends OpenApiTestCase
/**
* @requires PHP 8.1
*/
public function testAugmentTags(): void
public function testFilteredAugmentTags(): void
{
$this->skipLegacy();

Expand All @@ -25,4 +25,17 @@ public function testAugmentTags(): void

$this->assertCount(1, $analysis->openapi->tags);
}

/**
* @requires PHP 8.1
*/
public function testDedupedAugmentTags(): void
{
$this->skipLegacy();

$analysis = $this->analysisFromFixtures(['SurplusTag.php'], static::processors());

echo $analysis->openapi->toYaml();
$this->assertCount(3, $analysis->openapi->tags, 'Expecting 3 unique tags');
}
}

0 comments on commit e462ff5

Please sign in to comment.