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

api-platform / core / 9210912913

23 May 2024 03:37PM UTC coverage: 57.176% (-0.3%) from 57.512%
9210912913

Pull #6385

github

web-flow
Merge 146e374c2 into 002d8e514
Pull Request #6385: fix(symfony): Make `WriteListener` compatible with `MainController`

1 of 1 new or added line in 1 file covered. (100.0%)

59 existing lines in 15 files now uncovered.

9987 of 17467 relevant lines covered (57.18%)

36.38 hits per line

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

0.0
/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
final class QueryParameterValidateListener
33
{
34
    use OperationRequestInitiatorTrait;
35

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

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

UNCOV
49
        $this->resourceMetadataCollectionFactory = $resourceMetadataCollectionFactory;
×
50
    }
51

52
    public function onKernelRequest(RequestEvent $event): void
53
    {
UNCOV
54
        $request = $event->getRequest();
×
UNCOV
55
        $operation = $this->initializeOperation($request);
×
56

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

UNCOV
66
        if ('api_platform.symfony.main_controller' === $operation?->getController()) {
×
UNCOV
67
            return;
×
68
        }
69

70
        if (!($operation?->getQueryParameterValidationEnabled() ?? true) || !$operation instanceof HttpOperation) {
×
71
            return;
×
72
        }
73

74
        if ($this->provider instanceof ProviderInterface) {
×
75
            if (null === $operation->getQueryParameterValidationEnabled()) {
×
76
                $operation = $operation->withQueryParameterValidationEnabled('GET' === $request->getMethod());
×
77
            }
78

79
            $this->provider->provide($operation, $request->attributes->get('_api_uri_variables') ?? [], [
×
80
                'request' => $request,
×
81
                'uri_variables' => $request->attributes->get('_api_uri_variables') ?? [],
×
82
                'resource_class' => $operation->getClass(),
×
83
            ]);
×
84

85
            return;
×
86
        }
87

88
        $queryString = RequestParser::getQueryString($request);
×
89
        $queryParameters = $queryString ? RequestParser::parseRequestParams($queryString) : [];
×
90

91
        $class = $attributes['resource_class'];
×
92

93
        if ($options = $operation->getStateOptions()) {
×
94
            if ($options instanceof Options && $options->getEntityClass()) {
×
95
                $class = $options->getEntityClass();
×
96
            }
97

98
            if ($options instanceof ODMOptions && $options->getDocumentClass()) {
×
99
                $class = $options->getDocumentClass();
×
100
            }
101
        }
102

103
        $this->queryParameterValidator->validateFilters($class, $operation->getFilters() ?? [], $queryParameters);
×
104
    }
105
}
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