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

api-platform / core / 6148660584

11 Sep 2023 03:40PM UTC coverage: 37.077% (-0.1%) from 37.185%
6148660584

push

github

soyuka
chore(symfony): security after validate when validator installed

10090 of 27214 relevant lines covered (37.08%)

19.39 hits per line

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

56.38
/src/Serializer/AbstractItemNormalizer.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\Serializer;
15

16
use ApiPlatform\Exception\InvalidArgumentException;
17
use ApiPlatform\Exception\ItemNotFoundException;
18
use ApiPlatform\Metadata\ApiProperty;
19
use ApiPlatform\Metadata\CollectionOperationInterface;
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\Metadata\Util\CloneTrait;
28
use ApiPlatform\Symfony\Security\ResourceAccessCheckerInterface;
29
use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException;
30
use Symfony\Component\PropertyAccess\PropertyAccess;
31
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
32
use Symfony\Component\PropertyInfo\Type;
33
use Symfony\Component\Serializer\Encoder\CsvEncoder;
34
use Symfony\Component\Serializer\Encoder\XmlEncoder;
35
use Symfony\Component\Serializer\Exception\LogicException;
36
use Symfony\Component\Serializer\Exception\MissingConstructorArgumentsException;
37
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
38
use Symfony\Component\Serializer\Exception\RuntimeException;
39
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
40
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
41
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
42
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
43
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
44
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
45
use Symfony\Component\Serializer\Serializer;
46

47
/**
48
 * Base item normalizer.
49
 *
50
 * @author Kévin Dunglas <dunglas@gmail.com>
51
 */
