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

api-platform / core / 9710836697

28 Jun 2024 09:35AM UTC coverage: 63.285% (+1.2%) from 62.122%
9710836697

push

github

soyuka
docs: changelog v3.3.7

11104 of 17546 relevant lines covered (63.29%)

52.26 hits per line

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

55.5
/src/JsonApi/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\JsonApi\Serializer;
15

16
use ApiPlatform\Api\IriConverterInterface as LegacyIriConverterInterface;
17
use ApiPlatform\Api\ResourceClassResolverInterface as LegacyResourceClassResolverInterface;
18
use ApiPlatform\Exception\ItemNotFoundException;
19
use ApiPlatform\Metadata\ApiProperty;
20
use ApiPlatform\Metadata\IriConverterInterface;
21
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
22
use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
23
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
24
use ApiPlatform\Metadata\ResourceClassResolverInterface;
25
use ApiPlatform\Metadata\UrlGeneratorInterface;
26
use ApiPlatform\Metadata\Util\ClassInfoTrait;
27
use ApiPlatform\Serializer\AbstractItemNormalizer;
28
use ApiPlatform\Serializer\CacheKeyTrait;
29
use ApiPlatform\Serializer\ContextTrait;
30
use ApiPlatform\Serializer\TagCollectorInterface;
31
use ApiPlatform\Symfony\Security\ResourceAccessCheckerInterface;
32
use Symfony\Component\ErrorHandler\Exception\FlattenException;
33
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
34
use Symfony\Component\Serializer\Exception\LogicException;
35
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
36
use Symfony\Component\Serializer\Exception\RuntimeException;
37
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
38
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
39
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
40
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
41

42
/**
43
 * Converts between objects and array.
44
 *
45
 * @author Kévin Dunglas <dunglas@gmail.com>
46
 * @author Amrouche Hamza <hamza.simperfit@gmail.com>
47
 * @author Baptiste Meyer <baptiste.meyer@gmail.com>
48
 */
