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

api-platform / core / 5644825543

pending completion
5644825543

push

github

web-flow
feat(serializer): support for getSupportedTypes (symfony 6.3) (#5672)

109 of 109 new or added lines in 29 files covered. (100.0%)

10881 of 18245 relevant lines covered (59.64%)

20.04 hits per line

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

48.84
/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;
17
use ApiPlatform\Api\ResourceClassResolverInterface;
18
use ApiPlatform\Api\UrlGeneratorInterface;
19
use ApiPlatform\Exception\ItemNotFoundException;
20
use ApiPlatform\Metadata\ApiProperty;
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\Util\ClassInfoTrait;
25
use ApiPlatform\Serializer\AbstractItemNormalizer;
26
use ApiPlatform\Serializer\CacheKeyTrait;
27
use ApiPlatform\Serializer\ContextTrait;
28
use ApiPlatform\Symfony\Security\ResourceAccessCheckerInterface;
29
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
30
use Symfony\Component\Serializer\Exception\LogicException;
31
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
32
use Symfony\Component\Serializer\Exception\RuntimeException;
33
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
34
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
35
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
36
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
37

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

51
    public const FORMAT = 'jsonapi';
52

53
    private array $componentsCache = [];
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)
56
    {
57
        parent::__construct($propertyNameCollectionFactory, $propertyMetadataFactory, $iriConverter, $resourceClassResolver, $propertyAccessor, $nameConverter, $classMetadataFactory, $defaultContext, $resourceMetadataCollectionFactory, $resourceAccessChecker);
70✔
58
    }
59

60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function supportsNormalization(mixed $data, string $format = null, array $context = []): bool
64
    {
65
        return self::FORMAT === $format && parent::supportsNormalization($data, $format, $context);
×
66
    }
67

68
    public function getSupportedTypes($format): array
69
    {
70
        return self::FORMAT === $format ? parent::getSupportedTypes($format) : [];
28✔
71
    }
72

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

83
        if ($this->resourceClassResolver->isResourceClass($resourceClass)) {
6✔
84
            $resourceClass = $this->resourceClassResolver->getResourceClass($object, $context['resource_class'] ?? null);
6✔
85
        }
86

87
        if (($operation = $context['operation'] ?? null) && method_exists($operation, 'getItemUriTemplate')) {
6✔
88
            $context['item_uri_template'] = $operation->getItemUriTemplate();
×
89
        }
90

91
        $context = $this->initContext($resourceClass, $context);
6✔
92
        $iri = $this->iriConverter->getIriFromResource($object, UrlGeneratorInterface::ABS_PATH, $context['operation'] ?? null, $context);
6✔
93
        $context['iri'] = $iri;
6✔
94
        $context['api_normalize'] = true;
6✔
95

96
        if (!isset($context['cache_key'])) {
6✔
97
            $context['cache_key'] = $this->getCacheKey($format, $context);
6✔
98
        }
99

100
        $data = parent::normalize($object, $format, $context);
6✔
101
        if (!\is_array($data)) {
4✔
102
            return $data;
2✔
103
        }
104

105
        // Get and populate relations
106
        $allRelationshipsData = $this->getComponents($object, $format, $context)['relationships'];
2✔
107
        $populatedRelationContext = $context;
2✔
108
        $relationshipsData = $this->getPopulatedRelations($object, $format, $populatedRelationContext, $allRelationshipsData);
2✔
109

110
        // Do not include primary resources
111
        $context['api_included_resources'] = [$context['iri']];
2✔
112

113
        $includedResourcesData = $this->getRelatedResources($object, $format, $context, $allRelationshipsData);
2✔
114

115
        $resourceData = [
2✔
116
            'id' => $context['iri'],
2✔
117
            'type' => $this->getResourceShortName($resourceClass),
2✔
118
        ];
2✔
119

120
        if ($data) {
2✔
121
            $resourceData['attributes'] = $data;
2✔
122
        }
123

124
        if ($relationshipsData) {
2✔
125
            $resourceData['relationships'] = $relationshipsData;
×
126
        }
127

128
        $document = ['data' => $resourceData];
2✔
129

