• 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/Util/ParameterValidationConstraints.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\Util;
15

16
use ApiPlatform\Metadata\Parameter;
17
use ApiPlatform\OpenApi\Model\Parameter as OpenApiParameter;
18
use Symfony\Component\TypeInfo\Type\CollectionType;
19
use Symfony\Component\TypeInfo\Type\UnionType;
20
use Symfony\Component\Validator\Constraint;
21
use Symfony\Component\Validator\Constraints\All;
22
use Symfony\Component\Validator\Constraints\AtLeastOneOf;
23
use Symfony\Component\Validator\Constraints\Choice;
24
use Symfony\Component\Validator\Constraints\Collection;
25
use Symfony\Component\Validator\Constraints\Count;
26
use Symfony\Component\Validator\Constraints\DivisibleBy;
27
use Symfony\Component\Validator\Constraints\GreaterThan;
28
use Symfony\Component\Validator\Constraints\GreaterThanOrEqual;
29
use Symfony\Component\Validator\Constraints\Length;
30
use Symfony\Component\Validator\Constraints\LessThan;
31
use Symfony\Component\Validator\Constraints\LessThanOrEqual;
32
use Symfony\Component\Validator\Constraints\NotBlank;
33
use Symfony\Component\Validator\Constraints\NotNull;
34
use Symfony\Component\Validator\Constraints\Range;
35
use Symfony\Component\Validator\Constraints\Regex;
36
use Symfony\Component\Validator\Constraints\Sequentially;
37
use Symfony\Component\Validator\Constraints\Type;
38
use Symfony\Component\Validator\Constraints\Unique;
39

40
/**
41
 * Helper to get a set of validation constraints for a given Parameter.
42
 */
