Skip to content

Commit df587cd

Browse files
authored
Allow to ignore non OpenApi attributes (#1756)
1 parent c7e8d1f commit df587cd

File tree

5 files changed

+38
-6
lines changed

5 files changed

+38
-6
lines changed

src/Analysers/AttributeAnnotationFactory.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ class AttributeAnnotationFactory implements AnnotationFactoryInterface
1515
{
1616
use GeneratorAwareTrait;
1717

18+
protected bool $ignoreOtherAttributes = false;
19+
20+
public function __construct(bool $ignoreOtherAttributes = false)
21+
{
22+
$this->ignoreOtherAttributes = $ignoreOtherAttributes;
23+
}
24+
1825
public function isSupported(): bool
1926
{
2027
return \PHP_VERSION_ID >= 80100;
@@ -37,7 +44,11 @@ public function build(\Reflector $reflector, Context $context): array
3744
/** @var OA\AbstractAnnotation[] $annotations */
3845
$annotations = [];
3946
try {
40-
foreach ($reflector->getAttributes() as $attribute) {
47+
$attributeName = $this->ignoreOtherAttributes
48+
? [OA\AbstractAnnotation::class, \ReflectionAttribute::IS_INSTANCEOF]
49+
: [];
50+
51+
foreach ($reflector->getAttributes(...$attributeName) as $attribute) {
4152
if (class_exists($attribute->getName())) {
4253
$instance = $attribute->newInstance();
4354
if ($instance instanceof OA\AbstractAnnotation) {

src/Generator.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class Generator
5757
*
5858
* If set, it will override the version set in the <code>OpenApi</code> annotation.
5959
*
60-
* Due to the order of processing any conditional code using this (via <code>Context::$version</code>)
60+
* Due to the order of processing, any conditional code using this (via <code>Context::$version</code>)
6161
* must come only after the analysis is finished.
6262
*/
6363
protected ?string $version = null;
@@ -122,7 +122,11 @@ public function setNamespaces(?array $namespaces): Generator
122122

123123
public function getAnalyser(): AnalyserInterface
124124
{
125-
$this->analyser = $this->analyser ?: new ReflectionAnalyser([new AttributeAnnotationFactory(), new DocBlockAnnotationFactory()]);
125+
$generatorConfig = $this->getConfig()['generator'];
126+
$this->analyser = $this->analyser ?: new ReflectionAnalyser([
127+
new AttributeAnnotationFactory($generatorConfig['ignoreOtherAttributes']),
128+
new DocBlockAnnotationFactory(),
129+
]);
126130
$this->analyser->setGenerator($this);
127131

128132
return $this->analyser;
@@ -138,6 +142,9 @@ public function setAnalyser(?AnalyserInterface $analyser): Generator
138142
public function getDefaultConfig(): array
139143
{
140144
return [
145+
'generator' => [
146+
'ignoreOtherAttributes' => false,
147+
],
141148
'operationId' => [
142149
'hash' => true,
143150
],

tests/Analysers/AttributeAnnotationFactoryTest.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717
class AttributeAnnotationFactoryTest extends OpenApiTestCase
1818
{
19-
public function testReturnedAnnotationsCout(): void
19+
public function testReturnedAnnotationsCount(): void
2020
{
2121
$rc = new \ReflectionClass(UsingAttributes::class);
2222

@@ -34,4 +34,16 @@ public function testErrorOnInvalidAttribute(): void
3434

3535
(new AttributeAnnotationFactory())->build($rm, $this->getContext());
3636
}
37+
38+
public function testIgnoreOtherAttributes(): void
39+
{
40+
$rc = new \ReflectionClass(UsingAttributes::class);
41+
42+
(new AttributeAnnotationFactory())->build($rc, $context = $this->getContext());
43+
$this->assertIsArray($context->other);
44+
$this->assertCount(1, $context->other);
45+
46+
(new AttributeAnnotationFactory(true))->build($rc, $context = $this->getContext());
47+
$this->assertNull($context->other);
48+
}
3749
}

tests/Fixtures/PHP/Label.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66

77
namespace OpenApi\Tests\Fixtures\PHP;
88

9-
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::IS_REPEATABLE)]
9+
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_PROPERTY | \Attribute::IS_REPEATABLE)]
1010
class Label
1111
{
1212
protected $name;
1313

14-
public function __construct(string $name, array $numbers)
14+
public function __construct(string $name, array $numbers = [])
1515
{
1616
$this->name = $name;
1717
}

tests/Fixtures/UsingAttributes.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
namespace OpenApi\Tests\Fixtures;
88

99
use OpenApi\Attributes as OAT;
10+
use OpenApi\Tests\Fixtures\PHP\Label;
1011

12+
#[Label(name: 'custom')]
1113
#[OAT\Response()]
1214
#[OAT\Header(header: 'X-Rate-Limit', allowEmptyValue: true)]
1315
class UsingAttributes

0 commit comments

Comments
 (0)