• 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

77.14
/src/JsonApi/Serializer/ConstraintViolationListNormalizer.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\JsonApi\Serializer;
15

16
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
17
use ApiPlatform\Serializer\CacheableSupportsMethodInterface;
18
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
19
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
20
use Symfony\Component\Serializer\Serializer;
21
use Symfony\Component\Validator\ConstraintViolationInterface;
22
use Symfony\Component\Validator\ConstraintViolationListInterface;
23

24
/**
25
 * Converts {@see \Symfony\Component\Validator\ConstraintViolationListInterface} to a JSON API error representation.
26
 *
27
 * @author Héctor Hurtarte <hectorh30@gmail.com>
28
 */
29
final class ConstraintViolationListNormalizer implements NormalizerInterface, CacheableSupportsMethodInterface
30
{
31
    public const FORMAT = 'jsonapi';
32

33
    public function __construct(private readonly PropertyMetadataFactoryInterface $propertyMetadataFactory, private readonly ?NameConverterInterface $nameConverter = null)
34
    {
35
    }
58✔
36

37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function normalize(mixed $object, string $format = null, array $context = []): array
41
    {
42
        $violations = [];
2✔
43
        foreach ($object as $violation) {
2✔
44
            $violations[] = [
2✔
45
                'detail' => $violation->getMessage(),
2✔
46
                'source' => [
2✔
47
                    'pointer' => $this->getSourcePointerFromViolation($violation),
2✔
48
                ],
2✔
49
            ];
2✔
50
        }
51

52
        return ['errors' => $violations];
2✔
53
    }
54

55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function supportsNormalization(mixed $data, string $format = null, array $context = []): bool
59
    {
60
        return self::FORMAT === $format && $data instanceof ConstraintViolationListInterface;
2✔
61
    }
62

63
    public function getSupportedTypes($format): array
64
    {
65
        return self::FORMAT === $format ? [ConstraintViolationListInterface::class => true] : [];
44✔
66
    }
67

68
    public function hasCacheableSupportsMethod(): bool
69
    {
70
        if (method_exists(Serializer::class, 'getSupportedTypes')) {
×
71
            trigger_deprecation(
×
72
                'api-platform/core',
×
73
                '3.1',
×
74
                'The "%s()" method is deprecated, use "getSupportedTypes()" instead.',
×
75
                __METHOD__
×
76
            );
×
77
        }
78

79
        return true;
×
80
    }
81

82
    private function getSourcePointerFromViolation(ConstraintViolationInterface $violation): string
83
    {
84
        $fieldName = $violation->getPropertyPath();
2✔
85

86
        if (!$fieldName) {
2✔
87
            return 'data';
2✔
88
        }
89

90
        $class = $violation->getRoot()::class;
2✔
91
        $propertyMetadata = $this->propertyMetadataFactory
2✔
92
            ->create(
2✔
93
                // Im quite sure this requires some thought in case of validations over relationships
94
                $class,
2✔
95
                $fieldName
2✔
96
            );
2✔
97

98
        if (null !== $this->nameConverter) {
2✔
99
            $fieldName = $this->nameConverter->normalize($fieldName, $class, self::FORMAT);
2✔
100
        }
101

102
        $type = $propertyMetadata->getBuiltinTypes()[0] ?? null;
2✔
103
        if ($type && null !== $type->getClassName()) {
2✔
104
            return "data/relationships/$fieldName";
2✔
105
        }
106

107
        return "data/attributes/$fieldName";
2✔
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

© 2026 Coveralls, Inc