Skip to content
Closed
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
1 change: 1 addition & 0 deletions bin/openapi
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ foreach ($options["processor"] as $processor) {
}

$analyser = new ReflectionAnalyser([new DocBlockAnnotationFactory(), new AttributeAnnotationFactory()]);
$analyser->setGenerator($generator);

$openapi = $generator
->setVersion($options['version'])
Expand Down
5 changes: 1 addition & 4 deletions src/Analysers/AnalyserInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,8 @@

use OpenApi\Analysis;
use OpenApi\Context;
use OpenApi\Generator;

interface AnalyserInterface
interface AnalyserInterface extends GeneratorAwareInterface
{
public function setGenerator(Generator $generator): void;

public function fromFile(string $filename, Context $context): Analysis;
}
4 changes: 2 additions & 2 deletions src/Analysers/AnnotationFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
use OpenApi\Context;
use OpenApi\Generator;

interface AnnotationFactoryInterface
interface AnnotationFactoryInterface extends GeneratorAwareInterface
{
/**
* Checks if this factory is supported by the current runtime.
*/
public function isSupported(): bool;

public function setGenerator(Generator $generator): void;
public function setGenerator(Generator $generator);

/**
* @return array<OA\AbstractAnnotation> top level annotations
Expand Down
8 changes: 6 additions & 2 deletions src/Analysers/AttributeAnnotationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,17 @@ public function build(\Reflector $reflector, Context $context): array
/** @var OA\AbstractAnnotation[] $annotations */
$annotations = [];
try {
foreach ($reflector->getAttributes() as $attribute) {
$attributeArgs = $this->generator->isIgnoreOtherAttributes()
? [OA\AbstractAnnotation::class, \ReflectionAttribute::IS_INSTANCEOF]
: [];

foreach ($reflector->getAttributes(...$attributeArgs) as $attribute) {
if (class_exists($attribute->getName())) {
$instance = $attribute->newInstance();
if ($instance instanceof OA\AbstractAnnotation) {
$annotations[] = $instance;
} else {
if ($context->is('other') === false) {
if (false === $context->is('other')) {
$context->other = [];
}
$context->other[] = $instance;
Expand Down
4 changes: 3 additions & 1 deletion src/Analysers/DocBlockAnnotationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ public function isSupported(): bool
return DocBlockParser::isEnabled();
}

public function setGenerator(Generator $generator): void
public function setGenerator(Generator $generator)
{
$this->generator = $generator;

$this->docBlockParser->setAliases($generator->getAliases());

return $this;
}

public function build(\Reflector $reflector, Context $context): array
Expand Down
14 changes: 14 additions & 0 deletions src/Analysers/GeneratorAwareInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php declare(strict_types=1);

/**
* @license Apache 2.0
*/

namespace OpenApi\Analysers;

use OpenApi\Generator;

interface GeneratorAwareInterface
{
public function setGenerator(Generator $generator);
}
4 changes: 3 additions & 1 deletion src/Analysers/GeneratorAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ trait GeneratorAwareTrait
{
protected ?Generator $generator = null;

public function setGenerator(Generator $generator): void
public function setGenerator(Generator $generator)
{
$this->generator = $generator;

return $this;
}
}
4 changes: 3 additions & 1 deletion src/Analysers/ReflectionAnalyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,15 @@ public function __construct(array $annotationFactories = [])
}
}

public function setGenerator(Generator $generator): void
public function setGenerator(Generator $generator)
{
$this->generator = $generator;

foreach ($this->annotationFactories as $annotationFactory) {
$annotationFactory->setGenerator($generator);
}

return $this;
}

public function fromFile(string $filename, Context $context): Analysis
Expand Down
23 changes: 22 additions & 1 deletion src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@
*
* This is an object-oriented alternative to using the now deprecated <code>\OpenApi\scan()</code> function and
* static class properties of the <code>Analyzer</code> and <code>Analysis</code> classes.
*
* Supported generator config:
* <code>
* [
* 'generator' => [
* 'ignoreOtherAttributes' => true|false,
* ]
* ]
* </code>
*/
class Generator
{
Expand Down Expand Up @@ -135,6 +144,9 @@ public function setAnalyser(?AnalyserInterface $analyser): Generator
return $this;
}

/**
* @deprecated
*/
public function getDefaultConfig(): array
{
return [
Expand All @@ -146,7 +158,16 @@ public function getDefaultConfig(): array

public function getConfig(): array
{
return $this->config + $this->getDefaultConfig();
return $this->config + $this->getDefaultConfig() + [
'generator' => [
'ignoreOtherAttributes' => false,
],
];
}

public function isIgnoreOtherAttributes(): bool
{
return $this->getConfig()['generator']['ignoreOtherAttributes'];
}

protected function normaliseConfig(array $config): array
Expand Down
30 changes: 27 additions & 3 deletions tests/Analysers/AttributeAnnotationFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
namespace OpenApi\Tests\Analysers;

use OpenApi\Analysers\AttributeAnnotationFactory;
use OpenApi\Generator;
use OpenApi\Tests\Fixtures\UsingAttributes;
use OpenApi\Tests\Fixtures\InvalidPropertyAttribute;
use OpenApi\Tests\OpenApiTestCase;
Expand All @@ -16,11 +17,22 @@
*/
class AttributeAnnotationFactoryTest extends OpenApiTestCase
{
public function testReturnedAnnotationsCout(): void
protected function getFactory(?array $config = null): AttributeAnnotationFactory
{
$generator = new Generator();
if (null !== $config) {
$generator->setConfig($config);
}

return (new AttributeAnnotationFactory())
->setGenerator($generator);
}

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

$annotations = (new AttributeAnnotationFactory())->build($rc, $this->getContext());
$annotations = $this->getFactory()->build($rc, $this->getContext());
$this->assertCount(1, $annotations);
}

Expand All @@ -32,6 +44,18 @@ public function testErrorOnInvalidAttribute(): void
$this->expectException(\TypeError::class);
$this->expectExceptionMessage('OpenApi\Attributes\Property::__construct(): Argument #8 ($required) must be of type ?array');

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

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

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

$this->getFactory(['generator' => ['ignoreOtherAttributes' => 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
10 changes: 10 additions & 0 deletions tests/GeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,14 @@ public function testConfig(array $config, bool $expected): void
$generator->setConfig($config);
$this->assertOperationIdHash($generator, $expected);
}

public function testIsIgnoreOtherAttributes(): void
{
$generator = new Generator();

$this->assertFalse($generator->isIgnoreOtherAttributes());

$generator->setConfig(['generator' => ['ignoreOtherAttributes' => true]]);
$this->assertTrue($generator->isIgnoreOtherAttributes());
}
}