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

api-platform / core / 7142557150

08 Dec 2023 02:28PM UTC coverage: 36.003% (-1.4%) from 37.36%
7142557150

push

github

web-flow
fix(jsonld): remove link to ApiDocumentation when doc is disabled (#6029)

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

2297 existing lines in 182 files now uncovered.

9992 of 27753 relevant lines covered (36.0%)

147.09 hits per line

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

86.93
/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\Symfony\Security\ResourceAccessCheckerInterface;
31
use Symfony\Component\ErrorHandler\Exception\FlattenException;
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\Exception\RuntimeException;
36
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
37
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
38
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
39
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
40

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

54
    public const FORMAT = 'jsonapi';
55

56
    private array $componentsCache = [];
57

58
    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)
59
    {
60
        parent::__construct($propertyNameCollectionFactory, $propertyMetadataFactory, $iriConverter, $resourceClassResolver, $propertyAccessor, $nameConverter, $classMetadataFactory, $defaultContext, $resourceMetadataCollectionFactory, $resourceAccessChecker);
2,499✔
61
    }
62

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

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

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

86
        if ($this->resourceClassResolver->isResourceClass($resourceClass)) {
165✔
87
            $resourceClass = $this->resourceClassResolver->getResourceClass($object, $context['resource_class'] ?? null);
159✔
88
        }
89

90
        if (($operation = $context['operation'] ?? null) && method_exists($operation, 'getItemUriTemplate')) {
165✔
91
            $context['item_uri_template'] = $operation->getItemUriTemplate();
33✔
92
        }
93

94
        $context = $this->initContext($resourceClass, $context);
165✔
95
        $iri = $this->iriConverter->getIriFromResource($object, UrlGeneratorInterface::ABS_PATH, $context['operation'] ?? null, $context);
165✔
96
        $context['iri'] = $iri;
165✔
97
        $context['api_normalize'] = true;
165✔
98

99
        if (!isset($context['cache_key'])) {
165✔
100
            $context['cache_key'] = $this->getCacheKey($format, $context);
165✔
101
        }
102

103
        $data = parent::normalize($object, $format, $context);
165✔
104
        if (!\is_array($data)) {
165✔
UNCOV
105
            return $data;
×
106
        }
107

108
        // Get and populate relations
109
        ['relationships' => $allRelationshipsData, 'links' => $links] = $this->getComponents($object, $format, $context);
165✔
110
        $populatedRelationContext = $context;
165✔
111
        $relationshipsData = $this->getPopulatedRelations($object, $format, $populatedRelationContext, $allRelationshipsData);
165✔
112

113
        // Do not include primary resources
114
        $context['api_included_resources'] = [$context['iri']];
165✔
115

116
        $includedResourcesData = $this->getRelatedResources($object, $format, $context, $allRelationshipsData);
165✔
117

118
        $resourceData = [
165✔
119
            'id' => $context['iri'],
165✔
120
            'type' => $this->getResourceShortName($resourceClass),
165✔
121
        ];
165✔
122

123
        if ($data) {
165✔
124
            $resourceData['attributes'] = $data;
159✔
125
        }
126

127
        if ($relationshipsData) {
165✔
128
            $resourceData['relationships'] = $relationshipsData;
93✔
129
        }
130

131
        $document = [];
165✔
132

133
        if ($links) {
165✔
134
            $document['links'] = $links;
3✔
135
        }
136

137
        $document['data'] = $resourceData;
165✔
138

139
        if ($includedResourcesData) {
165✔
140
            $document['included'] = $includedResourcesData;
45✔
141
        }
142

143
        return $document;
165✔
144
    }
145

146
    /**
147
     * {@inheritdoc}
148
     */
149
    public function supportsDenormalization(mixed $data, string $type, string $format = null, array $context = []): bool
150
    {
151
        return self::FORMAT === $format && parent::supportsDenormalization($data, $type, $format, $context);
39✔
152
    }
