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

api-platform / core / 14954769666

11 May 2025 10:14AM UTC coverage: 0.0% (-8.5%) from 8.457%
14954769666

Pull #7135

github

web-flow
Merge bf21e0bc7 into 4dd0cdfc4
Pull Request #7135: fix(symfony,laravel): InvalidUriVariableException status code (e400)

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

11040 existing lines in 370 files now uncovered.

0 of 48303 relevant lines covered (0.0%)

0.0 hits per line

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

0.0
/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
    }
×
45

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

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

UNCOV
53
            foreach ($operations as $operationName => $operation) {
×
UNCOV
54
                $parameters = $operation->getParameters() ?? new Parameters();
×
UNCOV
55
                foreach ($parameters as $key => $parameter) {
×
UNCOV
56
                    $parameters->add($key, $this->addSchemaValidation($parameter));
×
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()) {
×
UNCOV
61
                    $parameters = $this->addFilterValidation($operation);
×
62
                }
63

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

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

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

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

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

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

UNCOV
89
        return $resourceMetadataCollection;
×
90
    }
91

92
    private function addSchemaValidation(Parameter $parameter, ?array $schema = null, ?bool $required = null, ?OpenApiParameter $openApi = null): Parameter
93
    {
UNCOV
94
        if (null !== $parameter->getConstraints()) {
×
UNCOV
95
            return $parameter;
×
96
        }
97

UNCOV
98
        $schema ??= $parameter->getSchema();
×
UNCOV
99
        $required ??= $parameter->getRequired() ?? false;
×
UNCOV
100
        $openApi ??= $parameter->getOpenApi();
×
101

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

UNCOV
110
        $assertions = [];
×
111

UNCOV
112
        if ($required && false !== ($allowEmptyValue = $openApi?->getAllowEmptyValue())) {
×
UNCOV
113
            $assertions[] = new NotNull(message: \sprintf('The parameter "%s" is required.', $parameter->getKey()));
×
114
        }
115

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

UNCOV
120
        if (isset($schema['exclusiveMinimum'])) {
×
UNCOV
121
            $assertions[] = new GreaterThan(value: $schema['exclusiveMinimum']);
×
122
        }
123

UNCOV
124
        if (isset($schema['exclusiveMaximum'])) {
×
UNCOV
125
            $assertions[] = new LessThan(value: $schema['exclusiveMaximum']);
×
126
        }
127

UNCOV
128
        if (isset($schema['minimum'])) {
×
UNCOV
129
            $assertions[] = new GreaterThanOrEqual(value: $schema['minimum']);
×
130
        }
131

UNCOV
132
        if (isset($schema['maximum'])) {
×
UNCOV
133
            $assertions[] = new LessThanOrEqual(value: $schema['maximum']);
×
134
        }
135

UNCOV
136
        if (isset($schema['pattern'])) {
×
UNCOV
137
            $assertions[] = new Regex('#'.$schema['pattern'].'#');
×
138
        }
139

UNCOV
140
        if (isset($schema['maxLength']) || isset($schema['minLength'])) {
×
UNCOV
141
            $assertions[] = new Length(min: $schema['minLength'] ?? null, max: $schema['maxLength'] ?? null);
×
142
        }
143

UNCOV
144
        if (isset($schema['minItems']) || isset($schema['maxItems'])) {
×
UNCOV
145
            $assertions[] = new Count(min: $schema['minItems'] ?? null, max: $schema['maxItems'] ?? null);
×
146
        }
147

UNCOV
148
        if (isset($schema['multipleOf'])) {
×
UNCOV
149
            $assertions[] = new DivisibleBy(value: $schema['multipleOf']);
×
150
        }
151

UNCOV
152
        if ($schema['uniqueItems'] ?? false) {
×
UNCOV
153
            $assertions[] = new Unique();
×
154
        }
155

UNCOV
156
        if (isset($schema['enum'])) {
×
UNCOV
157
            $assertions[] = new Choice(choices: $schema['enum']);
×
158
        }
159

UNCOV
160
        if (isset($schema['type']) && 'array' === $schema['type']) {
×
UNCOV
161
            $assertions[] = new Type(type: 'array');
×
162
        }
163

UNCOV
164
        if (!$assertions) {
×
UNCOV
165
            return $parameter;
×
166
        }
167

UNCOV
168
        if (1 === \count($assertions)) {
×
UNCOV
169
            return $parameter->withConstraints($assertions[0]);
×
170
        }
171

UNCOV
172
        return $parameter->withConstraints($assertions);
×
173
    }
174

175
    private function addFilterValidation(HttpOperation $operation): Parameters
176
    {
UNCOV
177
        $parameters = new Parameters();
×
UNCOV
178
        $internalPriority = -1;
×
179

UNCOV
180
        foreach ($operation->getFilters() as $filter) {
×
UNCOV
181
            if (!$this->filterLocator->has($filter)) {
×
182
                continue;
×
183
            }
184

UNCOV
185
            $filter = $this->filterLocator->get($filter);
×
UNCOV
186
            foreach ($filter->getDescription($operation->getClass()) as $parameterName => $definition) {
×
UNCOV
187
                $key = $parameterName;
×
UNCOV
188
                $required = $definition['required'] ?? false;
×
UNCOV
189
                $schema = $definition['schema'] ?? null;
×
190

UNCOV
191
                $openApi = null;
×
UNCOV
192
                if (isset($definition['openapi']) && $definition['openapi'] instanceof OpenApiParameter) {
×
UNCOV
193
                    $openApi = $definition['openapi'];
×
194
                }
195

196
                // The query parameter validator forced this, lets maintain BC on filters
UNCOV
197
                if (true === $required && !$openApi) {
×
198
                    $openApi = new OpenApiParameter(name: $key, in: 'query', allowEmptyValue: false);
×
199
                }
200

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

UNCOV
211
        return $parameters;
×
212
    }
213
}
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