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

api-platform / core / 9838795518

08 Jul 2024 11:33AM UTC coverage: 64.239% (+0.07%) from 64.173%
9838795518

push

github

web-flow
Merge pull request #6459 from soyuka/main

Merge 3.4

154 of 179 new or added lines in 24 files covered. (86.03%)

36 existing lines in 12 files now uncovered.

11326 of 17631 relevant lines covered (64.24%)

68.24 hits per line

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

82.86
/src/Metadata/Resource/Factory/ParameterResourceMetadataCollectionFactory.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\Metadata\Resource\Factory;
15

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

41
/**
42
 * Prepares Parameters documentation by reading its filter details and declaring an OpenApi parameter.
43
 *
44
 * @experimental
45
 */
46
final class ParameterResourceMetadataCollectionFactory implements ResourceMetadataCollectionFactoryInterface
47
{
48
    public function __construct(private readonly ?ResourceMetadataCollectionFactoryInterface $decorated = null, private readonly ?ContainerInterface $filterLocator = null)
49
    {
50
    }
563✔
51

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

56
        foreach ($resourceMetadataCollection as $i => $resource) {
12✔
57
            $operations = $resource->getOperations();
12✔
58

59
            $internalPriority = -1;
12✔
60
            foreach ($operations as $operationName => $operation) {
12✔
61
                $parameters = $operation->getParameters() ?? new Parameters();
12✔
62
                foreach ($parameters as $key => $parameter) {
12✔
63
                    $key = $parameter->getKey() ?? $key;
4✔
64
                    $parameter = $this->setDefaults($key, $parameter, $resourceClass);
4✔
65
                    $priority = $parameter->getPriority() ?? $internalPriority--;
4✔
66
                    $parameters->add($key, $parameter->withPriority($priority));
4✔
67
                }
68

69
                // As we deprecate the parameter validator, we declare a parameter for each filter transfering validation to the new system
70
                if ($operation->getFilters() && 0 === $parameters->count() && false === ($operation->getExtraProperties()['use_legacy_parameter_validator'] ?? true)) {
12✔
71
                    $parameters = $this->addFilterValidation($operation);
4✔
72
                }
73

74
                if (\count($parameters) > 0) {
12✔
75
                    $operations->add($operationName, $operation->withParameters($parameters));
4✔
76
                }
77
            }
78

79
            $resourceMetadataCollection[$i] = $resource->withOperations($operations->sort());
12✔
80

81
            if (!$graphQlOperations = $resource->getGraphQlOperations()) {
12✔
82
                continue;
8✔
83
            }
84

85
            $internalPriority = -1;
8✔
86
            foreach ($graphQlOperations as $operationName => $operation) {
8✔
87
                $parameters = $operation->getParameters() ?? new Parameters();
8✔
88
                foreach ($operation->getParameters() ?? [] as $key => $parameter) {
8✔
89
                    $key = $parameter->getKey() ?? $key;
4✔
90
                    $parameter = $this->setDefaults($key, $parameter, $resourceClass);
4✔
91
                    $priority = $parameter->getPriority() ?? $internalPriority--;
4✔
92
                    $parameters->add($key, $parameter->withPriority($priority));
4✔
93
                }
94

95
                $graphQlOperations[$operationName] = $operation->withParameters($parameters);
8✔
96
            }
97

98
            $resourceMetadataCollection[$i] = $resource->withGraphQlOperations($graphQlOperations);
8✔
99
        }
100

101
        return $resourceMetadataCollection;
12✔
102
    }
103

104
    private function setDefaults(string $key, Parameter $parameter, string $resourceClass): Parameter
