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

api-platform / core / 15133993414

20 May 2025 09:30AM UTC coverage: 26.313% (-1.2%) from 27.493%
15133993414

Pull #7161

github

web-flow
Merge e2c03d45f into 5459ba375
Pull Request #7161: fix(metadata): infer parameter string type from schema

0 of 2 new or added lines in 1 file covered. (0.0%)

11019 existing lines in 363 files now uncovered.

12898 of 49018 relevant lines covered (26.31%)

34.33 hits per line

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

88.44
/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\Serializer\Exception\CircularReferenceException;
30
use Symfony\Component\Serializer\Exception\LogicException;
31
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
32
use Symfony\Component\Serializer\Mapping\AttributeMetadataInterface;
33
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
34
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
35
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
36

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

48
    public const FORMAT = 'jsonhal';
49

50
    protected const HAL_CIRCULAR_REFERENCE_LIMIT_COUNTERS = 'hal_circular_reference_limit_counters';
51

52
    private array $componentsCache = [];
53
    private array $attributesMetadataCache = [];
54

55
    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)
56
    {
UNCOV
57
        $defaultContext[AbstractNormalizer::CIRCULAR_REFERENCE_HANDLER] = function ($object): ?array {
956✔
58
            $iri = $this->iriConverter->getIriFromResource($object);
×
59
            if (null === $iri) {
×
60
                return null;
×
61
            }
62

63
            return ['_links' => ['self' => ['href' => $iri]]];
×
UNCOV
64
        };
956✔
65

UNCOV
66
        parent::__construct($propertyNameCollectionFactory, $propertyMetadataFactory, $iriConverter, $resourceClassResolver, $propertyAccessor, $nameConverter, $classMetadataFactory, $defaultContext, $resourceMetadataCollectionFactory, $resourceAccessChecker, $tagCollector);
956✔
67
    }
68

69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
73
    {
UNCOV
74
        return self::FORMAT === $format && parent::supportsNormalization($data, $format, $context);
51✔
75
    }
76

77
    public function getSupportedTypes($format): array
78
    {
UNCOV
79
        return self::FORMAT === $format ? parent::getSupportedTypes($format) : [];
857✔
80
    }
81

82
    /**
83
     * {@inheritdoc}
84
     */
