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

api-platform / core / 18223414080

03 Oct 2025 01:18PM UTC coverage: 0.0% (-22.0%) from 21.956%
18223414080

Pull #7397

github

web-flow
Merge 69d085182 into 0b8237918
Pull Request #7397: fix(jsonschema/jsonld): make `@id` and `@type` properties required only in the JSON-LD schema for output

0 of 18 new or added lines in 2 files covered. (0.0%)

12304 existing lines in 405 files now uncovered.

0 of 53965 relevant lines covered (0.0%)

0.0 hits per line

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

0.0
/src/Hal/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\Hal\Serializer;
15

16
use ApiPlatform\Metadata\IriConverterInterface;
17
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
18
use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
19
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
20
use ApiPlatform\Metadata\ResourceAccessCheckerInterface;
21
use ApiPlatform\Metadata\ResourceClassResolverInterface;
22
use ApiPlatform\Metadata\UrlGeneratorInterface;
23
use ApiPlatform\Metadata\Util\ClassInfoTrait;
24
use ApiPlatform\Metadata\Util\TypeHelper;
25
use ApiPlatform\Serializer\AbstractItemNormalizer;
26
use ApiPlatform\Serializer\CacheKeyTrait;
27
use ApiPlatform\Serializer\ContextTrait;
28
use ApiPlatform\Serializer\TagCollectorInterface;
29
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
30
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
31
use Symfony\Component\PropertyInfo\Type as LegacyType;
32
use Symfony\Component\Serializer\Exception\CircularReferenceException;
33
use Symfony\Component\Serializer\Exception\LogicException;
34
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
35
use Symfony\Component\Serializer\Mapping\AttributeMetadataInterface;
36
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
37
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
38
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
39
use Symfony\Component\TypeInfo\Type;
40
use Symfony\Component\TypeInfo\Type\CollectionType;
41
use Symfony\Component\TypeInfo\Type\CompositeTypeInterface;
42
use Symfony\Component\TypeInfo\Type\ObjectType;
43

44
/**
45
 * Converts between objects and array including HAL metadata.
46
 *
47
 * @author Kévin Dunglas <dunglas@gmail.com>
48
 */