52
abstract class AbstractItemNormalizer extends AbstractObjectNormalizer
53
{
54
    use ClassInfoTrait;
55
    use CloneTrait;
56
    use ContextTrait;
57
    use InputOutputMetadataTrait;
58
    use OperationContextTrait;
59

60
    protected PropertyAccessorInterface $propertyAccessor;
61
    protected array $localCache = [];
62
    protected array $localFactoryOptionsCache = [];
63

64
    public function __construct(protected PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, protected PropertyMetadataFactoryInterface $propertyMetadataFactory, protected IriConverterInterface $iriConverter, protected ResourceClassResolverInterface $resourceClassResolver, PropertyAccessorInterface $propertyAccessor = null, NameConverterInterface $nameConverter = null, ClassMetadataFactoryInterface $classMetadataFactory = null, array $defaultContext = [], ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory = null, protected ?ResourceAccessCheckerInterface $resourceAccessChecker = null)
65
    {
66
        if (!isset($defaultContext['circular_reference_handler'])) {
126✔
67
            $defaultContext['circular_reference_handler'] = fn ($object): ?string => $this->iriConverter->getIriFromResource($object);
126✔
68
        }
69

70
        parent::__construct($classMetadataFactory, $nameConverter, null, null, \Closure::fromCallable($this->getObjectClass(...)), $defaultContext);
126✔
71
        $this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor();
126✔
72
        $this->resourceMetadataCollectionFactory = $resourceMetadataCollectionFactory;
126✔
73
    }
74

75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function supportsNormalization(mixed $data, string $format = null, array $context = []): bool
79
    {
80
        if (!\is_object($data) || is_iterable($data)) {
48✔
81
            return false;
24✔
82
        }
83

84
        $class = $context['force_resource_class'] ?? $this->getObjectClass($data);
30✔
85
        if (($context['output']['class'] ?? null) === $class) {
30✔
86
            return true;
3✔
87
        }
88

89
        return $this->resourceClassResolver->isResourceClass($class);
27✔
90
    }
91

92
    public function getSupportedTypes(?string $format): array
93
    {
94
        return [
57✔
95
            'object' => true,
57✔
96
        ];
57✔
97
    }
98

99
    /**
100
     * {@inheritdoc}
101
     */
102
    public function hasCacheableSupportsMethod(): bool
103
    {
104
        if (method_exists(Serializer::class, 'getSupportedTypes')) {
×
105
            trigger_deprecation(
×
106
                'api-platform/core',
×
107
                '3.1',
×
108
                'The "%s()" method is deprecated, use "getSupportedTypes()" instead.',
×
109
                __METHOD__
×
110
            );
×
111
        }
112

113
        return true;
×
114
    }
115

116
    /**
117
     * {@inheritdoc}
118
     *
119
     * @throws LogicException
120
     */
121
    public function normalize(mixed $object, string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
122
    {
123
        $resourceClass = $context['force_resource_class'] ?? $this->getObjectClass($object);
48✔
124
        if ($outputClass = $this->getOutputClass($context)) {
48✔
125
            if (!$this->serializer instanceof NormalizerInterface) {
3✔
126
                throw new LogicException('Cannot normalize the output because the injected serializer is not a normalizer');
×
127
            }
128

129
            unset($context['output'], $context['operation'], $context['operation_name']);
3✔
130
            $context['resource_class'] = $outputClass;
3✔
131
            $context['api_sub_level'] = true;
3✔
132
            $context[self::ALLOW_EXTRA_ATTRIBUTES] = false;
3✔
133

134
            return $this->serializer->normalize($object, $format, $context);
3✔
135
        }
136

137
        // Never remove this, with `application/json` we don't use our AbstractCollectionNormalizer and we need
138
        // to remove the collection operation from our context or we'll introduce security issues
139
        if (isset($context['operation']) && $context['operation'] instanceof CollectionOperationInterface) {
48✔
140
            unset($context['operation_name']);
×
141
            unset($context['operation']);
×
142
            unset($context['iri']);
×
143
        }
144

145
        if ($this->resourceClassResolver->isResourceClass($resourceClass)) {
48✔
146
            $context = $this->initContext($resourceClass, $context);
45✔
147
        }
148

149
        $context['api_normalize'] = true;
48✔
150
        $iri = $context['iri'] ??= $this->iriConverter->getIriFromResource($object, UrlGeneratorInterface::ABS_URL, $context['operation'] ?? null, $context);
48✔
151

152
        /*
153
         * When true, converts the normalized data array of a resource into an
154
         * IRI, if the normalized data array is empty.
155
         *
156
         * This is useful when traversing from a non-resource towards an attribute
157
         * which is a resource, as we do not have the benefit of {@see ApiProperty::isReadableLink}.
158
         *
159
         * It must not be propagated to resources, as {@see ApiProperty::isReadableLink}
160
         * should take effect.
161
         */
162
        $emptyResourceAsIri = $context['api_empty_resource_as_iri'] ?? false;
48✔
163
        unset($context['api_empty_resource_as_iri']);
48✔
164

165
        if (isset($context['resources'])) {
48✔
166
            $context['resources'][$iri] = $iri;
24✔
167
        }
168

169
        $data = parent::normalize($object, $format, $context);
48✔
170

171
        if ($emptyResourceAsIri && \is_array($data) && 0 === \count($data)) {
45✔
172
            return $iri;
×
173
        }
174

175
        return $data;
45✔
176
    }
177

178
    /**
179
     * {@inheritdoc}
180
     */
181
    public function supportsDenormalization(mixed $data, string $type, string $format = null, array $context = []): bool
182
    {
183
        if (($context['input']['class'] ?? null) === $type) {
9✔
184
            return true;
×
185
        }
186

187
        return $this->localCache[$type] ?? $this->localCache[$type] = $this->resourceClassResolver->isResourceClass($type);
9✔
188
    }
189

190
    /**
191
     * {@inheritdoc}
192
     */
193
    public function denormalize(mixed $data, string $class, string $format = null, array $context = []): mixed
194
    {
195
        $resourceClass = $class;
21✔
196

197
        if ($inputClass = $this->getInputClass($context)) {
21✔
198
            if (!$this->serializer instanceof DenormalizerInterface) {
×
199
                throw new LogicException('Cannot denormalize the input because the injected serializer is not a denormalizer');
×
200
            }
201

202
            unset($context['input'], $context['operation'], $context['operation_name']);
×
203
            $context['resource_class'] = $inputClass;
×
204

205
            try {
206
                return $this->serializer->denormalize($data, $inputClass, $format, $context);
×
207
            } catch (NotNormalizableValueException $e) {
×
208
                throw new UnexpectedValueException('The input data is misformatted.', $e->getCode(), $e);
×
209
            }
210
        }
211

212
        if (null === $objectToPopulate = $this->extractObjectToPopulate($resourceClass, $context, static::OBJECT_TO_POPULATE)) {
21✔
213
            $normalizedData = \is_scalar($data) ? [$data] : $this->prepareForDenormalization($data);
21✔
214
            $class = $this->getClassDiscriminatorResolvedClass($normalizedData, $class, $context);
21✔
215
        }
216

217
        $context['api_denormalize'] = true;
21✔
218

219
        if ($this->resourceClassResolver->isResourceClass($class)) {
21✔
220
            $resourceClass = $this->resourceClassResolver->getResourceClass($objectToPopulate, $class);
21✔
221
            $context['resource_class'] = $resourceClass;
21✔
222
        }
223

224
        if (\is_string($data)) {
21✔
225
            try {
226
                return $this->iriConverter->getResourceFromIri($data, $context + ['fetch_data' => true]);
×
227
            } catch (ItemNotFoundException $e) {
×
228
                throw new UnexpectedValueException($e->getMessage(), $e->getCode(), $e);
×
229
            } catch (InvalidArgumentException $e) {
×
230
                throw new UnexpectedValueException(sprintf('Invalid IRI "%s".', $data), $e->getCode(), $e);
×
231
            }
232
        }
233

234
        if (!\is_array($data)) {
21✔
235
            throw NotNormalizableValueException::createForUnexpectedDataType(sprintf('The type of the "%s" resource must be "array" (nested document) or "string" (IRI), "%s" given.', $resourceClass, \gettype($data)), $data, [Type::BUILTIN_TYPE_ARRAY, Type::BUILTIN_TYPE_STRING], $context['deserialization_path'] ?? null);
×
236
        }
237

238
        $previousObject = $this->clone($objectToPopulate);
21✔
239
        $object = parent::denormalize($data, $class, $format, $context);
21✔
240

241
        if (!$this->resourceClassResolver->isResourceClass($class)) {
12✔
242
            return $object;
×
243
        }
244

245
        // Bypass the post-denormalize attribute revert logic if the object could not be
246
        // cloned since we cannot possibly revert any changes made to it.
247
        if (null !== $objectToPopulate && null === $previousObject) {
12✔
248
            return $object;
×
249
        }
250

251
        $options = $this->getFactoryOptions($context);
12✔
252
        $propertyNames = iterator_to_array($this->propertyNameCollectionFactory->create($resourceClass, $options));
12✔
253

254
        // Revert attributes that aren't allowed to be changed after a post-denormalize check
255
        foreach (array_keys($data) as $attribute) {
12✔
256
            $attribute = $this->nameConverter ? $this->nameConverter->denormalize((string) $attribute) : $attribute;
12✔
257
            if (!\in_array($attribute, $propertyNames, true)) {
12✔
258
                continue;
×
259
            }
260

261
            if (!$this->canAccessAttributePostDenormalize($object, $previousObject, $attribute, $context)) {
12✔
262
                if (null !== $previousObject) {
×
263
                    $this->setValue($object, $attribute, $this->propertyAccessor->getValue($previousObject, $attribute));
×
264
                } else {
265
                    $propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $attribute, $options);
×
266
                    $this->setValue($object, $attribute, $propertyMetadata->getDefault());
×
267
                }
268
            }
269
        }
270

271
        return $object;
12✔
272
    }
273

274
    /**
275
     * Method copy-pasted from symfony/serializer.
276
     * Remove it after symfony/serializer version update @see https://github.com/symfony/symfony/pull/28263.
277
     *
278
     * {@inheritdoc}
279
     *
280
     * @internal
281
     */
282
    protected function instantiateObject(array &$data, string $class, array &$context, \ReflectionClass $reflectionClass, array|bool $allowedAttributes, string $format = null): object
283
    {
284
        if (null !== $object = $this->extractObjectToPopulate($class, $context, static::OBJECT_TO_POPULATE)) {
21✔
285
            unset($context[static::OBJECT_TO_POPULATE]);
×
286

287
            return $object;
×
288
        }
289

290
        $class = $this->getClassDiscriminatorResolvedClass($data, $class, $context);
21✔
291
        $reflectionClass = new \ReflectionClass($class);
21✔
292

293
        $constructor = $this->getConstructor($data, $class, $context, $reflectionClass, $allowedAttributes);
21✔
294
        if ($constructor) {
21✔
295
            $constructorParameters = $constructor->getParameters();
21✔
296

297
            $params = [];
21✔
298
            foreach ($constructorParameters as $constructorParameter) {
21✔
299
                $paramName = $constructorParameter->name;
×
300
                $key = $this->nameConverter ? $this->nameConverter->normalize($paramName, $class, $format, $context) : $paramName;
×
301

302
                $allowed = false === $allowedAttributes || (\is_array($allowedAttributes) && \in_array($paramName, $allowedAttributes, true));
×
303
                $ignored = !$this->isAllowedAttribute($class, $paramName, $format, $context);
×
304
                if ($constructorParameter->isVariadic()) {
×
305
                    if ($allowed && !$ignored && (isset($data[$key]) || \array_key_exists($key, $data))) {
×
306
                        if (!\is_array($data[$paramName])) {
×
307
                            throw new RuntimeException(sprintf('Cannot create an instance of %s from serialized data because the variadic parameter %s can only accept an array.', $class, $constructorParameter->name));
×
308
                        }
309

310
                        $params[] = $data[$paramName];
×
311
                    }
312
                } elseif ($allowed && !$ignored && (isset($data[$key]) || \array_key_exists($key, $data))) {
×
313
                    $constructorContext = $context;
×
314
                    $constructorContext['deserialization_path'] = $context['deserialization_path'] ?? $key;
×
315
                    try {
316
                        $params[] = $this->createConstructorArgument($data[$key], $key, $constructorParameter, $constructorContext, $format);
×
317
                    } catch (NotNormalizableValueException $exception) {
×
318
                        if (!isset($context['not_normalizable_value_exceptions'])) {
×
319
                            throw $exception;
×
320
                        }
321
                        $context['not_normalizable_value_exceptions'][] = $exception;
×
322
                    }
323

324
                    // Don't run set for a parameter passed to the constructor
325
                    unset($data[$key]);
×
326
                } elseif (isset($context[static::DEFAULT_CONSTRUCTOR_ARGUMENTS][$class][$key])) {
×
327
                    $params[] = $context[static::DEFAULT_CONSTRUCTOR_ARGUMENTS][$class][$key];
×
328
                } elseif ($constructorParameter->isDefaultValueAvailable()) {
×
329
                    $params[] = $constructorParameter->getDefaultValue();
×
330
                } else {
331
                    if (!isset($context['not_normalizable_value_exceptions'])) {
×
332
                        throw new MissingConstructorArgumentsException(sprintf('Cannot create an instance of "%s" from serialized data because its constructor requires parameter "%s" to be present.', $class, $constructorParameter->name), 0, null, [$constructorParameter->name]);
×
333
                    }
334

335
                    $exception = NotNormalizableValueException::createForUnexpectedDataType(sprintf('Failed to create object because the class misses the "%s" property.', $constructorParameter->name), $data, ['unknown'], $context['deserialization_path'] ?? null, true);
×
336
                    $context['not_normalizable_value_exceptions'][] = $exception;
×
337
                }
338
            }
339

340
            if (\count($context['not_normalizable_value_exceptions'] ?? []) > 0) {
21✔
341
                return $reflectionClass->newInstanceWithoutConstructor();
×
342
            }
343

344
            if ($constructor->isConstructor()) {
21✔
345
                return $reflectionClass->newInstanceArgs($params);
21✔
346
            }
347

348
            return $constructor->invokeArgs(null, $params);
×
349
        }
350

351
        return new $class();
×
352
    }
353

354
    protected function getClassDiscriminatorResolvedClass(array $data, string $class, array $context = []): string
355
    {
356
        if (null === $this->classDiscriminatorResolver || (null === $mapping = $this->classDiscriminatorResolver->getMappingForClass($class))) {
21✔
357
            return $class;
21✔
358
        }
359

360
        if (!isset($data[$mapping->getTypeProperty()])) {
×
361
            throw NotNormalizableValueException::createForUnexpectedDataType(sprintf('Type property "%s" not found for the abstract object "%s".', $mapping->getTypeProperty(), $class), null, ['string'], isset($context['deserialization_path']) ? $context['deserialization_path'].'.'.$mapping->getTypeProperty() : $mapping->getTypeProperty());
×
362
        }
363

364
        $type = $data[$mapping->getTypeProperty()];
×
365
        if (null === ($mappedClass = $mapping->getClassForType($type))) {
×
366
            throw NotNormalizableValueException::createForUnexpectedDataType(sprintf('The type "%s" is not a valid value.', $type), $type, ['string'], isset($context['deserialization_path']) ? $context['deserialization_path'].'.'.$mapping->getTypeProperty() : $mapping->getTypeProperty(), true);
×
367
        }
368

369
        return $mappedClass;
×
370
    }
371

372
    protected function createConstructorArgument($parameterData, string $key, \ReflectionParameter $constructorParameter, array &$context, string $format = null): mixed
373
    {
374
        return $this->createAndValidateAttributeValue($constructorParameter->name, $parameterData, $format, $context);
×
375
    }
376

377
    /**
378
     * {@inheritdoc}
379
     *
380
     * Unused in this context.
381
     *
382
     * @return string[]
383
     */
384
    protected function extractAttributes($object, $format = null, array $context = []): array
385
    {
386
        return [];
×
387
    }
388

389
    /**
390
     * {@inheritdoc}
391
     */
392
    protected function getAllowedAttributes(string|object $classOrObject, array $context, bool $attributesAsString = false): array|bool
393
    {
394
        if (!$this->resourceClassResolver->isResourceClass($context['resource_class'])) {
57✔
395
            return parent::getAllowedAttributes($classOrObject, $context, $attributesAsString);
3✔
396
        }
397

398
        $resourceClass = $this->resourceClassResolver->getResourceClass(null, $context['resource_class']); // fix for abstract classes and interfaces
54✔
399
        $options = $this->getFactoryOptions($context);
54✔
400
        $propertyNames = $this->propertyNameCollectionFactory->create($resourceClass, $options);
54✔
401

402
        $allowedAttributes = [];
54✔
403
        foreach ($propertyNames as $propertyName) {
54✔
404
            $propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $propertyName, $options);
54✔
405

406
            if (
407
                $this->isAllowedAttribute($classOrObject, $propertyName, null, $context)
54✔
408
                && (isset($context['api_normalize']) && $propertyMetadata->isReadable()
54✔
409
                    || isset($context['api_denormalize']) && ($propertyMetadata->isWritable() || !\is_object($classOrObject) && $propertyMetadata->isInitializable())
54✔
410
                )
411
            ) {
412
                $allowedAttributes[] = $propertyName;
54✔
413
            }
414
        }
415

416
        return $allowedAttributes;
54✔
417
    }
