Skip to content
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
13 changes: 12 additions & 1 deletion src/Analysers/AttributeAnnotationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ class AttributeAnnotationFactory implements AnnotationFactoryInterface
{
use GeneratorAwareTrait;

protected bool $ignoreOtherAttributes = false;

public function __construct(bool $ignoreOtherAttributes = false)
{
$this->ignoreOtherAttributes = $ignoreOtherAttributes;
}

public function isSupported(): bool
{
return \PHP_VERSION_ID >= 80100;
Expand All @@ -37,7 +44,11 @@ public function build(\Reflector $reflector, Context $context): array
/** @var OA\AbstractAnnotation[] $annotations */
$annotations = [];
try {
foreach ($reflector->getAttributes() as $attribute) {
$attributeName = $this->ignoreOtherAttributes
? [OA\AbstractAnnotation::class, \ReflectionAttribute::IS_INSTANCEOF]
: [];

foreach ($reflector->getAttributes(...$attributeName) as $attribute) {
if (class_exists($attribute->getName())) {
$instance = $attribute->newInstance();
if ($instance instanceof OA\AbstractAnnotation) {
Expand Down
11 changes: 9 additions & 2 deletions src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class Generator
*
* If set, it will override the version set in the <code>OpenApi</code> annotation.
*
* Due to the order of processing any conditional code using this (via <code>Context::$version</code>)
* Due to the order of processing, any conditional code using this (via <code>Context::$version</code>)
* must come only after the analysis is finished.
*/
protected ?string $version = null;
Expand Down Expand Up @@ -122,7 +122,11 @@ public function setNamespaces(?array $namespaces): Generator

public function getAnalyser(): AnalyserInterface
{
$this->analyser = $this->analyser ?: new ReflectionAnalyser([new AttributeAnnotationFactory(), new DocBlockAnnotationFactory()]);
$generatorConfig = $this->getConfig()['generator'];
$this->analyser = $this->analyser ?: new ReflectionAnalyser([
new AttributeAnnotationFactory($generatorConfig['ignoreOtherAttributes']),
new DocBlockAnnotationFactory(),
]);
$this->analyser->setGenerator($this);

return $this->analyser;
Expand All @@ -138,6 +142,9 @@ public function setAnalyser(?AnalyserInterface $analyser): Generator
public function getDefaultConfig(): array
{
return [
'generator' => [
'ignoreOtherAttributes' => false,
],
'operationId' => [
'hash' => true,
],
Expand Down
14 changes: 13 additions & 1 deletion tests/Analysers/AttributeAnnotationFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
class AttributeAnnotationFactoryTest extends OpenApiTestCase
{
public function testReturnedAnnotationsCout(): void
public function testReturnedAnnotationsCount(): void
{
$rc = new \ReflectionClass(UsingAttributes::class);

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

(new AttributeAnnotationFactory())->build($rm, $this->getContext());
}

public function testIgnoreOtherAttributes(): void
{
$rc = new \ReflectionClass(UsingAttributes::class);

(new AttributeAnnotationFactory())->build($rc, $context = $this->getContext());
$this->assertIsArray($context->other);
$this->assertCount(1, $context->other);

(new AttributeAnnotationFactory(true))->build($rc, $context = $this->getContext());
$this->assertNull($context->other);
}
}
4 changes: 2 additions & 2 deletions tests/Fixtures/PHP/Label.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

namespace OpenApi\Tests\Fixtures\PHP;

#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::IS_REPEATABLE)]
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_PROPERTY | \Attribute::IS_REPEATABLE)]
class Label
{
protected $name;

public function __construct(string $name, array $numbers)
public function __construct(string $name, array $numbers = [])
{
$this->name = $name;
}
Expand Down
2 changes: 2 additions & 0 deletions tests/Fixtures/UsingAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
namespace OpenApi\Tests\Fixtures;

use OpenApi\Attributes as OAT;
use OpenApi\Tests\Fixtures\PHP\Label;

#[Label(name: 'custom')]
#[OAT\Response()]
#[OAT\Header(header: 'X-Rate-Limit', allowEmptyValue: true)]
class UsingAttributes
Expand Down