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

api-platform / core / 16705318661

03 Aug 2025 01:05PM UTC coverage: 0.0% (-21.9%) from 21.944%
16705318661

Pull #7317

github

web-flow
Merge 1ca8642ff into d06b1a0a0
Pull Request #7317: Fix/4372 skip null values in hal

0 of 14 new or added lines in 3 files covered. (0.0%)

11680 existing lines in 376 files now uncovered.

0 of 51817 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/Symfony/Validator/State/ParameterValidatorProvider.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\Symfony\Validator\State;
15

16
use ApiPlatform\Metadata\HttpOperation;
17
use ApiPlatform\Metadata\Operation;
18
use ApiPlatform\Metadata\Parameter;
19
use ApiPlatform\Metadata\Parameters;
20
use ApiPlatform\State\ParameterNotFound;
21
use ApiPlatform\State\ProviderInterface;
22
use ApiPlatform\State\Util\ParameterParserTrait;
23
use ApiPlatform\Validator\Exception\ValidationException;
24
use Symfony\Component\HttpFoundation\Request;
25
use Symfony\Component\Validator\ConstraintViolation;
26
use Symfony\Component\Validator\ConstraintViolationInterface;
27
use Symfony\Component\Validator\ConstraintViolationList;
28
use Symfony\Component\Validator\Validator\ValidatorInterface;
29

30
/**
31
 * Validates parameters using the Symfony validator.
32
 *
33
 * @experimental
34
 */
35
final class ParameterValidatorProvider implements ProviderInterface
36
{
37
    use ParameterParserTrait;
38

39
    public function __construct(
40
        private readonly ValidatorInterface $validator,
41
        private readonly ProviderInterface $decorated,
42
    ) {
UNCOV
43
    }
×
44

45
    public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
46
    {
UNCOV
47
        if (!($request = $context['request'] ?? null) instanceof Request) {
×
48
            return $this->decorated->provide($operation, $uriVariables, $context);
×
49
        }
50

UNCOV
51
        $operation = $request->attributes->get('_api_operation') ?? $operation;
×
UNCOV
52
        if (!($operation->getQueryParameterValidationEnabled() ?? true)) {
×
UNCOV
53
            return $this->decorated->provide($operation, $uriVariables, $context);
×
54
        }
55

UNCOV
56
        $constraintViolationList = new ConstraintViolationList();
×
UNCOV
57
        $parameters = $operation->getParameters() ?? new Parameters();
×
58

UNCOV
59
        if ($operation instanceof HttpOperation) {
×
UNCOV
60
            foreach ($operation->getUriVariables() ?? [] as $key => $uriVariable) {
×
UNCOV
61
                if ($uriVariable->getValue() instanceof ParameterNotFound) {
×
UNCOV
62
                    $uriVariable->setValue($uriVariables[$key] ?? new ParameterNotFound());
×
63
                }
64

UNCOV
65
                $parameters->add($key, $uriVariable->withKey($key));
×
66
            }
67
        }
68

UNCOV
69
        foreach ($parameters as $parameter) {
×
UNCOV
70
            if (!$constraints = $parameter->getConstraints()) {
×
UNCOV
71
                continue;
×
72
            }
73

UNCOV
74
            $value = $parameter->getValue();
×
75

UNCOV
76
            if ($value instanceof ParameterNotFound) {
×
UNCOV
77
                $value = null;
×
78
            }
79

UNCOV
80
            $violations = $this->validator->validate($value, $constraints);
×
81

UNCOV
82
            foreach ($violations as $violation) {
×
UNCOV
83
                $constraintViolationList->add(new ConstraintViolation(
×
UNCOV
84
                    $violation->getMessage(),
×
UNCOV
85
                    $violation->getMessageTemplate(),
×
UNCOV
86
                    $violation->getParameters(),
×
UNCOV
87
                    $violation->getRoot(),
×
UNCOV
88
                    $this->getProperty($parameter, $violation),
×
UNCOV
89
                    $violation->getInvalidValue(),
×
UNCOV
90
                    $violation->getPlural(),
×
UNCOV
91
                    $violation->getCode(),
×
UNCOV
92
                    $violation->getConstraint(),
×
UNCOV
93
                    $violation->getCause()
×
UNCOV
94
                ));
×
95
            }
96
        }
97

UNCOV
98
        if (0 !== \count($constraintViolationList)) {
×
UNCOV
99
            throw new ValidationException($constraintViolationList);
×
100
        }
101

UNCOV
102
        return $this->decorated->provide($operation, $uriVariables, $context);
×
103
    }
104

105
    // There's a `property` inside Parameter but it's used for hydra:search only as here we want the parameter name instead
106
    private function getProperty(Parameter $parameter, ConstraintViolationInterface $violation): string
107
    {
UNCOV
108
        $key = $parameter->getKey();
×
109

UNCOV
110
        if (str_contains($key, '[:property]')) {
×
UNCOV
111
            return str_replace('[:property]', $violation->getPropertyPath(), $key);
×
112
        }
113

UNCOV
114
        if (str_contains($key, ':property')) {
×
115
            return str_replace(':property', $violation->getPropertyPath(), $key);
×
116
        }
117

UNCOV
118
        $openApi = $parameter->getOpenApi();
×
UNCOV
119
        if (false === $openApi) {
×
120
            $openApi = null;
×
121
        }
122

UNCOV
123
        if ('deepObject' === $openApi?->getStyle() && $p = $violation->getPropertyPath()) {
×
UNCOV
124
            return $key.$p;
×
125
        }
126

UNCOV
127
        return $key;
×
128
    }
129
}
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