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

api-platform / core / 5650127293

pending completion
5650127293

push

github

web-flow
fix: don't implement deprecated CacheableSupportsMethodInterface with Symfony 6.3+ (#5696)

* fix: don't implement deprecated CacheableSupportsMethodInterface

* fix: a check, and add tests

* fix ApiGatewayNormalizerTest

* more fixes

* fix more tests

* fix lowest

* only trigger the deprecation for Symfony 6.3

167 of 167 new or added lines in 23 files covered. (100.0%)

10865 of 18368 relevant lines covered (59.15%)

19.9 hits per line

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

73.53
/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);
82✔
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;
4✔
47
    }
48

49
    public function getSupportedTypes($format): array
50
    {
51
        return $format === static::FORMAT ? [ConstraintViolationListInterface::class => true] : [];
46✔
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
    protected function getMessagesAndViolations(ConstraintViolationListInterface $constraintViolationList): array
69
    {
70
        $violations = $messages = [];
24✔
71

72
        foreach ($constraintViolationList as $violation) {
24✔
73
            $class = \is_object($root = $violation->getRoot()) ? $root::class : null;
24✔
74

75
            if ($this->nameConverter instanceof AdvancedNameConverterInterface) {
24✔
76
                $propertyPath = $this->nameConverter->normalize($violation->getPropertyPath(), $class, static::FORMAT);
8✔
77
            } elseif ($this->nameConverter instanceof NameConverterInterface) {
16✔
78
                $propertyPath = $this->nameConverter->normalize($violation->getPropertyPath());
8✔
79
            } else {
80
                $propertyPath = $violation->getPropertyPath();
8✔
81
            }
82

83
            $violationData = [
24✔
84
                'propertyPath' => $propertyPath,
24✔
85
                'message' => $violation->getMessage(),
24✔
86
                'code' => $violation->getCode(),
24✔
87
            ];
24✔
88

89
            if ($hint = $violation->getParameters()['hint'] ?? false) {
24✔
90
                $violationData['hint'] = $hint;
×
91
            }
92

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

104
            $violations[] = $violationData;
24✔
105
            $messages[] = ($violationData['propertyPath'] ? "{$violationData['propertyPath']}: " : '').$violationData['message'];
24✔
106
        }
107

108
        return [$messages, $violations];
24✔
109
    }
110
}
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