Skip to content

Commit

Permalink
Use AbstractAnnotation::isRoot() as strict check in `Analysis::getA…
Browse files Browse the repository at this point in the history
…nnotationsOfType()` (#1425)
  • Loading branch information
DerManoMann authored Mar 8, 2023
1 parent 40a23b0 commit 33960e0
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 16 deletions.
26 changes: 10 additions & 16 deletions src/Analysis.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,28 +297,22 @@ public function getTraitsOfClass(string $source, bool $direct = false): array
}

/**
* @param string|array $classes One ore more class names
* @param bool $strict in non-strict mode child classes are also detected
* @param class-string|array<class-string> $classes one or more class names
* @param bool $strict in non-strict mode child classes are also detected
*
* @return OA\AbstractAnnotation[]
*/
public function getAnnotationsOfType($classes, bool $strict = false): array
{
$unique = new \SplObjectStorage();
$annotations = [];
if ($strict) {
foreach ((array) $classes as $class) {
foreach ($this->annotations as $annotation) {
if (get_class($annotation) === $class) {
$annotations[] = $annotation;
}
}
}
} else {
foreach ((array) $classes as $class) {
foreach ($this->annotations as $annotation) {
if ($annotation instanceof $class) {
$annotations[] = $annotation;
}

foreach ((array) $classes as $class) {
/** @var OA\AbstractAnnotation $annotation */
foreach ($this->annotations as $annotation) {
if ($annotation instanceof $class && (!$strict || ($annotation->isRoot($class) && !$unique->contains($annotation)))) {
$unique->attach($annotation);
$annotations[] = $annotation;
}
}
}
Expand Down
48 changes: 48 additions & 0 deletions tests/Fixtures/Scratch/AttributeInheritance.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php declare(strict_types=1);

/**
* @license Apache 2.0
*/

namespace OpenApi\Tests\Fixtures\Scratch;

use OpenApi\Attributes as OAT;

#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::TARGET_PROPERTY)]
class AttributeInheritanceSchema extends OAT\Schema
{
}

#[OAT\Schema]
class Base
{
#[OAT\Property]
public int $id;
}

#[AttributeInheritanceSchema]
class Child1 extends Base
{
#[OAT\Property]
public string $name;
}

#[OAT\Schema]
class Child2 extends Base
{
#[OAT\Property]
public string $title;
}

#[OAT\Info(
title: 'Attribute Inheritance Scratch',
version: '1.0'
)]
#[OAT\Get(
path: '/api/endpoint',
description: 'An endpoint',
responses: [new OAT\Response(response: 200, description: 'OK')]
)]
class AttributeInheritanceEndpoint
{
}
36 changes: 36 additions & 0 deletions tests/Fixtures/Scratch/AttributeInheritance.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
openapi: 3.0.0
info:
title: 'Attribute Inheritance Scratch'
version: '1.0'
paths:
/api/endpoint:
get:
description: 'An endpoint'
responses:
'200':
description: OK
components:
schemas:
Base:
properties:
id:
type: integer
type: object
Child1:
type: object
allOf:
-
$ref: '#/components/schemas/Base'
-
properties:
name:
type: string
Child2:
type: object
allOf:
-
$ref: '#/components/schemas/Base'
-
properties:
title:
type: string

0 comments on commit 33960e0

Please sign in to comment.