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

api-platform / core / 15063570239

16 May 2025 07:56AM UTC coverage: 27.494% (-0.001%) from 27.495%
15063570239

push

github

web-flow
fix: error formats (#7148)

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

11139 existing lines in 371 files now uncovered.

13476 of 49015 relevant lines covered (27.49%)

74.77 hits per line

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

97.14
/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\TypeInfo\Type\CollectionType;
25
use Symfony\Component\TypeInfo\Type\UnionType;
26
use Symfony\Component\Validator\Constraints\All;
27
use Symfony\Component\Validator\Constraints\Choice;
28
use Symfony\Component\Validator\Constraints\Collection;
29
use Symfony\Component\Validator\Constraints\Count;
30
use Symfony\Component\Validator\Constraints\DivisibleBy;
31
use Symfony\Component\Validator\Constraints\GreaterThan;
32
use Symfony\Component\Validator\Constraints\GreaterThanOrEqual;
33
use Symfony\Component\Validator\Constraints\Length;
34
use Symfony\Component\Validator\Constraints\LessThan;
35
use Symfony\Component\Validator\Constraints\LessThanOrEqual;
36
use Symfony\Component\Validator\Constraints\NotBlank;
37
use Symfony\Component\Validator\Constraints\NotNull;
38
use Symfony\Component\Validator\Constraints\Range;
39
use Symfony\Component\Validator\Constraints\Regex;
40
use Symfony\Component\Validator\Constraints\Type;
41
use Symfony\Component\Validator\Constraints\Unique;
42

43
final class ParameterValidationResourceMetadataCollectionFactory implements ResourceMetadataCollectionFactoryInterface
44
{
45
    public function __construct(
46
        private readonly ?ResourceMetadataCollectionFactoryInterface $decorated = null,
47
        private readonly ?ContainerInterface $filterLocator = null,
48
    ) {
UNCOV
49
    }
2,083✔
50

51
    public function create(string $resourceClass): ResourceMetadataCollection
52
    {
UNCOV
53
        $resourceMetadataCollection = $this->decorated?->create($resourceClass) ?? new ResourceMetadataCollection($resourceClass);
141✔
54

UNCOV
55
        foreach ($resourceMetadataCollection as $i => $resource) {
141✔
UNCOV
56
            $operations = $resource->getOperations();
134✔
57

UNCOV
58
            foreach ($operations as $operationName => $operation) {
134✔
UNCOV
59
                $parameters = $operation->getParameters() ?? new Parameters();
134✔
UNCOV
60
                foreach ($parameters as $key => $parameter) {
134✔
UNCOV
61
                    $parameters->add($key, $this->addSchemaValidation($parameter));
31✔
62
                }
63

64
                // As we deprecate the parameter validator, we declare a parameter for each filter transfering validation to the new system
UNCOV
65
                if ($operation->getFilters() && 0 === $parameters->count()) {
134✔
UNCOV
66
                    $parameters = $this->addFilterValidation($operation);
26✔
67
                }
68

UNCOV
69
                if (\count($parameters) > 0) {
134✔
UNCOV
70
                    $operations->add($operationName, $operation->withParameters($parameters));
53✔
71
                }
72
            }
73

UNCOV
74
            $resourceMetadataCollection[$i] = $resource->withOperations($operations->sort());
134✔
75

UNCOV
76
            if (!$graphQlOperations = $resource->getGraphQlOperations()) {
134✔
UNCOV
77
                continue;
60✔
78
            }
79

UNCOV
80
            foreach ($graphQlOperations as $operationName => $operation) {
109✔
UNCOV
81
                $parameters = $operation->getParameters() ?? new Parameters();
109✔
UNCOV
82
                foreach ($operation->getParameters() ?? [] as $key => $parameter) {
109✔
UNCOV
83
                    $parameters->add($key, $this->addSchemaValidation($parameter));
4✔
84
                }
85

UNCOV
86
                if (\count($parameters) > 0) {
109✔
UNCOV
87
                    $graphQlOperations[$operationName] = $operation->withParameters($parameters);
4✔
88
                }
89
            }
90

UNCOV
91
            $resourceMetadataCollection[$i] = $resource->withGraphQlOperations($graphQlOperations);
109✔
92
        }
93

UNCOV
94
        return $resourceMetadataCollection;
141✔
95
    }
96

97
    private function addSchemaValidation(Parameter $parameter, ?array $schema = null, ?bool $required = null, ?OpenApiParameter $openApi = null): Parameter
98
    {
UNCOV
99
        if (null !== $parameter->getConstraints()) {
53✔
UNCOV
100
            return $parameter;
6✔
101
        }
102

UNCOV
103
        $schema ??= $parameter->getSchema();
51✔
UNCOV
104
        $required ??= $parameter->getRequired() ?? false;
51✔
UNCOV
105
        $openApi ??= $parameter->getOpenApi();
51✔
106

107
        // When it's an array of openapi parameters take the first one as it's probably just a variant of the query parameter,
108
        // only getAllowEmptyValue is used here anyways
UNCOV
109
        if (\is_array($openApi)) {
51✔
UNCOV
110
            $openApi = $openApi[0];
11✔
UNCOV
111
        } elseif (false === $openApi) {
49✔
UNCOV
112
            $openApi = null;
22✔
113
        }
114

UNCOV
115
        $assertions = [];
51✔
UNCOV
116
        $allowEmptyValue = $openApi?->getAllowEmptyValue();
51✔
UNCOV
117
        if (false === ($allowEmptyValue ?? $openApi?->getAllowEmptyValue())) {
51✔
UNCOV
118
            $assertions[] = new NotBlank(allowNull: !$required);
23✔
119
        }
120

UNCOV
121
        $minimum = $schema['exclusiveMinimum'] ?? $schema['minimum'] ?? null;
51✔
UNCOV
122
        $exclusiveMinimum = isset($schema['exclusiveMinimum']);
51✔
UNCOV
123
        $maximum = $schema['exclusiveMaximum'] ?? $schema['maximum'] ?? null;
51✔
UNCOV
124
        $exclusiveMaximum = isset($schema['exclusiveMaximum']);
51✔
125

UNCOV
126
        if ($minimum && $maximum) {
51✔
UNCOV
127
            if (!$exclusiveMinimum && !$exclusiveMaximum) {
4✔
UNCOV
128
                $assertions[] = new Range(min: $minimum, max: $maximum);
4✔
129
            } else {
UNCOV
130
                $assertions[] = $exclusiveMinimum ? new GreaterThan(value: $minimum) : new GreaterThanOrEqual(value: $minimum);
4✔
UNCOV
131
                $assertions[] = $exclusiveMaximum ? new LessThan(value: $maximum) : new LessThanOrEqual(value: $maximum);
4✔
132
            }
UNCOV
133
        } elseif ($minimum) {
51✔
134
            $assertions[] = $exclusiveMinimum ? new GreaterThan(value: $minimum) : new GreaterThanOrEqual(value: $minimum);
2✔
UNCOV
135
        } elseif ($maximum) {
51✔
136
            $assertions[] = $exclusiveMaximum ? new LessThan(value: $maximum) : new LessThanOrEqual(value: $maximum);
2✔
137
        }
138

UNCOV
139
        if (isset($schema['pattern'])) {
51✔
UNCOV
140
            $assertions[] = new Regex('#'.$schema['pattern'].'#');
6✔
141
        }
142

UNCOV
143
        if (isset($schema['maxLength']) || isset($schema['minLength'])) {
51✔
UNCOV
144
            $assertions[] = new Length(min: $schema['minLength'] ?? null, max: $schema['maxLength'] ?? null);
6✔
145
        }
146

UNCOV
147
        if (isset($schema['multipleOf'])) {
51✔
UNCOV
148
            $assertions[] = new DivisibleBy(value: $schema['multipleOf']);
6✔
149
        }
150

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

UNCOV
155
        if ($properties = $parameter->getExtraProperties()['_properties'] ?? []) {
51✔
UNCOV
156
            $fields = [];
10✔
UNCOV
157
            foreach ($properties as $propertyName) {
10✔
UNCOV
158
                $fields[$propertyName] = $assertions;
10✔
159
            }
160

UNCOV
161
            return $parameter->withConstraints(new Collection(fields: $fields, allowMissingFields: true));
10✔
162
        }
163

UNCOV
164
        $isCollectionType = fn ($t) => $t instanceof CollectionType;
49✔
UNCOV
165
        $isCollection = $parameter->getNativeType()?->isSatisfiedBy($isCollectionType) ?? false;
49✔
166

167
        // type-info 7.2
UNCOV
168
        if (!$isCollection && $parameter->getNativeType() instanceof UnionType) {
49✔
169
            foreach ($parameter->getNativeType()->getTypes() as $t) {
×
170
                if ($isCollection = $t->isSatisfiedBy($isCollectionType)) {
×
171
                    break;
×
172
                }
173
            }
174
        }
175

UNCOV
176
        if ($isCollection) {
49✔
UNCOV
177
            $assertions = $assertions ? [new All($assertions)] : [];
19✔
178
        }
179

UNCOV
180
        if ($required && false !== $allowEmptyValue) {
49✔
UNCOV
181
            $assertions[] = new NotNull(message: \sprintf('The parameter "%s" is required.', $parameter->getKey()));
6✔
182
        }
183

UNCOV
184
        if (isset($schema['minItems']) || isset($schema['maxItems'])) {
49✔
UNCOV
185
            $assertions[] = new Count(min: $schema['minItems'] ?? null, max: $schema['maxItems'] ?? null);
6✔
186
        }
187

UNCOV
188
        if ($schema['uniqueItems'] ?? false) {
49✔
UNCOV
189
            $assertions[] = new Unique();
6✔
190
        }
191

UNCOV
192
        if (isset($schema['type']) && 'array' === $schema['type']) {
49✔
UNCOV
193
            $assertions[] = new Type(type: 'array');
4✔
194
        }
195

UNCOV
196
        if (!$assertions) {
49✔
UNCOV
197
            return $parameter;
40✔
198
        }
199

UNCOV
200
        if (1 === \count($assertions)) {
23✔
UNCOV
201
            return $parameter->withConstraints($assertions[0]);
23✔
202
        }
203

UNCOV
204
        return $parameter->withConstraints($assertions);
4✔
205
    }
206

207
    private function addFilterValidation(HttpOperation $operation): Parameters
208
    {
UNCOV
209
        $parameters = new Parameters();
26✔
UNCOV
210
        $internalPriority = -1;
26✔
211

UNCOV
212
        foreach ($operation->getFilters() as $filter) {
26✔
UNCOV
213
            if (!$this->filterLocator->has($filter)) {
26✔
214
                continue;
1✔
215
            }
216

UNCOV
217
            $filter = $this->filterLocator->get($filter);
26✔
UNCOV
218
            foreach ($filter->getDescription($operation->getClass()) as $parameterName => $definition) {
26✔
UNCOV
219
                $key = $parameterName;
25✔
UNCOV
220
                $required = $definition['required'] ?? false;
25✔
UNCOV
221
                $schema = $definition['schema'] ?? null;
25✔
222

UNCOV
223
                $openApi = null;
25✔
UNCOV
224
                if (isset($definition['openapi']) && $definition['openapi'] instanceof OpenApiParameter) {
25✔
UNCOV
225
                    $openApi = $definition['openapi'];
11✔
226
                }
227

228
                // The query parameter validator forced this, lets maintain BC on filters
UNCOV
229
                if (true === $required && !$openApi) {
25✔
230
                    $openApi = new OpenApiParameter(name: $key, in: 'query', allowEmptyValue: false);
4✔
231
                }
232

UNCOV
233
                $parameters->add($key, $this->addSchemaValidation(
25✔
234
                    // we disable openapi and hydra on purpose as their docs comes from filters see the condition for addFilterValidation above
UNCOV
235
                    new QueryParameter(key: $key, property: $definition['property'] ?? null, priority: $internalPriority--, schema: $schema, openApi: false, hydra: false),
25✔
UNCOV
236
                    $schema,
25✔
UNCOV
237
                    $required,
25✔
UNCOV
238
                    $openApi
25✔
UNCOV
239
                ));
25✔
240
            }
241
        }
242

UNCOV
243
        return $parameters;
26✔
244
    }
245
}
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