418

419
    /**
420
     * {@inheritdoc}
421
     */
422
    protected function isAllowedAttribute(object|string $classOrObject, string $attribute, string $format = null, array $context = []): bool
423
    {
424
        if (!parent::isAllowedAttribute($classOrObject, $attribute, $format, $context)) {
57✔
425
            return false;
×
426
        }
427

428
        return $this->canAccessAttribute(\is_object($classOrObject) ? $classOrObject : null, $attribute, $context);
57✔
429
    }
430

431
    /**
432
     * Check if access to the attribute is granted.
433
     */
434
    protected function canAccessAttribute(?object $object, string $attribute, array $context = []): bool
435
    {
436
        if (!$this->resourceClassResolver->isResourceClass($context['resource_class'])) {
57✔
437
            return true;
3✔
438
        }
439

440
        $options = $this->getFactoryOptions($context);
54✔
441
        $propertyMetadata = $this->propertyMetadataFactory->create($context['resource_class'], $attribute, $options);
54✔
442
        $security = $propertyMetadata->getSecurity();
54✔
443
        if (null !== $this->resourceAccessChecker && $security) {
54✔
444
            return $this->resourceAccessChecker->isGranted($context['resource_class'], $security, [
×
445
                'object' => $object,
×
446
            ]);
×
447
        }
448

449
        return true;
54✔
450
    }