85
    public function normalize(mixed $object, ?string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
86
    {
UNCOV
87
        $resourceClass = $this->getObjectClass($object);
52✔
UNCOV
88
        if ($this->getOutputClass($context)) {
52✔
UNCOV
89
            return parent::normalize($object, $format, $context);
3✔
90
        }
91

UNCOV
92
        $previousResourceClass = $context['resource_class'] ?? null;
52✔
UNCOV
93
        if ($this->resourceClassResolver->isResourceClass($resourceClass) && (null === $previousResourceClass || $this->resourceClassResolver->isResourceClass($previousResourceClass))) {
52✔
UNCOV
94
            $resourceClass = $this->resourceClassResolver->getResourceClass($object, $previousResourceClass);
49✔
95
        }
96

UNCOV
97
        $context = $this->initContext($resourceClass, $context);
52✔
98

UNCOV
99
        $iri = $context['iri'] ??= $this->iriConverter->getIriFromResource($object, UrlGeneratorInterface::ABS_PATH, $context['operation'] ?? null, $context);
52✔
UNCOV
100
        $context['object'] = $object;
52✔
UNCOV
101
        $context['format'] = $format;
52✔
UNCOV
102
        $context['api_normalize'] = true;
52✔
103

UNCOV
104
        if (!isset($context['cache_key'])) {
52✔
UNCOV
105
            $context['cache_key'] = $this->getCacheKey($format, $context);
52✔
106
        }
107

UNCOV
108
        $data = parent::normalize($object, $format, $context);
52✔
109

UNCOV
110
        if (!\is_array($data)) {
52✔
111
            return $data;
×
112
        }
113

UNCOV
114
        $metadata = [
52✔
UNCOV
115
            '_links' => [
52✔
UNCOV
116
                'self' => [
52✔
UNCOV
117
                    'href' => $iri,
52✔
UNCOV
118
                ],
52✔
UNCOV
119
            ],
52✔
UNCOV
120
        ];
52✔
UNCOV
121
        $components = $this->getComponents($object, $format, $context);
52✔
UNCOV
122
        $metadata = $this->populateRelation($metadata, $object, $format, $context, $components, 'links');
52✔
UNCOV
123
        $metadata = $this->populateRelation($metadata, $object, $format, $context, $components, 'embedded');
52✔
124

UNCOV
125
        return $metadata + $data;
52✔
126
    }
127

128
    /**
129
     * {@inheritdoc}
130
     */
131
    public function supportsDenormalization(mixed $data, string $type, ?string $format = null, array $context = []): bool
132
    {
133
        // prevent the use of lower priority normalizers (e.g. serializer.normalizer.object) for this format
UNCOV
134
        return self::FORMAT === $format;
1✔
135
    }
136

137
    /**
138
     * {@inheritdoc}
139
     *
140
     * @throws LogicException
141
     */
142
    public function denormalize(mixed $data, string $type, ?string $format = null, array $context = []): never
143
    {
UNCOV
144
        throw new LogicException(\sprintf('%s is a read-only format.', self::FORMAT));
1✔
145
    }
146

147
    /**
148
     * {@inheritdoc}
149
     */
150
    protected function getAttributes($object, $format = null, array $context = []): array
151
    {
UNCOV
152
        return $this->getComponents($object, $format, $context)['states'];
52✔
153
    }
154

155
    /**
156
     * Gets HAL components of the resource: states, links and embedded.
157
     */
158
    private function getComponents(object $object, ?string $format, array $context): array
159
    {
UNCOV
160
        $cacheKey = $this->getObjectClass($object).'-'.$context['cache_key'];
52✔
161

UNCOV
162
        if (isset($this->componentsCache[$cacheKey])) {
52✔
UNCOV
163
            return $this->componentsCache[$cacheKey];
51✔
164
        }
165

UNCOV
166
        $attributes = parent::getAttributes($object, $format, $context);
52✔
UNCOV
167
        $options = $this->getFactoryOptions($context);
52✔
168

UNCOV
169
        $components = [
52✔
UNCOV
170
            'states' => [],
52✔
UNCOV
171
            'links' => [],
52✔
UNCOV
172
            'embedded' => [],
52✔
UNCOV
173
        ];
52✔
174

UNCOV
175
        foreach ($attributes as $attribute) {
52✔
UNCOV
176
            $propertyMetadata = $this->propertyMetadataFactory->create($context['resource_class'], $attribute, $options);
51✔
177

UNCOV
178
            $types = $propertyMetadata->getBuiltinTypes() ?? [];
51✔
179

180
            // prevent declaring $attribute as attribute if it's already declared as relationship
UNCOV
181
            $isRelationship = false;
51✔
182

UNCOV
183
            foreach ($types as $type) {
51✔
UNCOV
184
                $isOne = $isMany = false;
49✔
185

UNCOV
186
                if (null !== $type) {
49✔
UNCOV
187
                    if ($type->isCollection()) {
49✔
UNCOV
188
                        $valueType = $type->getCollectionValueTypes()[0] ?? null;
19✔
UNCOV
189
                        $isMany = null !== $valueType && ($className = $valueType->getClassName()) && $this->resourceClassResolver->isResourceClass($className);
19✔
190
                    } else {
UNCOV
191
                        $className = $type->getClassName();
49✔
UNCOV
192
                        $isOne = $className && $this->resourceClassResolver->isResourceClass($className);
49✔
193
                    }
194
                }
195

UNCOV
196
                if (!$isOne && !$isMany) {
49✔
197
                    // don't declare it as an attribute too quick: maybe the next type is a valid resource
UNCOV
198
                    continue;
49✔
199
                }
200

UNCOV
201
                $relation = ['name' => $attribute, 'cardinality' => $isOne ? 'one' : 'many', 'iri' => null, 'operation' => null];
34✔
202

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

209
                    $operation = $this->resourceMetadataCollectionFactory->create($className)->getOperation(
1✔
210
                        operationName: $uriTemplate,
1✔
211
                        httpOperation: true
1✔
212
                    );
1✔
213

214
                    $relation['iri'] = $this->iriConverter->getIriFromResource($object, UrlGeneratorInterface::ABS_PATH, $operation, $childContext);
1✔
215
                    $relation['operation'] = $operation;
1✔
216
                    $cacheKey = null;
1✔
217
                }
218

UNCOV
219
                if ($propertyMetadata->isReadableLink()) {
34✔
UNCOV
220
                    $components['embedded'][] = $relation;
10✔
221
                }
222

UNCOV
223
                $components['links'][] = $relation;
34✔
UNCOV
224
                $isRelationship = true;
34✔
225
            }
226

227
            // if all types are not relationships, declare it as an attribute
UNCOV
228
            if (!$isRelationship) {
51✔
UNCOV
229
                $components['states'][] = $attribute;
51✔
230
            }
231
        }
232

UNCOV
233
        if ($cacheKey && false !== $context['cache_key']) {
52✔
UNCOV
234
            $this->componentsCache[$cacheKey] = $components;
51✔
235
        }
236

UNCOV
237
        return $components;
52✔
238
    }
