• 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

71.43
/src/JsonLd/Serializer/ItemNormalizer.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\Api\ResourceClassResolverInterface;
18
use ApiPlatform\Api\UrlGeneratorInterface;
19
use ApiPlatform\JsonLd\AnonymousContextBuilderInterface;
20
use ApiPlatform\JsonLd\ContextBuilderInterface;
21
use ApiPlatform\Metadata\HttpOperation;
22
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
23
use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
24
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
25
use ApiPlatform\Metadata\Util\ClassInfoTrait;
26
use ApiPlatform\Serializer\AbstractItemNormalizer;
27
use ApiPlatform\Serializer\ContextTrait;
28
use ApiPlatform\Symfony\Security\ResourceAccessCheckerInterface;
29
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
30
use Symfony\Component\Serializer\Exception\LogicException;
31
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
32
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
33
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
34

35
/**
36
 * Converts between objects and array including JSON-LD and Hydra metadata.
37
 *
38
 * @author Kévin Dunglas <dunglas@gmail.com>
39
 */
40
final class ItemNormalizer extends AbstractItemNormalizer
41
{
42
    use ClassInfoTrait;
43
    use ContextTrait;
44
    use JsonLdContextTrait;
45

46
    public const FORMAT = 'jsonld';
47

48
    public function __construct(ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory, PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, IriConverterInterface $iriConverter, ResourceClassResolverInterface $resourceClassResolver, private readonly ContextBuilderInterface $contextBuilder, PropertyAccessorInterface $propertyAccessor = null, NameConverterInterface $nameConverter = null, ClassMetadataFactoryInterface $classMetadataFactory = null, array $defaultContext = [], ResourceAccessCheckerInterface $resourceAccessChecker = null)
49
    {
50
        parent::__construct($propertyNameCollectionFactory, $propertyMetadataFactory, $iriConverter, $resourceClassResolver, $propertyAccessor, $nameConverter, $classMetadataFactory, $defaultContext, $resourceMetadataCollectionFactory, $resourceAccessChecker);
56✔
51
    }
52

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

61
    public function getSupportedTypes($format): array
62
    {
63
        return self::FORMAT === $format ? parent::getSupportedTypes($format) : [];
28✔
64
    }
65

66
    /**
67
     * {@inheritdoc}
68
     *
69
     * @throws LogicException
70
     */
71
    public function normalize(mixed $object, string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
72
    {
73
        $resourceClass = $this->getObjectClass($object);
12✔
74

75
        if ($this->getOutputClass($context)) {
12✔
76
            return parent::normalize($object, $format, $context);
2✔
77
        }
78

79
        // TODO: we should not remove the resource_class in the normalizeRawCollection as we would find out anyway that it's not the same as the requested one
80
        $previousResourceClass = $context['resource_class'] ?? null;
12✔
81
        $metadata = [];
12✔
82
        if ($isResourceClass = $this->resourceClassResolver->isResourceClass($resourceClass)) {
12✔
83
            $resourceClass = $this->resourceClassResolver->getResourceClass($object, $context['resource_class'] ?? null);
10✔
84
            $context = $this->initContext($resourceClass, $context);
10✔
85
            $metadata = $this->addJsonLdContext($this->contextBuilder, $resourceClass, $context);
10✔
86
        } elseif ($this->contextBuilder instanceof AnonymousContextBuilderInterface) {
2✔
87
            if ($context['api_collection_sub_level'] ?? false) {
2✔
88
                unset($context['api_collection_sub_level']);
×
89
                $context['output']['genid'] = true;
×
90
                $context['output']['iri'] = null;
×
91
            }
92

93
            // We should improve what's behind the context creation, its probably more complicated then it should
94
            $metadata = $this->createJsonLdContext($this->contextBuilder, $object, $context);
2✔
95
        }
96

97
        // maybe not needed anymore
98
        if (isset($context['operation']) && $previousResourceClass !== $resourceClass) {
12✔
99
            unset($context['operation'], $context['operation_name']);
×
100
        }
101

102
        if (($operation = $context['operation'] ?? null) && method_exists($operation, 'getItemUriTemplate') && ($itemUriTemplate = $operation->getItemUriTemplate())) {
12✔
103
            $context['item_uri_template'] = $itemUriTemplate;
×
104
        }
105

106
        if ($iri = $this->iriConverter->getIriFromResource($object, UrlGeneratorInterface::ABS_PATH, $context['operation'] ?? null, $context)) {
12✔
107
            $context['iri'] = $iri;
12✔
108
            $metadata['@id'] = $iri;
12✔
109
        }
110

111
        $context['api_normalize'] = true;
12✔
112

113
        $data = parent::normalize($object, $format, $context);
12✔
114
        if (!\is_array($data)) {
10✔
115
            return $data;
×
116
        }
117

118
        if (!isset($metadata['@type']) && $isResourceClass) {
10✔
119
            $operation = $context['operation'] ?? $this->resourceMetadataCollectionFactory->create($resourceClass)->getOperation();
10✔
120

121
            $types = $operation instanceof HttpOperation ? $operation->getTypes() : null;
10✔
122
            if (null === $types) {
10✔
123
                $types = [$operation->getShortName()];
10✔
124
            }
125
            $metadata['@type'] = 1 === \count($types) ? $types[0] : $types;
10✔
126
        }
127

128
        return $metadata + $data;
10✔
129
    }
130

131
    /**
132
     * {@inheritdoc}
133
     */
134
    public function supportsDenormalization(mixed $data, string $type, string $format = null, array $context = []): bool
135
    {
136
        return self::FORMAT === $format && parent::supportsDenormalization($data, $type, $format, $context);
×
137
    }
138

139
    /**
140
     * {@inheritdoc}
141
     *
142
     * @throws NotNormalizableValueException
143
     */
144
    public function denormalize(mixed $data, string $class, string $format = null, array $context = []): mixed
145
    {
146
        // Avoid issues with proxies if we populated the object
147
        if (isset($data['@id']) && !isset($context[self::OBJECT_TO_POPULATE])) {
×
148
            if (true !== ($context['api_allow_update'] ?? true)) {
×
149
                throw new NotNormalizableValueException('Update is not allowed for this operation.');
×
150
            }
151

152
            $context[self::OBJECT_TO_POPULATE] = $this->iriConverter->getResourceFromIri($data['@id'], $context + ['fetch_data' => true]);
×
153
        }
154

155
        return parent::denormalize($data, $class, $format, $context);
×
156
    }
157
}
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