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

api-platform / core / 7142557150

08 Dec 2023 02:28PM UTC coverage: 36.003% (-1.4%) from 37.36%
7142557150

push

github

web-flow
fix(jsonld): remove link to ApiDocumentation when doc is disabled (#6029)

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

2297 existing lines in 182 files now uncovered.

9992 of 27753 relevant lines covered (36.0%)

147.09 hits per line

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

74.29
/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
    }
2,499✔
36

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

52
        return ['errors' => $violations];
9✔
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;
6✔
61
    }
62

63
    public function getSupportedTypes($format): array
64
    {
65
        return self::FORMAT === $format ? [ConstraintViolationListInterface::class => true] : [];
2,361✔
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();
9✔
85

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

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

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

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

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