From 2848c5e2ef41d4c508c52587cb54d1f09b468aa6 Mon Sep 17 00:00:00 2001 From: Christoph Kappestein Date: Wed, 21 Aug 2024 20:00:54 +0200 Subject: [PATCH] fix handle header param and add test case --- src/Parser/Attribute.php | 5 +- tests/Parser/Attribute/PropertyController.php | 61 +++++++++++++++++++ tests/Parser/AttributeTest.php | 10 +++ 3 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 tests/Parser/Attribute/PropertyController.php diff --git a/src/Parser/Attribute.php b/src/Parser/Attribute.php index cd39a35..7645d27 100644 --- a/src/Parser/Attribute.php +++ b/src/Parser/Attribute.php @@ -447,6 +447,9 @@ private function getFromAttribute(\ReflectionParameter $parameter, \ReflectionMe foreach ($attributes as $attribute) { $param = $attribute->newInstance(); $args = $this->getParamArgsFromType($parameter->getName(), $parameter->getType(), !$parameter->isOptional()); + if (empty($args)) { + continue; + } if ($param instanceof Attr\Param) { if (!empty($param->name)) { @@ -468,7 +471,7 @@ private function getFromAttribute(\ReflectionParameter $parameter, \ReflectionMe } return new Attr\QueryParam(...$args); - } elseif ($param instanceof Attr\HeaderParam) { + } elseif ($param instanceof Attr\Header) { if (!empty($param->name)) { $args[0] = $param->name; } diff --git a/tests/Parser/Attribute/PropertyController.php b/tests/Parser/Attribute/PropertyController.php new file mode 100644 index 0000000..30bc5e4 --- /dev/null +++ b/tests/Parser/Attribute/PropertyController.php @@ -0,0 +1,61 @@ + + * + * Copyright (c) Christoph Kappestein + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +namespace PSX\Api\Tests\Parser\Attribute; + +use PSX\Api\Attribute\Body; +use PSX\Api\Attribute\Description; +use PSX\Api\Attribute\Get; +use PSX\Api\Attribute\Header; +use PSX\Api\Attribute\Param; +use PSX\Api\Attribute\Path; +use PSX\Api\Attribute\Query; +use PSX\Api\Tests\Parser\Model\Incoming; +use PSX\Api\Tests\Parser\Model\Outgoing; +use PSX\DateTime\DateTime; +use PSX\DateTime\DayOfWeek; + +/** + * PropertyController + * + * @author Christoph Kappestein + * @license http://www.apache.org/licenses/LICENSE-2.0 + * @link https://phpsx.org + */ +class PropertyController +{ + #[Get] + #[Path('/foo/:fooId')] + #[Description('Test description')] + protected function doGet( + #[Header('Content-Type')] string $contentType, + #[Param] string $fooId, + #[Query] string $foo, + #[Query] int $integer, + #[Query] float $number, + #[Query] DateTime $date, + #[Query] bool $boolean, + #[Query] string $string, + #[Query] DayOfWeek $dayOfWeek, + #[Body] Incoming $body): Outgoing + { + return new Outgoing('foo'); + } +} diff --git a/tests/Parser/AttributeTest.php b/tests/Parser/AttributeTest.php index 4e1e378..57260ca 100644 --- a/tests/Parser/AttributeTest.php +++ b/tests/Parser/AttributeTest.php @@ -25,6 +25,7 @@ use PSX\Api\Parser\Attribute as AttributeParser; use PSX\Api\SpecificationInterface; use PSX\Api\Tests\Parser\Attribute\BarController; +use PSX\Api\Tests\Parser\Attribute\PropertyController; use PSX\Api\Tests\Parser\Attribute\TestController; use PSX\Schema\TypeFactory; @@ -78,4 +79,13 @@ public function testParseInvalid() $annotation = new AttributeParser($this->schemaManager); $annotation->parse('foo'); } + + public function testParseProperty() + { + $specification = $this->apiManager->getApi(PropertyController::class); + $operation = $specification->getOperations()->get('PSX.Api.Tests.Parser.Attribute.PropertyController.doGet'); + + $this->assertInstanceOf(OperationInterface::class, $operation); + $this->assertEquals('Test description', $operation->getDescription()); + } }