130
        if ($includedResourcesData) {
2✔
131
            $document['included'] = $includedResourcesData;
×
132
        }
133

134
        return $document;
2✔
135
    }
136

137
    /**
138
     * {@inheritdoc}
139
     */
140
    public function supportsDenormalization(mixed $data, string $type, string $format = null, array $context = []): bool
141
    {
142
        return self::FORMAT === $format && parent::supportsDenormalization($data, $type, $format, $context);
×
143
    }
144

145
    /**
146
     * {@inheritdoc}
147
     *
148
     * @throws NotNormalizableValueException
149
     */
150
    public function denormalize(mixed $data, string $class, string $format = null, array $context = []): mixed
151
    {
152
        // Avoid issues with proxies if we populated the object
153
        if (!isset($context[self::OBJECT_TO_POPULATE]) && isset($data['data']['id'])) {
10✔
154
            if (true !== ($context['api_allow_update'] ?? true)) {
2✔
155
                throw new NotNormalizableValueException('Update is not allowed for this operation.');
2✔
156
            }
157

158
            $context[self::OBJECT_TO_POPULATE] = $this->iriConverter->getResourceFromIri(
×
159
                $data['data']['id'],
×
160
                $context + ['fetch_data' => false]
×
161
            );
×
162
        }
163

164
        // Merge attributes and relationships, into format expected by the parent normalizer
165
        $dataToDenormalize = array_merge(
8✔
166
            $data['data']['attributes'] ?? [],
8✔
167
            $data['data']['relationships'] ?? []
8✔
168
        );
8✔
169

170
        return parent::denormalize(
8✔
171
            $dataToDenormalize,
8✔
172
            $class,
8✔
173
            $format,
8✔
174
            $context
8✔
175
        );
8✔
176
    }
177

178
    /**
179
     * {@inheritdoc}
180
     */
181
    protected function getAttributes(object $object, string $format = null, array $context = []): array
182
    {
183
        return $this->getComponents($object, $format, $context)['attributes'];
4✔
184
    }
185

186
    /**
187
     * {@inheritdoc}
188
     */
189
    protected function setAttributeValue(object $object, string $attribute, mixed $value, string $format = null, array $context = []): void
190
    {
191
        parent::setAttributeValue($object, $attribute, \is_array($value) && \array_key_exists('data', $value) ? $value['data'] : $value, $format, $context);
8✔
192
    }
193

194
    /**
195
     * {@inheritdoc}
196
     *
197
     * @see http://jsonapi.org/format/#document-resource-object-linkage
198
     *
199
     * @throws RuntimeException
200
     * @throws UnexpectedValueException
201
     */
202
    protected function denormalizeRelation(string $attributeName, ApiProperty $propertyMetadata, string $className, mixed $value, ?string $format, array $context): object
203
    {
204
        if (!\is_array($value) || !isset($value['id'], $value['type'])) {
4✔
205
            throw new UnexpectedValueException('Only resource linkage supported currently, see: http://jsonapi.org/format/#document-resource-object-linkage.');
2✔
206
        }
207

208
        try {
209
            return $this->iriConverter->getResourceFromIri($value['id'], $context + ['fetch_data' => true]);
2✔
210
        } catch (ItemNotFoundException $e) {
×
211
            throw new RuntimeException($e->getMessage(), $e->getCode(), $e);
×
212
        }
213
    }
214

215
    /**
216
     * {@inheritdoc}
217
     *
218
     * @see http://jsonapi.org/format/#document-resource-object-linkage
219
     */
220
    protected function normalizeRelation(ApiProperty $propertyMetadata, ?object $relatedObject, string $resourceClass, ?string $format, array $context): \ArrayObject|array|string|null
221
    {
222
        if (null !== $relatedObject) {
×
223
            $iri = $this->iriConverter->getIriFromResource($relatedObject);
×
224
            $context['iri'] = $iri;
×
225

226
            if (isset($context['resources'])) {
×
227
                $context['resources'][$iri] = $iri;
×
228
            }
229
        }
230

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

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

241
            return $normalizedRelatedObject;
×
242
        }
243