153

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

167
            $context[self::OBJECT_TO_POPULATE] = $this->iriConverter->getResourceFromIri(
×
168
                $data['data']['id'],
×
169
                $context + ['fetch_data' => false]
×
170
            );
×
171
        }
172

173
        // Merge attributes and relationships, into format expected by the parent normalizer
174
        $dataToDenormalize = array_merge(
39✔
175
            $data['data']['attributes'] ?? [],
39✔
176
            $data['data']['relationships'] ?? []
39✔
177
        );
39✔
178

179
        return parent::denormalize(
39✔
180
            $dataToDenormalize,
39✔
181
            $class,
39✔
182
            $format,
39✔
183
            $context
39✔
184
        );
39✔
185
    }
186

187
    /**
188
     * {@inheritdoc}
189
     */
190
    protected function getAttributes(object $object, string $format = null, array $context = []): array
191
    {
192
        return $this->getComponents($object, $format, $context)['attributes'];
165✔
193
    }
194

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

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

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

233
            return null;
×
234
        }
235
    }
236

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

248
            if (isset($context['resources'])) {
93✔
249
                $context['resources'][$iri] = $iri;
93✔
250
            }
251
        }
252

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

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

263
            return $normalizedRelatedObject;
93✔
264
        }
265

266
        return [
93✔
267
            'data' => [
93✔
268
                'type' => $this->getResourceShortName($resourceClass),
93✔
269
                'id' => $iri,
93✔
270
            ],
93✔
271
        ];
93✔
272
    }
273

274
    /**
275
     * {@inheritdoc}
276
     */
277
    protected function isAllowedAttribute(object|string $classOrObject, string $attribute, string $format = null, array $context = []): bool
278
    {
279
        return preg_match('/^\\w[-\\w_]*$/', $attribute) && parent::isAllowedAttribute($classOrObject, $attribute, $format, $context);
174✔
280
    }
281

282
    /**
283
     * Gets JSON API components of the resource: attributes, relationships, meta and links.
284
     */
285
    private function getComponents(object $object, ?string $format, array $context): array
