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

api-platform / core / 20819730869

08 Jan 2026 02:12PM UTC coverage: 25.305%. Remained the same
20819730869

push

github

web-flow
refactor: make method parameter names match interfaces (#7643)

112 of 164 new or added lines in 34 files covered. (68.29%)

214 existing lines in 22 files now uncovered.

14741 of 58254 relevant lines covered (25.3%)

30.23 hits per line

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

94.74
/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\JsonLd\AnonymousContextBuilderInterface;
17
use ApiPlatform\JsonLd\ContextBuilderInterface;
18
use ApiPlatform\Metadata\Exception\ItemNotFoundException;
19
use ApiPlatform\Metadata\HttpOperation;
20
use ApiPlatform\Metadata\IriConverterInterface;
21
use ApiPlatform\Metadata\Operation\Factory\OperationMetadataFactoryInterface;
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\ResourceAccessCheckerInterface;
26
use ApiPlatform\Metadata\ResourceClassResolverInterface;
27
use ApiPlatform\Metadata\UrlGeneratorInterface;
28
use ApiPlatform\Metadata\Util\ClassInfoTrait;
29
use ApiPlatform\Serializer\AbstractItemNormalizer;
30
use ApiPlatform\Serializer\ContextTrait;
31
use ApiPlatform\Serializer\TagCollectorInterface;
32
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
33
use Symfony\Component\Serializer\Exception\LogicException;
34
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
35
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
36
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
37

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

49
    public const FORMAT = 'jsonld';
50
    private const JSONLD_KEYWORDS = [
51
        '@context',
52
        '@direction',
53
        '@graph',
54
        '@id',
55
        '@import',
56
        '@included',
57
        '@index',
58
        '@json',
59
        '@language',
60
        '@list',
61
        '@nest',
62
        '@none',
63
        '@prefix',
64
        '@propagate',
65
        '@protected',
66
        '@reverse',
67
        '@set',
68
        '@type',
69
        '@value',
70
        '@version',
71
        '@vocab',
72
    ];
73

74
    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, protected ?TagCollectorInterface $tagCollector = null, private ?OperationMetadataFactoryInterface $operationMetadataFactory = null)
75
    {
76
        parent::__construct($propertyNameCollectionFactory, $propertyMetadataFactory, $iriConverter, $resourceClassResolver, $propertyAccessor, $nameConverter, $classMetadataFactory, $defaultContext, $resourceMetadataCollectionFactory, $resourceAccessChecker, $tagCollector);
794✔
77
    }
78

79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
83
    {
84
        return self::FORMAT === $format && parent::supportsNormalization($data, $format, $context);
490✔
85
    }
86

87
    /**
88
     * {@inheritdoc}
89
     */
90
    public function getSupportedTypes(?string $format): array
91
    {
92
        return self::FORMAT === $format ? parent::getSupportedTypes($format) : [];
628✔
93
    }
94

95
    /**
96
     * {@inheritdoc}
97
     *
98
     * @throws LogicException
99
     */
