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

api-platform / core / 18060089452

27 Sep 2025 12:57PM UTC coverage: 0.0% (-21.8%) from 21.793%
18060089452

Pull #7397

github

web-flow
Merge 479f46b8d into abe0438be
Pull Request #7397: fix(jsonschema/jsonld): make `@id` and `@type` properties required only in the JSON-LD schema for output

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

11967 existing lines in 393 files now uncovered.

0 of 53914 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 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
    }
×
35

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

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

UNCOV
43
            foreach ($operations as $operationName => $operation) {
×
UNCOV
44
                $parameters = $operation->getParameters() ?? new Parameters();
×
UNCOV
45
                foreach ($parameters as $key => $parameter) {
×
UNCOV
46
                    $parameters->add($key, $this->addSchemaValidation($parameter));
×
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()) {
×
UNCOV
51
                    $parameters = $this->addFilterValidation($operation);
×
52
                }
53

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

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

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

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

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

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

UNCOV
79
        return $resourceMetadataCollection;
×
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()) {
×
UNCOV
85
            return $parameter;
×
86
        }
87

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

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

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

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

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

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

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

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

UNCOV
126
                $parameters->add($key, $this->addSchemaValidation(
×
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),
×
UNCOV
129
                    $schema,
×
UNCOV
130
                    $required,
×
UNCOV
131
                    $openApi
×
UNCOV
132
                ));
×
133
            }
134
        }
135

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