Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query param can be an array #185

Merged
merged 2 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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