100
    public function normalize(mixed $data, ?string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
101
    {
102
        $resourceClass = $this->getObjectClass($data);
470✔
103
        $outputClass = $this->getOutputClass($context);
470✔
104

105
        if ($outputClass && !($context['item_uri_template'] ?? null)) {
470✔
106
            return parent::normalize($data, $format, $context);
8✔
107
        }
108

109
        // 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
110
        $previousResourceClass = $context['resource_class'] ?? null;
470✔
111
        $metadata = [];
470✔
112
        if ($isResourceClass = $this->resourceClassResolver->isResourceClass($resourceClass) && (null === $previousResourceClass || $this->resourceClassResolver->isResourceClass($previousResourceClass))) {
470✔
113
            $resourceClass = $this->resourceClassResolver->getResourceClass($data, $previousResourceClass);
452✔
114
            $context = $this->initContext($resourceClass, $context);
452✔
115
            $metadata = $this->addJsonLdContext($this->contextBuilder, $resourceClass, $context);
452✔
116
        } elseif ($this->contextBuilder instanceof AnonymousContextBuilderInterface) {
22✔
117
            if ($context['api_collection_sub_level'] ?? false) {
22✔
118
                unset($context['api_collection_sub_level']);
10✔
119
                $context['output']['gen_id'] ??= true;
10✔
120
                $context['output']['iri'] = null;
10✔
121
            }
122

123
            if (isset($context['item_uri_template']) && $this->operationMetadataFactory) {
22✔
124
                $context['output']['operation'] = $this->operationMetadataFactory->create($context['item_uri_template']);
4✔
125
            } elseif ($this->resourceClassResolver->isResourceClass($resourceClass)) {
20✔
126
                $context['output']['operation'] = $this->resourceMetadataCollectionFactory->create($resourceClass)->getOperation();
4✔
127
            }
128

129
            // We should improve what's behind the context creation, its probably more complicated then it should
130
            $metadata = $this->createJsonLdContext($this->contextBuilder, $data, $context);
22✔
131
        }
132

133
        // Special case: non-resource got serialized and contains a resource therefore we need to reset part of the context
134
        if ($previousResourceClass !== $resourceClass && $resourceClass !== $outputClass) {
470✔
135
            unset($context['operation'], $context['operation_name'], $context['output']);
24✔
136
        }
137

138
        if (true === ($context['output']['gen_id'] ?? true) && true === ($context['force_iri_generation'] ?? true) && $iri = $this->iriConverter->getIriFromResource($data, UrlGeneratorInterface::ABS_PATH, $context['operation'] ?? null, $context)) {
470✔
139
            $context['iri'] = $iri;
470✔
140
            $metadata['@id'] = $iri;
470✔
141
        }
142

143
        $context['api_normalize'] = true;
470✔
144

145
        $normalizedData = parent::normalize($data, $format, $context);
470✔
146
        if (!\is_array($normalizedData)) {
470✔
NEW
147
            return $normalizedData;
×
148
        }
149

150
        $operation = $context['operation'] ?? null;
470✔
151

152
        if ($this->operationMetadataFactory && isset($context['item_uri_template']) && !$operation) {
470✔
153
            $operation = $this->operationMetadataFactory->create($context['item_uri_template']);
6✔
154
        }
155

156
        if ($isResourceClass && !$operation) {
470✔
157
            $operation = $this->resourceMetadataCollectionFactory->create($resourceClass)->getOperation();
276✔
158
        }
159

160
        if (!isset($metadata['@type']) && $operation) {
470✔
161
            $types = $operation instanceof HttpOperation ? $operation->getTypes() : null;
452✔
162
            if (null === $types) {
452✔
163
                $types = [$operation->getShortName()];
452✔
164
            }
165
            $metadata['@type'] = 1 === \count($types) ? $types[0] : $types;
452✔
166
        }
167

168
        return $metadata + $normalizedData;
470✔
169
    }
170

171
    /**
172
     * {@inheritdoc}
173
     */
174
    public function supportsDenormalization(mixed $data, string $type, ?string $format = null, array $context = []): bool
175
    {
176
        return self::FORMAT === $format && parent::supportsDenormalization($data, $type, $format, $context);
15✔
177
    }
178

179
    /**
180
     * {@inheritdoc}
181
     *
182
     * @throws NotNormalizableValueException
183
     */
184
    public function denormalize(mixed $data, string $type, ?string $format = null, array $context = []): mixed
185
    {
186
        // Avoid issues with proxies if we populated the object
187
        if (isset($data['@id']) && !isset($context[self::OBJECT_TO_POPULATE])) {
15✔
188
            if (true !== ($context['api_allow_update'] ?? true)) {
2✔
189
                throw new NotNormalizableValueException('Update is not allowed for this operation.');
×
190
            }
191

192
            try {
193
                $context[self::OBJECT_TO_POPULATE] = $this->iriConverter->getResourceFromIri($data['@id'], $context + ['fetch_data' => true], $context['operation'] ?? null);
2✔
194
            } catch (ItemNotFoundException $e) {
2✔
195
                $operation = $context['operation'] ?? null;
2✔
196

197
                if (!('PUT' === $operation?->getMethod() && ($operation->getExtraProperties()['standard_put'] ?? true))) {
2✔
198
                    throw $e;
×
199
                }
200
            }
201
        }
202

203
        return parent::denormalize($data, $type, $format, $context);
15✔
204
    }
205

206
    protected function getAllowedAttributes(string|object $classOrObject, array $context, bool $attributesAsString = false): array|bool
207
    {
208
        $allowedAttributes = parent::getAllowedAttributes($classOrObject, $context, $attributesAsString);
471✔
209
        if (\is_array($allowedAttributes) && ($context['api_denormalize'] ?? false)) {
471✔
210
            $allowedAttributes = array_merge($allowedAttributes, self::JSONLD_KEYWORDS);
13✔
211
        }
212

213
        return $allowedAttributes;
471✔
214
    }
215
}
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