Skip to content

Commit

Permalink
Skip constructor properties only for attributes (#1087)
Browse files Browse the repository at this point in the history
* Skip constructor properties only for attributes
* Add promoted properties test with attribute/annotation mix
  • Loading branch information
DerManoMann committed Jan 31, 2022
1 parent 76f495b commit e87cec1
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 13 deletions.
5 changes: 5 additions & 0 deletions src/Analysers/AttributeAnnotationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ public function build(\Reflector $reflector, Context $context): array
return [];
}

if ($reflector instanceof \ReflectionProperty && method_exists($reflector, 'isPromoted') && $reflector->isPromoted()) {
// handled via __construct() parameter
return [];
}

// no proper way to inject
Generator::$context = $context;

Expand Down
4 changes: 0 additions & 4 deletions src/Analysers/ReflectionAnalyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,6 @@ protected function analyzeFqdn(string $fqdn, Analysis $analysis, array $details)

foreach ($rc->getProperties() as $property) {
if (in_array($property->name, $details['properties'])) {
if (method_exists($property, 'isPromoted') && $property->isPromoted()) {
// handled via __construct() parameter
continue;
}
$definition['properties'][$property->getName()] = $ctx = new Context([
'property' => $property->getName(),
'comment' => $property->getDocComment() ?: Generator::UNDEFINED,
Expand Down
25 changes: 25 additions & 0 deletions tests/Analysers/ReflectionAnalyserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@
use OpenApi\Analysers\AttributeAnnotationFactory;
use OpenApi\Analysers\DocBlockAnnotationFactory;
use OpenApi\Analysers\ReflectionAnalyser;
use OpenApi\Analysers\TokenAnalyser;
use OpenApi\Analysis;
use OpenApi\Annotations\OpenApi;
use OpenApi\Annotations\Operation;
use OpenApi\Annotations\Property;
use OpenApi\Annotations\Response;
use OpenApi\Annotations\Schema;
use OpenApi\Attributes\Get;
use OpenApi\Context;
use OpenApi\Generator;
Expand Down Expand Up @@ -170,4 +173,26 @@ public function testApiMixedBasic(AnalyserInterface $analyser): void
$this->assertTrue($analysis->validate());
$this->assertSpecEquals($analysis->openapi, file_get_contents($spec));
}

/**
* @requires PHP 8.1
*/
public function testPhp8PromotedProperties(): void
{
if ($this->getAnalyzer() instanceof TokenAnalyser) {
$this->markTestSkipped();
}

$analysis = $this->analysisFromFixtures(['PHP/Php8PromotedProperties.php']);
$schemas = $analysis->getAnnotationsOfType(Schema::class, true);

$this->assertCount(1, $schemas);
$analysis->process((new Generator())->getProcessors());

/** @var Property[] $properties */
$properties = $analysis->getAnnotationsOfType(Property::class);
$this->assertCount(2, $properties);
$this->assertEquals('id', $properties[0]->property);
$this->assertEquals('labels', $properties[1]->property);
}
}
7 changes: 4 additions & 3 deletions tests/Analysers/TokenAnalyserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,16 +262,17 @@ public function testPhp8AttributeMix(): void
/**
* @requires PHP 8
*/
public function testPhp8NamedProperty(): void
public function testPhp8PromotedProperties(): void
{
$analysis = $this->analysisFromFixtures(['PHP/Php8NamedProperty.php'], [], new TokenAnalyser());
$analysis = $this->analysisFromFixtures(['PHP/Php8PromotedProperties.php'], [], new TokenAnalyser());
$schemas = $analysis->getAnnotationsOfType(Schema::class, true);

$this->assertCount(1, $schemas);
$analysis->process((new Generator())->getProcessors());

/** @var Property[] $properties */
$properties = $analysis->getAnnotationsOfType(Property::class, true);
$properties = $analysis->getAnnotationsOfType(Property::class);
// ignores the attribute on $id
$this->assertCount(1, $properties);
$this->assertEquals('labels', $properties[0]->property);
}
Expand Down
7 changes: 3 additions & 4 deletions tests/Analysers/TokenScannerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,11 @@ public function scanCases()
],
];

yield 'Php8NamedProperty' => [
'PHP/Php8NamedProperty.php',
yield 'Php8PromotedProperties' => [
'PHP/Php8PromotedProperties.php',
[
'OpenApi\\Tests\\Fixtures\\PHP\\Php8NamedProperty' => [
'OpenApi\\Tests\\Fixtures\\PHP\\Php8PromotedProperties' => [
'uses' => [
'Label' => 'OpenApi\\Tests\\Fixtures\\PHP\\Label',
'Property' => 'OpenApi\\Attributes\\Property',
],
'interfaces' => [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@
namespace OpenApi\Tests\Fixtures\PHP;

use OpenApi\Attributes\Property;
use OpenApi\Tests\Fixtures\PHP\Label;

/**
* @OA\Schema()
*/
class Php8NamedProperty
class Php8PromotedProperties
{
public function __construct(
/**
Expand Down

0 comments on commit e87cec1

Please sign in to comment.