Skip to content

Commit

Permalink
fix handle header param and add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskapp committed Aug 21, 2024
1 parent e295e2b commit 2848c5e
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/Parser/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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;
}
Expand Down
61 changes: 61 additions & 0 deletions tests/Parser/Attribute/PropertyController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
/*
* PSX is an open source PHP framework to develop RESTful APIs.
* For the current version and information visit <https://phpsx.org>
*
* Copyright (c) Christoph Kappestein <[email protected]>
*
* 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 <[email protected]>
* @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');
}
}
10 changes: 10 additions & 0 deletions tests/Parser/AttributeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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());
}
}

0 comments on commit 2848c5e

Please sign in to comment.