Skip to content

Commit

Permalink
Query param can be an array
Browse files Browse the repository at this point in the history
  • Loading branch information
bastien-phi committed Feb 23, 2024
1 parent 0a765f6 commit ddb2155
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/Validation/RequestValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<array-key, mixed>|null
*/
private function getQueryParam(string $parameterName): string|array|null
{
return Arr::get($this->request->query->all(), $this->convertQueryParameterToDotted($parameterName));
}
Expand Down
14 changes: 14 additions & 0 deletions tests/Fixtures/Arrays.v1.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: {}
17 changes: 17 additions & 0 deletions tests/RequestValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit ddb2155

Please sign in to comment.