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

api-platform / core / 14635100171

24 Apr 2025 06:39AM UTC coverage: 8.271% (+0.02%) from 8.252%
14635100171

Pull #6904

github

web-flow
Merge c9cefd82e into a3e5e53ea
Pull Request #6904: feat(graphql): added support for graphql subscriptions to work for actions

0 of 73 new or added lines in 3 files covered. (0.0%)

1999 existing lines in 144 files now uncovered.

13129 of 158728 relevant lines covered (8.27%)

13.6 hits per line

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

86.23
/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\Serializer\AbstractItemNormalizer;
25
use ApiPlatform\Serializer\CacheKeyTrait;
26
use ApiPlatform\Serializer\ContextTrait;
27
use ApiPlatform\Serializer\TagCollectorInterface;
28
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
29
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
30
use Symfony\Component\PropertyInfo\Type as LegacyType;
31
use Symfony\Component\Serializer\Exception\CircularReferenceException;
32
use Symfony\Component\Serializer\Exception\LogicException;
33
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
34
use Symfony\Component\Serializer\Mapping\AttributeMetadataInterface;
35
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
36
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
37
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
38
use Symfony\Component\TypeInfo\Type;
39
use Symfony\Component\TypeInfo\Type\CollectionType;
40
use Symfony\Component\TypeInfo\Type\CompositeTypeInterface;
41
use Symfony\Component\TypeInfo\Type\ObjectType;
42
use Symfony\Component\TypeInfo\Type\WrappingTypeInterface;
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
    {
64
        $defaultContext[AbstractNormalizer::CIRCULAR_REFERENCE_HANDLER] = function ($object): ?array {
1,172✔
65
            $iri = $this->iriConverter->getIriFromResource($object);
×
66
            if (null === $iri) {
×
67
                return null;
×
68
            }
69

70
            return ['_links' => ['self' => ['href' => $iri]]];
×
71
        };
1,172✔
72

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

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

84
    public function getSupportedTypes($format): array
85
    {
86
        return self::FORMAT === $format ? parent::getSupportedTypes($format) : [];
1,045✔
87
    }
88

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

99
        $previousResourceClass = $context['resource_class'] ?? null;
70✔
100
        if ($this->resourceClassResolver->isResourceClass($resourceClass) && (null === $previousResourceClass || $this->resourceClassResolver->isResourceClass($previousResourceClass))) {
70✔
101
            $resourceClass = $this->resourceClassResolver->getResourceClass($object, $previousResourceClass);
66✔
102
        }
103

104
        $context = $this->initContext($resourceClass, $context);
70✔
105

106
        $iri = $context['iri'] ??= $this->iriConverter->getIriFromResource($object, UrlGeneratorInterface::ABS_PATH, $context['operation'] ?? null, $context);
70✔
107
        $context['object'] = $object;
70✔
108
        $context['format'] = $format;
70✔
109
        $context['api_normalize'] = true;
70✔
110

111
        if (!isset($context['cache_key'])) {
70✔
112
            $context['cache_key'] = $this->getCacheKey($format, $context);
70✔
113
        }
114

115
        $data = parent::normalize($object, $format, $context);
70✔
116

117
        if (!\is_array($data)) {
70✔
118
            return $data;
×
119
        }
120

121
        $metadata = [
70✔
122
            '_links' => [
70✔
123
                'self' => [
70✔
124
                    'href' => $iri,
70✔
125
                ],
70✔
126
            ],
70✔
127
        ];
70✔
128
        $components = $this->getComponents($object, $format, $context);
70✔
129
        $metadata = $this->populateRelation($metadata, $object, $format, $context, $components, 'links');
70✔
130
        $metadata = $this->populateRelation($metadata, $object, $format, $context, $components, 'embedded');
70✔
131

132
        return $metadata + $data;
70✔
133
    }
134

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

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

154
    /**
155
     * {@inheritdoc}
156
     */
157
    protected function getAttributes($object, $format = null, array $context = []): array
158
    {
159
        return $this->getComponents($object, $format, $context)['states'];
70✔
160
    }