239

240
    /**
241
     * Populates _links and _embedded keys.
242
     */
243
    private function populateRelation(array $data, object $object, ?string $format, array $context, array $components, string $type): array
244
    {
UNCOV
245
        $class = $this->getObjectClass($object);
52✔
246

UNCOV
247
        if ($this->isHalCircularReference($object, $context)) {
52✔
248
            return $this->handleHalCircularReference($object, $format, $context);
×
249
        }
250

UNCOV
251
        $attributesMetadata = \array_key_exists($class, $this->attributesMetadataCache) ?
52✔
UNCOV
252
            $this->attributesMetadataCache[$class] :
52✔
UNCOV
253
            $this->attributesMetadataCache[$class] = $this->classMetadataFactory ? $this->classMetadataFactory->getMetadataFor($class)->getAttributesMetadata() : null;
52✔
254

UNCOV
255
        $key = '_'.$type;
52✔
UNCOV
256
        foreach ($components[$type] as $relation) {
52✔
UNCOV
257
            if (null !== $attributesMetadata && $this->isMaxDepthReached($attributesMetadata, $class, $relation['name'], $context)) {
34✔
UNCOV
258
                continue;
4✔
259
            }
260

UNCOV
261
            $relationName = $relation['name'];
34✔
UNCOV
262
            if ($this->nameConverter) {
34✔
UNCOV
263
                $relationName = $this->nameConverter->normalize($relationName, $class, $format, $context);
33✔
264
            }
265

266
            // if we specify the uriTemplate, then the link takes the uriTemplate defined.
UNCOV
267
            if ('links' === $type && $iri = $relation['iri']) {
34✔
268
                $data[$key][$relationName]['href'] = $iri;
1✔
269
                continue;
1✔
270
            }
271

UNCOV
272
            $childContext = $this->createChildContext($context, $relationName, $format);
34✔
UNCOV
273
            unset($childContext['iri'], $childContext['uri_variables'], $childContext['operation'], $childContext['operation_name']);
34✔
274

UNCOV
275
            if ($operation = $relation['operation']) {
34✔
276
                $childContext['operation'] = $operation;
1✔
277
                $childContext['operation_name'] = $operation->getName();
1✔
278
            }
279

UNCOV
280
            $attributeValue = $this->getAttributeValue($object, $relation['name'], $format, $childContext);
34✔
281

UNCOV
282
            if (empty($attributeValue)) {
34✔
UNCOV
283
                continue;
21✔
284
            }
285

UNCOV
286
            if ('one' === $relation['cardinality']) {
22✔
UNCOV
287
                if ('links' === $type) {
19✔
UNCOV
288
                    $data[$key][$relationName]['href'] = $this->getRelationIri($attributeValue);
18✔
UNCOV
289
                    continue;
18✔
290
                }
291

UNCOV
292
                $data[$key][$relationName] = $attributeValue;
8✔
UNCOV
293
                continue;
8✔
294
            }
295

296
            // many
297
            $data[$key][$relationName] = [];
8✔
298
            foreach ($attributeValue as $rel) {
8✔
299
                if ('links' === $type) {
8✔
300
                    $rel = ['href' => $this->getRelationIri($rel)];
7✔
301
                }
302

303
                $data[$key][$relationName][] = $rel;
8✔
304
            }
305
        }
306

UNCOV
307
        return $data;
52✔
308
    }