451

452
    /**
453
     * Check if access to the attribute is granted.
454
     */
455
    protected function canAccessAttributePostDenormalize(?object $object, ?object $previousObject, string $attribute, array $context = []): bool
456
    {
457
        $options = $this->getFactoryOptions($context);
12✔
458
        $propertyMetadata = $this->propertyMetadataFactory->create($context['resource_class'], $attribute, $options);
12✔
459
        $security = $propertyMetadata->getSecurityPostDenormalize();
12✔
460
        if ($this->resourceAccessChecker && $security) {
12✔
461
            return $this->resourceAccessChecker->isGranted($context['resource_class'], $security, [
×
462
                'object' => $object,
×
463
                'previous_object' => $previousObject,
×
464
            ]);
×
465
        }
466

467
        return true;
12✔
468
    }
469

470
    /**
471
     * {@inheritdoc}
472
     */
473
    protected function setAttributeValue(object $object, string $attribute, mixed $value, string $format = null, array $context = []): void
474
    {
475
        try {
476
            $this->setValue($object, $attribute, $this->createAttributeValue($attribute, $value, $format, $context));
21✔
477
        } catch (NotNormalizableValueException $exception) {
9✔
478
            // Only throw if collecting denormalization errors is disabled.
479
            if (!isset($context['not_normalizable_value_exceptions'])) {
6✔
480
                throw $exception;
6✔
481
            }
482
        }
483
    }
484

485
    /**
486
     * Validates the type of the value. Allows using integers as floats for JSON formats.
487
     *
488
     * @throws NotNormalizableValueException
489
     */
490
    protected function validateType(string $attribute, Type $type, mixed $value, string $format = null, array $context = []): void