244
        return [
×
245
            'data' => [
×
246
                'type' => $this->getResourceShortName($resourceClass),
×
247
                'id' => $iri,
×
248
            ],
×
249
        ];
×
250
    }
251

252
    /**
253
     * {@inheritdoc}
254
     */
255
    protected function isAllowedAttribute(object|string $classOrObject, string $attribute, string $format = null, array $context = []): bool
256
    {
257
        return preg_match('/^\\w[-\\w_]*$/', $attribute) && parent::isAllowedAttribute($classOrObject, $attribute, $format, $context);
12✔
258
    }
259

260
    /**
261
     * Gets JSON API components of the resource: attributes, relationships, meta and links.
262
     */
263
    private function getComponents(object $object, ?string $format, array $context): array
264
    {
265
        $cacheKey = $this->getObjectClass($object).'-'.$context['cache_key'];
4✔
266

267
        if (isset($this->componentsCache[$cacheKey])) {
4✔
268
            return $this->componentsCache[$cacheKey];
2✔
269
        }
270

271
        $attributes = parent::getAttributes($object, $format, $context);
4✔
272

273
        $options = $this->getFactoryOptions($context);
4✔
274

275
        $components = [
4✔
276
            'links' => [],
4✔
277
            'relationships' => [],
4✔
278
            'attributes' => [],
4✔
279
            'meta' => [],
4✔
280
        ];
4✔
281

282
        foreach ($attributes as $attribute) {
4✔
283
            $propertyMetadata = $this
4✔
284
                ->propertyMetadataFactory
4✔
285
                ->create($context['resource_class'], $attribute, $options);
4✔
286

287
            // TODO: 3.0 support multiple types, default value of types will be [] instead of null
288
            $type = $propertyMetadata->getBuiltinTypes()[0] ?? null;
4✔
289
            $isOne = $isMany = false;
4✔
290

291
            if (null !== $type) {
4✔
292
                if ($type->isCollection()) {
×
293
                    $collectionValueType = $type->getCollectionValueTypes()[0] ?? null;
×
294
                    $isMany = $collectionValueType && ($className = $collectionValueType->getClassName()) && $this->resourceClassResolver->isResourceClass($className);
×
295
                } else {
296
                    $isOne = ($className = $type->getClassName()) && $this->resourceClassResolver->isResourceClass($className);
×
297
                }
298
            }
299

300
            if (!isset($className) || !$isOne && !$isMany) {
4✔
301
                $components['attributes'][] = $attribute;
4✔
302

303
                continue;
4✔
304
            }
305

306
            $relation = [
×
307
                'name' => $attribute,
×
308
                'type' => $this->getResourceShortName($className),
×
309
                'cardinality' => $isOne ? 'one' : 'many',
×
310
            ];
×
311

312
            $components['relationships'][] = $relation;
×
313
        }
314

315
        if (false !== $context['cache_key']) {
4✔
316
            $this->componentsCache[$cacheKey] = $components;
4✔
317
        }
318

319
        return $components;
4✔
320
    }
321

322
    /**
323
     * Populates relationships keys.
324
     *
325
     * @throws UnexpectedValueException
326
     */
327
    private function getPopulatedRelations(object $object, ?string $format, array $context, array $relationships): array
328
    {
329
        $data = [];
2✔
330

331
        if (!isset($context['resource_class'])) {
2✔
332
            return $data;
×
333
        }
334

335
        unset($context['api_included']);
2✔
336
        foreach ($relationships as $relationshipDataArray) {
2✔
337
            $relationshipName = $relationshipDataArray['name'];
×
338

339
            $attributeValue = $this->getAttributeValue($object, $relationshipName, $format, $context);
×
340

341
            if ($this->nameConverter) {
×
342
                $relationshipName = $this->nameConverter->normalize($relationshipName, $context['resource_class'], self::FORMAT, $context);
×
343
            }
344

345
            if (!$attributeValue) {
×
346
                continue;
×
347
            }
348

349
            $data[$relationshipName] = [
×
350
                'data' => [],
×
351
            ];
×
352

353
            // Many to one relationship
354
            if ('one' === $relationshipDataArray['cardinality']) {
×
355
                unset($attributeValue['data']['attributes']);
×
356
                $data[$relationshipName] = $attributeValue;
×
357

358
                continue;
×
359
            }
360

361
            // Many to many relationship
362
            foreach ($attributeValue as $attributeValueElement) {
×
363
                if (!isset($attributeValueElement['data'])) {
×
364
                    throw new UnexpectedValueException(sprintf('The JSON API attribute \'%s\' must contain a "data" key.', $relationshipName));
×
365
                }
366
                unset($attributeValueElement['data']['attributes']);
×
367
                $data[$relationshipName]['data'][] = $attributeValueElement['data'];
×
368
            }
369
        }
370

371
        return $data;
2✔
372
    }