161

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

169
        if (isset($this->componentsCache[$cacheKey])) {
70✔
170
            return $this->componentsCache[$cacheKey];
68✔
171
        }
172

173
        $attributes = parent::getAttributes($object, $format, $context);
70✔
174
        $options = $this->getFactoryOptions($context);
70✔
175

176
        $components = [
70✔
177
            'states' => [],
70✔
178
            'links' => [],
70✔
179
            'embedded' => [],
70✔
180
        ];
70✔
181

182
        foreach ($attributes as $attribute) {
70✔
183
            $propertyMetadata = $this->propertyMetadataFactory->create($context['resource_class'], $attribute, $options);
68✔
184

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

194
            // prevent declaring $attribute as attribute if it's already declared as relationship
195
            $isRelationship = false;
68✔
196
            $typeIsResourceClass = function (Type $type) use (&$typeIsResourceClass, &$className): bool {
68✔
197
                return match (true) {
198
                    $type instanceof WrappingTypeInterface => $type->wrappedTypeIsSatisfiedBy($typeIsResourceClass),
66✔
199
                    $type instanceof CompositeTypeInterface => $type->composedTypesAreSatisfiedBy($typeIsResourceClass),
66✔
200
                    default => $type instanceof ObjectType && $this->resourceClassResolver->isResourceClass($className = $type->getClassName()),
66✔
201
                };
202
            };
68✔
203

204
            foreach ($types as $type) {
68✔
205
                $isOne = $isMany = false;
66✔
206

207
                /** @var Type|LegacyType|null $valueType */
208
                $valueType = null;
66✔
209

210
                if ($type instanceof LegacyType) {
66✔
211
                    if ($type->isCollection()) {
×
212
                        $valueType = $type->getCollectionValueTypes()[0] ?? null;
×
213
                        $isMany = null !== $valueType && ($className = $valueType->getClassName()) && $this->resourceClassResolver->isResourceClass($className);
×
214
                    } else {
215
                        $className = $type->getClassName();
×
216
                        $isOne = $className && $this->resourceClassResolver->isResourceClass($className);
×
217
                    }
218
                } elseif ($type instanceof Type) {
66✔
219
                    $typeIsCollection = function (Type $type) use (&$typeIsCollection, &$valueType): bool {
66✔
220
                        return match (true) {
221
                            $type instanceof CollectionType => null !== $valueType = $type->getCollectionValueType(),
66✔
222
                            $type instanceof WrappingTypeInterface => $type->wrappedTypeIsSatisfiedBy($typeIsCollection),
66✔
223
                            $type instanceof CompositeTypeInterface => $type->composedTypesAreSatisfiedBy($typeIsCollection),
66✔
224
                            default => false,
66✔
225
                        };
226
                    };
66✔
227

228
                    if ($type->isSatisfiedBy($typeIsCollection)) {
66✔
229
                        $isMany = $valueType->isSatisfiedBy($typeIsResourceClass);
21✔
230
                    } else {
231
                        $isOne = $type->isSatisfiedBy($typeIsResourceClass);
66✔
232
                    }
233
                }
234

235
                if (!$isOne && !$isMany) {
66✔
236
                    // don't declare it as an attribute too quick: maybe the next type is a valid resource
237
                    continue;
66✔
238
                }
239

240
                $relation = ['name' => $attribute, 'cardinality' => $isOne ? 'one' : 'many', 'iri' => null, 'operation' => null];
38✔
241

242
                // if we specify the uriTemplate, generates its value for link definition
243
                // @see ApiPlatform\Serializer\AbstractItemNormalizer:getAttributeValue logic for intentional duplicate content
244
                if (($className ?? false) && $uriTemplate = $propertyMetadata->getUriTemplate()) {
38✔
UNCOV
245
                    $childContext = $this->createChildContext($context, $attribute, $format);
1✔
UNCOV
246
                    unset($childContext['iri'], $childContext['uri_variables'], $childContext['resource_class'], $childContext['operation'], $childContext['operation_name']);
1✔
247

UNCOV
248
                    $operation = $this->resourceMetadataCollectionFactory->create($className)->getOperation(
1✔
UNCOV
249
                        operationName: $uriTemplate,
1✔
UNCOV
250
                        httpOperation: true
1✔
UNCOV
251
                    );
1✔
252

UNCOV
253
                    $relation['iri'] = $this->iriConverter->getIriFromResource($object, UrlGeneratorInterface::ABS_PATH, $operation, $childContext);
1✔
UNCOV
254
                    $relation['operation'] = $operation;
1✔
UNCOV
255
                    $cacheKey = null;
1✔
256
                }
257

258
                if ($propertyMetadata->isReadableLink()) {
38✔
259
                    $components['embedded'][] = $relation;
12✔
260
                }
261

262
                $components['links'][] = $relation;
38✔
263
                $isRelationship = true;
38✔
264
            }
265

266
            // if all types are not relationships, declare it as an attribute
267
            if (!$isRelationship) {
68✔
268
                $components['states'][] = $attribute;
68✔
269
            }
270
        }