49
final class ItemNormalizer extends AbstractItemNormalizer
50
{
51
    use CacheKeyTrait;
52
    use ClassInfoTrait;
53
    use ContextTrait;
54

55
    public const FORMAT = 'jsonapi';
56

57
    private array $componentsCache = [];
58

59
    public function __construct(PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, IriConverterInterface|LegacyIriConverterInterface $iriConverter, ResourceClassResolverInterface|LegacyResourceClassResolverInterface $resourceClassResolver, ?PropertyAccessorInterface $propertyAccessor = null, ?NameConverterInterface $nameConverter = null, ?ClassMetadataFactoryInterface $classMetadataFactory = null, array $defaultContext = [], ?ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory = null, ?ResourceAccessCheckerInterface $resourceAccessChecker = null, protected ?TagCollectorInterface $tagCollector = null)
60
    {
61
        parent::__construct($propertyNameCollectionFactory, $propertyMetadataFactory, $iriConverter, $resourceClassResolver, $propertyAccessor, $nameConverter, $classMetadataFactory, $defaultContext, $resourceMetadataCollectionFactory, $resourceAccessChecker, $tagCollector);
291✔
62
    }
63

64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
68
    {
69
        return self::FORMAT === $format && parent::supportsNormalization($data, $format, $context) && !($data instanceof \Exception || $data instanceof FlattenException);
24✔
70
    }
71

72
    public function getSupportedTypes($format): array
73
    {
74
        return self::FORMAT === $format ? parent::getSupportedTypes($format) : [];
211✔
75
    }
76

77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function normalize(mixed $object, ?string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
81
    {
82
        $resourceClass = $this->getObjectClass($object);
36✔
83
        if ($this->getOutputClass($context)) {
36✔
84
            return parent::normalize($object, $format, $context);
4✔
85
        }
86

87
        $previousResourceClass = $context['resource_class'] ?? null;
36✔
88
        if ($this->resourceClassResolver->isResourceClass($resourceClass) && (null === $previousResourceClass || $this->resourceClassResolver->isResourceClass($previousResourceClass))) {
36✔
89
            $resourceClass = $this->resourceClassResolver->getResourceClass($object, $previousResourceClass);
32✔
90
        }
91

92
        if (($operation = $context['operation'] ?? null) && method_exists($operation, 'getItemUriTemplate')) {
36✔
93
            $context['item_uri_template'] = $operation->getItemUriTemplate();
×
94
        }
95

96
        $context = $this->initContext($resourceClass, $context);
36✔
97
        $iri = $this->iriConverter->getIriFromResource($object, UrlGeneratorInterface::ABS_PATH, $context['operation'] ?? null, $context);
36✔
98
        $context['iri'] = $iri;
36✔
99
        $context['object'] = $object;
36✔
100
        $context['format'] = $format;
36✔
101
        $context['api_normalize'] = true;
36✔
102

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

107
        $data = parent::normalize($object, $format, $context);
36✔
108
        if (!\is_array($data)) {
32✔
109
            return $data;
4✔
110
        }
111

112
        // Get and populate relations
113
        ['relationships' => $allRelationshipsData, 'links' => $links] = $this->getComponents($object, $format, $context);
28✔
114
        $populatedRelationContext = $context;
28✔
115
        $relationshipsData = $this->getPopulatedRelations($object, $format, $populatedRelationContext, $allRelationshipsData);
28✔
116

117
        // Do not include primary resources
118
        $context['api_included_resources'] = [$context['iri']];
28✔
119

120
        $includedResourcesData = $this->getRelatedResources($object, $format, $context, $allRelationshipsData);
28✔
121

122
        $resourceData = [
28✔
123
            'id' => $context['iri'],
28✔
124
            'type' => $this->getResourceShortName($resourceClass),
28✔
125
        ];
28✔
126

127
        if ($data) {
28✔
128
            $resourceData['attributes'] = $data;
24✔
129
        }
130

131
        if ($relationshipsData) {
28✔
132
            $resourceData['relationships'] = $relationshipsData;
4✔
133
        }
134

135
        $document = [];
28✔
136

137
        if ($links) {
28✔
138
            $document['links'] = $links;
×
139
        }
140

141
        $document['data'] = $resourceData;
28✔
142

143
        if ($includedResourcesData) {
28✔
144
            $document['included'] = $includedResourcesData;
×
145
        }
146

147
        return $document;
28✔
148
    }
149

150
    /**
151
     * {@inheritdoc}
152
     */
153
    public function supportsDenormalization(mixed $data, string $type, ?string $format = null, array $context = []): bool
154
    {
155
        return self::FORMAT === $format && parent::supportsDenormalization($data, $type, $format, $context);
×
156
    }
157

158
    /**
159
     * {@inheritdoc}
160
     *
161
     * @throws NotNormalizableValueException
162
     */
163
    public function denormalize(mixed $data, string $class, ?string $format = null, array $context = []): mixed
164
    {
165
        // Avoid issues with proxies if we populated the object
166
        if (!isset($context[self::OBJECT_TO_POPULATE]) && isset($data['data']['id'])) {
20✔
167
            if (true !== ($context['api_allow_update'] ?? true)) {
4✔
168
                throw new NotNormalizableValueException('Update is not allowed for this operation.');
4✔
169
            }
170

171
            $context[self::OBJECT_TO_POPULATE] = $this->iriConverter->getResourceFromIri(
×
172
                $data['data']['id'],
×
173
                $context + ['fetch_data' => false]
×
174
            );
×
175
        }
176

177
        // Merge attributes and relationships, into format expected by the parent normalizer
178
        $dataToDenormalize = array_merge(
16✔
179
            $data['data']['attributes'] ?? [],
16✔
180
            $data['data']['relationships'] ?? []
16✔
181
        );
16✔
182

183
        return parent::denormalize(
16✔
184
            $dataToDenormalize,
16✔
185
            $class,
16✔
186
            $format,
16✔
187
            $context
16✔
188
        );
16✔
189
    }
190

191
    /**
192
     * {@inheritdoc}
193
     */
194
    protected function getAttributes(object $object, ?string $format = null, array $context = []): array
195
    {
196
        return $this->getComponents($object, $format, $context)['attributes'];
32✔
197
    }
198

199
    /**
200
     * {@inheritdoc}
201
     */
202
    protected function setAttributeValue(object $object, string $attribute, mixed $value, ?string $format = null, array $context = []): void
203
    {
204
        parent::setAttributeValue($object, $attribute, \is_array($value) && \array_key_exists('data', $value) ? $value['data'] : $value, $format, $context);
16✔
205
    }
206

207
    /**
208
     * {@inheritdoc}
209
     *
210
     * @see http://jsonapi.org/format/#document-resource-object-linkage
211
     *
212
     * @throws RuntimeException
213
     * @throws UnexpectedValueException
214
     */
215
    protected function denormalizeRelation(string $attributeName, ApiProperty $propertyMetadata, string $className, mixed $value, ?string $format, array $context): ?object
216
    {
217
        if (!\is_array($value) || !isset($value['id'], $value['type'])) {
8✔
218
            throw new UnexpectedValueException('Only resource linkage supported currently, see: http://jsonapi.org/format/#document-resource-object-linkage.');
4✔
219
        }
220

221
        try {
222
            return $this->iriConverter->getResourceFromIri($value['id'], $context + ['fetch_data' => true]);
4✔
223
        } catch (ItemNotFoundException $e) {
×
224
            if (!isset($context['not_normalizable_value_exceptions'])) {
×
225
                throw new RuntimeException($e->getMessage(), $e->getCode(), $e);
×
226
            }
227
            $context['not_normalizable_value_exceptions'][] = NotNormalizableValueException::createForUnexpectedDataType(
×
228
                $e->getMessage(),
×
229
                $value,
×
230
                [$className],
×
231
                $context['deserialization_path'] ?? null,
×
232
                true,
×
233
                $e->getCode(),
×
234
                $e
×
235
            );
×
236

237
            return null;
×
238
        }
239
    }
240

241
    /**
242
     * {@inheritdoc}
243
     *
244
     * @see http://jsonapi.org/format/#document-resource-object-linkage
245
     */
246
    protected function normalizeRelation(ApiProperty $propertyMetadata, ?object $relatedObject, string $resourceClass, ?string $format, array $context): \ArrayObject|array|string|null
247
    {
248
        if (null !== $relatedObject) {
×
249
            $iri = $this->iriConverter->getIriFromResource($relatedObject);
×
250
            $context['iri'] = $iri;
×
251

252
            if (!$this->tagCollector && isset($context['resources'])) {
×
253
                $context['resources'][$iri] = $iri;
×
254
            }
255
        }
256

257
        if (null === $relatedObject || isset($context['api_included'])) {
×
258
            if (!$this->serializer instanceof NormalizerInterface) {
×
259
                throw new LogicException(sprintf('The injected serializer must be an instance of "%s".', NormalizerInterface::class));
×
260
            }
261

262
            $normalizedRelatedObject = $this->serializer->normalize($relatedObject, $format, $context);
×
263
            if (!\is_string($normalizedRelatedObject) && !\is_array($normalizedRelatedObject) && !$normalizedRelatedObject instanceof \ArrayObject && null !== $normalizedRelatedObject) {
×
264
                throw new UnexpectedValueException('Expected normalized relation to be an IRI, array, \ArrayObject or null');
×
265
            }
266

267
            return $normalizedRelatedObject;
×
268
        }
269

270
        $context['data'] = [
×
271
            'data' => [
×
272
                'type' => $this->getResourceShortName($resourceClass),
×
273
                'id' => $iri,
×
274
            ],
×
275
        ];
×
276

277
        $context['iri'] = $iri;
×
278
        $context['object'] = $relatedObject;
×
279
        unset($context['property_metadata']);
×
280
        unset($context['api_attribute']);
×
281

282
        if ($this->tagCollector) {
×
283
            $this->tagCollector->collect($context);
×
284
        }
285

286
        return $context['data'];
×
287
    }
288

289
    /**
290
     * {@inheritdoc}
291
     */
292
    protected function isAllowedAttribute(object|string $classOrObject, string $attribute, ?string $format = null, array $context = []): bool
293
    {
294
        return preg_match('/^\\w[-\\w_]*$/', $attribute) && parent::isAllowedAttribute($classOrObject, $attribute, $format, $context);
48✔
295
    }
296

297
    /**
298
     * Gets JSON API components of the resource: attributes, relationships, meta and links.
299
     */
300
    private function getComponents(object $object, ?string $format, array $context): array
301
    {
302
        $cacheKey = $this->getObjectClass($object).'-'.$context['cache_key'];
32✔
303

304
        if (isset($this->componentsCache[$cacheKey])) {
32✔
305
            return $this->componentsCache[$cacheKey];
28✔
306
        }
307

308
        $attributes = parent::getAttributes($object, $format, $context);
32✔
309

310
        $options = $this->getFactoryOptions($context);
32✔
311

312
        $components = [
32✔
313
            'links' => [],
32✔
314
            'relationships' => [],
32✔
315
            'attributes' => [],
32✔
316
            'meta' => [],
32✔
317
        ];
32✔
318

319
        foreach ($attributes as $attribute) {
32✔
320
            $propertyMetadata = $this
28✔
321
                ->propertyMetadataFactory
28✔
322
                ->create($context['resource_class'], $attribute, $options);
28✔
323

324
            $types = $propertyMetadata->getBuiltinTypes() ?? [];
28✔
325

326
            // prevent declaring $attribute as attribute if it's already declared as relationship
327
            $isRelationship = false;
28✔
328

329
            foreach ($types as $type) {
28✔
330
                $isOne = $isMany = false;
20✔
331

332
                if ($type->isCollection()) {
20✔
333
                    $collectionValueType = $type->getCollectionValueTypes()[0] ?? null;
8✔
334
                    $isMany = $collectionValueType && ($className = $collectionValueType->getClassName()) && $this->resourceClassResolver->isResourceClass($className);
8✔
335
                } else {
336
                    $isOne = ($className = $type->getClassName()) && $this->resourceClassResolver->isResourceClass($className);
20✔
337
                }
338

339
                if (!isset($className) || !$isOne && !$isMany) {
20✔
340
                    // don't declare it as an attribute too quick: maybe the next type is a valid resource
341
                    continue;
20✔
342
                }
343

344
                $relation = [
4✔
345
                    'name' => $attribute,
4✔
346
                    'type' => $this->getResourceShortName($className),
4✔
347
                    'cardinality' => $isOne ? 'one' : 'many',
4✔
348
                ];
4✔
349

350
                // if we specify the uriTemplate, generates its value for link definition
351
                // @see ApiPlatform\Serializer\AbstractItemNormalizer:getAttributeValue logic for intentional duplicate content
352
                if ($itemUriTemplate = $propertyMetadata->getUriTemplate()) {
4✔
353
                    $attributeValue = $this->propertyAccessor->getValue($object, $attribute);
×
354
                    $resourceClass = $this->resourceClassResolver->getResourceClass($attributeValue, $className);
×
355
                    $childContext = $this->createChildContext($context, $attribute, $format);
×
356
                    unset($childContext['iri'], $childContext['uri_variables'], $childContext['resource_class'], $childContext['operation']);
×
357

358
                    $operation = $this->resourceMetadataCollectionFactory->create($resourceClass)->getOperation(
×
359
                        operationName: $itemUriTemplate,
×
360
                        httpOperation: true
×
361
                    );
×
362

363
                    $components['links'][$attribute] = $this->iriConverter->getIriFromResource($object, UrlGeneratorInterface::ABS_PATH, $operation, $childContext);
×
364
                }
365

366
                $components['relationships'][] = $relation;
4✔
367
                $isRelationship = true;
4✔
368
            }
369

370
            // if all types are not relationships, declare it as an attribute
371
            if (!$isRelationship) {
28✔
372
                $components['attributes'][] = $attribute;
28✔
373
            }
374
        }
375

376
        if (false !== $context['cache_key']) {
32✔
377
            $this->componentsCache[$cacheKey] = $components;
32✔
378
        }
379

380
        return $components;
32✔
381
    }
382

383
    /**
384
     * Populates relationships keys.
385
     *
386
     * @throws UnexpectedValueException
387
     */
388
    private function getPopulatedRelations(object $object, ?string $format, array $context, array $relationships): array
389
    {
390
        $data = [];
28✔
391

392
        if (!isset($context['resource_class'])) {
28✔
393
            return $data;
×
394
        }
395

396
        unset($context['api_included']);
28✔
397
        foreach ($relationships as $relationshipDataArray) {
28✔
398
            $relationshipName = $relationshipDataArray['name'];
4✔
399

400
            $attributeValue = $this->getAttributeValue($object, $relationshipName, $format, $context);
4✔
401

402
            if ($this->nameConverter) {
4✔
403
                $relationshipName = $this->nameConverter->normalize($relationshipName, $context['resource_class'], self::FORMAT, $context);
4✔
404
            }
405

406
            $data[$relationshipName] = [
4✔
407
                'data' => [],
4✔
408
            ];
4✔
409

410
            if (!$attributeValue) {
4✔
411
                continue;
4✔
412
            }
413

414
            // Many to one relationship
415
            if ('one' === $relationshipDataArray['cardinality']) {
×
416
                unset($attributeValue['data']['attributes']);
×
417
                $data[$relationshipName] = $attributeValue;
×
418

419
                continue;
×
420
            }
421

422
            // Many to many relationship
423
            foreach ($attributeValue as $attributeValueElement) {
×
424
                if (!isset($attributeValueElement['data'])) {
×
425
                    throw new UnexpectedValueException(sprintf('The JSON API attribute \'%s\' must contain a "data" key.', $relationshipName));
×
426
                }
427
                unset($attributeValueElement['data']['attributes']);
×
428
                $data[$relationshipName]['data'][] = $attributeValueElement['data'];
×
429
            }
430
        }
431

432
        return $data;
28✔
433
    }
434

435
    /**
436
     * Populates included keys.
437
     */
438
    private function getRelatedResources(object $object, ?string $format, array $context, array $relationships): array
439
    {
440
        if (!isset($context['api_included'])) {
28✔
441
            return [];
28✔
442
        }
443

444
        $included = [];
×
445
        foreach ($relationships as $relationshipDataArray) {
×
446
            $relationshipName = $relationshipDataArray['name'];
×
447

448
            if (!$this->shouldIncludeRelation($relationshipName, $context)) {
×
449
                continue;
×
450
            }
451

452
            $relationContext = $context;
×
453
            $relationContext['api_included'] = $this->getIncludedNestedResources($relationshipName, $context);
×
454

455
            $attributeValue = $this->getAttributeValue($object, $relationshipName, $format, $relationContext);
×
456

457
            if (!$attributeValue) {
×
458
                continue;
×
459
            }
460

461
            // Many to many relationship
462
            $attributeValues = $attributeValue;
×
463
            // Many to one relationship
464
            if ('one' === $relationshipDataArray['cardinality']) {
×
465
                $attributeValues = [$attributeValue];
×
466
            }
467

468
            foreach ($attributeValues as $attributeValueElement) {
×
469
                if (isset($attributeValueElement['data'])) {
×
470
                    $this->addIncluded($attributeValueElement['data'], $included, $context);
×
471
                    if (isset($attributeValueElement['included']) && \is_array($attributeValueElement['included'])) {
×
472
                        foreach ($attributeValueElement['included'] as $include) {
×
473
                            $this->addIncluded($include, $included, $context);
×
474
                        }
475
                    }
476
                }
477
            }
478
        }
479

480
        return $included;
×
481
    }
482

483
    /**
484
     * Add data to included array if it's not already included.
485
     */
486
    private function addIncluded(array $data, array &$included, array &$context): void
487
    {
488
        if (isset($data['id']) && !\in_array($data['id'], $context['api_included_resources'], true)) {
×
489
            $included[] = $data;
×
490
            // Track already included resources
491
            $context['api_included_resources'][] = $data['id'];
×
492
        }
493
    }
494

495
    /**
496
     * Figures out if the relationship is in the api_included hash or has included nested resources (path).
497
     */
498
    private function shouldIncludeRelation(string $relationshipName, array $context): bool
499
    {
500
        $normalizedName = $this->nameConverter ? $this->nameConverter->normalize($relationshipName, $context['resource_class'], self::FORMAT, $context) : $relationshipName;
×
501

502
        return \in_array($normalizedName, $context['api_included'], true) || \count($this->getIncludedNestedResources($relationshipName, $context)) > 0;
×
503
    }
504

505
    /**
506
     * Returns the names of the nested resources from a path relationship.
507
     */
508
    private function getIncludedNestedResources(string $relationshipName, array $context): array
509
    {
510
        $normalizedName = $this->nameConverter ? $this->nameConverter->normalize($relationshipName, $context['resource_class'], self::FORMAT, $context) : $relationshipName;
×
511

512
        $filtered = array_filter($context['api_included'] ?? [], static fn (string $included): bool => str_starts_with($included, $normalizedName.'.'));
×
513

514
        return array_map(static fn (string $nested): string => substr($nested, strpos($nested, '.') + 1), $filtered);
×
515
    }
516

517
    // TODO: this code is similar to the one used in JsonLd
518
    private function getResourceShortName(string $resourceClass): string
519
    {
520
        if ($this->resourceClassResolver->isResourceClass($resourceClass)) {
28✔
521
            $resourceMetadata = $this->resourceMetadataCollectionFactory->create($resourceClass);
28✔
522

523
            return $resourceMetadata->getOperation()->getShortName();
28✔
524
        }
525

526
        return (new \ReflectionClass($resourceClass))->getShortName();
4✔
527
    }
528
}
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