• 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

10.71
/src/JsonApi/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\JsonApi\Serializer;
15

16
use ApiPlatform\Api\IriConverterInterface;
17
use ApiPlatform\Api\ResourceClassResolverInterface;
18
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
19
use ApiPlatform\Metadata\Util\ClassInfoTrait;
20
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
21
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
22

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

31
    public const FORMAT = 'jsonapi';
32

33
    public function __construct(private readonly NormalizerInterface $decorated, private readonly IriConverterInterface $iriConverter, private readonly ResourceClassResolverInterface $resourceClassResolver, private readonly ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory)
34
    {
35
    }
54✔
36

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

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

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

57
    public function hasCacheableSupportsMethod(): bool
58
    {
59
        trigger_deprecation('api-platform/core', '3.1', 'The "%s()" method is deprecated, use "getSupportedTypes()" instead.', __METHOD__);
×
60

61
        return $this->decorated instanceof CacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod();
×
62
    }
63

64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function normalize(mixed $object, string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
68
    {
69
        if (isset($context['api_resource'])) {
×
70
            $originalResource = $context['api_resource'];
×
71
            unset($context['api_resource']);
×
72
        }
73

74
        $data = $this->decorated->normalize($object, $format, $context);
×
75
        if (!\is_array($data) || isset($context['api_attribute'])) {
×
76
            return $data;
×
77
        }
78

79
        if (isset($originalResource)) {
×
80
            $resourceClass = $this->resourceClassResolver->getResourceClass($originalResource);
×
81
            $resourceData = [
×
82
                'id' => $this->iriConverter->getIriFromResource($originalResource),
×
83
                'type' => $this->resourceMetadataFactory->create($resourceClass)->getOperation()->getShortName(),
×
84
            ];
×
85
        } else {
86
            $resourceData = [
×
87
                'id' => $this->iriConverter->getIriFromResource($object),
×
88
                'type' => (new \ReflectionClass($this->getObjectClass($object)))->getShortName(),
×
89
            ];
×
90
        }
91

92
        if ($data) {
×
93
            $resourceData['attributes'] = $data;
×
94
        }
95

96
        return ['data' => $resourceData];
×
97
    }
98
}
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