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

api-platform / core / 15133993414

20 May 2025 09:30AM UTC coverage: 26.313% (-1.2%) from 27.493%
15133993414

Pull #7161

github

web-flow
Merge e2c03d45f into 5459ba375
Pull Request #7161: fix(metadata): infer parameter string type from schema

0 of 2 new or added lines in 1 file covered. (0.0%)

11019 existing lines in 363 files now uncovered.

12898 of 49018 relevant lines covered (26.31%)

34.33 hits per line

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

94.74
/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\Operation;
17
use ApiPlatform\Metadata\Parameter;
18
use ApiPlatform\State\ParameterNotFound;
19
use ApiPlatform\State\ProviderInterface;
20
use ApiPlatform\State\Util\ParameterParserTrait;
21
use ApiPlatform\Validator\Exception\ValidationException;
22
use Symfony\Component\HttpFoundation\Request;
23
use Symfony\Component\Validator\ConstraintViolation;
24
use Symfony\Component\Validator\ConstraintViolationInterface;
25
use Symfony\Component\Validator\ConstraintViolationList;
26
use Symfony\Component\Validator\Validator\ValidatorInterface;
27

28
/**
29
 * Validates parameters using the Symfony validator.
30
 *
31
 * @experimental
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
    }
799✔
42

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

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

UNCOV
54
        $constraintViolationList = new ConstraintViolationList();
798✔
UNCOV
55
        foreach ($operation->getParameters() ?? [] as $parameter) {
798✔
UNCOV
56
            if (!$constraints = $parameter->getConstraints()) {
372✔
UNCOV
57
                continue;
327✔
58
            }
59

UNCOV
60
            $value = $parameter->getValue();
277✔
UNCOV
61
            if ($value instanceof ParameterNotFound) {
277✔
UNCOV
62
                $value = null;
240✔
63
            }
64

UNCOV
65
            $violations = $this->validator->validate($value, $constraints);
277✔
66

UNCOV
67
            foreach ($violations as $violation) {
277✔
UNCOV
68
                $constraintViolationList->add(new ConstraintViolation(
30✔
UNCOV
69
                    $violation->getMessage(),
30✔
UNCOV
70
                    $violation->getMessageTemplate(),
30✔
UNCOV
71
                    $violation->getParameters(),
30✔
UNCOV
72
                    $violation->getRoot(),
30✔
UNCOV
73
                    $this->getProperty($parameter, $violation),
30✔
UNCOV
74
                    $violation->getInvalidValue(),
30✔
UNCOV
75
                    $violation->getPlural(),
30✔
UNCOV
76
                    $violation->getCode(),
30✔
UNCOV
77
                    $violation->getConstraint(),
30✔
UNCOV
78
                    $violation->getCause()
30✔
UNCOV
79
                ));
30✔
80
            }
81
        }
82

UNCOV
83
        if (0 !== \count($constraintViolationList)) {
798✔
UNCOV
84
            throw new ValidationException($constraintViolationList);
30✔
85
        }
86

UNCOV
87
        return $this->decorated->provide($operation, $uriVariables, $context);
798✔
88
    }
89

90
    // There's a `property` inside Parameter but it's used for hydra:search only as here we want the parameter name instead
91
    private function getProperty(Parameter $parameter, ConstraintViolationInterface $violation): string
92
    {
UNCOV
93
        $key = $parameter->getKey();
30✔
94

UNCOV
95
        if (str_contains($key, '[:property]')) {
30✔
UNCOV
96
            return str_replace('[:property]', $violation->getPropertyPath(), $key);
3✔
97
        }
98

UNCOV
99
        if (str_contains($key, ':property')) {
28✔
100
            return str_replace(':property', $violation->getPropertyPath(), $key);
×
101
        }
102

UNCOV
103
        if ($p = $violation->getPropertyPath()) {
28✔
UNCOV
104
            return $key.$p;
4✔
105
        }
106

UNCOV
107
        return $key;
25✔
108
    }
109
}
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