271

272
        if ($cacheKey && false !== $context['cache_key']) {
70✔
273
            $this->componentsCache[$cacheKey] = $components;
68✔
274
        }
275

276
        return $components;
70✔
277
    }
278

279
    /**
280
     * Populates _links and _embedded keys.
281
     */
282
    private function populateRelation(array $data, object $object, ?string $format, array $context, array $components, string $type): array
283
    {
284
        $class = $this->getObjectClass($object);
70✔
285

286
        if ($this->isHalCircularReference($object, $context)) {
70✔
287
            return $this->handleHalCircularReference($object, $format, $context);
×
288
        }
289

290
        $attributesMetadata = \array_key_exists($class, $this->attributesMetadataCache) ?
70✔
291
            $this->attributesMetadataCache[$class] :
70✔
292
            $this->attributesMetadataCache[$class] = $this->classMetadataFactory ? $this->classMetadataFactory->getMetadataFor($class)->getAttributesMetadata() : null;
70✔
293

294
        $key = '_'.$type;
70✔
295
        foreach ($components[$type] as $relation) {
70✔
296
            if (null !== $attributesMetadata && $this->isMaxDepthReached($attributesMetadata, $class, $relation['name'], $context)) {
38✔
297
                continue;
5✔
298
            }
299

300
            $relationName = $relation['name'];
38✔
301
            if ($this->nameConverter) {
38✔
302
                $relationName = $this->nameConverter->normalize($relationName, $class, $format, $context);
36✔
303
            }
304

305
            // if we specify the uriTemplate, then the link takes the uriTemplate defined.
306
            if ('links' === $type && $iri = $relation['iri']) {
38✔
UNCOV
307
                $data[$key][$relationName]['href'] = $iri;
1✔
UNCOV
308
                continue;
1✔
309
            }
310

311
            $childContext = $this->createChildContext($context, $relationName, $format);
38✔
312
            unset($childContext['iri'], $childContext['uri_variables'], $childContext['operation'], $childContext['operation_name']);
38✔
313

314
            if ($operation = $relation['operation']) {
38✔
UNCOV
315
                $childContext['operation'] = $operation;
1✔
UNCOV
316
                $childContext['operation_name'] = $operation->getName();
1✔
317
            }
318

319
            $attributeValue = $this->getAttributeValue($object, $relation['name'], $format, $childContext);
38✔
320

321
            if (empty($attributeValue)) {
38✔
322
                continue;
23✔
323
            }
324

325
            if ('one' === $relation['cardinality']) {
25✔
326
                if ('links' === $type) {
22✔
327
                    $data[$key][$relationName]['href'] = $this->getRelationIri($attributeValue);
21✔
328
                    continue;
21✔
329
                }
330

331
                $data[$key][$relationName] = $attributeValue;
9✔
332
                continue;
9✔
333
            }
334

335
            // many
UNCOV
336
            $data[$key][$relationName] = [];
8✔
UNCOV
337
            foreach ($attributeValue as $rel) {
8✔
UNCOV
338
                if ('links' === $type) {
8✔
UNCOV
339
                    $rel = ['href' => $this->getRelationIri($rel)];
7✔
340
                }
341

UNCOV
342
                $data[$key][$relationName][] = $rel;
8✔
343
            }
344
        }
345

346
        return $data;
70✔
347
    }
