From 49130e3ebb2040d93714b182b5832eb703a5640c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Ondr=C3=A1=C4=8Dek?= Date: Wed, 8 Sep 2021 01:31:12 +0200 Subject: [PATCH] RequestParameter: add parameter location validation and its default value MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Roman Ondráček --- .../Controller/RequestParameter.php | 7 ++++- .../Controller/RequestParameter.phpt | 28 ++++++++++++++++++- .../Loader/DoctrineMultiAnnotationLoader.phpt | 5 ++-- .../Controllers/AnnotationMultiController.php | 4 +-- .../Controllers/AttributeMultiController.php | 4 +-- 5 files changed, 40 insertions(+), 8 deletions(-) diff --git a/src/Core/Annotation/Controller/RequestParameter.php b/src/Core/Annotation/Controller/RequestParameter.php index 53a305aa..553934c6 100644 --- a/src/Core/Annotation/Controller/RequestParameter.php +++ b/src/Core/Annotation/Controller/RequestParameter.php @@ -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; @@ -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, @@ -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; diff --git a/tests/cases/Core/Annotation/Controller/RequestParameter.phpt b/tests/cases/Core/Annotation/Controller/RequestParameter.phpt index 3465e09e..83b201bc 100644 --- a/tests/cases/Core/Annotation/Controller/RequestParameter.phpt +++ b/tests/cases/Core/Annotation/Controller/RequestParameter.phpt @@ -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 @@ -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'); +}); diff --git a/tests/cases/Core/DI/Loader/DoctrineMultiAnnotationLoader.phpt b/tests/cases/Core/DI/Loader/DoctrineMultiAnnotationLoader.phpt index 96425ed6..6ee3e401 100644 --- a/tests/cases/Core/DI/Loader/DoctrineMultiAnnotationLoader.phpt +++ b/tests/cases/Core/DI/Loader/DoctrineMultiAnnotationLoader.phpt @@ -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; @@ -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()); } diff --git a/tests/fixtures/Controllers/AnnotationMultiController.php b/tests/fixtures/Controllers/AnnotationMultiController.php index ce2c264c..a29d8883 100644 --- a/tests/fixtures/Controllers/AnnotationMultiController.php +++ b/tests/fixtures/Controllers/AnnotationMultiController.php @@ -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 diff --git a/tests/fixtures/Controllers/AttributeMultiController.php b/tests/fixtures/Controllers/AttributeMultiController.php index 3a7b2b52..df02dec5 100644 --- a/tests/fixtures/Controllers/AttributeMultiController.php +++ b/tests/fixtures/Controllers/AttributeMultiController.php @@ -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 { }