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

api-platform / core / 17063252025

19 Aug 2025 07:54AM UTC coverage: 22.209% (-0.03%) from 22.236%
17063252025

push

github

soyuka
Merge 4.1

11 of 157 new or added lines in 17 files covered. (7.01%)

2 existing lines in 2 files now uncovered.

11699 of 52676 relevant lines covered (22.21%)

24.2 hits per line

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

6.06
/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 Symfony\Component\PropertyInfo\PropertyInfoExtractor;
18
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
19
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
20
use Symfony\Component\TypeInfo\Type\ObjectType;
21
use Symfony\Component\Validator\ConstraintViolationInterface;
22
use Symfony\Component\Validator\ConstraintViolationListInterface;
23

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

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

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

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

63
    /**
64
     * @param string|null $format
65
     */
66
    public function getSupportedTypes($format): array
67
    {
68
        return self::FORMAT === $format ? [ConstraintViolationListInterface::class => true] : [];
528✔
69
    }
70

71
    private function getSourcePointerFromViolation(ConstraintViolationInterface $violation): string
72
    {
73
        $fieldName = $violation->getPropertyPath();
×
74

75
        if (!$fieldName) {
×
76
            return 'data';
×
77
        }
78

NEW
79
        $root = $violation->getRoot();
×
80

NEW
81
        if (!\is_object($root)) {
×
NEW
82
            return "data/attributes/$fieldName";
×
83
        }
84

NEW
85
        $class = $root::class;
×
86
        $propertyMetadata = $this->propertyMetadataFactory
×
87
            ->create(
×
88
                // Im quite sure this requires some thought in case of validations over relationships
89
                $class,
×
90
                $fieldName
×
91
            );
×
92

93
        if (null !== $this->nameConverter) {
×
94
            $fieldName = $this->nameConverter->normalize($fieldName, $class, self::FORMAT);
×
95
        }
96

97
        if (!method_exists(PropertyInfoExtractor::class, 'getType')) {
×
98
            $type = $propertyMetadata->getBuiltinTypes()[0] ?? null;
×
99
            if ($type && null !== $type->getClassName()) {
×
100
                return "data/relationships/$fieldName";
×
101
            }
102
        } else {
103
            if ($propertyMetadata->getNativeType()?->isSatisfiedBy(fn ($t) => $t instanceof ObjectType)) {
×
104
                return "data/relationships/$fieldName";
×
105
            }
106
        }
107

108
        return "data/attributes/$fieldName";
×
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

© 2025 Coveralls, Inc