286
    {
287
        $cacheKey = $this->getObjectClass($object).'-'.$context['cache_key'];
165✔
288

289
        if (isset($this->componentsCache[$cacheKey])) {
165✔
290
            return $this->componentsCache[$cacheKey];
165✔
291
        }
292

293
        $attributes = parent::getAttributes($object, $format, $context);
165✔
294

295
        $options = $this->getFactoryOptions($context);
165✔
296

297
        $components = [
165✔
298
            'links' => [],
165✔
299
            'relationships' => [],
165✔
300
            'attributes' => [],
165✔
301
            'meta' => [],
165✔
302
        ];
165✔
303

304
        foreach ($attributes as $attribute) {
165✔
305
            $propertyMetadata = $this
165✔
306
                ->propertyMetadataFactory
165✔
307
                ->create($context['resource_class'], $attribute, $options);
165✔
308

309
            $types = $propertyMetadata->getBuiltinTypes() ?? [];
165✔
310

311
            // prevent declaring $attribute as attribute if it's already declared as relationship
312
            $isRelationship = false;
165✔
313

314
            foreach ($types as $type) {
165✔
315
                $isOne = $isMany = false;
153✔
316

317
                if ($type->isCollection()) {
153✔
318
                    $collectionValueType = $type->getCollectionValueTypes()[0] ?? null;
111✔
319
                    $isMany = $collectionValueType && ($className = $collectionValueType->getClassName()) && $this->resourceClassResolver->isResourceClass($className);
111✔
320
                } else {
321
                    $isOne = ($className = $type->getClassName()) && $this->resourceClassResolver->isResourceClass($className);
153✔
322
                }
323

324
                if (!isset($className) || !$isOne && !$isMany) {
153✔
325
                    // don't declare it as an attribute too quick: maybe the next type is a valid resource
326
                    continue;
147✔
327
                }
328

329
                $relation = [
135✔
330
                    'name' => $attribute,
135✔
331
                    'type' => $this->getResourceShortName($className),
135✔
332
                    'cardinality' => $isOne ? 'one' : 'many',
135✔
333
                ];
135✔
334

335
                // if we specify the uriTemplate, generates its value for link definition
336
                // @see ApiPlatform\Serializer\AbstractItemNormalizer:getAttributeValue logic for intentional duplicate content
337
                if ($itemUriTemplate = $propertyMetadata->getUriTemplate()) {
135✔
338
                    $attributeValue = $this->propertyAccessor->getValue($object, $attribute);
3✔
339
                    $resourceClass = $this->resourceClassResolver->getResourceClass($attributeValue, $className);
3✔
340
                    $childContext = $this->createChildContext($context, $attribute, $format);
3✔
341
                    unset($childContext['iri'], $childContext['uri_variables'], $childContext['resource_class'], $childContext['operation']);
3✔
342

343
                    $operation = $this->resourceMetadataCollectionFactory->create($resourceClass)->getOperation(
3✔
344
                        operationName: $itemUriTemplate,
3✔
345
                        httpOperation: true
3✔
346
                    );
3✔
347

348
                    $components['links'][$attribute] = $this->iriConverter->getIriFromResource($object, UrlGeneratorInterface::ABS_PATH, $operation, $childContext);
3✔
349
                }
350

351
                $components['relationships'][] = $relation;
135✔
352
                $isRelationship = true;
135✔
353
            }
354

355
            // if all types are not relationships, declare it as an attribute
356
            if (!$isRelationship) {
165✔
357
                $components['attributes'][] = $attribute;
159✔
358
            }
359
        }
360

361
        if (false !== $context['cache_key']) {
165✔
362
            $this->componentsCache[$cacheKey] = $components;
165✔
363
        }
364

365
        return $components;
165✔
366
    }
367

368
    /**
369
     * Populates relationships keys.
370
     *
371
     * @throws UnexpectedValueException
372
     */
373
    private function getPopulatedRelations(object $object, ?string $format, array $context, array $relationships): array
374
    {
375
        $data = [];
165✔
376

377
        if (!isset($context['resource_class'])) {
165✔
378
            return $data;
×
379
        }
380

381
        unset($context['api_included']);
165✔
382
        foreach ($relationships as $relationshipDataArray) {
165✔
383
            $relationshipName = $relationshipDataArray['name'];
135✔
384

385
            $attributeValue = $this->getAttributeValue($object, $relationshipName, $format, $context);
135✔
386

387
            if ($this->nameConverter) {
135✔
388
                $relationshipName = $this->nameConverter->normalize($relationshipName, $context['resource_class'], self::FORMAT, $context);
135✔
389
            }
390

391
            if (!$attributeValue) {
135✔
392
                continue;
99✔
393
            }
394

395
            $data[$relationshipName] = [
93✔
396
                'data' => [],
93✔
397
            ];
93✔
398

399
            // Many to one relationship
400
            if ('one' === $relationshipDataArray['cardinality']) {
93✔
401
                unset($attributeValue['data']['attributes']);
90✔
402
                $data[$relationshipName] = $attributeValue;
90✔
403

404
                continue;
90✔
405
            }
406

407
            // Many to many relationship
408
            foreach ($attributeValue as $attributeValueElement) {
33✔
409
                if (!isset($attributeValueElement['data'])) {
33✔
410
                    throw new UnexpectedValueException(sprintf('The JSON API attribute \'%s\' must contain a "data" key.', $relationshipName));
×
411
                }
412
                unset($attributeValueElement['data']['attributes']);
33✔
413
                $data[$relationshipName]['data'][] = $attributeValueElement['data'];
33✔
414
            }
415
        }
416

417
        return $data;
165✔
418
    }
419

420
    /**
421
     * Populates included keys.
422
     */