43
trait ParameterValidationConstraints
44
{
45
    /**
46
     * @param Parameter $parameter readonly
47
     *
48
     * @return list<Constraint>
49
     */
50
    public static function getParameterValidationConstraints(Parameter $parameter, ?array $schema = null, ?bool $required = null, ?OpenApiParameter $openApi = null): array
51
    {
UNCOV
52
        $schema ??= $parameter->getSchema();
×
UNCOV
53
        $required ??= $parameter->getRequired() ?? false;
×
UNCOV
54
        $openApi ??= $parameter->getOpenApi();
×
55

56
        // When it's an array of openapi parameters take the first one as it's probably just a variant of the query parameter,
57
        // only getAllowEmptyValue is used here anyways
UNCOV
58
        if (\is_array($openApi)) {
×
UNCOV
59
            $openApi = $openApi[0];
×
UNCOV
60
        } elseif (false === $openApi) {
×
UNCOV
61
            $openApi = null;
×
62
        }
63

UNCOV
64
        $assertions = [];
×
UNCOV
65
        $allowEmptyValue = $openApi?->getAllowEmptyValue();
×
UNCOV
66
        if (false === ($allowEmptyValue ?? $openApi?->getAllowEmptyValue())) {
×
UNCOV
67
            $assertions[] = new NotBlank(allowNull: !$required);
×
68
        }
69

UNCOV
70
        $minimum = $schema['exclusiveMinimum'] ?? $schema['minimum'] ?? null;
×
UNCOV
71
        $exclusiveMinimum = isset($schema['exclusiveMinimum']);
×
UNCOV
72
        $maximum = $schema['exclusiveMaximum'] ?? $schema['maximum'] ?? null;
×
UNCOV
73
        $exclusiveMaximum = isset($schema['exclusiveMaximum']);
×
74

UNCOV
75
        if ($minimum && $maximum) {
×
UNCOV
76
            if (!$exclusiveMinimum && !$exclusiveMaximum) {
×
UNCOV
77
                $assertions[] = new Range(min: $minimum, max: $maximum);
×
78
            } else {
UNCOV
79
                $assertions[] = $exclusiveMinimum ? new GreaterThan(value: $minimum) : new GreaterThanOrEqual(value: $minimum);
×
UNCOV
80
                $assertions[] = $exclusiveMaximum ? new LessThan(value: $maximum) : new LessThanOrEqual(value: $maximum);
×
81
            }
UNCOV
82
        } elseif ($minimum) {
×
83
            $assertions[] = $exclusiveMinimum ? new GreaterThan(value: $minimum) : new GreaterThanOrEqual(value: $minimum);
×
UNCOV
84
        } elseif ($maximum) {
×
85
            $assertions[] = $exclusiveMaximum ? new LessThan(value: $maximum) : new LessThanOrEqual(value: $maximum);
×
86
        }
87

UNCOV
88
        if (isset($schema['pattern'])) {
×
UNCOV
89
            $assertions[] = new Regex('#'.$schema['pattern'].'#');
×
90
        }
91

UNCOV
92
        if (isset($schema['maxLength']) || isset($schema['minLength'])) {
×
UNCOV
93
            $assertions[] = new Length(min: $schema['minLength'] ?? null, max: $schema['maxLength'] ?? null);
×
94
        }
95

UNCOV
96
        if (isset($schema['multipleOf'])) {
×
UNCOV
97
            $assertions[] = new DivisibleBy(value: $schema['multipleOf']);
×
98
        }
99

UNCOV
100
        if (isset($schema['enum'])) {
×
UNCOV
101
            $assertions[] = new Choice(choices: $schema['enum']);
×
102
        }
103

UNCOV
104
        if ($properties = $parameter->getExtraProperties()['_properties'] ?? []) {
×
UNCOV
105
            $fields = [];
×
UNCOV
106
            foreach ($properties as $propertyName) {
×
UNCOV
107
                $fields[$propertyName] = $assertions;
×
108
            }
109

UNCOV
110
            return [new Collection(fields: $fields, allowMissingFields: true)];
×
111
        }
112

UNCOV
113
        $isCollectionType = fn ($t) => $t instanceof CollectionType;
×
UNCOV
114
        $isCollection = $parameter->getNativeType()?->isSatisfiedBy($isCollectionType) ?? false;
×
115

116
        // type-info 7.2
UNCOV
117
        if (!$isCollection && $parameter->getNativeType() instanceof UnionType) {
×
118
            foreach ($parameter->getNativeType()->getTypes() as $t) {
×
119
                if ($isCollection = $t->isSatisfiedBy($isCollectionType)) {
×
120
                    break;
×
121
                }
122
            }
123
        }
124

UNCOV
125
        if ($isCollection) {
×
UNCOV
126
            if (true === ($parameter->getCastToArray() ?? false)) {
×
UNCOV
127
                $assertions = $assertions ? [new All($assertions)] : [];
×
128
            } else {
UNCOV
129
                $assertions = $assertions ? [new AtLeastOneOf([new Sequentially($assertions), new All($assertions)])] : [];
×
130
            }
131
        }
132

UNCOV
133
        if ($required && false !== $allowEmptyValue) {
×
UNCOV
134
            $assertions[] = new NotNull(message: \sprintf('The parameter "%s" is required.', $parameter->getKey()));
×
135
        }
136

UNCOV
137
        if (isset($schema['minItems']) || isset($schema['maxItems'])) {
×
UNCOV
138
            $assertions[] = new Count(min: $schema['minItems'] ?? null, max: $schema['maxItems'] ?? null);
×
139
        }
140

UNCOV
141
        if ($schema['uniqueItems'] ?? false) {
×
UNCOV
142
            $assertions[] = new Unique();
×
143
        }
144

UNCOV
145
        if (isset($schema['type']) && 'array' === $schema['type']) {
×
UNCOV
146
            $assertions[] = new Type(type: 'array');
×
147
        }
148

UNCOV
149
        if (isset($schema['type']) && $parameter->getCastToNativeType()) {
×
UNCOV
150
            $assertion = match ($schema['type']) {
×
UNCOV
151
                'boolean', 'integer' => new Type(type: $schema['type']),
×
UNCOV
152
                'number' => new Type(type: 'float'),
×
UNCOV
153
                default => null,
×
UNCOV
154
            };
×
155

UNCOV
156
            if ($assertion) {
×
UNCOV
157
                $assertions[] = $assertion;
×
158
            }
159
        }
160

UNCOV
161
        return $assertions;
×
162
    }
163
}
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

© 2026 Coveralls, Inc