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

api-platform / core / 18089937549

29 Sep 2025 07:56AM UTC coverage: 21.764% (-0.3%) from 22.093%
18089937549

Pull #7416

github

web-flow
Merge 061bcc790 into abe0438be
Pull Request #7416: fix(laravel): serializer attributes on Eloquent methods

0 of 151 new or added lines in 11 files covered. (0.0%)

5028 existing lines in 173 files now uncovered.

11889 of 54626 relevant lines covered (21.76%)

25.32 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.19
/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
    {
52
        $schema ??= $parameter->getSchema();
36✔
53
        $required ??= $parameter->getRequired() ?? false;
36✔
54
        $openApi ??= $parameter->getOpenApi();
36✔
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
58
        if (\is_array($openApi)) {
36✔
59
            $openApi = $openApi[0];
8✔
60
        } elseif (false === $openApi) {
34✔
61
            $openApi = null;
8✔
62
        }
63

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

161
        return $assertions;
34✔
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