-
-
Notifications
You must be signed in to change notification settings - Fork 973
throw an exception if required filter is not set #1692
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
Changes from all commits
31e937a
2367a47
da476c1
261e522
d624665
0186719
fe516ff
1e353af
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| Feature: Validate filters based upon filter description | ||
|
|
||
| @createSchema | ||
| Scenario: Required filter should not throw an error if set | ||
| When I am on "/filter_validators?required=foo" | ||
| Then the response status code should be 200 | ||
|
|
||
| When I am on "/filter_validators?required=" | ||
| Then the response status code should be 200 | ||
|
|
||
| Scenario: Required filter should throw an error if not set | ||
| When I am on "/filter_validators" | ||
| Then the response status code should be 400 | ||
| And the JSON node "detail" should be equal to 'Query parameter "required" is required' | ||
|
|
||
| Scenario: Required filter should not throw an error if set | ||
| When I am on "/array_filter_validators?arrayRequired[]=foo&indexedArrayRequired[foo]=foo" | ||
| Then the response status code should be 200 | ||
|
|
||
| Scenario: Required filter should throw an error if not set | ||
| When I am on "/array_filter_validators" | ||
| Then the response status code should be 400 | ||
| And the JSON node "detail" should match '/^Query parameter "arrayRequired\[\]" is required\nQuery parameter "indexedArrayRequired\[foo\]" is required$/' | ||
|
|
||
| When I am on "/array_filter_validators?arrayRequired=foo&indexedArrayRequired[foo]=foo" | ||
| Then the response status code should be 400 | ||
| And the JSON node "detail" should be equal to 'Query parameter "arrayRequired[]" is required' | ||
|
|
||
| When I am on "/array_filter_validators?arrayRequired[foo]=foo" | ||
| Then the response status code should be 400 | ||
| And the JSON node "detail" should match '/^Query parameter "arrayRequired\[\]" is required\nQuery parameter "indexedArrayRequired\[foo\]" is required$/' | ||
|
|
||
| When I am on "/array_filter_validators?arrayRequired[]=foo" | ||
| Then the response status code should be 400 | ||
| And the JSON node "detail" should be equal to 'Query parameter "indexedArrayRequired[foo]" is required' | ||
|
|
||
| When I am on "/array_filter_validators?arrayRequired[]=foo&indexedArrayRequired[bar]=bar" | ||
| Then the response status code should be 400 | ||
| And the JSON node "detail" should be equal to 'Query parameter "indexedArrayRequired[foo]" is required' |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the API Platform project. | ||
| * | ||
| * (c) Kévin Dunglas <dunglas@gmail.com> | ||
| * | ||
| * 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\Core\Exception; | ||
|
|
||
| /** | ||
| * Filter validation exception. | ||
| * | ||
| * @author Julien DENIAU <julien.deniau@gmail.com> | ||
| */ | ||
| final class FilterValidationException extends \Exception implements ExceptionInterface | ||
| { | ||
| private $constraintViolationList; | ||
|
|
||
| public function __construct(array $constraintViolationList, string $message = '', int $code = 0, \Exception $previous = null) | ||
| { | ||
| $this->constraintViolationList = $constraintViolationList; | ||
|
|
||
| parent::__construct($message ?: $this->__toString(), $code, $previous); | ||
| } | ||
|
|
||
| public function __toString(): string | ||
| { | ||
| return implode("\n", $this->constraintViolationList); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the API Platform project. | ||
| * | ||
| * (c) Kévin Dunglas <dunglas@gmail.com> | ||
| * | ||
| * 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\Core\Filter; | ||
|
|
||
| use ApiPlatform\Core\Api\FilterLocatorTrait; | ||
| use ApiPlatform\Core\Exception\FilterValidationException; | ||
| use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface; | ||
| use ApiPlatform\Core\Util\RequestAttributesExtractor; | ||
| use Psr\Container\ContainerInterface; | ||
| use Symfony\Component\HttpKernel\Event\GetResponseEvent; | ||
|
|
||
| /** | ||
| * Validates query parameters depending on filter description. | ||
| * | ||
| * @author Julien Deniau <julien.deniau@gmail.com> | ||
| */ | ||
| final class QueryParameterValidateListener | ||
| { | ||
| use FilterLocatorTrait; | ||
|
|
||
| private $resourceMetadataFactory; | ||
|
|
||
| public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory, ContainerInterface $filterLocator) | ||
| { | ||
| $this->resourceMetadataFactory = $resourceMetadataFactory; | ||
| $this->setFilterLocator($filterLocator); | ||
| } | ||
|
|
||
| public function onKernelRequest(GetResponseEvent $event) | ||
| { | ||
| $request = $event->getRequest(); | ||
| if ( | ||
| !$request->isMethodSafe(false) | ||
| || !($attributes = RequestAttributesExtractor::extractAttributes($request)) | ||
| || !isset($attributes['collection_operation_name']) | ||
| || 'get' !== ($operationName = $attributes['collection_operation_name']) | ||
| ) { | ||
| return; | ||
| } | ||
|
|
||
| $resourceMetadata = $this->resourceMetadataFactory->create($attributes['resource_class']); | ||
| $resourceFilters = $resourceMetadata->getCollectionOperationAttribute($operationName, 'filters', [], true); | ||
|
|
||
| $errorList = []; | ||
| foreach ($resourceFilters as $filterId) { | ||
| if (!$filter = $this->getFilter($filterId)) { | ||
| continue; | ||
| } | ||
|
|
||
| foreach ($filter->getDescription($attributes['resource_class']) as $name => $data) { | ||
| if (!($data['required'] ?? false)) { // property is not required | ||
| continue; | ||
| } | ||
|
|
||
| if (!$this->isRequiredFilterValid($name, $request)) { | ||
| $errorList[] = sprintf('Query parameter "%s" is required', $name); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if ($errorList) { | ||
| throw new FilterValidationException($errorList); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Test if required filter is valid. It validates array notation too like "required[bar]". | ||
| */ | ||
| private function isRequiredFilterValid($name, $request): bool | ||
| { | ||
| $matches = []; | ||
| parse_str($name, $matches); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've mentioned this a long time ago, but we really should stop using bad built-in PHP functions like https://uri.thephpleague.com/5.0/components/parsers/#queryparserextract
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this really necessary for this use case? Maybe can we do that at some point in another PR and replace all calls to |
||
| if (!$matches) { | ||
| return false; | ||
| } | ||
|
|
||
| $rootName = array_keys($matches)[0] ?? ''; | ||
| if (!$rootName) { | ||
| return false; | ||
| } | ||
|
|
||
| if (\is_array($matches[$rootName])) { | ||
| $keyName = array_keys($matches[$rootName])[0]; | ||
|
|
||
| $queryParameter = $request->query->get($rootName); | ||
|
|
||
| return \is_array($queryParameter) && isset($queryParameter[$keyName]); | ||
| } | ||
|
|
||
| return null !== $request->query->get($rootName); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please move it in the
ApiPlatform\EventListenernamespace.What about
ValidateQueryParametersListenerfor the name?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's okay if it's refactored later as said in #1692 (comment) I guess? Anyway if it's an eventlistener it feels weird to have it in filters.