348

349
    /**
350
     * Gets the IRI of the given relation.
351
     *
352
     * @throws UnexpectedValueException
353
     */
354
    private function getRelationIri(mixed $rel): string
355
    {
356
        if (!(\is_array($rel) || \is_string($rel))) {
24✔
357
            throw new UnexpectedValueException('Expected relation to be an IRI or array');
×
358
        }
359

360
        return \is_string($rel) ? $rel : $rel['_links']['self']['href'];
24✔
361
    }
362

363
    /**
364
     * Is the max depth reached for the given attribute?
365
     *
366
     * @param AttributeMetadataInterface[] $attributesMetadata
367
     */
368
    private function isMaxDepthReached(array $attributesMetadata, string $class, string $attribute, array &$context): bool
369
    {
370
        if (
371
            !($context[self::ENABLE_MAX_DEPTH] ?? false)
34✔
372
            || !isset($attributesMetadata[$attribute])
34✔
373
            || null === $maxDepth = $attributesMetadata[$attribute]->getMaxDepth()
34✔
374
        ) {
375
            return false;
31✔
376
        }
377

378
        $key = \sprintf(self::DEPTH_KEY_PATTERN, $class, $attribute);
5✔
379
        if (!isset($context[$key])) {
5✔
380
            $context[$key] = 1;
5✔
381

382
            return false;
5✔
383
        }
384

385
        if ($context[$key] === $maxDepth) {
5✔
386
            return true;
5✔
387
        }
388

389
        ++$context[$key];
×
390

391
        return false;
×
392
    }
393

394
    /**
395
     * Detects if the configured circular reference limit is reached.
396
     *
397
     * @throws CircularReferenceException
398
     */
399
    protected function isHalCircularReference(object $object, array &$context): bool
400
    {
401
        $objectHash = spl_object_hash($object);
70✔
402

403
        $circularReferenceLimit = $context[AbstractNormalizer::CIRCULAR_REFERENCE_LIMIT] ?? $this->defaultContext[AbstractNormalizer::CIRCULAR_REFERENCE_LIMIT];
70✔
404
        if (isset($context[self::HAL_CIRCULAR_REFERENCE_LIMIT_COUNTERS][$objectHash])) {
70✔
405
            if ($context[self::HAL_CIRCULAR_REFERENCE_LIMIT_COUNTERS][$objectHash] >= $circularReferenceLimit) {
×
406
                unset($context[self::HAL_CIRCULAR_REFERENCE_LIMIT_COUNTERS][$objectHash]);
×
407

408
                return true;
×
409
            }
410

411
            ++$context[self::HAL_CIRCULAR_REFERENCE_LIMIT_COUNTERS][$objectHash];
×
412
        } else {
413
            $context[self::HAL_CIRCULAR_REFERENCE_LIMIT_COUNTERS][$objectHash] = 1;
70✔
414
        }
415

416
        return false;
70✔
417
    }
418

419
    /**
420
     * Handles a circular reference.
421
     *
422
     * If a circular reference handler is set, it will be called. Otherwise, a
423
     * {@class CircularReferenceException} will be thrown.
424
     *
425
     * @final
426
     *
427
     * @throws CircularReferenceException
428
     */
429
    protected function handleHalCircularReference(object $object, ?string $format = null, array $context = []): mixed
430
    {
431
        $circularReferenceHandler = $context[AbstractNormalizer::CIRCULAR_REFERENCE_HANDLER] ?? $this->defaultContext[AbstractNormalizer::CIRCULAR_REFERENCE_HANDLER];
×
432
        if ($circularReferenceHandler) {
×
433
            return $circularReferenceHandler($object, $format, $context);
×
434
        }
435

436
        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]));
×
437
    }
438
}
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