Skip to content

Commit 3d83540

Browse files
authored
Allow to disable default tag descriptions (#1859)
1 parent dfd637f commit 3d83540

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

src/Processors/AugmentTags.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@
1616
class AugmentTags
1717
{
1818
/** @var array<string> */
19-
protected array $whitelist = [];
19+
protected array $whitelist;
20+
protected bool $withDescription;
2021

21-
public function __construct(array $whitelist = [])
22+
public function __construct(array $whitelist = [], bool $withDescription = true)
2223
{
2324
$this->whitelist = $whitelist;
25+
$this->withDescription = $withDescription;
2426
}
2527

2628
/**
@@ -33,6 +35,16 @@ public function setWhitelist(array $whitelist): AugmentTags
3335
return $this;
3436
}
3537

38+
/**
39+
* Enables/disables generation of default tag descriptions.
40+
*/
41+
public function setWithDescription(bool $withDescription): AugmentTags
42+
{
43+
$this->withDescription = $withDescription;
44+
45+
return $this;
46+
}
47+
3648
public function __invoke(Analysis $analysis): void
3749
{
3850
/** @var OA\Operation[] $operations */
@@ -62,7 +74,12 @@ public function __invoke(Analysis $analysis): void
6274
$declatedTagNames = array_keys($declaredTags);
6375
foreach ($usedTagNames as $tagName) {
6476
if (!in_array($tagName, $declatedTagNames)) {
65-
$analysis->openapi->merge([new OA\Tag(['name' => $tagName, 'description' => $tagName])]);
77+
$analysis->openapi->merge([new OA\Tag([
78+
'name' => $tagName,
79+
'description' => $this->withDescription
80+
? $tagName
81+
: Generator::UNDEFINED,
82+
])]);
6683
}
6784
}
6885
}

tests/Processors/AugmentTagsTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
namespace OpenApi\Tests\Processors;
88

9+
use OpenApi\Generator;
910
use OpenApi\Tests\OpenApiTestCase;
1011

1112
class AugmentTagsTest extends OpenApiTestCase
@@ -67,4 +68,23 @@ public function testAllowUnusedTagsWildcard(): void
6768

6869
$this->assertCount(3, $analysis->openapi->tags, 'Expecting all tags to be preserved');
6970
}
71+
72+
/**
73+
* @requires PHP 8.1
74+
*/
75+
public function testWithoutDescriptions(): void
76+
{
77+
$analysis = $this->analysisFromFixtures(
78+
['Processors/EntityControllerClass.php'],
79+
static::processors(),
80+
null,
81+
[
82+
'augmentTags' => ['withDescription' => false],
83+
]
84+
);
85+
86+
$this->assertNotEmpty($analysis->openapi->tags);
87+
$firstTag = $analysis->openapi->tags[0];
88+
$this->assertEquals(Generator::UNDEFINED, $firstTag->description);
89+
}
7090
}

0 commit comments

Comments
 (0)