105
    {
106
        if (null === $parameter->getKey()) {
4✔
107
            $parameter = $parameter->withKey($key);
4✔
108
        }
109

110
        $filter = $parameter->getFilter();
4✔
111
        if (\is_string($filter) && $this->filterLocator->has($filter)) {
4✔
112
            $filter = $this->filterLocator->get($filter);
4✔
113
        }
114

115
        if ($filter instanceof SerializerFilterInterface && null === $parameter->getProvider()) {
4✔
116
            $parameter = $parameter->withProvider('api_platform.serializer.filter_parameter_provider');
4✔
117
        }
118

119
        // Read filter description to populate the Parameter
120
        $description = $filter instanceof FilterInterface ? $filter->getDescription($resourceClass) : [];
4✔
121
        if (($schema = $description[$key]['schema'] ?? null) && null === $parameter->getSchema()) {
4✔
122
            $parameter = $parameter->withSchema($schema);
×
123
        }
124

125
        if (null === $parameter->getProperty() && ($property = $description[$key]['property'] ?? null)) {
4✔
126
            $parameter = $parameter->withProperty($property);
4✔
127
        }
128

129
        if (null === $parameter->getRequired() && ($required = $description[$key]['required'] ?? null)) {
4✔
130
            $parameter = $parameter->withRequired($required);
×
131
        }
132

133
        if (null === $parameter->getOpenApi() && $openApi = $description[$key]['openapi'] ?? null) {
4✔
134
            if ($openApi instanceof OpenApiParameter) {
×
135
                $parameter = $parameter->withOpenApi($openApi);
×
136
            } elseif (\is_array($openApi)) {
×
NEW
137
                $schema = $schema ?? $openApi['schema'] ?? [];
×
138
                $parameter = $parameter->withOpenApi(new OpenApiParameter(
×
139
                    $key,
×
140
                    $parameter instanceof HeaderParameterInterface ? 'header' : 'query',
×
141
                    $description[$key]['description'] ?? '',
×
142
                    $description[$key]['required'] ?? $openApi['required'] ?? false,
×
143
                    $openApi['deprecated'] ?? false,
×
144
                    $openApi['allowEmptyValue'] ?? true,
×
145
                    $schema,
×
146
                    $openApi['style'] ?? null,
×
147
                    $openApi['explode'] ?? ('array' === ($schema['type'] ?? null)),
×
148
                    $openApi['allowReserved'] ?? false,
×
149
                    $openApi['example'] ?? null,
×
150
                    isset(
×
151
                        $openApi['examples']
×
152
                    ) ? new \ArrayObject($openApi['examples']) : null
×
153
                ));
×
154
            }
155
        }
156

157
        $schema = $parameter->getSchema() ?? (($openApi = $parameter->getOpenApi()) ? $openApi->getSchema() : null);
4✔
158

159
        // Only add validation if the Symfony Validator is installed
160
        if (interface_exists(ValidatorInterface::class) && !$parameter->getConstraints()) {
4✔
161
            $parameter = $this->addSchemaValidation($parameter, $schema, $parameter->getRequired() ?? $description['required'] ?? false, $parameter->getOpenApi() ?: null);
4✔
162
        }
163

164
        return $parameter;
4✔
165
    }
166

167
    private function addSchemaValidation(Parameter $parameter, ?array $schema = null, bool $required = false, ?OpenApiParameter $openApi = null): Parameter
168
    {
169
        $assertions = [];
4✔
170

171
        if ($required && false !== ($allowEmptyValue = $openApi?->getAllowEmptyValue())) {
4✔
172
            $assertions[] = new NotNull(message: sprintf('The parameter "%s" is required.', $parameter->getKey()));
4✔
173
        }
174

175
        if (false === ($allowEmptyValue ?? $openApi?->getAllowEmptyValue())) {
4✔
176
            $assertions[] = new NotBlank(allowNull: !$required);
4✔
177
        }
178

179
        if (isset($schema['exclusiveMinimum'])) {
4✔
180
            $assertions[] = new GreaterThan(value: $schema['exclusiveMinimum']);
4✔
181
        }
182

183
        if (isset($schema['exclusiveMaximum'])) {
4✔
184
            $assertions[] = new LessThan(value: $schema['exclusiveMaximum']);
4✔
185
        }
186

187
        if (isset($schema['minimum'])) {
4✔
188
            $assertions[] = new GreaterThanOrEqual(value: $schema['minimum']);
4✔
189
        }
190

191
        if (isset($schema['maximum'])) {
4✔
192
            $assertions[] = new LessThanOrEqual(value: $schema['maximum']);
4✔
193
        }
194

195
        if (isset($schema['pattern'])) {
4✔
196
            $assertions[] = new Regex($schema['pattern']);
4✔
197
        }
198

199
        if (isset($schema['maxLength']) || isset($schema['minLength'])) {
4✔
200
            $assertions[] = new Length(min: $schema['minLength'] ?? null, max: $schema['maxLength'] ?? null);
4✔
201
        }
202

203
        if (isset($schema['minItems']) || isset($schema['maxItems'])) {
4✔
204
            $assertions[] = new Count(min: $schema['minItems'] ?? null, max: $schema['maxItems'] ?? null);
4✔
205
        }
206

207
        if (isset($schema['multipleOf'])) {
4✔
208
            $assertions[] = new DivisibleBy(value: $schema['multipleOf']);
4✔
209
        }
210

211
        if ($schema['uniqueItems'] ?? false) {
4✔
212
            $assertions[] = new Unique();
4✔
213
        }
214

215
        if (isset($schema['enum'])) {
4✔
216
            $assertions[] = new Choice(choices: $schema['enum']);
4✔
217
        }
218

219
        if (isset($schema['type']) && 'array' === $schema['type']) {
4✔
220
            $assertions[] = new Type(type: 'array');
4✔
221
        }
222

223
        if (!$assertions) {
4✔
224
            return $parameter;
4✔
225
        }
226

227
        if (1 === \count($assertions)) {
4✔
228
            return $parameter->withConstraints($assertions[0]);
4✔
229
        }
230

231
        return $parameter->withConstraints($assertions);
4✔
232
    }
