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

api-platform / core / 6417366242

05 Oct 2023 09:43AM UTC coverage: 37.131% (+0.05%) from 37.083%
6417366242

push

github

web-flow
fix(validation): normalize constraint violation list (#5866)

27 of 27 new or added lines in 4 files covered. (100.0%)

10156 of 27352 relevant lines covered (37.13%)

20.01 hits per line

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

82.46
/src/Serializer/AbstractConstraintViolationListNormalizer.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\Serializer;
15

16
use Symfony\Component\Serializer\NameConverter\AdvancedNameConverterInterface;
17
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
18
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
19
use Symfony\Component\Serializer\Serializer;
20
use Symfony\Component\Validator\ConstraintViolation;
21
use Symfony\Component\Validator\ConstraintViolationListInterface;
22

23
/**
24
 * Common features regarding Constraint Violation normalization.
25
 *
26
 * @author Kévin Dunglas <dunglas@gmail.com>
27
 *
28
 * @internal
29
 */
30
abstract class AbstractConstraintViolationListNormalizer implements NormalizerInterface, CacheableSupportsMethodInterface
31
{
32
    public const FORMAT = null; // Must be overridden
33

34
    private readonly ?array $serializePayloadFields;
35

36
    public function __construct(array $serializePayloadFields = null, private readonly ?NameConverterInterface $nameConverter = null)
37
    {
38
        $this->serializePayloadFields = null === $serializePayloadFields ? null : array_flip($serializePayloadFields);
123✔
39
    }
40

41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function supportsNormalization(mixed $data, string $format = null, array $context = []): bool
45
    {
46
        return static::FORMAT === $format && $data instanceof ConstraintViolationListInterface;
6✔
47
    }
48

49
    public function getSupportedTypes($format): array
50
    {
51
        return $format === static::FORMAT ? [ConstraintViolationListInterface::class => true] : [];
78✔
52
    }
53

54
    public function hasCacheableSupportsMethod(): bool
55
    {
56
        if (method_exists(Serializer::class, 'getSupportedTypes')) {
×
57
            trigger_deprecation(
×
58
                'api-platform/core',
×
59
                '3.1',
×
60
                'The "%s()" method is deprecated, use "getSupportedTypes()" instead.',
×
61
                __METHOD__
×
62
            );
×
63
        }
64

65
        return true;
×
66
    }
67

68
    /**
69
     * return string[].
70
     */
71
    protected function getViolations(ConstraintViolationListInterface $constraintViolationList): array
72
    {
73
        $violations = [];
27✔
74

75
        foreach ($constraintViolationList as $violation) {
27✔
76
            $class = \is_object($root = $violation->getRoot()) ? $root::class : null;
27✔
77

78
            if ($this->nameConverter instanceof AdvancedNameConverterInterface) {
27✔
79
                $propertyPath = $this->nameConverter->normalize($violation->getPropertyPath(), $class, static::FORMAT);
9✔
80
            } elseif ($this->nameConverter instanceof NameConverterInterface) {
18✔
81
                $propertyPath = $this->nameConverter->normalize($violation->getPropertyPath());
9✔
82
            } else {
83
                $propertyPath = $violation->getPropertyPath();
9✔
84
            }
85

86
            $violationData = [
27✔
87
                'propertyPath' => $propertyPath,
27✔
88
                'message' => $violation->getMessage(),
27✔
89
                'code' => $violation->getCode(),
27✔
90
            ];
27✔
91

92
            if ($hint = $violation->getParameters()['hint'] ?? false) {
27✔
93
                $violationData['hint'] = $hint;
×
94
            }
95

96
            $constraint = $violation instanceof ConstraintViolation ? $violation->getConstraint() : null;
27✔
97
            if (
98
                [] !== $this->serializePayloadFields
27✔
99
                && $constraint
100
                && $constraint->payload
27✔
101
                // If some fields are whitelisted, only them are added
102
                && $payloadFields = null === $this->serializePayloadFields ? $constraint->payload : array_intersect_key($constraint->payload, $this->serializePayloadFields)
27✔
103
            ) {
104
                $violationData['payload'] = $payloadFields;
18✔
105
            }
106

107
            $violations[] = $violationData;
27✔
108
        }
109

110
        return $violations;
27✔
111
    }
112

113
    protected function getMessagesAndViolations(ConstraintViolationListInterface $constraintViolationList): array
114
    {
115
        trigger_deprecation('api-platform', '3.2', sprintf('"%s::%s" will be removed in 4.0, use "%1$s::%s', __CLASS__, __METHOD__, 'getViolations'));
9✔
116
        $violations = $messages = [];
9✔
117

118
        foreach ($constraintViolationList as $violation) {
9✔
119
            $class = \is_object($root = $violation->getRoot()) ? $root::class : null;
9✔
120

121
            if ($this->nameConverter instanceof AdvancedNameConverterInterface) {
9✔
122
                $propertyPath = $this->nameConverter->normalize($violation->getPropertyPath(), $class, static::FORMAT);
3✔
123
            } elseif ($this->nameConverter instanceof NameConverterInterface) {
6✔
124
                $propertyPath = $this->nameConverter->normalize($violation->getPropertyPath());
3✔
125
            } else {
126
                $propertyPath = $violation->getPropertyPath();
3✔
127
            }
128

129
            $violationData = [
9✔
130
                'propertyPath' => $propertyPath,
9✔
131
                'message' => $violation->getMessage(),
9✔
132
                'code' => $violation->getCode(),
9✔
133
            ];
9✔
134

135
            if ($hint = $violation->getParameters()['hint'] ?? false) {
9✔
136
                $violationData['hint'] = $hint;
×
137
            }
138

139
            $constraint = $violation instanceof ConstraintViolation ? $violation->getConstraint() : null;
9✔
140
            if (
141
                [] !== $this->serializePayloadFields
9✔
142
                && $constraint
143
                && $constraint->payload
9✔
144
                // If some fields are whitelisted, only them are added
145
                && $payloadFields = null === $this->serializePayloadFields ? $constraint->payload : array_intersect_key($constraint->payload, $this->serializePayloadFields)
9✔
146
            ) {
147
                $violationData['payload'] = $payloadFields;
9✔
148
            }
149

150
            $violations[] = $violationData;
9✔
151
            $messages[] = ($violationData['propertyPath'] ? "{$violationData['propertyPath']}: " : '').$violationData['message'];
9✔
152
        }
153

154
        return [$messages, $violations];
9✔
155
    }
156
}
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