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

api-platform / core / 17723449516

15 Sep 2025 05:52AM UTC coverage: 0.0% (-22.6%) from 22.578%
17723449516

Pull #7383

github

web-flow
Merge fa5b61e35 into 949c3c975
Pull Request #7383: fix(metadata): compute isWritable during updates

0 of 6 new or added lines in 4 files covered. (0.0%)

11356 existing lines in 371 files now uncovered.

0 of 48868 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
 * @experimental
44
 */
45
trait ParameterValidationConstraints
46
{
47
    /**
48
     * @param Parameter $parameter readonly
49
     *
50
     * @return list<Constraint>
51
     */
52
    public static function getParameterValidationConstraints(Parameter $parameter, ?array $schema = null, ?bool $required = null, ?OpenApiParameter $openApi = null): array
53
    {
UNCOV
54
        $schema ??= $parameter->getSchema();
×
UNCOV
55
        $required ??= $parameter->getRequired() ?? false;
×
UNCOV
56
        $openApi ??= $parameter->getOpenApi();
×
57

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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