233

234
    private function addFilterValidation(HttpOperation $operation): Parameters
235
    {
236
        $parameters = new Parameters();
4✔
237
        $internalPriority = -1;
4✔
238

239
        foreach ($operation->getFilters() as $filter) {
4✔
240
            if (!$this->filterLocator->has($filter)) {
4✔
NEW
241
                continue;
×
242
            }
243

244
            $filter = $this->filterLocator->get($filter);
4✔
245
            foreach ($filter->getDescription($operation->getClass()) as $parameterName => $definition) {
4✔
246
                $key = $parameterName;
4✔
247
                $required = $definition['required'] ?? false;
4✔
248
                $schema = $definition['schema'] ?? null;
4✔
249

250
                if (isset($definition['swagger'])) {
4✔
251
                    trigger_deprecation('api-platform/core', '3.4', 'The key "swagger" in a filter description is deprecated, use "schema" or "openapi" instead.');
4✔
252
                    $schema = $schema ?? $definition['swagger'];
4✔
253
                }
254

255
                $openApi = null;
4✔
256
                if (isset($definition['openapi'])) {
4✔
257
                    trigger_deprecation('api-platform/core', '3.4', sprintf('The key "openapi" in a filter description should be a "%s" class or use "schema" to specify the JSON Schema.', OpenApiParameter::class));
4✔
258
                    if ($definition['openapi'] instanceof OpenApiParameter) {
4✔
NEW
259
                        $openApi = $definition['openapi'];
×
260
                    } else {
261
                        $schema = $schema ?? $openApi;
4✔
262
                    }
263
                }
264

265
                if (isset($schema['allowEmptyValue']) && !$openApi) {
4✔
266
                    trigger_deprecation('api-platform/core', '3.4', 'The "allowEmptyValue" option should be declared using an "openapi" parameter.');
4✔
267
                    $openApi = new OpenApiParameter(name: $key, in: 'query', allowEmptyValue: $schema['allowEmptyValue']);
4✔
268
                }
269

270
                // The query parameter validator forced this, lets maintain BC on filters
271
                if (true === $required && !$openApi) {
4✔
272
                    $openApi = new OpenApiParameter(name: $key, in: 'query', allowEmptyValue: false);
4✔
273
                }
274

275
                if (\is_bool($schema['exclusiveMinimum'] ?? null)) {
4✔
276
                    trigger_deprecation('api-platform/core', '3.4', 'The "exclusiveMinimum" schema value should be a number not a boolean.');
4✔
277
                    $schema['exclusiveMinimum'] = $schema['minimum'];
4✔
278
                    unset($schema['minimum']);
4✔
279
                }
280

281
                if (\is_bool($schema['exclusiveMaximum'] ?? null)) {
4✔
282
                    trigger_deprecation('api-platform/core', '3.4', 'The "exclusiveMaximum" schema value should be a number not a boolean.');
4✔
283
                    $schema['exclusiveMaximum'] = $schema['maximum'];
4✔
284
                    unset($schema['maximum']);
4✔
285
                }
286

287
                $parameters->add($key, $this->addSchemaValidation(
4✔
288
                    // we disable openapi and hydra on purpose as their docs comes from filters see the condition for addFilterValidation above
289
                    new QueryParameter(key: $key, property: $definition['property'] ?? null, priority: $internalPriority--, schema: $schema, openApi: false, hydra: false),
4✔
290
                    $schema,
4✔
291
                    $required,
4✔
292
                    $openApi
4✔
293
                ));
4✔
294
            }
295
        }
296

297
        return $parameters;
4✔
298
    }
299
}
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