• 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

58.62
/src/JsonApi/Serializer/ErrorNormalizer.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 Symfony\Component\ErrorHandler\Exception\FlattenException;
17
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
18

19
/**
20
 * Converts {@see \Exception} or {@see FlattenException} or to a JSON API error representation.
21
 *
22
 * @author Héctor Hurtarte <hectorh30@gmail.com>
23
 */
24
final class ErrorNormalizer implements NormalizerInterface
25
{
26
    public const FORMAT = 'jsonapi';
27

28
    public function __construct(private ?NormalizerInterface $itemNormalizer = null)
29
    {
UNCOV
30
    }
950✔
31

32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function normalize(mixed $object, ?string $format = null, array $context = []): array
36
    {
UNCOV
37
        $jsonApiObject = $this->itemNormalizer->normalize($object, $format, $context);
3✔
UNCOV
38
        $error = $jsonApiObject['data']['attributes'];
3✔
UNCOV
39
        $error['id'] = $jsonApiObject['data']['id'];
3✔
UNCOV
40
        if (isset($error['type'])) {
3✔
UNCOV
41
            $error['links'] = ['type' => $error['type']];
3✔
42
        }
43

UNCOV
44
        if (!isset($error['code']) && method_exists($object, 'getId')) {
3✔
UNCOV
45
            $error['code'] = $object->getId();
3✔
46
        }
47

UNCOV
48
        if (!isset($error['violations'])) {
3✔
UNCOV
49
            return ['errors' => [$error]];
3✔
50
        }
51

52
        $errors = [];
×
53
        foreach ($error['violations'] as $violation) {
×
54
            $e = ['detail' => $violation['message']] + $error;
×
55
            if (isset($error['links']['type'])) {
×
56
                $type = $error['links']['type'];
×
57
                $e['links']['type'] = \sprintf('%s/%s', $type, $violation['propertyPath']);
×
58
                $e['id'] = str_replace($type, $e['links']['type'], $e['id']);
×
59
            }
60
            if (isset($e['code'])) {
×
61
                $e['code'] = \sprintf('%s/%s', $error['code'], $violation['propertyPath']);
×
62
            }
63
            unset($e['violations']);
×
64
            $errors[] = $e;
×
65
        }
66

67
        return ['errors' => $errors];
×
68
    }
69

70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
74
    {
UNCOV
75
        return self::FORMAT === $format && ($data instanceof \Exception || $data instanceof FlattenException);
3✔
76
    }
77

78
    public function getSupportedTypes($format): array
79
    {
UNCOV
80
        if (self::FORMAT === $format) {
890✔
UNCOV
81
            return [
73✔
UNCOV
82
                \Exception::class => true,
73✔
UNCOV
83
                FlattenException::class => true,
73✔
UNCOV
84
            ];
73✔
85
        }
86

UNCOV
87
        return [];
817✔
88
    }
89
}
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