• 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

68.18
/src/JsonLd/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\JsonLd\Serializer;
15

16
use ApiPlatform\Api\IriConverterInterface;
17
use ApiPlatform\Exception\InvalidArgumentException;
18
use ApiPlatform\JsonLd\AnonymousContextBuilderInterface;
19
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
20
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
21

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

30
    public const FORMAT = 'jsonld';
31

32
    public function __construct(private readonly NormalizerInterface $decorated, private readonly IriConverterInterface $iriConverter, private AnonymousContextBuilderInterface $anonymousContextBuilder)
33
    {
34
    }
62✔
35

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

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

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

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

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

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

73
        /*
74
         * Converts the normalized data array of a resource into an IRI, if the
75
         * normalized data array is empty.
76
         *
77
         * This is useful when traversing from a non-resource towards an attribute
78
         * which is a resource, as we do not have the benefit of {@see ApiProperty::isReadableLink}.
79
         *
80
         * It must not be propagated to resources, as {@see ApiProperty::isReadableLink}
81
         * should take effect.
82
         */
83
        $context['api_empty_resource_as_iri'] = true;
8✔
84

85
        $data = $this->decorated->normalize($object, $format, $context);
8✔
86
        if (!\is_array($data) || !$data) {
8✔
87
            return $data;
2✔
88
        }
89

90
        if (isset($originalResource)) {
6✔
91
            try {
92
                $context['output']['iri'] = $this->iriConverter->getIriFromResource($originalResource);
4✔
93
            } catch (InvalidArgumentException) {
×
94
                // The original resource has no identifiers
95
            }
96
            $context['api_resource'] = $originalResource;
4✔
97
        }
98

99
        $metadata = $this->createJsonLdContext($this->anonymousContextBuilder, $object, $context);
6✔
100

101
        return $metadata + $data;
6✔
102
    }
103
}
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