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

Validating query arguments of type array #182

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
40 changes: 35 additions & 5 deletions src/PSR7/Validators/QueryArgumentsValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ServerRequestInterface;

use function parse_str;
use function array_filter;
use function explode;
use function urldecode;

/**
* @see https://swagger.io/docs/specification/describing-parameters/
Expand Down Expand Up @@ -69,11 +71,39 @@ private function validateQueryArguments(OperationAddress $addr, array $parsedQue
private function parseQueryArguments(RequestInterface $message): array
{
if ($message instanceof ServerRequestInterface) {
$parsedQueryArguments = $message->getQueryParams();
} else {
parse_str($message->getUri()->getQuery(), $parsedQueryArguments);
return $message->getQueryParams();
}

return $parsedQueryArguments;
return $this->parseQueryString($message->getUri()->getQuery());
}

/**
* @see https://www.php.net/manual/en/function.parse-str.php#76792
*
* @return array
*/
private function parseQueryString(string $queryString): array
{
$queryParameterPairs = explode('&', urldecode($queryString));
$filteredParameterPairs = array_filter(
$queryParameterPairs,
static function ($item) {
return $item !== '';
}
);

$arr = [];
foreach ($filteredParameterPairs as $i) {
[$key, $value] = explode('=', $i);

if (! isset($arr[$key])) {
$arr[$key] = $value;
continue;
}

$arr[$key] = [$arr[$key], $value];
}

return $arr;
}
}
25 changes: 20 additions & 5 deletions tests/PSR7/BaseValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,27 @@ protected function makeGoodServerRequest(string $path, string $method): ServerRe
switch ($method . ' ' . $path) {
case 'get /read':
return $request
->withUri(new Uri($path . '?filter=age&limit=10'))
->withQueryParams(['filter' => 'age', 'limit' => 10, 'offset' => 0]);
->withUri(new Uri($path))
->withQueryParams([
'filter' => 'age',
'limit' => 10,
'offset' => 0,
'queryArgB[]' => [
'value1',
'20',
],
]);

case 'get /path1':
return $request
->withUri(new Uri($path . '?queryArgA=20'))
->withUri(new Uri($path))
->withQueryParams([
'queryArgA' => '20',
'queryArgB[]' => [
'value1',
'20',
],
])
->withHeader('Header-A', 'value A');

case 'post /cookies':
Expand All @@ -86,11 +101,11 @@ protected function makeGoodRequest(string $path, string $method): RequestInterfa
switch ($method . ' ' . $path) {
case 'get /read':
return $request
->withUri(new Uri($path . '?filter=age&limit=10&offset=0'));
->withUri(new Uri($path . '?filter=age&limit=10&offset=0&queryArgB[]=value1&queryArgB[]=value2'));

case 'get /path1':
return $request
->withUri(new Uri($path . '?queryArgA=20'))
->withUri(new Uri($path . '?queryArgA=20&queryArgB[]=value1&queryArgB[]=value2'))
->withHeader('Header-A', 'value A');

case 'post /cookies':
Expand Down
10 changes: 9 additions & 1 deletion tests/PSR7/HeadersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,15 @@ final class HeadersTest extends BaseValidatorTest
{
public function testItValidatesRequestQueryArgumentsGreen(): void
{
$request = (new ServerRequest('get', new Uri('/path1?queryArgA=20')))->withHeader('header-a', 'value A');
$request = (new ServerRequest('get', new Uri('/path1')))
->withQueryParams([
'queryArgA' => 20,
'queryArgB[]' => [
'value1',
'value2',
],
])
->withHeader('header-a', 'value A');

$validator = (new ValidatorBuilder())->fromYamlFile($this->apiSpecFile)->getServerRequestValidator();
$validator->validate($request);
Expand Down
8 changes: 7 additions & 1 deletion tests/PSR7/RoutedServerRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ final class RoutedServerRequestTest extends BaseValidatorTest
{
public function testItValidatesMessageGreen(): void
{
$request = $this->makeGoodServerRequest('/path1', 'get');
$request = $this->makeGoodServerRequest('/path1', 'get')
->withQueryParams([
'queryArgB[]' => [
'value1',
'value2',
],
]);

$validator = (new ValidatorBuilder())->fromYamlFile($this->apiSpecFile)->getRoutedRequestValidator();
$validator->validate(new OperationAddress('/path1', 'get'), $request);
Expand Down
3 changes: 2 additions & 1 deletion tests/PSR7/ServerRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ final class ServerRequestTest extends BaseValidatorTest
{
public function testItValidatesMessageGreen(): void
{
$request = $this->makeGoodServerRequest('/path1', 'get');
$request = $this->makeGoodServerRequest('/path1', 'get')
->withQueryParams(['queryArgB[]' => ['value1']]);

$validator = (new ValidatorBuilder())->fromYamlFile($this->apiSpecFile)->getServerRequestValidator();
$validator->validate($request);
Expand Down
1 change: 1 addition & 0 deletions tests/stubs/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ paths:
parameters:
- $ref: 'schemas.yaml#/components/parameters/HeaderA'
- $ref: 'schemas.yaml#/components/parameters/QueryArgumentA'
- $ref: 'schemas.yaml#/components/parameters/QueryArgumentB'
description: Get Path1
responses:
200:
Expand Down
10 changes: 10 additions & 0 deletions tests/stubs/schemas.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,13 @@ components:
schema:
type: number
format: float
QueryArgumentB:
in: query
name: queryArgB[]
description: query argument B which is array
required: true
schema:
type: array
items:
- type: string
example: value1