Skip to content

Commit

Permalink
Allow finding child attributes in method args
Browse files Browse the repository at this point in the history
  • Loading branch information
ruscon authored and Oleg Coroliov committed Dec 23, 2022
1 parent b8f7731 commit 1fcf960
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 10 deletions.
11 changes: 6 additions & 5 deletions src/Analysers/AttributeAnnotationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
namespace OpenApi\Analysers;

use OpenApi\Annotations as OA;
use OpenApi\Attributes as OAT;
use OpenApi\Context;
use OpenApi\Generator;

Expand Down Expand Up @@ -52,13 +51,15 @@ public function build(\Reflector $reflector, Context $context): array
if ($reflector instanceof \ReflectionMethod) {
// also look at parameter attributes
foreach ($reflector->getParameters() as $rp) {
foreach ([OAT\Property::class, OAT\Parameter::class, OAT\PathParameter::class] as $attributeName) {
foreach ($rp->getAttributes($attributeName) as $attribute) {
foreach ([OA\Property::class, OA\Parameter::class, OA\RequestBody::class] as $attributeName) {
foreach ($rp->getAttributes($attributeName, \ReflectionAttribute::IS_INSTANCEOF) as $attribute) {
$instance = $attribute->newInstance();
$type = (($rnt = $rp->getType()) && $rnt instanceof \ReflectionNamedType) ? $rnt->getName() : Generator::UNDEFINED;
$nullable = $rnt ? $rnt->allowsNull() : true;

if ($instance instanceof OAT\Property) {
if ($instance instanceof OA\RequestBody) {
$instance->required = !$nullable;
} else if ($instance instanceof OA\Property) {
$instance->property = $rp->getName();
if (Generator::isDefault($instance->type)) {
$instance->type = $type;
Expand All @@ -78,7 +79,7 @@ public function build(\Reflector $reflector, Context $context): array

if (($rrt = $reflector->getReturnType()) && $rrt instanceof \ReflectionNamedType) {
foreach ($annotations as $annotation) {
if ($annotation instanceof OAT\Property && Generator::isDefault($annotation->type)) {
if ($annotation instanceof OA\Property && Generator::isDefault($annotation->type)) {
// pick up simple return types
$annotation->type = $rrt->getName();
}
Expand Down
16 changes: 16 additions & 0 deletions src/Attributes/CookieParameter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php declare(strict_types=1);

/**
* @license Apache 2.0
*/

namespace OpenApi\Attributes;

#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::TARGET_PROPERTY | \Attribute::TARGET_PARAMETER | \Attribute::IS_REPEATABLE)]
class CookieParameter extends Parameter
{
/**
* @inheritdoc
*/
public $in = 'cookie';
}
16 changes: 16 additions & 0 deletions src/Attributes/HeaderParameter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php declare(strict_types=1);

/**
* @license Apache 2.0
*/

namespace OpenApi\Attributes;

#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::TARGET_PROPERTY | \Attribute::TARGET_PARAMETER | \Attribute::IS_REPEATABLE)]
class HeaderParameter extends Parameter
{
/**
* @inheritdoc
*/
public $in = 'header';
}
2 changes: 1 addition & 1 deletion src/Attributes/ParameterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function __construct(
) {
parent::__construct([
'parameter' => $parameter ?? Generator::UNDEFINED,
'name' => $name ?? Generator::UNDEFINED,
'name' => Generator::isDefault($this->name) ? $name : $this->name,
'description' => $description ?? Generator::UNDEFINED,
'in' => Generator::isDefault($this->in) ? $in : $this->in,
'required' => $required ?? Generator::UNDEFINED,
Expand Down
12 changes: 10 additions & 2 deletions src/Attributes/PathParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@
namespace OpenApi\Attributes;

#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::TARGET_PROPERTY | \Attribute::TARGET_PARAMETER | \Attribute::IS_REPEATABLE)]
class PathParameter extends \OpenApi\Annotations\PathParameter
class PathParameter extends Parameter
{
use ParameterTrait;
/**
* @inheritdoc
*/
public $in = 'path';

/**
* @inheritdoc
*/
public $required = true;
}
16 changes: 16 additions & 0 deletions src/Attributes/QueryParameter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php declare(strict_types=1);

/**
* @license Apache 2.0
*/

namespace OpenApi\Attributes;

#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::TARGET_PROPERTY | \Attribute::TARGET_PARAMETER | \Attribute::IS_REPEATABLE)]
class QueryParameter extends Parameter
{
/**
* @inheritdoc
*/
public $in = 'query';
}
5 changes: 3 additions & 2 deletions src/Attributes/RequestBody.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
namespace OpenApi\Attributes;

use OpenApi\Generator;
use OpenApi\Annotations as OA;

#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::TARGET_PARAMETER)]
class RequestBody extends \OpenApi\Annotations\RequestBody
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::TARGET_PROPERTY | \Attribute::TARGET_PARAMETER)]
class RequestBody extends OA\RequestBody
{
/**
* @param array<MediaType>|JsonContent|XmlContent|Attachable|null $content
Expand Down

0 comments on commit 1fcf960

Please sign in to comment.