491
    {
492
        $builtinType = $type->getBuiltinType();
12✔
493
        if (Type::BUILTIN_TYPE_FLOAT === $builtinType && null !== $format && str_contains($format, 'json')) {
12✔
494
            $isValid = \is_float($value) || \is_int($value);
×
495
        } else {
496
            $isValid = \call_user_func('is_'.$builtinType, $value);
12✔
497
        }
498

499
        if (!$isValid) {
12✔
500
            throw NotNormalizableValueException::createForUnexpectedDataType(sprintf('The type of the "%s" attribute must be "%s", "%s" given.', $attribute, $builtinType, \gettype($value)), $value, [$builtinType], $context['deserialization_path'] ?? null);
×
501
        }
502
    }
503

504
    /**
505
     * Denormalizes a collection of objects.
506
     *
507
     * @throws NotNormalizableValueException
508
     */
509
    protected function denormalizeCollection(string $attribute, ApiProperty $propertyMetadata, Type $type, string $className, mixed $value, ?string $format, array $context): array
510
    {
511
        if (!\is_array($value)) {
9✔
512
            throw NotNormalizableValueException::createForUnexpectedDataType(sprintf('The type of the "%s" attribute must be "array", "%s" given.', $attribute, \gettype($value)), $value, [Type::BUILTIN_TYPE_ARRAY], $context['deserialization_path'] ?? null);
3✔
513
        }
514

515
        $collectionKeyType = $type->getCollectionKeyTypes()[0] ?? null;
6✔
516
        $collectionKeyBuiltinType = $collectionKeyType?->getBuiltinType();
6✔
517
        $childContext = $this->createChildContext($this->createOperationContext($context, $className), $attribute, $format);
6✔
518
        $values = [];
6✔
519
        foreach ($value as $index => $obj) {
6✔
520
            if (null !== $collectionKeyBuiltinType && !\call_user_func('is_'.$collectionKeyBuiltinType, $index)) {
6✔
521
                throw NotNormalizableValueException::createForUnexpectedDataType(sprintf('The type of the key "%s" must be "%s", "%s" given.', $index, $collectionKeyBuiltinType, \gettype($index)), $index, [$collectionKeyBuiltinType], ($context['deserialization_path'] ?? false) ? sprintf('key(%s)', $context['deserialization_path']) : null, true);
3✔
522
            }
523

524
            $values[$index] = $this->denormalizeRelation($attribute, $propertyMetadata, $className, $obj, $format, $childContext);
3✔
525
        }
526

527
        return $values;
3✔
528
    }
529

530
    /**
531
     * Denormalizes a relation.
532
     *
533
     * @throws LogicException
534
     * @throws UnexpectedValueException
535
     * @throws NotNormalizableValueException
536
     */
537
    protected function denormalizeRelation(string $attributeName, ApiProperty $propertyMetadata, string $className, mixed $value, ?string $format, array $context): ?object
538
    {
539
        if (\is_string($value)) {
×
540
            try {
541
                return $this->iriConverter->getResourceFromIri($value, $context + ['fetch_data' => true]);
×
542
            } catch (ItemNotFoundException $e) {
×
543
                throw new UnexpectedValueException($e->getMessage(), $e->getCode(), $e);
×
544
            } catch (InvalidArgumentException $e) {
×
545
                throw new UnexpectedValueException(sprintf('Invalid IRI "%s".', $value), $e->getCode(), $e);
×
546
            }
547
        }
548

549
        if ($propertyMetadata->isWritableLink()) {
×
550
            $context['api_allow_update'] = true;
×
551

552
            if (!$this->serializer instanceof DenormalizerInterface) {
×
553
                throw new LogicException(sprintf('The injected serializer must be an instance of "%s".', DenormalizerInterface::class));
×
554
            }
555

556
            $item = $this->serializer->denormalize($value, $className, $format, $context);
×
557
            if (!\is_object($item) && null !== $item) {
×
558
                throw new \UnexpectedValueException('Expected item to be an object or null.');
×
559
            }
560

561
            return $item;
×
562
        }
563

564
        if (!\is_array($value)) {
×
565
            throw NotNormalizableValueException::createForUnexpectedDataType(sprintf('The type of the "%s" attribute must be "array" (nested document) or "string" (IRI), "%s" given.', $attributeName, \gettype($value)), $value, [Type::BUILTIN_TYPE_ARRAY, Type::BUILTIN_TYPE_STRING], $context['deserialization_path'] ?? null);
×
566
        }
567

568
        throw new UnexpectedValueException(sprintf('Nested documents for attribute "%s" are not allowed. Use IRIs instead.', $attributeName));
×
569
    }
570

571
    /**
572
     * Gets the options for the property name collection / property metadata factories.
573
     */
574
    protected function getFactoryOptions(array $context): array
575
    {
576
        $options = [];
57✔
577
        if (isset($context[self::GROUPS])) {
57✔
578
            /* @see https://github.com/symfony/symfony/blob/v4.2.6/src/Symfony/Component/PropertyInfo/Extractor/SerializerExtractor.php */
579
            $options['serializer_groups'] = (array) $context[self::GROUPS];
3✔
580
        }
581

582
        $operationCacheKey = ($context['resource_class'] ?? '').($context['operation_name'] ?? '').($context['api_normalize'] ?? '');
57✔
583
        if ($operationCacheKey && isset($this->localFactoryOptionsCache[$operationCacheKey])) {
57✔
584
            return $options + $this->localFactoryOptionsCache[$operationCacheKey];
57✔
585
        }
586

587
        // This is a hot spot
588
        if (isset($context['resource_class'])) {
57✔
589
            // Note that the groups need to be read on the root operation
590
            if ($operation = ($context['root_operation'] ?? null)) {
57✔
591
                $options['normalization_groups'] = $operation->getNormalizationContext()['groups'] ?? null;
6✔
592
                $options['denormalization_groups'] = $operation->getDenormalizationContext()['groups'] ?? null;
6✔
593
                $options['operation_name'] = $operation->getName();
6✔
594
            }
595
        }
596

597
        return $options + $this->localFactoryOptionsCache[$operationCacheKey] = $options;
57✔
598
    }
