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

api-platform / core / 20745879029

06 Jan 2026 10:43AM UTC coverage: 28.884% (+0.04%) from 28.843%
20745879029

Pull #7647

github

web-flow
Merge 4e69048e9 into d23ab4301
Pull Request #7647: fix(doctrine): fix partial fetch with same entity included multiple time with different fields

32 of 117 new or added lines in 2 files covered. (27.35%)

378 existing lines in 45 files now uncovered.

16832 of 58274 relevant lines covered (28.88%)

78.61 hits per line

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

94.92
/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\Count;
25
use Symfony\Component\Validator\Constraints\DivisibleBy;
26
use Symfony\Component\Validator\Constraints\GreaterThan;
27
use Symfony\Component\Validator\Constraints\GreaterThanOrEqual;
28
use Symfony\Component\Validator\Constraints\Length;
29
use Symfony\Component\Validator\Constraints\LessThan;
30
use Symfony\Component\Validator\Constraints\LessThanOrEqual;
31
use Symfony\Component\Validator\Constraints\NotBlank;
32
use Symfony\Component\Validator\Constraints\NotNull;
33
use Symfony\Component\Validator\Constraints\Range;
34
use Symfony\Component\Validator\Constraints\Regex;
35
use Symfony\Component\Validator\Constraints\Sequentially;
36
use Symfony\Component\Validator\Constraints\Type;
37
use Symfony\Component\Validator\Constraints\Unique;
38

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

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

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

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

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

87
        if (isset($schema['pattern'])) {
62✔
88
            $assertions[] = new Regex('#'.$schema['pattern'].'#');
6✔
89
        }
90

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

95
        if (isset($schema['multipleOf'])) {
62✔
96
            $assertions[] = new DivisibleBy(value: $schema['multipleOf']);
6✔
97
        }
98

99
        if (isset($schema['enum'])) {
62✔
100
            $assertions[] = new Choice(choices: $schema['enum']);
18✔
101
        }
102

103
        $isCollectionType = fn ($t) => $t instanceof CollectionType;
62✔
104
        $isCollection = $parameter->getNativeType()?->isSatisfiedBy($isCollectionType) ?? false;
62✔
105

106
        // type-info 7.2
107
        if (!$isCollection && $parameter->getNativeType() instanceof UnionType) {
62✔
108
            foreach ($parameter->getNativeType()->getTypes() as $t) {
×
109
                if ($isCollection = $t->isSatisfiedBy($isCollectionType)) {
×
110
                    break;
×
111
                }
112
            }
113
        }
114

115
        if ($isCollection) {
62✔
116
            if (true === ($parameter->getCastToArray() ?? false)) {
18✔
117
                $assertions = $assertions ? [new All($assertions)] : [];
4✔
118
            } else {
119
                $assertions = $assertions ? [new AtLeastOneOf([new Sequentially($assertions), new All($assertions)])] : [];
18✔
120
            }
121
        }
122

123
        if ($required && false !== $allowEmptyValue) {
62✔
124
            $assertions[] = new NotNull();
8✔
125
        }
126

127
        if (isset($schema['minItems']) || isset($schema['maxItems'])) {
62✔
128
            $assertions[] = new Count(min: $schema['minItems'] ?? null, max: $schema['maxItems'] ?? null);
6✔
129
        }
130

131
        if ($schema['uniqueItems'] ?? false) {
62✔
132
            $assertions[] = new Unique();
6✔
133
        }
134

135
        if (isset($schema['type']) && 'array' === $schema['type']) {
62✔
136
            $assertions[] = new Type(type: 'array');
4✔
137
        }
138

139
        if (isset($schema['type']) && $parameter->getCastToNativeType()) {
62✔
140
            $assertion = match ($schema['type']) {
6✔
141
                'boolean', 'integer' => new Type(type: $schema['type']),
6✔
142
                'number' => new Type(type: 'float'),
4✔
143
                default => null,
4✔
144
            };
6✔
145

146
            if ($assertion) {
6✔
147
                $assertions[] = $assertion;
6✔
148
            }
149
        }
150

151
        return $assertions;
62✔
152
    }
153
}
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