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

api-platform / core / 5644825543

pending completion
5644825543

push

github

web-flow
feat(serializer): support for getSupportedTypes (symfony 6.3) (#5672)

109 of 109 new or added lines in 29 files covered. (100.0%)

10881 of 18245 relevant lines covered (59.64%)

20.04 hits per line

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

12.5
/src/GraphQl/Serializer/ObjectNormalizer.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\GraphQl\Serializer;
15

16
use ApiPlatform\Api\IdentifiersExtractorInterface;
17
use ApiPlatform\Api\IriConverterInterface;
18
use ApiPlatform\Metadata\Util\ClassInfoTrait;
19
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
20
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
21
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
22

23
/**
24
 * Decorates the output with GraphQL metadata when appropriate, but otherwise just
25
 * passes through to the decorated normalizer.
26
 */
27
final class ObjectNormalizer implements NormalizerInterface, CacheableSupportsMethodInterface
28
{
29
    use ClassInfoTrait;
30

31
    public const FORMAT = 'graphql';
32
    public const ITEM_RESOURCE_CLASS_KEY = '#itemResourceClass';
33
    public const ITEM_IDENTIFIERS_KEY = '#itemIdentifiers';
34

35
    public function __construct(private readonly NormalizerInterface $decorated, private readonly IriConverterInterface $iriConverter, private readonly IdentifiersExtractorInterface $identifiersExtractor)
36
    {
37
    }
54✔
38

39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function supportsNormalization(mixed $data, string $format = null, array $context = []): bool
43
    {
44
        return self::FORMAT === $format && $this->decorated->supportsNormalization($data, $format, $context);
×
45
    }
46

47
    public function getSupportedTypes($format): array
48
    {
49
        // @deprecated remove condition when support for symfony versions under 6.3 is dropped
50
        if (!method_exists($this->decorated, 'getSupportedTypes')) {
16✔
51
            return [
×
52
                '*' => $this->decorated instanceof CacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod(),
×
53
            ];
×
54
        }
55

56
        return self::FORMAT === $format ? $this->decorated->getSupportedTypes($format) : [];
16✔
57
    }
58

59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function hasCacheableSupportsMethod(): bool
63
    {
64
        trigger_deprecation('api-platform/core', '3.1', 'The "%s()" method is deprecated, use "getSupportedTypes()" instead.', __METHOD__);
×
65

66
        return $this->decorated instanceof CacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod();
×
67
    }
68

69
    /**
70
     * {@inheritdoc}
71
     *
72
     * @throws UnexpectedValueException
73
     */
74
    public function normalize(mixed $object, string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
75
    {
76
        if (isset($context['api_resource'])) {
×
77
            $originalResource = $context['api_resource'];
×
78
            unset($context['api_resource']);
×
79
        }
80

81
        $data = $this->decorated->normalize($object, $format, $context);
×
82
        if (!\is_array($data)) {
×
83
            throw new UnexpectedValueException('Expected data to be an array.');
×
84
        }
85

86
        if (!isset($originalResource)) {
×
87
            return $data;
×
88
        }
89

90
        if (isset($data['id'])) {
×
91
            $data['_id'] = $data['id'];
×
92
            $data['id'] = $this->iriConverter->getIriFromResource($originalResource);
×
93
        }
94

95
        if (!($context['no_resolver_data'] ?? false)) {
×
96
            $data[self::ITEM_RESOURCE_CLASS_KEY] = $this->getObjectClass($originalResource);
×
97
            $data[self::ITEM_IDENTIFIERS_KEY] = $this->identifiersExtractor->getIdentifiersFromItem($originalResource);
×
98
        }
99

100
        return $data;
×
101
    }
102
}
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

© 2026 Coveralls, Inc