599

600
    /**
601
     * {@inheritdoc}
602
     *
603
     * @throws UnexpectedValueException
604
     */
605
    protected function getAttributeValue(object $object, string $attribute, string $format = null, array $context = []): mixed
606
    {
607
        $context['api_attribute'] = $attribute;
45✔
608
        $propertyMetadata = $this->propertyMetadataFactory->create($context['resource_class'], $attribute, $this->getFactoryOptions($context));
45✔
609

610
        if ($context['api_denormalize'] ?? false) {
45✔
611
            return $this->propertyAccessor->getValue($object, $attribute);
×
612
        }
613

614
        $types = $propertyMetadata->getBuiltinTypes() ?? [];
45✔
615

616
        foreach ($types as $type) {
45✔
617
            if (
618
                $type->isCollection()
36✔
619
                && ($collectionValueType = $type->getCollectionValueTypes()[0] ?? null)
36✔
620
                && ($className = $collectionValueType->getClassName())
36✔
621
                && $this->resourceClassResolver->isResourceClass($className)
36✔
622
            ) {
623
                $childContext = $this->createChildContext($this->createOperationContext($context, $className), $attribute, $format);
12✔
624

625
                // @see ApiPlatform\Hal\Serializer\ItemNormalizer:getComponents logic for intentional duplicate content
626
                // @see ApiPlatform\JsonApi\Serializer\ItemNormalizer:getComponents logic for intentional duplicate content
627
                if ('jsonld' === $format && $itemUriTemplate = $propertyMetadata->getUriTemplate()) {
12✔
628
                    $operation = $this->resourceMetadataCollectionFactory->create($className)->getOperation(
×
629
                        operationName: $itemUriTemplate,
×
630
                        forceCollection: true,
×
631
                        httpOperation: true
×
632
                    );
×
633

634
                    return $this->iriConverter->getIriFromResource($object, UrlGeneratorInterface::ABS_PATH, $operation, $childContext);
×
635
                }
636

637
                $attributeValue = $this->propertyAccessor->getValue($object, $attribute);
12✔
638

639
                if (!is_iterable($attributeValue)) {
12✔
640
                    throw new UnexpectedValueException('Unexpected non-iterable value for to-many relation.');
×
641
                }
642

643
                $resourceClass = $this->resourceClassResolver->getResourceClass($attributeValue, $className);
12✔
644

645
                return $this->normalizeCollectionOfRelations($propertyMetadata, $attributeValue, $resourceClass, $format, $childContext);
12✔
646
            }
647

648
            if (
649
                ($className = $type->getClassName())
36✔
650
                && $this->resourceClassResolver->isResourceClass($className)
36✔
651
            ) {
652
                $childContext = $this->createChildContext($this->createOperationContext($context, $className), $attribute, $format);
18✔
653
                unset($childContext['iri'], $childContext['uri_variables']);
18✔
654

655
                if ('jsonld' === $format && $uriTemplate = $propertyMetadata->getUriTemplate()) {
18✔
656
                    $operation = $this->resourceMetadataCollectionFactory->create($className)->getOperation(
×
657
                        operationName: $uriTemplate,
×
658
                        httpOperation: true
×
659
                    );
×
660

661
                    return $this->iriConverter->getIriFromResource($object, UrlGeneratorInterface::ABS_PATH, $operation, $childContext);
×
662
                }
663

664
                $attributeValue = $this->propertyAccessor->getValue($object, $attribute);
18✔
665

666
                if (!\is_object($attributeValue) && null !== $attributeValue) {
18✔
667
                    throw new UnexpectedValueException('Unexpected non-object value for to-one relation.');
×
668
                }
669

670
                $resourceClass = $this->resourceClassResolver->getResourceClass($attributeValue, $className);
18✔
671

672
                return $this->normalizeRelation($propertyMetadata, $attributeValue, $resourceClass, $format, $childContext);
18✔
673
            }
674

675
            if (!$this->serializer instanceof NormalizerInterface) {
36✔
676
                throw new LogicException(sprintf('The injected serializer must be an instance of "%s".', NormalizerInterface::class));
×
677
            }
678

679
            unset(
36✔
680
                $context['resource_class'],
36✔
681
                $context['force_resource_class'],
36✔
682
            );
36✔
683

684
            // Anonymous resources
685
            if ($type->getClassName()) {
36✔
686
                $childContext = $this->createChildContext($this->createOperationContext($context, null), $attribute, $format);
12✔
687
                $childContext['output']['gen_id'] = $propertyMetadata->getGenId() ?? true;
12✔
688

689
                $attributeValue = $this->propertyAccessor->getValue($object, $attribute);
12✔
690

691
                return $this->serializer->normalize($attributeValue, $format, $childContext);
12✔
692
            }
693

694
            if ('array' === $type->getBuiltinType()) {
33✔
695
                $childContext = $this->createChildContext($this->createOperationContext($context, null), $attribute, $format);
12✔
696

697
                $attributeValue = $this->propertyAccessor->getValue($object, $attribute);
12✔
698

699
                return $this->serializer->normalize($attributeValue, $format, $childContext);
12✔
700
            }
701
        }
702

703
        if (!$this->serializer instanceof NormalizerInterface) {
42✔
704
            throw new LogicException(sprintf('The injected serializer must be an instance of "%s".', NormalizerInterface::class));
×
705
        }
706

707
        unset($context['resource_class']);
42✔
708
        unset($context['force_resource_class']);
42✔
709

710
        $attributeValue = $this->propertyAccessor->getValue($object, $attribute);
42✔
711

712
        return $this->serializer->normalize($attributeValue, $format, $context);
39✔
713
    }
