Skip to content

Commit

Permalink
RequestParameter: add parameter location validation and its default v…
Browse files Browse the repository at this point in the history
…alue

Signed-off-by: Roman Ondráček <ondracek.roman@centrum.cz>
  • Loading branch information
Roman3349 authored and f3l1x committed Sep 8, 2021
1 parent 3d69d66 commit e114cfa
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 8 deletions.
7 changes: 6 additions & 1 deletion src/Annotation/Controller/RequestParameter.php
Expand Up @@ -2,6 +2,7 @@

namespace Apitte\Core\Annotation\Controller;

use Apitte\Core\Schema\EndpointParameter;
use Attribute;
use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor;
use Doctrine\Common\Annotations\Annotation\Target;
Expand Down Expand Up @@ -40,7 +41,7 @@ final class RequestParameter
public function __construct(
string $name,
string $type,
string $in,
string $in = EndpointParameter::IN_PATH,
bool $required = true,
bool $allowEmpty = false,
bool $deprecated = false,
Expand All @@ -55,6 +56,10 @@ public function __construct(
throw new AnnotationException('Empty @RequestParameter type given');
}

if (!in_array($in, EndpointParameter::IN, true)) {
throw new AnnotationException('Invalid @RequestParameter in given');
}

$this->name = $name;
$this->type = $type;
$this->required = $required;
Expand Down
28 changes: 27 additions & 1 deletion tests/cases/Annotation/Controller/RequestParameter.phpt
Expand Up @@ -24,9 +24,28 @@ test(function (): void {
);

Assert::same('Parameter', $requestParameter->getName());
Assert::same('Desc', $requestParameter->getDescription());
Assert::same(EndpointParameter::TYPE_STRING, $requestParameter->getType());
Assert::same(EndpointParameter::IN_QUERY, $requestParameter->getIn());
Assert::true($requestParameter->isRequired());
Assert::false($requestParameter->isAllowEmpty());
Assert::false($requestParameter->isDeprecated());
Assert::same('Desc', $requestParameter->getDescription());
});

// OK - short
test(function (): void {
$requestParameter = new RequestParameter(
'Parameter',
EndpointParameter::TYPE_STRING
);

Assert::same('Parameter', $requestParameter->getName());
Assert::same(EndpointParameter::TYPE_STRING, $requestParameter->getType());
Assert::same(EndpointParameter::IN_PATH, $requestParameter->getIn());
Assert::true($requestParameter->isRequired());
Assert::false($requestParameter->isAllowEmpty());
Assert::false($requestParameter->isDeprecated());
Assert::null($requestParameter->getDescription());
});

// Exception - no type
Expand All @@ -35,3 +54,10 @@ test(function (): void {
new RequestParameter('Param', '', EndpointParameter::IN_PATH);
}, AnnotationException::class, 'Empty @RequestParameter type given');
});

// Exception - invalid parameter location
test(function (): void {
Assert::exception(function (): void {
new RequestParameter('Param', EndpointParameter::TYPE_STRING, 'invalid');
}, AnnotationException::class, 'Invalid @RequestParameter in given');
});
5 changes: 3 additions & 2 deletions tests/cases/DI/Loader/DoctrineMultiAnnotationLoader.phpt
Expand Up @@ -8,6 +8,7 @@ require_once __DIR__ . '/../../../bootstrap.php';

use Apitte\Core\DI\Loader\DoctrineAnnotationLoader;
use Apitte\Core\Schema\Builder\Controller\Controller;
use Apitte\Core\Schema\EndpointParameter;
use Apitte\Core\Schema\SchemaBuilder;
use Nette\DI\ContainerBuilder;
use Tester\Assert;
Expand Down Expand Up @@ -62,14 +63,14 @@ function testRequestParameters(Controller $controller): void

Assert::equal('name_value', $firstParameter->getName());
Assert::equal('type_value', $firstParameter->getType());
Assert::equal('in_value', $firstParameter->getIn());
Assert::equal(EndpointParameter::IN_PATH, $firstParameter->getIn());
Assert::null($firstParameter->getDescription());

$secondParameter = $requestParametersMethod->getParameters()['name_value_2'];

Assert::equal('name_value_2', $secondParameter->getName());
Assert::equal('type_value_2', $secondParameter->getType());
Assert::equal('in_value_2', $secondParameter->getIn());
Assert::equal(EndpointParameter::IN_QUERY, $secondParameter->getIn());
Assert::null($secondParameter->getDescription());
}

Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/Controllers/AnnotationMultiController.php
Expand Up @@ -29,8 +29,8 @@ public function responses(): void

/**
* @RequestParameters({
* @RequestParameter(name="name_value", type="type_value", in="in_value"),
* @RequestParameter(in="in_value_2", type="type_value_2", name="name_value_2")
* @RequestParameter(name="name_value", type="type_value", in="path"),
* @RequestParameter(in="query", type="type_value_2", name="name_value_2")
* })
*/
public function requestParameters(): void
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/Controllers/AttributeMultiController.php
Expand Up @@ -18,8 +18,8 @@ public function responses(): void
{
}

#[RequestParameter(name: 'name_value', type: 'type_value', in: 'in_value')]
#[RequestParameter(in: 'in_value_2', type: 'type_value_2', name: 'name_value_2')]
#[RequestParameter(name: 'name_value', type: 'type_value', in: 'path')]
#[RequestParameter(in: 'query', type: 'type_value_2', name: 'name_value_2')]
public function requestParameters(): void
{
}
Expand Down

0 comments on commit e114cfa

Please sign in to comment.