From ddb21552f8dc3418e844f2276643c366424e7644 Mon Sep 17 00:00:00 2001 From: Bastien Philippe Date: Fri, 23 Feb 2024 10:37:41 +0100 Subject: [PATCH 1/2] Query param can be an array --- src/Validation/RequestValidator.php | 5 ++++- tests/Fixtures/Arrays.v1.yml | 14 ++++++++++++++ tests/RequestValidatorTest.php | 17 +++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/Validation/RequestValidator.php b/src/Validation/RequestValidator.php index 5d6b3aa..74be429 100644 --- a/src/Validation/RequestValidator.php +++ b/src/Validation/RequestValidator.php @@ -219,7 +219,10 @@ private function hasQueryParam(string $parameterName): bool return Arr::has($this->request->query->all(), $this->convertQueryParameterToDotted($parameterName)); } - private function getQueryParam(string $parameterName): ?string + /** + * @return string|array|null + */ + private function getQueryParam(string $parameterName): string|array|null { return Arr::get($this->request->query->all(), $this->convertQueryParameterToDotted($parameterName)); } diff --git a/tests/Fixtures/Arrays.v1.yml b/tests/Fixtures/Arrays.v1.yml index e1f91f7..a666a23 100644 --- a/tests/Fixtures/Arrays.v1.yml +++ b/tests/Fixtures/Arrays.v1.yml @@ -87,5 +87,19 @@ paths: type: array items: type: string + /parameter-as-array: + parameters: + - name: arrayParam + in: query + schema: + type: array + items: + type: string + get: + summary: Get arrays of string + tags: [ ] + responses: + '204': + description: No Content components: schemas: {} diff --git a/tests/RequestValidatorTest.php b/tests/RequestValidatorTest.php index 8a3449e..22c0f15 100644 --- a/tests/RequestValidatorTest.php +++ b/tests/RequestValidatorTest.php @@ -519,6 +519,23 @@ public function test_handles_query_parameters(): void ->assertValid(); } + public function test_handles_array_query_parameters(): void + { + Spectator::using('Arrays.v1.yml'); + + // When testing query parameters, they are not found nor checked by RequestValidator->validateParameters(). + Route::get('/parameter-as-array', function () { + return response()->noContent(); + })->middleware(Middleware::class); + + $this->get('/parameter-as-array?arrayParam=foo') + ->assertValidationMessage("The data (string) must match the type: array") + ->assertInvalidRequest(); + + $this->get('/parameter-as-array?arrayParam[]=foo&arrayParam[]=bar') + ->assertValidRequest(); + } + public function test_handles_query_parameters_int(): void { Spectator::using('Test.v1.json'); From 8cdd7f64e42ec451295767e397b7f2803e6eb59d Mon Sep 17 00:00:00 2001 From: bastien-phi Date: Fri, 23 Feb 2024 09:38:32 +0000 Subject: [PATCH 2/2] PHP Linting (Pint) --- tests/RequestValidatorTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/RequestValidatorTest.php b/tests/RequestValidatorTest.php index 22c0f15..1dc4e45 100644 --- a/tests/RequestValidatorTest.php +++ b/tests/RequestValidatorTest.php @@ -529,7 +529,7 @@ public function test_handles_array_query_parameters(): void })->middleware(Middleware::class); $this->get('/parameter-as-array?arrayParam=foo') - ->assertValidationMessage("The data (string) must match the type: array") + ->assertValidationMessage('The data (string) must match the type: array') ->assertInvalidRequest(); $this->get('/parameter-as-array?arrayParam[]=foo&arrayParam[]=bar')