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

api-platform / core / 15935978034

27 Jun 2025 09:07PM UTC coverage: 22.499% (-0.001%) from 22.5%
15935978034

push

github

web-flow
fix(metadata): support stateOptions in YAML and XML for Doctrine ORM/ODM (#7217)

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

2 existing lines in 1 file now uncovered.

11075 of 49224 relevant lines covered (22.5%)

21.83 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
/**
27
 * @experimental
28
 */
29
final class ParameterValidationResourceMetadataCollectionFactory implements ResourceMetadataCollectionFactoryInterface
30
{
31
    use ParameterValidationConstraints;
32

33
    public function __construct(
34
        private readonly ?ResourceMetadataCollectionFactoryInterface $decorated = null,
35
        private readonly ?ContainerInterface $filterLocator = null,
36
    ) {
37
    }
564✔
38

39
    public function create(string $resourceClass): ResourceMetadataCollection
40
    {
41
        $resourceMetadataCollection = $this->decorated?->create($resourceClass) ?? new ResourceMetadataCollection($resourceClass);
84✔
42

43
        foreach ($resourceMetadataCollection as $i => $resource) {
84✔
44
            $operations = $resource->getOperations();
82✔
45

46
            foreach ($operations as $operationName => $operation) {
82✔
47
                $parameters = $operation->getParameters() ?? new Parameters();
82✔
48
                foreach ($parameters as $key => $parameter) {
82✔
49
                    $parameters->add($key, $this->addSchemaValidation($parameter));
28✔
50
                }
51

52
                // As we deprecate the parameter validator, we declare a parameter for each filter transfering validation to the new system
53
                if ($operation->getFilters() && 0 === $parameters->count()) {
82✔
54
                    $parameters = $this->addFilterValidation($operation);
4✔
55
                }
56

57
                if (\count($parameters) > 0) {
82✔
58
                    $operations->add($operationName, $operation->withParameters($parameters));
32✔
59
                }
60
            }
61

62
            $resourceMetadataCollection[$i] = $resource->withOperations($operations->sort());
82✔
63

64
            if (!$graphQlOperations = $resource->getGraphQlOperations()) {
82✔
65
                continue;
42✔
66
            }
67

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

74
                if (\count($parameters) > 0) {
60✔
75
                    $graphQlOperations[$operationName] = $operation->withParameters($parameters);
2✔
76
                }
77
            }
78

79
            $resourceMetadataCollection[$i] = $resource->withGraphQlOperations($graphQlOperations);
60✔
80
        }
81

82
        return $resourceMetadataCollection;
84✔
83
    }
84

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

91
        $assertions = $this->getParameterValidationConstraints($parameter, $schema, $required, $openApi);
30✔
92
        if (!$assertions) {
30✔
93
            return $parameter;
24✔
94
        }
95

96
        if (1 === \count($assertions)) {
14✔
97
            return $parameter->withConstraints($assertions[0]);
14✔
98
        }
99

100
        return $parameter->withConstraints($assertions);
2✔
101
    }
102

103
    private function addFilterValidation(HttpOperation $operation): Parameters
104
    {
105
        $parameters = new Parameters();
4✔
106
        $internalPriority = -1;
4✔
107

108
        foreach ($operation->getFilters() as $filter) {
4✔
109
            if (!$this->filterLocator->has($filter)) {
4✔
UNCOV
110
                continue;
×
111
            }
112

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

119
                $openApi = null;
4✔
120
                if (isset($definition['openapi']) && $definition['openapi'] instanceof OpenApiParameter) {
4✔
121
                    $openApi = $definition['openapi'];
2✔
122
                }
123

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

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

139
        return $parameters;
4✔
140
    }
141
}
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