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

api-platform / core / 19171073905

07 Nov 2025 02:17PM UTC coverage: 0.0% (-24.3%) from 24.303%
19171073905

push

github

web-flow
feat(symfony): allow symfony makers namespace configuration (#7497)

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

14716 existing lines in 467 files now uncovered.

0 of 56762 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
final class ParameterValidatorProvider implements ProviderInterface
34
{
35
    use ParameterParserTrait;
36

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

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

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

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

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

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

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

UNCOV
72
            $value = $parameter->getValue();
×
73

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

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

UNCOV
80
            foreach ($violations as $violation) {
×
UNCOV
81
                $constraintViolationList->add(new ConstraintViolation(
×
UNCOV
82
                    $violation->getMessage(),
×
UNCOV
83
                    $violation->getMessageTemplate(),
×
UNCOV
84
                    $violation->getParameters(),
×
UNCOV
85
                    $violation->getRoot(),
×
UNCOV
86
                    $this->getProperty($parameter, $violation),
×
UNCOV
87
                    $violation->getInvalidValue(),
×
UNCOV
88
                    $violation->getPlural(),
×
UNCOV
89
                    $violation->getCode(),
×
90
                    // TODO: remove these with symfony ^7
UNCOV
91
                    method_exists($violation, 'getConstraint') ? $violation->getConstraint() : null, // @phpstan-ignore-line symfony/validator 6.4 is still allowed and this may be true
×
UNCOV
92
                    method_exists($violation, 'getCause') ? $violation->getCause() : null // @phpstan-ignore-line symfony/validator 6.4 is still allowed and this may be true
×
UNCOV
93
                ));
×
94
            }
95
        }
96

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

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

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

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

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

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

UNCOV
122
        if (\is_array($openApi)) {
×
123
            foreach ($openApi as $oa) {
×
124
                if ('deepObject' === $oa->getStyle() && ($oa->getName() === $key || str_starts_with($oa->getName(), $key.'['))) {
×
125
                    return $key.$violation->getPropertyPath();
×
126
                }
127
            }
UNCOV
128
        } elseif ('deepObject' === $openApi?->getStyle() && $p = $violation->getPropertyPath()) {
×
UNCOV
129
            return $key.$p;
×
130
        }
131

UNCOV
132
        return $key;
×
133
    }
134
}
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