423
    private function getRelatedResources(object $object, ?string $format, array $context, array $relationships): array
424
    {
425
        if (!isset($context['api_included'])) {
165✔
426
            return [];
117✔
427
        }
428

429
        $included = [];
48✔
430
        foreach ($relationships as $relationshipDataArray) {
48✔
431
            $relationshipName = $relationshipDataArray['name'];
48✔
432

433
            if (!$this->shouldIncludeRelation($relationshipName, $context)) {
48✔
434
                continue;
39✔
435
            }
436

437
            $relationContext = $context;
45✔
438
            $relationContext['api_included'] = $this->getIncludedNestedResources($relationshipName, $context);
45✔
439

440
            $attributeValue = $this->getAttributeValue($object, $relationshipName, $format, $relationContext);
45✔
441

442
            if (!$attributeValue) {
45✔
443
                continue;
×
444
            }
445

446
            // Many to many relationship
447
            $attributeValues = $attributeValue;
45✔
448
            // Many to one relationship
449
            if ('one' === $relationshipDataArray['cardinality']) {
45✔
450
                $attributeValues = [$attributeValue];
39✔
451
            }
452

453
            foreach ($attributeValues as $attributeValueElement) {
45✔
454
                if (isset($attributeValueElement['data'])) {
45✔
455
                    $this->addIncluded($attributeValueElement['data'], $included, $context);
45✔
456
                    if (isset($attributeValueElement['included']) && \is_array($attributeValueElement['included'])) {
45✔
457
                        foreach ($attributeValueElement['included'] as $include) {
15✔
458
                            $this->addIncluded($include, $included, $context);
15✔
459
                        }
460
                    }
461
                }
462
            }
463
        }
464

465
        return $included;
48✔
466
    }
467

468
    /**
469
     * Add data to included array if it's not already included.
470
     */
471
    private function addIncluded(array $data, array &$included, array &$context): void
472
    {
473
        if (isset($data['id']) && !\in_array($data['id'], $context['api_included_resources'], true)) {
45✔
474
            $included[] = $data;
45✔
475
            // Track already included resources
476
            $context['api_included_resources'][] = $data['id'];
45✔
477
        }
478
    }
479

480
    /**
481
     * Figures out if the relationship is in the api_included hash or has included nested resources (path).
482
     */
483
    private function shouldIncludeRelation(string $relationshipName, array $context): bool
484
    {
485
        $normalizedName = $this->nameConverter ? $this->nameConverter->normalize($relationshipName, $context['resource_class'], self::FORMAT, $context) : $relationshipName;
48✔
486

487
        return \in_array($normalizedName, $context['api_included'], true) || \count($this->getIncludedNestedResources($relationshipName, $context)) > 0;
48✔
488
    }
489

490
    /**
491
     * Returns the names of the nested resources from a path relationship.
492
     */
493
    private function getIncludedNestedResources(string $relationshipName, array $context): array
494
    {
495
        $normalizedName = $this->nameConverter ? $this->nameConverter->normalize($relationshipName, $context['resource_class'], self::FORMAT, $context) : $relationshipName;
48✔
496

497
        $filtered = array_filter($context['api_included'] ?? [], static fn (string $included): bool => str_starts_with($included, $normalizedName.'.'));
48✔
498

499
        return array_map(static fn (string $nested): string => substr($nested, strpos($nested, '.') + 1), $filtered);
48✔
500
    }
501

502
    // TODO: this code is similar to the one used in JsonLd
503
    private function getResourceShortName(string $resourceClass): string
504
    {
505
        if ($this->resourceClassResolver->isResourceClass($resourceClass)) {
165✔
506
            $resourceMetadata = $this->resourceMetadataCollectionFactory->create($resourceClass);
159✔
507

508
            return $resourceMetadata->getOperation()->getShortName();
159✔
509
        }
510

511
        return (new \ReflectionClass($resourceClass))->getShortName();
6✔
512
    }
513
}
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