• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 10508490522

22 Aug 2024 12:54PM UTC coverage: 64.191% (+0.002%) from 64.189%
10508490522

push

github

web-flow
fix: parameter context for filters (#6535)

2 of 3 new or added lines in 3 files covered. (66.67%)

180 existing lines in 28 files now uncovered.

11532 of 17965 relevant lines covered (64.19%)

68.58 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

36.11
/src/Symfony/EventListener/QueryParameterValidateListener.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
declare(strict_types=1);
13

14
namespace ApiPlatform\Symfony\EventListener;
15

16
use ApiPlatform\Doctrine\Odm\State\Options as ODMOptions;
17
use ApiPlatform\Doctrine\Orm\State\Options;
18
use ApiPlatform\Metadata\HttpOperation;
19
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
20
use ApiPlatform\ParameterValidator\ParameterValidator;
21
use ApiPlatform\State\ProviderInterface;
22
use ApiPlatform\State\Util\OperationRequestInitiatorTrait;
23
use ApiPlatform\State\Util\RequestParser;
24
use ApiPlatform\Symfony\Util\RequestAttributesExtractor;
25
use Symfony\Component\HttpKernel\Event\RequestEvent;
26

27
/**
28
 * Validates query parameters depending on filter description.
29
 *
30
 * @author Julien Deniau <julien.deniau@mapado.com>
31
 *
32
 * @deprecated
33
 */
34
final class QueryParameterValidateListener
35
{
36
    use OperationRequestInitiatorTrait;
37

38
    public const OPERATION_ATTRIBUTE_KEY = 'query_parameter_validate';
39
    private ?ParameterValidator $queryParameterValidator = null;
40
    private ?ProviderInterface $provider = null;
41

42
    public function __construct($queryParameterValidator, ?ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory = null)
43
    {
UNCOV
44
        if ($queryParameterValidator instanceof ProviderInterface) {
114✔
45
            $this->provider = $queryParameterValidator;
×
46
        } else {
UNCOV
47
            trigger_deprecation('api-platform/core', '3.3', 'Use a "%s" as first argument in "%s" instead of "%s".', ProviderInterface::class, self::class, ParameterValidator::class);
114✔
UNCOV
48
            $this->queryParameterValidator = $queryParameterValidator;
114✔
49
        }
50

UNCOV
51
        $this->resourceMetadataCollectionFactory = $resourceMetadataCollectionFactory;
114✔
52
    }
53

54
    public function onKernelRequest(RequestEvent $event): void
55
    {
UNCOV
56
        $request = $event->getRequest();
114✔
UNCOV
57
        $operation = $this->initializeOperation($request);
114✔
58

59
        if (
UNCOV
60
            !$request->isMethodSafe()
114✔
UNCOV
61
            || !($attributes = RequestAttributesExtractor::extractAttributes($request))
114✔
UNCOV
62
            || 'GET' !== $request->getMethod()
114✔
UNCOV
63
            || $request->attributes->get('_api_platform_disable_listeners')
114✔
64
        ) {
UNCOV
65
            return;
40✔
66
        }
67

UNCOV
68
        if ('api_platform.symfony.main_controller' === $operation?->getController()) {
92✔
UNCOV
69
            return;
92✔
70
        }
71

72
        if (!($operation->getExtraProperties()['use_legacy_parameter_validator'] ?? true)) {
×
73
            return;
×
74
        }
75

76
        if (!($operation?->getQueryParameterValidationEnabled() ?? true) || !$operation instanceof HttpOperation) {
×
77
            return;
×
78
        }
79

80
        if ($this->provider instanceof ProviderInterface) {
×
81
            if (null === $operation->getQueryParameterValidationEnabled()) {
×
82
                $operation = $operation->withQueryParameterValidationEnabled('GET' === $request->getMethod());
×
83
            }
84

85
            $this->provider->provide($operation, $request->attributes->get('_api_uri_variables') ?? [], [
×
86
                'request' => $request,
×
87
                'uri_variables' => $request->attributes->get('_api_uri_variables') ?? [],
×
88
                'resource_class' => $operation->getClass(),
×
89
            ]);
×
90

91
            return;
×
92
        }
93

94
        $queryString = RequestParser::getQueryString($request);
×
95
        $queryParameters = $queryString ? RequestParser::parseRequestParams($queryString) : [];
×
96

97
        $class = $attributes['resource_class'];
×
98

99
        if ($options = $operation->getStateOptions()) {
×
100
            if ($options instanceof Options && $options->getEntityClass()) {
×
101
                $class = $options->getEntityClass();
×
102
            }
103

104
            if ($options instanceof ODMOptions && $options->getDocumentClass()) {
×
105
                $class = $options->getDocumentClass();
×
106
            }
107
        }
108

109
        $this->queryParameterValidator->validateFilters($class, $operation->getFilters() ?? [], $queryParameters);
×
110
    }
111
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc