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

api-platform / core / 11182119487

04 Oct 2024 02:50PM UTC coverage: 7.837% (+0.4%) from 7.441%
11182119487

push

github

soyuka
Merge 4.0

0 of 266 new or added lines in 25 files covered. (0.0%)

9900 existing lines in 360 files now uncovered.

12939 of 165109 relevant lines covered (7.84%)

27.02 hits per line

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

98.81
/src/Validator/Metadata/Resource/Factory/ParameterValidationResourceMetadataCollectionFactory.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\Validator\Metadata\Resource\Factory;
15

16
use ApiPlatform\Metadata\HttpOperation;
17
use ApiPlatform\Metadata\Parameter;
18
use ApiPlatform\Metadata\Parameters;
19
use ApiPlatform\Metadata\QueryParameter;
20
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
21
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
22
use ApiPlatform\OpenApi\Model\Parameter as OpenApiParameter;
23
use Psr\Container\ContainerInterface;
24
use Symfony\Component\Validator\Constraints\Choice;
25
use Symfony\Component\Validator\Constraints\Count;
26
use Symfony\Component\Validator\Constraints\DivisibleBy;
27
use Symfony\Component\Validator\Constraints\GreaterThan;
28
use Symfony\Component\Validator\Constraints\GreaterThanOrEqual;
29
use Symfony\Component\Validator\Constraints\Length;
30
use Symfony\Component\Validator\Constraints\LessThan;
31
use Symfony\Component\Validator\Constraints\LessThanOrEqual;
32
use Symfony\Component\Validator\Constraints\NotBlank;
33
use Symfony\Component\Validator\Constraints\NotNull;
34
use Symfony\Component\Validator\Constraints\Regex;
35
use Symfony\Component\Validator\Constraints\Type;
36
use Symfony\Component\Validator\Constraints\Unique;
37

