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

api-platform / core / 14726133838

29 Apr 2025 07:51AM UTC coverage: 0.0% (-23.4%) from 23.443%
14726133838

Pull #7114

github

web-flow
Merge b5ddc44e4 into 4b1b94cad
Pull Request #7114: Merge 4.1

0 of 88 new or added lines in 20 files covered. (0.0%)

10373 existing lines in 330 files now uncovered.

0 of 49280 relevant lines covered (0.0%)

0.0 hits per line

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

0.0
/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;
21
use Symfony\Component\TypeInfo\Type\CompositeTypeInterface;
22
use Symfony\Component\TypeInfo\Type\ObjectType;
23
use Symfony\Component\TypeInfo\Type\WrappingTypeInterface;
24
use Symfony\Component\Validator\ConstraintViolationInterface;
25
use Symfony\Component\Validator\ConstraintViolationListInterface;
26

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

36
    public function __construct(private readonly PropertyMetadataFactoryInterface $propertyMetadataFactory, private readonly ?NameConverterInterface $nameConverter = null)
37
    {
UNCOV
38
    }
×
39

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

55
        return ['errors' => $violations];
×
56
    }
57

58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
62
    {
63
        return self::FORMAT === $format && $data instanceof ConstraintViolationListInterface;
×
64
    }
65

66
    public function getSupportedTypes($format): array
67
    {
UNCOV
68
        return self::FORMAT === $format ? [ConstraintViolationListInterface::class => true] : [];
×
69
    }
70

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

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

79
        $class = $violation->getRoot()::class;
×
80
        $propertyMetadata = $this->propertyMetadataFactory
×
81
            ->create(
×
82
                // Im quite sure this requires some thought in case of validations over relationships
83
                $class,
×
84
                $fieldName
×
85
            );
×
86

87
        if (null !== $this->nameConverter) {
×
88
            $fieldName = $this->nameConverter->normalize($fieldName, $class, self::FORMAT);
×
89
        }
90

91
        if (!method_exists(PropertyInfoExtractor::class, 'getType')) {
×
92
            $type = $propertyMetadata->getBuiltinTypes()[0] ?? null;
×
93
            if ($type && null !== $type->getClassName()) {
×
94
                return "data/relationships/$fieldName";
×
95
            }
96
        } else {
97
            $typeIsObject = static function (Type $type) use (&$typeIsObject): bool {
×
UNCOV
98
                return match (true) {
×
99
                    $type instanceof WrappingTypeInterface => $type->wrappedTypeIsSatisfiedBy($typeIsObject),
×
100
                    $type instanceof CompositeTypeInterface => $type->composedTypesAreSatisfiedBy($typeIsObject),
×
101
                    default => $type instanceof ObjectType,
×
UNCOV
102
                };
×
103
            };
×
104

105
            if ($propertyMetadata->getNativeType()?->isSatisfiedBy($typeIsObject)) {
×
106
                return "data/relationships/$fieldName";
×
107
            }
108
        }
109

110
        return "data/attributes/$fieldName";
×
111
    }
112
}
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