714

715
    /**
716
     * Normalizes a collection of relations (to-many).
717
     *
718
     * @throws UnexpectedValueException
719
     */
720
    protected function normalizeCollectionOfRelations(ApiProperty $propertyMetadata, iterable $attributeValue, string $resourceClass, ?string $format, array $context): array
721
    {
722
        $value = [];
12✔
723
        foreach ($attributeValue as $index => $obj) {
12✔
724
            if (!\is_object($obj) && null !== $obj) {
×
725
                throw new UnexpectedValueException('Unexpected non-object element in to-many relation.');
×
726
            }
727

728
            // update context, if concrete object class deviates from general relation class (e.g. in case of polymorphic resources)
729
            $objResourceClass = $this->resourceClassResolver->getResourceClass($obj, $resourceClass);
×
730
            $context['resource_class'] = $objResourceClass;
×
731
            if ($this->resourceMetadataCollectionFactory) {
×
732
                $context['operation'] = $this->resourceMetadataCollectionFactory->create($objResourceClass)->getOperation();
×
733
            }
734

735
            $value[$index] = $this->normalizeRelation($propertyMetadata, $obj, $resourceClass, $format, $context);
×
736
        }
737

738
        return $value;
12✔
739
    }
740

741
    /**
742
     * Normalizes a relation.
743
     *
744
     * @throws LogicException
745
     * @throws UnexpectedValueException
746
     */
747
    protected function normalizeRelation(ApiProperty $propertyMetadata, ?object $relatedObject, string $resourceClass, ?string $format, array $context): \ArrayObject|array|string|null
748
    {
749
        if (null === $relatedObject || !empty($context['attributes']) || $propertyMetadata->isReadableLink()) {
18✔
750
            if (!$this->serializer instanceof NormalizerInterface) {
12✔
751
                throw new LogicException(sprintf('The injected serializer must be an instance of "%s".', NormalizerInterface::class));
×
752
            }
753

754
            $relatedContext = $this->createOperationContext($context, $resourceClass);
12✔
755
            $normalizedRelatedObject = $this->serializer->normalize($relatedObject, $format, $relatedContext);
12✔
756
            if (!\is_string($normalizedRelatedObject) && !\is_array($normalizedRelatedObject) && !$normalizedRelatedObject instanceof \ArrayObject && null !== $normalizedRelatedObject) {
12✔
757
                throw new UnexpectedValueException('Expected normalized relation to be an IRI, array, \ArrayObject or null');
×
758
            }
759

760
            return $normalizedRelatedObject;
12✔
761
        }
762

763
        $iri = $this->iriConverter->getIriFromResource($relatedObject);
6✔
764

765
        if (isset($context['resources'])) {
6✔
766
            $context['resources'][$iri] = $iri;
×
767
        }
768

769
        $push = $propertyMetadata->getPush() ?? false;
6✔
770
        if (isset($context['resources_to_push']) && $push) {
6✔
771
            $context['resources_to_push'][$iri] = $iri;
×
772
        }
773

774
        return $iri;
6✔
775
    }
776

777
    private function createAttributeValue(string $attribute, mixed $value, string $format = null, array &$context = []): mixed
778
    {
779
        try {
780
            return $this->createAndValidateAttributeValue($attribute, $value, $format, $context);
21✔
781
        } catch (NotNormalizableValueException $exception) {
9✔
782
            if (!isset($context['not_normalizable_value_exceptions'])) {
6✔
783
                throw $exception;
6✔
784
            }
785
            $context['not_normalizable_value_exceptions'][] = $exception;
×
786

787
            throw $exception;
×
788
        }
789
    }
790

791
    private function createAndValidateAttributeValue(string $attribute, mixed $value, string $format = null, array $context = []): mixed
