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

api-platform / core / 18089958695

29 Sep 2025 07:57AM UTC coverage: 21.569% (-0.2%) from 21.797%
18089958695

push

github

web-flow
fix(openapi): define items type for HydraCollectionBaseSchema hydra:member (#7419)

Co-authored-by: Thibaut Cholley <thibaut.cholley@elsie-sante.fr>

0 of 1 new or added line in 1 file covered. (0.0%)

12063 existing lines in 401 files now uncovered.

11765 of 54545 relevant lines covered (21.57%)

12.57 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
    ) {
UNCOV
34
    }
362✔
35

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

UNCOV
40
        foreach ($resourceMetadataCollection as $i => $resource) {
56✔
UNCOV
41
            $operations = $resource->getOperations();
55✔
42

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

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

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

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

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

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

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

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

UNCOV
79
        return $resourceMetadataCollection;
56✔
80
    }
81

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

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

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

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

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

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

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

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

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

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

UNCOV
136
        return $parameters;
3✔
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