309

310
    /**
311
     * Gets the IRI of the given relation.
312
     *
313
     * @throws UnexpectedValueException
314
     */
315
    private function getRelationIri(mixed $rel): string
316
    {
UNCOV
317
        if (!(\is_array($rel) || \is_string($rel))) {
21✔
318
            throw new UnexpectedValueException('Expected relation to be an IRI or array');
×
319
        }
320

UNCOV
321
        return \is_string($rel) ? $rel : $rel['_links']['self']['href'];
21✔
322
    }
323

324
    /**
325
     * Is the max depth reached for the given attribute?
326
     *
327
     * @param AttributeMetadataInterface[] $attributesMetadata
328
     */
329
    private function isMaxDepthReached(array $attributesMetadata, string $class, string $attribute, array &$context): bool
330
    {
331
        if (
UNCOV
332
            !($context[self::ENABLE_MAX_DEPTH] ?? false)
32✔
UNCOV
333
            || !isset($attributesMetadata[$attribute])
32✔
UNCOV
334
            || null === $maxDepth = $attributesMetadata[$attribute]->getMaxDepth()
32✔
335
        ) {
UNCOV
336
            return false;
29✔
337
        }
338

UNCOV
339
        $key = \sprintf(self::DEPTH_KEY_PATTERN, $class, $attribute);
4✔
UNCOV
340
        if (!isset($context[$key])) {
4✔
UNCOV
341
            $context[$key] = 1;
4✔
342

UNCOV
343
            return false;
4✔
344
        }
345

UNCOV
346
        if ($context[$key] === $maxDepth) {
4✔
UNCOV
347
            return true;
4✔
348
        }
349

350
        ++$context[$key];
×
351

352
        return false;
×
353
    }
354

355
    /**
356
     * Detects if the configured circular reference limit is reached.
357
     *
358
     * @throws CircularReferenceException
359
     */
360
    protected function isHalCircularReference(object $object, array &$context): bool
361
    {
UNCOV
362
        $objectHash = spl_object_hash($object);
52✔
363

UNCOV
364
        $circularReferenceLimit = $context[AbstractNormalizer::CIRCULAR_REFERENCE_LIMIT] ?? $this->defaultContext[AbstractNormalizer::CIRCULAR_REFERENCE_LIMIT];
52✔
UNCOV
365
        if (isset($context[self::HAL_CIRCULAR_REFERENCE_LIMIT_COUNTERS][$objectHash])) {
52✔
366
            if ($context[self::HAL_CIRCULAR_REFERENCE_LIMIT_COUNTERS][$objectHash] >= $circularReferenceLimit) {
×
367
                unset($context[self::HAL_CIRCULAR_REFERENCE_LIMIT_COUNTERS][$objectHash]);
×
368

369
                return true;
×
370
            }
371

372
            ++$context[self::HAL_CIRCULAR_REFERENCE_LIMIT_COUNTERS][$objectHash];
×
373
        } else {
UNCOV
374
            $context[self::HAL_CIRCULAR_REFERENCE_LIMIT_COUNTERS][$objectHash] = 1;
52✔
375
        }
376

UNCOV
377
        return false;
52✔
378
    }
379

380
    /**
381
     * Handles a circular reference.
382
     *
383
     * If a circular reference handler is set, it will be called. Otherwise, a
384
     * {@class CircularReferenceException} will be thrown.
385
     *
386
     * @final
387
     *
388
     * @throws CircularReferenceException
389
     */
390
    protected function handleHalCircularReference(object $object, ?string $format = null, array $context = []): mixed
391
    {
392
        $circularReferenceHandler = $context[AbstractNormalizer::CIRCULAR_REFERENCE_HANDLER] ?? $this->defaultContext[AbstractNormalizer::CIRCULAR_REFERENCE_HANDLER];
×
393
        if ($circularReferenceHandler) {
×
394
            return $circularReferenceHandler($object, $format, $context);
×
395
        }
396

397
        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]));
×
398
    }
399
}
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