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

api-platform / core / 15133993414

20 May 2025 09:30AM UTC coverage: 26.313% (-1.2%) from 27.493%
15133993414

Pull #7161

github

web-flow
Merge e2c03d45f into 5459ba375
Pull Request #7161: fix(metadata): infer parameter string type from schema

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

11019 existing lines in 363 files now uncovered.

12898 of 49018 relevant lines covered (26.31%)

34.33 hits per line

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

7.41
/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
    {
UNCOV
33
    }
950✔
34

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

50
        return ['errors' => $violations];
×
51
    }
52

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

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

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

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

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

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

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

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