49
final class ItemNormalizer extends AbstractItemNormalizer
50
{
51
    use CacheKeyTrait;
52
    use ClassInfoTrait;
53
    use ContextTrait;
54

55
    public const FORMAT = 'jsonhal';
56

57
    protected const HAL_CIRCULAR_REFERENCE_LIMIT_COUNTERS = 'hal_circular_reference_limit_counters';
58

59
    private array $componentsCache = [];
60
    private array $attributesMetadataCache = [];
61

62
    public function __construct(PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, IriConverterInterface $iriConverter, ResourceClassResolverInterface $resourceClassResolver, ?PropertyAccessorInterface $propertyAccessor = null, ?NameConverterInterface $nameConverter = null, ?ClassMetadataFactoryInterface $classMetadataFactory = null, array $defaultContext = [], ?ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory = null, ?ResourceAccessCheckerInterface $resourceAccessChecker = null, ?TagCollectorInterface $tagCollector = null)
63
    {
UNCOV
64
        $defaultContext[AbstractNormalizer::CIRCULAR_REFERENCE_HANDLER] = function ($object): ?array {
×
65
            $iri = $this->iriConverter->getIriFromResource($object);
×
66
            if (null === $iri) {
×
67
                return null;
×
68
            }
69

70
            return ['_links' => ['self' => ['href' => $iri]]];
×
UNCOV
71
        };
×
72

UNCOV
73
        parent::__construct($propertyNameCollectionFactory, $propertyMetadataFactory, $iriConverter, $resourceClassResolver, $propertyAccessor, $nameConverter, $classMetadataFactory, $defaultContext, $resourceMetadataCollectionFactory, $resourceAccessChecker, $tagCollector);
×
74
    }
75

76
    /**
77
     * {@inheritdoc}
78
     */
79
    public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
80
    {
UNCOV
81
        return self::FORMAT === $format && parent::supportsNormalization($data, $format, $context);
×
82
    }
83

84
    /**
85
     * @param string|null $format
86
     */
87
    public function getSupportedTypes($format): array
88
    {
UNCOV
89
        return self::FORMAT === $format ? parent::getSupportedTypes($format) : [];
×
90
    }
91

92
    /**
93
     * {@inheritdoc}
94
     */
95
    public function normalize(mixed $object, ?string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
96
    {
UNCOV
97
        $resourceClass = $this->getObjectClass($object);
×
UNCOV
98
        if ($this->getOutputClass($context)) {
×
UNCOV
99
            return parent::normalize($object, $format, $context);
×
100
        }
101

UNCOV
102
        $previousResourceClass = $context['resource_class'] ?? null;
×
UNCOV
103
        if ($this->resourceClassResolver->isResourceClass($resourceClass) && (null === $previousResourceClass || $this->resourceClassResolver->isResourceClass($previousResourceClass))) {
×
UNCOV
104
            $resourceClass = $this->resourceClassResolver->getResourceClass($object, $previousResourceClass);
×
105
        }
106

UNCOV
107
        $context = $this->initContext($resourceClass, $context);
×
108

UNCOV
109
        $iri = $context['iri'] ??= $this->iriConverter->getIriFromResource($object, UrlGeneratorInterface::ABS_PATH, $context['operation'] ?? null, $context);
×
UNCOV
110
        $context['object'] = $object;
×
UNCOV
111
        $context['format'] = $format;
×
UNCOV
112
        $context['api_normalize'] = true;
×
113

UNCOV
114
        if (!isset($context['cache_key'])) {
×
UNCOV
115
            $context['cache_key'] = $this->getCacheKey($format, $context);
×
116
        }
117

UNCOV
118
        $data = parent::normalize($object, $format, $context);
×
119

UNCOV
120
        if (!\is_array($data)) {
×
121
            return $data;
×
122
        }
123

UNCOV
124
        $metadata = [
×
UNCOV
125
            '_links' => [
×
UNCOV
126
                'self' => [
×
UNCOV
127
                    'href' => $iri,
×
UNCOV
128
                ],
×
UNCOV
129
            ],
×
UNCOV
130
        ];
×
UNCOV
131
        $components = $this->getComponents($object, $format, $context);
×
UNCOV
132
        $metadata = $this->populateRelation($metadata, $object, $format, $context, $components, 'links');
×
UNCOV
133
        $metadata = $this->populateRelation($metadata, $object, $format, $context, $components, 'embedded');
×
134

UNCOV
135
        return $metadata + $data;
×
136
    }
137

138
    /**
139
     * {@inheritdoc}
140
     */
141
    public function supportsDenormalization(mixed $data, string $type, ?string $format = null, array $context = []): bool
142
    {
143
        // prevent the use of lower priority normalizers (e.g. serializer.normalizer.object) for this format
144
        return self::FORMAT === $format;
×
145
    }
146

147
    /**
148
     * {@inheritdoc}
149
     *
150
     * @throws LogicException
151
     */
152
    public function denormalize(mixed $data, string $type, ?string $format = null, array $context = []): never
153
    {
154
        throw new LogicException(\sprintf('%s is a read-only format.', self::FORMAT));
×
155
    }
156

157
    /**
158
     * {@inheritdoc}
159
     */
160
    protected function getAttributes(object $object, ?string $format = null, array $context = []): array
161
    {
UNCOV
162
        return $this->getComponents($object, $format, $context)['states'];
×
163
    }
164

165
    /**
166
     * Gets HAL components of the resource: states, links and embedded.
167
     */
168
    private function getComponents(object $object, ?string $format, array $context): array
169
    {
UNCOV
170
        $cacheKey = $this->getObjectClass($object).'-'.$context['cache_key'];
×
171

UNCOV
172
        if (isset($this->componentsCache[$cacheKey])) {
×
UNCOV
173
            return $this->componentsCache[$cacheKey];
×
174
        }
175

UNCOV
176
        $attributes = parent::getAttributes($object, $format, $context);
×
UNCOV
177
        $options = $this->getFactoryOptions($context);
×
178

UNCOV
179
        $components = [
×
UNCOV
180
            'states' => [],
×
UNCOV
181
            'links' => [],
×
UNCOV
182
            'embedded' => [],
×
UNCOV
183
        ];
×
184

UNCOV
185
        foreach ($attributes as $attribute) {
×
UNCOV
186
            $propertyMetadata = $this->propertyMetadataFactory->create($context['resource_class'], $attribute, $options);
×
187

UNCOV
188
            if (method_exists(PropertyInfoExtractor::class, 'getType')) {
×
UNCOV
189
                $type = $propertyMetadata->getNativeType();
×
UNCOV
190
                $types = $type instanceof CompositeTypeInterface ? $type->getTypes() : (null === $type ? [] : [$type]);
×
191
                /** @var class-string|null $className */
UNCOV
192
                $className = null;
×
193
            } else {
194
                $types = $propertyMetadata->getBuiltinTypes() ?? [];
×
195
            }
196

197
            // prevent declaring $attribute as attribute if it's already declared as relationship
UNCOV
198
            $isRelationship = false;
×
UNCOV
199
            $typeIsResourceClass = function (Type $type) use (&$className): bool {
×
UNCOV
200
                return $type instanceof ObjectType && $this->resourceClassResolver->isResourceClass($className = $type->getClassName());
×
UNCOV
201
            };
×
202

UNCOV
203
            foreach ($types as $type) {
×
UNCOV
204
                $isOne = $isMany = false;
×
205

206
                /** @var Type|LegacyType|null $valueType */
UNCOV
207
                $valueType = null;
×
208

UNCOV
209
                if ($type instanceof LegacyType) {
×
210
                    if ($type->isCollection()) {
×
211
                        $valueType = $type->getCollectionValueTypes()[0] ?? null;
×
212
                        $isMany = null !== $valueType && ($className = $valueType->getClassName()) && $this->resourceClassResolver->isResourceClass($className);
×
213
                    } else {
214
                        $className = $type->getClassName();
×
215
                        $isOne = $className && $this->resourceClassResolver->isResourceClass($className);
×
216
                    }
UNCOV
217
                } elseif ($type instanceof Type) {
×
UNCOV
218
                    if ($type->isSatisfiedBy(fn ($t) => $t instanceof CollectionType)) {
×
UNCOV
219
                        $isMany = TypeHelper::getCollectionValueType($type)?->isSatisfiedBy($typeIsResourceClass);
×
220
                    } else {
UNCOV
221
                        $isOne = $type->isSatisfiedBy($typeIsResourceClass);
×
222
                    }
223
                }
224

UNCOV
225
                if (!$isOne && !$isMany) {
×
226
                    // don't declare it as an attribute too quick: maybe the next type is a valid resource
UNCOV
227
                    continue;
×
228
                }
229

UNCOV
230
                $relation = ['name' => $attribute, 'cardinality' => $isOne ? 'one' : 'many', 'iri' => null, 'operation' => null];
×
231

232
                // if we specify the uriTemplate, generates its value for link definition
233
                // @see ApiPlatform\Serializer\AbstractItemNormalizer:getAttributeValue logic for intentional duplicate content
UNCOV
234
                if (($className ?? false) && $uriTemplate = $propertyMetadata->getUriTemplate()) {
×
235
                    $childContext = $this->createChildContext($context, $attribute, $format);
×
236
                    unset($childContext['iri'], $childContext['uri_variables'], $childContext['resource_class'], $childContext['operation'], $childContext['operation_name']);
×
237

238
                    $operation = $this->resourceMetadataCollectionFactory->create($className)->getOperation(
×
239
                        operationName: $uriTemplate,
×
240
                        httpOperation: true
×
241
                    );
×
242

243
                    $relation['iri'] = $this->iriConverter->getIriFromResource($object, UrlGeneratorInterface::ABS_PATH, $operation, $childContext);
×
244
                    $relation['operation'] = $operation;
×
245
                    $cacheKey = null;
×
246
                }
247

UNCOV
248
                if ($propertyMetadata->isReadableLink()) {
×
UNCOV
249
                    $components['embedded'][] = $relation;
×
250
                }
251

UNCOV
252
                $components['links'][] = $relation;
×
UNCOV
253
                $isRelationship = true;
×
254
            }
255

256
            // if all types are not relationships, declare it as an attribute
UNCOV
257
            if (!$isRelationship) {
×
UNCOV
258
                $components['states'][] = $attribute;
×
259
            }
260
        }
261

UNCOV
262
        if ($cacheKey && false !== $context['cache_key']) {
×
UNCOV
263
            $this->componentsCache[$cacheKey] = $components;
×
264
        }
265

UNCOV
266
        return $components;
×
267
    }
268

269
    /**
270
     * Populates _links and _embedded keys.
271
     */
272
    private function populateRelation(array $data, object $object, ?string $format, array $context, array $components, string $type): array
273
    {
UNCOV
274
        $class = $this->getObjectClass($object);
×
275

UNCOV
276
        if ($this->isHalCircularReference($object, $context)) {
×
277
            return $this->handleHalCircularReference($object, $format, $context);
×
278
        }
279

UNCOV
280
        $attributesMetadata = \array_key_exists($class, $this->attributesMetadataCache) ?
×
UNCOV
281
            $this->attributesMetadataCache[$class] :
×
UNCOV
282
            $this->attributesMetadataCache[$class] = $this->classMetadataFactory ? $this->classMetadataFactory->getMetadataFor($class)->getAttributesMetadata() : null;
×
283

UNCOV
284
        $key = '_'.$type;
×
UNCOV
285
        foreach ($components[$type] as $relation) {
×
UNCOV
286
            if (null !== $attributesMetadata && $this->isMaxDepthReached($attributesMetadata, $class, $relation['name'], $context)) {
×
287
                continue;
×
288
            }
289

UNCOV
290
            $relationName = $relation['name'];
×
UNCOV
291
            if ($this->nameConverter) {
×
UNCOV
292
                $relationName = $this->nameConverter->normalize($relationName, $class, $format, $context);
×
293
            }
294

295
            // if we specify the uriTemplate, then the link takes the uriTemplate defined.
UNCOV
296
            if ('links' === $type && $iri = $relation['iri']) {
×
297
                $data[$key][$relationName]['href'] = $iri;
×
298
                continue;
×
299
            }
300

UNCOV
301
            $childContext = $this->createChildContext($context, $relationName, $format);
×
UNCOV
302
            unset($childContext['iri'], $childContext['uri_variables'], $childContext['operation'], $childContext['operation_name']);
×
303

UNCOV
304
            if ($operation = $relation['operation']) {
×
305
                $childContext['operation'] = $operation;
×
306
                $childContext['operation_name'] = $operation->getName();
×
307
            }
308

UNCOV
309
            $attributeValue = $this->getAttributeValue($object, $relation['name'], $format, $childContext);
×
310

UNCOV
311
            if (empty($attributeValue) && ($context[self::SKIP_NULL_TO_ONE_RELATIONS] ?? $this->defaultContext[self::SKIP_NULL_TO_ONE_RELATIONS] ?? true)) {
×
UNCOV
312
                continue;
×
313
            }
314

UNCOV
315
            if ('one' === $relation['cardinality']) {
×
UNCOV
316
                if ('links' === $type) {
×
UNCOV
317
                    if (null !== $attributeValue) {
×
UNCOV
318
                        $data[$key][$relationName]['href'] = $this->getRelationIri($attributeValue);
×
UNCOV
319
                        continue;
×
320
                    }
321
                }
322

UNCOV
323
                $data[$key][$relationName] = $attributeValue;
×
UNCOV
324
                continue;
×
325
            }
326

327
            // many
328
            $data[$key][$relationName] = [];
×
329
            foreach ($attributeValue as $rel) {
×
330
                if ('links' === $type) {
×
331
                    $rel = ['href' => $this->getRelationIri($rel)];
×
332
                }
333

334
                $data[$key][$relationName][] = $rel;
×
335
            }
336
        }
337

UNCOV
338
        return $data;
×
339
    }
340

341
    /**
342
     * Gets the IRI of the given relation.
343
     *
344
     * @throws UnexpectedValueException
345
     */
346
    private function getRelationIri(mixed $rel): string
347
    {
UNCOV
348
        if (!(\is_array($rel) || \is_string($rel))) {
×
349
            throw new UnexpectedValueException('Expected relation to be an IRI or array');
×
350
        }
351

UNCOV
352
        return \is_string($rel) ? $rel : $rel['_links']['self']['href'];
×
353
    }
354

355
    /**
356
     * Is the max depth reached for the given attribute?
357
     *
358
     * @param AttributeMetadataInterface[] $attributesMetadata
359
     */
360
    private function isMaxDepthReached(array $attributesMetadata, string $class, string $attribute, array &$context): bool
361
    {
362
        if (
UNCOV
363
            !($context[self::ENABLE_MAX_DEPTH] ?? false)
×
UNCOV
364
            || !isset($attributesMetadata[$attribute])
×
UNCOV
365
            || null === $maxDepth = $attributesMetadata[$attribute]->getMaxDepth()
×
366
        ) {
UNCOV
367
            return false;
×
368
        }
369

370
        $key = \sprintf(self::DEPTH_KEY_PATTERN, $class, $attribute);
×
371
        if (!isset($context[$key])) {
×
372
            $context[$key] = 1;
×
373

374
            return false;
×
375
        }
376

377
        if ($context[$key] === $maxDepth) {
×
378
            return true;
×
379
        }
380

381
        ++$context[$key];
×
382

383
        return false;
×
384
    }
385

386
    /**
387
     * Detects if the configured circular reference limit is reached.
388
     *
389
     * @throws CircularReferenceException
390
     */
391
    protected function isHalCircularReference(object $object, array &$context): bool
392
    {
UNCOV
393
        $objectHash = spl_object_hash($object);
×
394

UNCOV
395
        $circularReferenceLimit = $context[AbstractNormalizer::CIRCULAR_REFERENCE_LIMIT] ?? $this->defaultContext[AbstractNormalizer::CIRCULAR_REFERENCE_LIMIT];
×
UNCOV
396
        if (isset($context[self::HAL_CIRCULAR_REFERENCE_LIMIT_COUNTERS][$objectHash])) {
×
397
            if ($context[self::HAL_CIRCULAR_REFERENCE_LIMIT_COUNTERS][$objectHash] >= $circularReferenceLimit) {
×
398
                unset($context[self::HAL_CIRCULAR_REFERENCE_LIMIT_COUNTERS][$objectHash]);
×
399

400
                return true;
×
401
            }
402

403
            ++$context[self::HAL_CIRCULAR_REFERENCE_LIMIT_COUNTERS][$objectHash];
×
404
        } else {
UNCOV
405
            $context[self::HAL_CIRCULAR_REFERENCE_LIMIT_COUNTERS][$objectHash] = 1;
×
406
        }
407

UNCOV
408
        return false;
×
409
    }
410

411
    /**
412
     * Handles a circular reference.
413
     *
414
     * If a circular reference handler is set, it will be called. Otherwise, a
415
     * {@class CircularReferenceException} will be thrown.
416
     *
417
     * @final
418
     *
419
     * @throws CircularReferenceException
420
     */
421
    protected function handleHalCircularReference(object $object, ?string $format = null, array $context = []): mixed
422
    {
423
        $circularReferenceHandler = $context[AbstractNormalizer::CIRCULAR_REFERENCE_HANDLER] ?? $this->defaultContext[AbstractNormalizer::CIRCULAR_REFERENCE_HANDLER];
×
424
        if ($circularReferenceHandler) {
×
425
            return $circularReferenceHandler($object, $format, $context);
×
426
        }
427

428
        throw new CircularReferenceException(\sprintf('A circular reference has been detected when serializing the object of class "%s" (configured limit: %d).', get_debug_type($object), $context[AbstractNormalizer::CIRCULAR_REFERENCE_LIMIT] ?? $this->defaultContext[AbstractNormalizer::CIRCULAR_REFERENCE_LIMIT]));
×
429
    }
430
}
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

© 2025 Coveralls, Inc