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

api-platform / core / 9838430407

08 Jul 2024 11:06AM UTC coverage: 64.738% (-0.1%) from 64.857%
9838430407

push

github

web-flow
feat: deprecate query parameter validator (#6454)

51 of 60 new or added lines in 8 files covered. (85.0%)

34 existing lines in 10 files now uncovered.

11480 of 17733 relevant lines covered (64.74%)

68.99 hits per line

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

0.0
/src/ParameterValidator/Validator/Required.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\ParameterValidator\Validator;
15

16
use ApiPlatform\State\Util\RequestParser;
17

18
/**
19
 * @deprecated use Parameter constraint instead
20
 */
21
final class Required implements ValidatorInterface
22
{
23
    use CheckFilterDeprecationsTrait;
24

25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function validate(string $name, array $filterDescription, array $queryParameters): array
29
    {
30
        // filter is not required, the `checkRequired` method can not break
UNCOV
31
        if (!($filterDescription['required'] ?? false)) {
×
UNCOV
32
            return [];
×
33
        }
34

35
        // if query param is not given, then break
36
        if (!$this->requestHasQueryParameter($queryParameters, $name)) {
×
37
            return [
×
38
                sprintf('Query parameter "%s" is required', $name),
×
39
            ];
×
40
        }
41

42
        $this->checkFilterDeprecations($filterDescription);
×
43

44
        // if query param is empty and the configuration does not allow it
45
        if (!($filterDescription['openapi']['allowEmptyValue'] ?? $filterDescription['swagger']['allowEmptyValue'] ?? false) && empty($this->requestGetQueryParameter($queryParameters, $name))) {
×
46
            return [
×
47
                sprintf('Query parameter "%s" does not allow empty value', $name),
×
48
            ];
×
49
        }
50

51
        return [];
×
52
    }
53

54
    /**
55
     * Test if request has required parameter.
56
     */
57
    private function requestHasQueryParameter(array $queryParameters, string $name): bool
58
    {
59
        $matches = RequestParser::parseRequestParams($name);
×
60
        if (!$matches) {
×
61
            return false;
×
62
        }
63

64
        $rootName = array_keys($matches)[0] ?? '';
×
65
        if (!$rootName) {
×
66
            return false;
×
67
        }
68

69
        if (\is_array($matches[$rootName])) {
×
70
            $keyName = array_keys($matches[$rootName])[0];
×
71

72
            $queryParameter = $queryParameters[(string) $rootName] ?? null;
×
73

74
            return \is_array($queryParameter) && isset($queryParameter[$keyName]);
×
75
        }
76

77
        return \array_key_exists((string) $rootName, $queryParameters);
×
78
    }
79

80
    /**
81
     * Test if required filter is valid. It validates array notation too like "required[bar]".
82
     */
83
    private function requestGetQueryParameter(array $queryParameters, string $name)
84
    {
85
        $matches = RequestParser::parseRequestParams($name);
×
86
        if (empty($matches)) {
×
87
            return null;
×
88
        }
89

90
        $rootName = array_keys($matches)[0] ?? '';
×
91
        if (!$rootName) {
×
92
            return null;
×
93
        }
94

95
        if (\is_array($matches[$rootName])) {
×
96
            $keyName = array_keys($matches[$rootName])[0];
×
97

98
            $queryParameter = $queryParameters[(string) $rootName] ?? null;
×
99

100
            if (\is_array($queryParameter) && isset($queryParameter[$keyName])) {
×
101
                return $queryParameter[$keyName];
×
102
            }
103

104
            return null;
×
105
        }
106

107
        return $queryParameters[(string) $rootName];
×
108
    }
109
}
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

© 2026 Coveralls, Inc