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

api-platform / core / 14008635868

22 Mar 2025 12:39PM UTC coverage: 8.52% (+0.005%) from 8.515%
14008635868

Pull #7042

github

web-flow
Merge fdd88ef56 into 47a6dffbb
Pull Request #7042: Purge parent collections in inheritance cases

4 of 9 new or added lines in 1 file covered. (44.44%)

540 existing lines in 57 files now uncovered.

13394 of 157210 relevant lines covered (8.52%)

22.93 hits per line

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

92.59
/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\Serializer\NameConverter\NameConverterInterface;
18
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
19
use Symfony\Component\Validator\ConstraintViolationInterface;
20
use Symfony\Component\Validator\ConstraintViolationListInterface;
21

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

31
    public function __construct(private readonly PropertyMetadataFactoryInterface $propertyMetadataFactory, private readonly ?NameConverterInterface $nameConverter = null)
32
    {
33
    }
2,008✔
34

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

UNCOV
50
        return ['errors' => $violations];
1✔
51
    }
52

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

61
    public function getSupportedTypes($format): array
62
    {
63
        return self::FORMAT === $format ? [ConstraintViolationListInterface::class => true] : [];
1,885✔
64
    }
65

66
    private function getSourcePointerFromViolation(ConstraintViolationInterface $violation): string
67
    {
UNCOV
68
        $fieldName = $violation->getPropertyPath();
1✔
69

UNCOV
70
        if (!$fieldName) {
1✔
71
            return 'data';
×
72
        }
73

UNCOV
74
        $class = $violation->getRoot()::class;
1✔
UNCOV
75
        $propertyMetadata = $this->propertyMetadataFactory
1✔
UNCOV
76
            ->create(
1✔
77
                // Im quite sure this requires some thought in case of validations over relationships
UNCOV
78
                $class,
1✔
UNCOV
79
                $fieldName
1✔
UNCOV
80
            );
1✔
81

UNCOV
82
        if (null !== $this->nameConverter) {
1✔
UNCOV
83
            $fieldName = $this->nameConverter->normalize($fieldName, $class, self::FORMAT);
1✔
84
        }
85

UNCOV
86
        $type = $propertyMetadata->getBuiltinTypes()[0] ?? null;
1✔
UNCOV
87
        if ($type && null !== $type->getClassName()) {
1✔
88
            return "data/relationships/$fieldName";
×
89
        }
90

UNCOV
91
        return "data/attributes/$fieldName";
1✔
92
    }
93
}
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