38
final class ParameterValidationResourceMetadataCollectionFactory implements ResourceMetadataCollectionFactoryInterface
39
{
40
    public function __construct(
41
        private readonly ?ResourceMetadataCollectionFactoryInterface $decorated = null,
42
        private readonly ?ContainerInterface $filterLocator = null,
43
    ) {
UNCOV
44
    }
2,684✔
45

46
    public function create(string $resourceClass): ResourceMetadataCollection
47
    {
UNCOV
48
        $resourceMetadataCollection = $this->decorated?->create($resourceClass) ?? new ResourceMetadataCollection($resourceClass);
111✔
49

UNCOV
50
        foreach ($resourceMetadataCollection as $i => $resource) {
111✔
UNCOV
51
            $operations = $resource->getOperations();
100✔
52

UNCOV
53
            foreach ($operations as $operationName => $operation) {
100✔
UNCOV
54
                $parameters = $operation->getParameters() ?? new Parameters();
100✔
UNCOV
55
                foreach ($parameters as $key => $parameter) {
100✔
UNCOV
56
                    $parameters->add($key, $this->addSchemaValidation($parameter));
15✔
57
                }
58

59
                // As we deprecate the parameter validator, we declare a parameter for each filter transfering validation to the new system
UNCOV
60
                if ($operation->getFilters() && 0 === $parameters->count()) {
100✔
UNCOV
61
                    $parameters = $this->addFilterValidation($operation);
21✔
62
                }
63

UNCOV
64
                if (\count($parameters) > 0) {
100✔
UNCOV
65
                    $operations->add($operationName, $operation->withParameters($parameters));
31✔
66
                }
67
            }
68

UNCOV
69
            $resourceMetadataCollection[$i] = $resource->withOperations($operations->sort());
100✔
70

UNCOV
71
            if (!$graphQlOperations = $resource->getGraphQlOperations()) {
100✔
UNCOV
72
                continue;
49✔
73
            }
74

UNCOV
75
            foreach ($graphQlOperations as $operationName => $operation) {
82✔
UNCOV
76
                $parameters = $operation->getParameters() ?? new Parameters();
82✔
UNCOV
77
                foreach ($operation->getParameters() ?? [] as $key => $parameter) {
82✔
UNCOV
78
                    $parameters->add($key, $this->addSchemaValidation($parameter));
6✔
79
                }
80

UNCOV
81
                if (\count($parameters) > 0) {
82✔
UNCOV
82
                    $graphQlOperations[$operationName] = $operation->withParameters($parameters);
6✔
83
                }
84
            }
85

UNCOV
86
            $resourceMetadataCollection[$i] = $resource->withGraphQlOperations($graphQlOperations);
82✔
87
        }
88

UNCOV
89
        return $resourceMetadataCollection;
111✔
90
    }
91

92
    private function addSchemaValidation(Parameter $parameter, ?array $schema = null, ?bool $required = null, ?OpenApiParameter $openApi = null): Parameter
93
    {
UNCOV
94
        $schema ??= $parameter->getSchema();
31✔
UNCOV
95
        $required ??= $parameter->getRequired() ?? false;
31✔
UNCOV
96
        $openApi ??= $parameter->getOpenApi();
31✔
97

98
        // When it's an array of openapi parameters take the first one as it's probably just a variant of the query parameter,
99
        // only getAllowEmptyValue is used here anyways
UNCOV
100
        if (\is_array($openApi)) {
31✔
101
            $openApi = $openApi[0];
×
UNCOV
102
        } elseif (false === $openApi) {
31✔
UNCOV
103
            $openApi = null;
19✔
104
        }
105

UNCOV
106
        $assertions = [];
31✔
107

UNCOV
108
        if ($required && false !== ($allowEmptyValue = $openApi?->getAllowEmptyValue())) {
31✔
UNCOV
109
            $assertions[] = new NotNull(message: \sprintf('The parameter "%s" is required.', $parameter->getKey()));
7✔
110
        }
111

UNCOV
112
        if (false === ($allowEmptyValue ?? $openApi?->getAllowEmptyValue())) {
31✔
UNCOV
113
            $assertions[] = new NotBlank(allowNull: !$required);
14✔
114
        }
115

UNCOV
116
        if (isset($schema['exclusiveMinimum'])) {
31✔
UNCOV
117
            $assertions[] = new GreaterThan(value: $schema['exclusiveMinimum']);
7✔
118
        }
119

UNCOV
120
        if (isset($schema['exclusiveMaximum'])) {
31✔
UNCOV
121
            $assertions[] = new LessThan(value: $schema['exclusiveMaximum']);
7✔
122
        }
123

UNCOV
124
        if (isset($schema['minimum'])) {
31✔
UNCOV
125
            $assertions[] = new GreaterThanOrEqual(value: $schema['minimum']);
7✔
126
        }
127

UNCOV
128
        if (isset($schema['maximum'])) {
31✔
UNCOV
129
            $assertions[] = new LessThanOrEqual(value: $schema['maximum']);
7✔
130
        }
131

UNCOV
132
        if (isset($schema['pattern'])) {
31✔
UNCOV
133
            $assertions[] = new Regex($schema['pattern']);
7✔
134
        }
135

UNCOV
136
        if (isset($schema['maxLength']) || isset($schema['minLength'])) {
31✔
UNCOV
137
            $assertions[] = new Length(min: $schema['minLength'] ?? null, max: $schema['maxLength'] ?? null);
7✔
138
        }
139

UNCOV
140
        if (isset($schema['minItems']) || isset($schema['maxItems'])) {
31✔
UNCOV
141
            $assertions[] = new Count(min: $schema['minItems'] ?? null, max: $schema['maxItems'] ?? null);
7✔
142
        }
143

UNCOV
144
        if (isset($schema['multipleOf'])) {
31✔
UNCOV
145
            $assertions[] = new DivisibleBy(value: $schema['multipleOf']);
7✔
146
        }
147

UNCOV
148
        if ($schema['uniqueItems'] ?? false) {
31✔
UNCOV
149
            $assertions[] = new Unique();
7✔
150
        }
151

UNCOV
152
        if (isset($schema['enum'])) {
31✔
UNCOV
153
            $assertions[] = new Choice(choices: $schema['enum']);
12✔
154
        }
155

UNCOV
156
        if (isset($schema['type']) && 'array' === $schema['type']) {
31✔
UNCOV
157
            $assertions[] = new Type(type: 'array');
6✔
158
        }
159

UNCOV
160
        if (!$assertions) {
31✔
UNCOV
161
            return $parameter;
28✔
162
        }
163

UNCOV
164
        if (1 === \count($assertions)) {
15✔
UNCOV
165
            return $parameter->withConstraints($assertions[0]);
15✔
166
        }
167

UNCOV
168
        return $parameter->withConstraints($assertions);
6✔
169
    }
170

171
    private function addFilterValidation(HttpOperation $operation): Parameters
172
    {
UNCOV
173
        $parameters = new Parameters();
21✔
UNCOV
174
        $internalPriority = -1;
21✔
175

UNCOV
176
        foreach ($operation->getFilters() as $filter) {
21✔
UNCOV
177
            if (!$this->filterLocator->has($filter)) {
21✔
178
                continue;
2✔
179
            }
180

UNCOV
181
            $filter = $this->filterLocator->get($filter);
21✔
UNCOV
182
            foreach ($filter->getDescription($operation->getClass()) as $parameterName => $definition) {
21✔
UNCOV
183
                $key = $parameterName;
19✔
UNCOV
184
                $required = $definition['required'] ?? false;
19✔
UNCOV
185
                $schema = $definition['schema'] ?? null;
19✔
186

UNCOV
187
                $openApi = null;
19✔
UNCOV
188
                if (isset($definition['openapi']) && $definition['openapi'] instanceof OpenApiParameter) {
19✔
UNCOV
189
                    $openApi = $definition['openapi'];
10✔
190
                }
191

192
                // The query parameter validator forced this, lets maintain BC on filters
UNCOV
193
                if (true === $required && !$openApi) {
19✔
194
                    $openApi = new OpenApiParameter(name: $key, in: 'query', allowEmptyValue: false);
4✔
195
                }
196

UNCOV
197
                $parameters->add($key, $this->addSchemaValidation(
19✔
198
                    // we disable openapi and hydra on purpose as their docs comes from filters see the condition for addFilterValidation above
UNCOV
199
                    new QueryParameter(key: $key, property: $definition['property'] ?? null, priority: $internalPriority--, schema: $schema, openApi: false, hydra: false),
19✔
UNCOV
200
                    $schema,
19✔
UNCOV
201
                    $required,
19✔
UNCOV
202
                    $openApi
19✔
UNCOV
203
                ));
19✔
204
            }
205
        }
206

UNCOV
207
        return $parameters;
21✔
208
    }
209
}
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