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

api-platform / core / 17487610263

05 Sep 2025 08:12AM UTC coverage: 22.608% (+0.3%) from 22.319%
17487610263

push

github

web-flow
chore: remove @experimental flag from parameters (#7357)

12049 of 53296 relevant lines covered (22.61%)

26.21 hits per line

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

96.23
/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 ApiPlatform\Validator\Util\ParameterValidationConstraints;
24
use Psr\Container\ContainerInterface;
25

26
final class ParameterValidationResourceMetadataCollectionFactory implements ResourceMetadataCollectionFactoryInterface
27
{
28
    use ParameterValidationConstraints;
29

30
    public function __construct(
31
        private readonly ?ResourceMetadataCollectionFactoryInterface $decorated = null,
32
        private readonly ?ContainerInterface $filterLocator = null,
33
    ) {
34
    }
722✔
35

36
    public function create(string $resourceClass): ResourceMetadataCollection
37
    {
38
        $resourceMetadataCollection = $this->decorated?->create($resourceClass) ?? new ResourceMetadataCollection($resourceClass);
112✔
39

40
        foreach ($resourceMetadataCollection as $i => $resource) {
112✔
41
            $operations = $resource->getOperations();
110✔
42

43
            foreach ($operations as $operationName => $operation) {
110✔
44
                $parameters = $operation->getParameters() ?? new Parameters();
110✔
45
                foreach ($parameters as $key => $parameter) {
110✔
46
                    $parameters->add($key, $this->addSchemaValidation($parameter));
30✔
47
                }
48

49
                // As we deprecate the parameter validator, we declare a parameter for each filter transfering validation to the new system
50
                if ($operation->getFilters() && 0 === $parameters->count()) {
110✔
51
                    $parameters = $this->addFilterValidation($operation);
6✔
52
                }
53

54
                if (\count($parameters) > 0) {
110✔
55
                    $operations->add($operationName, $operation->withParameters($parameters));
36✔
56
                }
57
            }
58

59
            $resourceMetadataCollection[$i] = $resource->withOperations($operations->sort());
110✔
60

61
            if (!$graphQlOperations = $resource->getGraphQlOperations()) {
110✔
62
                continue;
54✔
63
            }
64

65
            foreach ($graphQlOperations as $operationName => $operation) {
80✔
66
                $parameters = $operation->getParameters() ?? new Parameters();
80✔
67
                foreach ($operation->getParameters() ?? [] as $key => $parameter) {
80✔
68
                    $parameters->add($key, $this->addSchemaValidation($parameter));
2✔
69
                }
70

71
                if (\count($parameters) > 0) {
80✔
72
                    $graphQlOperations[$operationName] = $operation->withParameters($parameters);
2✔
73
                }
74
            }
75

76
            $resourceMetadataCollection[$i] = $resource->withGraphQlOperations($graphQlOperations);
80✔
77
        }
78

79
        return $resourceMetadataCollection;
112✔
80
    }
81

82
    private function addSchemaValidation(Parameter $parameter, ?array $schema = null, ?bool $required = null, ?OpenApiParameter $openApi = null): Parameter
83
    {
84
        if (null !== $parameter->getConstraints()) {
36✔
85
            return $parameter;
4✔
86
        }
87

88
        $assertions = $this->getParameterValidationConstraints($parameter, $schema, $required, $openApi);
34✔
89
        if (!$assertions) {
34✔
90
            return $parameter;
30✔
91
        }
92

93
        if (1 === \count($assertions)) {
12✔
94
            return $parameter->withConstraints($assertions[0]);
12✔
95
        }
96

97
        return $parameter->withConstraints($assertions);
2✔
98
    }
99

100
    private function addFilterValidation(HttpOperation $operation): Parameters
101
    {
102
        $parameters = new Parameters();
6✔
103
        $internalPriority = -1;
6✔
104

105
        foreach ($operation->getFilters() as $filter) {
6✔
106
            if (!$this->filterLocator->has($filter)) {
6✔
107
                continue;
×
108
            }
109

110
            $filter = $this->filterLocator->get($filter);
6✔
111
            foreach ($filter->getDescription($operation->getClass()) as $parameterName => $definition) {
6✔
112
                $key = $parameterName;
6✔
113
                $required = $definition['required'] ?? false;
6✔
114
                $schema = $definition['schema'] ?? null;
6✔
115

116
                $openApi = null;
6✔
117
                if (isset($definition['openapi']) && $definition['openapi'] instanceof OpenApiParameter) {
6✔
118
                    $openApi = $definition['openapi'];
2✔
119
                }
120

121
                // The query parameter validator forced this, lets maintain BC on filters
122
                if (true === $required && !$openApi) {
6✔
123
                    $openApi = new OpenApiParameter(name: $key, in: 'query', allowEmptyValue: false);
×
124
                }
125

126
                $parameters->add($key, $this->addSchemaValidation(
6✔
127
                    // we disable openapi and hydra on purpose as their docs comes from filters see the condition for addFilterValidation above
128
                    new QueryParameter(key: $key, property: $definition['property'] ?? null, priority: $internalPriority--, schema: $schema, openApi: false, hydra: false),
6✔
129
                    $schema,
6✔
130
                    $required,
6✔
131
                    $openApi
6✔
132
                ));
6✔
133
            }
134
        }
135

136
        return $parameters;
6✔
137
    }
138
}
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