373

374
    /**
375
     * Populates included keys.
376
     */
377
    private function getRelatedResources(object $object, ?string $format, array $context, array $relationships): array
378
    {
379
        if (!isset($context['api_included'])) {
2✔
380
            return [];
2✔
381
        }
382

383
        $included = [];
×
384
        foreach ($relationships as $relationshipDataArray) {
×
385
            $relationshipName = $relationshipDataArray['name'];
×
386

387
            if (!$this->shouldIncludeRelation($relationshipName, $context)) {
×
388
                continue;
×
389
            }
390

391
            $relationContext = $context;
×
392
            $relationContext['api_included'] = $this->getIncludedNestedResources($relationshipName, $context);
×
393

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

396
            if (!$attributeValue) {
×
397
                continue;
×
398
            }
399

400
            // Many to many relationship
401
            $attributeValues = $attributeValue;
×
402
            // Many to one relationship
403
            if ('one' === $relationshipDataArray['cardinality']) {
×
404
                $attributeValues = [$attributeValue];
×
405
            }
406

407
            foreach ($attributeValues as $attributeValueElement) {
×
408
                if (isset($attributeValueElement['data'])) {
×
409
                    $this->addIncluded($attributeValueElement['data'], $included, $context);
×
410
                    if (isset($attributeValueElement['included']) && \is_array($attributeValueElement['included'])) {
×
411
                        foreach ($attributeValueElement['included'] as $include) {
×
412
                            $this->addIncluded($include, $included, $context);
×
413
                        }
414
                    }
415
                }
416
            }
417
        }
418

419
        return $included;
×
420
    }
421

422
    /**
423
     * Add data to included array if it's not already included.
424
     */
425
    private function addIncluded(array $data, array &$included, array &$context): void
426
    {
427
        if (isset($data['id']) && !\in_array($data['id'], $context['api_included_resources'], true)) {
×
428
            $included[] = $data;
×
429
            // Track already included resources
430
            $context['api_included_resources'][] = $data['id'];
×
431
        }
432
    }
433

434
    /**
435
     * Figures out if the relationship is in the api_included hash or has included nested resources (path).
436
     */
437
    private function shouldIncludeRelation(string $relationshipName, array $context): bool
438
    {
439
        $normalizedName = $this->nameConverter ? $this->nameConverter->normalize($relationshipName, $context['resource_class'], self::FORMAT, $context) : $relationshipName;
×
440

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

444
    /**
445
     * Returns the names of the nested resources from a path relationship.
446
     */
447
    private function getIncludedNestedResources(string $relationshipName, array $context): array
448
    {
449
        $normalizedName = $this->nameConverter ? $this->nameConverter->normalize($relationshipName, $context['resource_class'], self::FORMAT, $context) : $relationshipName;
×
450

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

453
        return array_map(static fn (string $nested): string => substr($nested, strpos($nested, '.') + 1), $filtered);
×
454
    }
455

456
    // TODO: this code is similar to the one used in JsonLd
457
    private function getResourceShortName(string $resourceClass): string
458
    {
459
        if ($this->resourceClassResolver->isResourceClass($resourceClass)) {
2✔
460
            $resourceMetadata = $this->resourceMetadataCollectionFactory->create($resourceClass);
2✔
461

462
            return $resourceMetadata->getOperation()->getShortName();
2✔
463
        }
464

465
        return (new \ReflectionClass($resourceClass))->getShortName();
×
466
    }
467
}
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