Skip to content

Commit

Permalink
feat: use openapi array to validate filter parameters (#5114)
Browse files Browse the repository at this point in the history
* feat: use openapi array to validate filter parameters

The filter validators were only using the 'swagger' entry from the
filter description. It should also use the 'openapi' entry, as swagger
is deprecated.

* test(filters): test using openapi for query parameters validation
  • Loading branch information
norival authored Nov 14, 2022
1 parent 6e2f920 commit 62af874
Show file tree
Hide file tree
Showing 15 changed files with 710 additions and 14 deletions.
12 changes: 8 additions & 4 deletions src/Api/QueryParameterValidator/Validator/ArrayItems.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

final class ArrayItems implements ValidatorInterface
{
use CheckFilterDeprecationsTrait;

/**
* {@inheritdoc}
*/
Expand All @@ -24,9 +26,11 @@ public function validate(string $name, array $filterDescription, array $queryPar
return [];
}

$maxItems = $filterDescription['swagger']['maxItems'] ?? null;
$minItems = $filterDescription['swagger']['minItems'] ?? null;
$uniqueItems = $filterDescription['swagger']['uniqueItems'] ?? false;
$this->checkFilterDeprecations($filterDescription);

$maxItems = $filterDescription['openapi']['maxItems'] ?? $filterDescription['swagger']['maxItems'] ?? null;
$minItems = $filterDescription['openapi']['minItems'] ?? $filterDescription['swagger']['minItems'] ?? null;
$uniqueItems = $filterDescription['openapi']['uniqueItems'] ?? $filterDescription['swagger']['uniqueItems'] ?? false;

$errorList = [];

Expand Down Expand Up @@ -60,7 +64,7 @@ private function getValue(string $name, array $filterDescription, array $queryPa
return $value;
}

$collectionFormat = $filterDescription['swagger']['collectionFormat'] ?? 'csv';
$collectionFormat = $filterDescription['openapi']['collectionFormat'] ?? $filterDescription['swagger']['collectionFormat'] ?? 'csv';

return explode(self::getSeparator($collectionFormat), (string) $value) ?: []; // @phpstan-ignore-line
}
Expand Down
12 changes: 8 additions & 4 deletions src/Api/QueryParameterValidator/Validator/Bounds.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

final class Bounds implements ValidatorInterface
{
use CheckFilterDeprecationsTrait;

/**
* {@inheritdoc}
*/
Expand All @@ -25,21 +27,23 @@ public function validate(string $name, array $filterDescription, array $queryPar
return [];
}

$maximum = $filterDescription['swagger']['maximum'] ?? null;
$minimum = $filterDescription['swagger']['minimum'] ?? null;
$this->checkFilterDeprecations($filterDescription);

$maximum = $filterDescription['openapi']['maximum'] ?? $filterDescription['swagger']['maximum'] ?? null;
$minimum = $filterDescription['openapi']['minimum'] ?? $filterDescription['swagger']['minimum'] ?? null;

$errorList = [];

if (null !== $maximum) {
if (($filterDescription['swagger']['exclusiveMaximum'] ?? false) && $value >= $maximum) {
if (($filterDescription['openapi']['exclusiveMaximum'] ?? $filterDescription['swagger']['exclusiveMaximum'] ?? false) && $value >= $maximum) {
$errorList[] = sprintf('Query parameter "%s" must be less than %s', $name, $maximum);
} elseif ($value > $maximum) {
$errorList[] = sprintf('Query parameter "%s" must be less than or equal to %s', $name, $maximum);
}
}

if (null !== $minimum) {
if (($filterDescription['swagger']['exclusiveMinimum'] ?? false) && $value <= $minimum) {
if (($filterDescription['openapi']['exclusiveMinimum'] ?? $filterDescription['swagger']['exclusiveMinimum'] ?? false) && $value <= $minimum) {
$errorList[] = sprintf('Query parameter "%s" must be greater than %s', $name, $minimum);
} elseif ($value < $minimum) {
$errorList[] = sprintf('Query parameter "%s" must be greater than or equal to %s', $name, $minimum);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Api\QueryParameterValidator\Validator;

/**
* @internal
*/
trait CheckFilterDeprecationsTrait
{
protected function checkFilterDeprecations(array $filterDescription): void
{
if (\array_key_exists('swagger', $filterDescription)) {
trigger_deprecation(
'api-platform/core',
'3.0',
'Using the "swagger" key in filters description is deprecated, use "openapi" instead.'
);
}
}
}
6 changes: 5 additions & 1 deletion src/Api/QueryParameterValidator/Validator/Enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

final class Enum implements ValidatorInterface
{
use CheckFilterDeprecationsTrait;

/**
* {@inheritdoc}
*/
Expand All @@ -25,7 +27,9 @@ public function validate(string $name, array $filterDescription, array $queryPar
return [];
}

$enum = $filterDescription['swagger']['enum'] ?? null;
$this->checkFilterDeprecations($filterDescription);

$enum = $filterDescription['openapi']['enum'] ?? $filterDescription['swagger']['enum'] ?? null;

if (null !== $enum && !\in_array($value, $enum, true)) {
return [
Expand Down
8 changes: 6 additions & 2 deletions src/Api/QueryParameterValidator/Validator/Length.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

final class Length implements ValidatorInterface
{
use CheckFilterDeprecationsTrait;

/**
* {@inheritdoc}
*/
Expand All @@ -25,8 +27,10 @@ public function validate(string $name, array $filterDescription, array $queryPar
return [];
}

$maxLength = $filterDescription['swagger']['maxLength'] ?? null;
$minLength = $filterDescription['swagger']['minLength'] ?? null;
$this->checkFilterDeprecations($filterDescription);

$maxLength = $filterDescription['openapi']['maxLength'] ?? $filterDescription['swagger']['maxLength'] ?? null;
$minLength = $filterDescription['openapi']['minLength'] ?? $filterDescription['swagger']['minLength'] ?? null;

$errorList = [];

Expand Down
6 changes: 5 additions & 1 deletion src/Api/QueryParameterValidator/Validator/MultipleOf.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

final class MultipleOf implements ValidatorInterface
{
use CheckFilterDeprecationsTrait;

/**
* {@inheritdoc}
*/
Expand All @@ -25,7 +27,9 @@ public function validate(string $name, array $filterDescription, array $queryPar
return [];
}

$multipleOf = $filterDescription['swagger']['multipleOf'] ?? null;
$this->checkFilterDeprecations($filterDescription);

$multipleOf = $filterDescription['openapi']['multipleOf'] ?? $filterDescription['swagger']['multipleOf'] ?? null;

if (null !== $multipleOf && 0 !== ($value % $multipleOf)) {
return [
Expand Down
6 changes: 5 additions & 1 deletion src/Api/QueryParameterValidator/Validator/Pattern.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

final class Pattern implements ValidatorInterface
{
use CheckFilterDeprecationsTrait;

/**
* {@inheritdoc}
*/
Expand All @@ -25,7 +27,9 @@ public function validate(string $name, array $filterDescription, array $queryPar
return [];
}

$pattern = $filterDescription['swagger']['pattern'] ?? null;
$this->checkFilterDeprecations($filterDescription);

$pattern = $filterDescription['openapi']['pattern'] ?? $filterDescription['swagger']['pattern'] ?? null;

if (null !== $pattern && !preg_match($pattern, $value)) {
return [
Expand Down
6 changes: 5 additions & 1 deletion src/Api/QueryParameterValidator/Validator/Required.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

final class Required implements ValidatorInterface
{
use CheckFilterDeprecationsTrait;

/**
* {@inheritdoc}
*/
Expand All @@ -34,8 +36,10 @@ public function validate(string $name, array $filterDescription, array $queryPar
];
}

$this->checkFilterDeprecations($filterDescription);

// if query param is empty and the configuration does not allow it
if (!($filterDescription['swagger']['allowEmptyValue'] ?? false) && empty($this->requestGetQueryParameter($queryParameters, $name))) {
if (!($filterDescription['openapi']['allowEmptyValue'] ?? $filterDescription['swagger']['allowEmptyValue'] ?? false) && empty($this->requestGetQueryParameter($queryParameters, $name))) {
return [
sprintf('Query parameter "%s" does not allow empty value', $name),
];
Expand Down
Loading

0 comments on commit 62af874

Please sign in to comment.