792
    {
793
        $propertyMetadata = $this->propertyMetadataFactory->create($context['resource_class'], $attribute, $this->getFactoryOptions($context));
21✔
794
        $types = $propertyMetadata->getBuiltinTypes() ?? [];
21✔
795
        $isMultipleTypes = \count($types) > 1;
21✔
796

797
        foreach ($types as $type) {
21✔
798
            if (null === $value && ($type->isNullable() || ($context[static::DISABLE_TYPE_ENFORCEMENT] ?? false))) {
21✔
799
                return $value;
×
800
            }
801

802
            $collectionValueType = $type->getCollectionValueTypes()[0] ?? null;
21✔
803

804
            /* From @see AbstractObjectNormalizer::validateAndDenormalize() */
805
            // Fix a collection that contains the only one element
806
            // This is special to xml format only
807
            if ('xml' === $format && null !== $collectionValueType && (!\is_array($value) || !\is_int(key($value)))) {
21✔
808
                $value = [$value];
×
809
            }
810

811
            if (
812
                $type->isCollection()
21✔
813
                && null !== $collectionValueType
21✔
814
                && null !== ($className = $collectionValueType->getClassName())
21✔
815
                && $this->resourceClassResolver->isResourceClass($className)
21✔
816
            ) {
817
                $resourceClass = $this->resourceClassResolver->getResourceClass(null, $className);
9✔
818
                $context['resource_class'] = $resourceClass;
9✔
819

820
                return $this->denormalizeCollection($attribute, $propertyMetadata, $type, $resourceClass, $value, $format, $context);
9✔
821
            }
822

823
            if (
824
                null !== ($className = $type->getClassName())
15✔
825
                && $this->resourceClassResolver->isResourceClass($className)
15✔
826
            ) {
827
                $resourceClass = $this->resourceClassResolver->getResourceClass(null, $className);
6✔
828
                $childContext = $this->createChildContext($this->createOperationContext($context, $resourceClass), $attribute, $format);
6✔
829

830
                return $this->denormalizeRelation($attribute, $propertyMetadata, $resourceClass, $value, $format, $childContext);
6✔
831
            }
832

833
            if (
834
                $type->isCollection()
12✔
835
                && null !== $collectionValueType
12✔
836
                && null !== ($className = $collectionValueType->getClassName())
12✔
837
                && \is_array($value)
12✔
838
            ) {
839
                if (!$this->serializer instanceof DenormalizerInterface) {
×
840
                    throw new LogicException(sprintf('The injected serializer must be an instance of "%s".', DenormalizerInterface::class));
×
841
                }
842

843
                unset($context['resource_class']);
×
844

845
                return $this->serializer->denormalize($value, $className.'[]', $format, $context);
×
846
            }
847

848
            if (null !== $className = $type->getClassName()) {
12✔
849
                if (!$this->serializer instanceof DenormalizerInterface) {
×
850
                    throw new LogicException(sprintf('The injected serializer must be an instance of "%s".', DenormalizerInterface::class));
×
851
                }
852

853
                unset($context['resource_class']);
×
854

855
                return $this->serializer->denormalize($value, $className, $format, $context);
×
856
            }
857

858
            /* From @see AbstractObjectNormalizer::validateAndDenormalize() */
859
            // In XML and CSV all basic datatypes are represented as strings, it is e.g. not possible to determine,
860
            // if a value is meant to be a string, float, int or a boolean value from the serialized representation.
861
            // That's why we have to transform the values, if one of these non-string basic datatypes is expected.
862
            if (\is_string($value) && (XmlEncoder::FORMAT === $format || CsvEncoder::FORMAT === $format)) {
12✔
863
                if ('' === $value && $type->isNullable() && \in_array($type->getBuiltinType(), [Type::BUILTIN_TYPE_BOOL, Type::BUILTIN_TYPE_INT, Type::BUILTIN_TYPE_FLOAT], true)) {
×
864
                    return null;
×
865
                }
866

867
                switch ($type->getBuiltinType()) {
×
868
                    case Type::BUILTIN_TYPE_BOOL:
869
                        // according to http://www.w3.org/TR/xmlschema-2/#boolean, valid representations are "false", "true", "0" and "1"
870
                        if ('false' === $value || '0' === $value) {
×
871
                            $value = false;
×
872
                        } elseif ('true' === $value || '1' === $value) {
×
873
                            $value = true;
×
874
                        } else {
875
                            // union/intersect types: try the next type, if not valid, an exception will be thrown at the end
876
                            if ($isMultipleTypes) {
×
877
                                break 2;
×
878
                            }
879
                            throw NotNormalizableValueException::createForUnexpectedDataType(sprintf('The type of the "%s" attribute for class "%s" must be bool ("%s" given).', $attribute, $className, $value), $value, [Type::BUILTIN_TYPE_BOOL], $context['deserialization_path'] ?? null);
×
880
                        }
881
                        break;
×
882
                    case Type::BUILTIN_TYPE_INT:
883
                        if (ctype_digit($value) || ('-' === $value[0] && ctype_digit(substr($value, 1)))) {
×
884
                            $value = (int) $value;
×
885
                        } else {
886
                            // union/intersect types: try the next type, if not valid, an exception will be thrown at the end
887
                            if ($isMultipleTypes) {
×
888
                                break 2;
×
889
                            }
890
                            throw NotNormalizableValueException::createForUnexpectedDataType(sprintf('The type of the "%s" attribute for class "%s" must be int ("%s" given).', $attribute, $className, $value), $value, [Type::BUILTIN_TYPE_INT], $context['deserialization_path'] ?? null);
×
891
                        }
892
                        break;
×
893
                    case Type::BUILTIN_TYPE_FLOAT:
894
                        if (is_numeric($value)) {
×
895
                            return (float) $value;
×
896
                        }
897

898
                        switch ($value) {
899
                            case 'NaN':
×
900
                                return \NAN;
×
901
                            case 'INF':
×
902
                                return \INF;
×
903
                            case '-INF':
×
904
                                return -\INF;
×
905
                            default:
906
                                // union/intersect types: try the next type, if not valid, an exception will be thrown at the end
907
                                if ($isMultipleTypes) {
×
908
                                    break 3;
×
909
                                }
910
                                throw NotNormalizableValueException::createForUnexpectedDataType(sprintf('The type of the "%s" attribute for class "%s" must be float ("%s" given).', $attribute, $className, $value), $value, [Type::BUILTIN_TYPE_FLOAT], $context['deserialization_path'] ?? null);
×
911
                        }
912
                }
913
            }
914

915
            if ($context[static::DISABLE_TYPE_ENFORCEMENT] ?? false) {
12✔
916
                return $value;
×
917
            }
918

919
            try {
920
                $this->validateType($attribute, $type, $value, $format, $context);
12✔
921

922
                break;
12✔
923
            } catch (NotNormalizableValueException $e) {
×
924
                // union/intersect types: try the next type
925
                if (!$isMultipleTypes) {
×
926
                    throw $e;
×
927
                }
928
            }
929
        }
930

931
        return $value;
12✔
932
    }
933

934
    /**
935
     * Sets a value of the object using the PropertyAccess component.
936
     */
937
    private function setValue(object $object, string $attributeName, mixed $value): void
938
    {
939
        try {
940
            $this->propertyAccessor->setValue($object, $attributeName, $value);
12✔
941
        } catch (NoSuchPropertyException) {
3✔
942
            // Properties not found are ignored
943
        }
944
    }
945
}
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