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

api-platform / core / 17054069864

18 Aug 2025 10:27PM UTC coverage: 21.952% (+0.2%) from 21.769%
17054069864

Pull #7151

github

web-flow
Merge 0da010d8d into 6491bfc7a
Pull Request #7151: fix: 7119 parameter array shape uses invalid syntax

11524 of 52497 relevant lines covered (21.95%)

11.86 hits per line

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

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

16
use ApiPlatform\Metadata\Exception\InvalidArgumentException;
17
use ApiPlatform\Metadata\HttpOperation;
18
use ApiPlatform\Metadata\IriConverterInterface;
19
use ApiPlatform\Metadata\Operation;
20
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
21
use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
22
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
23
use ApiPlatform\Metadata\ResourceAccessCheckerInterface;
24
use ApiPlatform\Metadata\ResourceClassResolverInterface;
25
use ApiPlatform\Metadata\UrlGeneratorInterface;
26
use Psr\Log\LoggerInterface;
27
use Psr\Log\NullLogger;
28
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
29
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
30
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
31
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
32

33
/**
34
 * Generic item normalizer.
35
 *
36
 * @author Kévin Dunglas <dunglas@gmail.com>
37
 */
38
class ItemNormalizer extends AbstractItemNormalizer
39
{
40
    private readonly LoggerInterface $logger;
41

42
    public function __construct(PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, IriConverterInterface $iriConverter, ResourceClassResolverInterface $resourceClassResolver, ?PropertyAccessorInterface $propertyAccessor = null, ?NameConverterInterface $nameConverter = null, ?ClassMetadataFactoryInterface $classMetadataFactory = null, ?LoggerInterface $logger = null, ?ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory = null, ?ResourceAccessCheckerInterface $resourceAccessChecker = null, array $defaultContext = [], protected ?TagCollectorInterface $tagCollector = null)
43
    {
44
        parent::__construct($propertyNameCollectionFactory, $propertyMetadataFactory, $iriConverter, $resourceClassResolver, $propertyAccessor, $nameConverter, $classMetadataFactory, $defaultContext, $resourceMetadataFactory, $resourceAccessChecker, $tagCollector);
302✔
45

46
        $this->logger = $logger ?: new NullLogger();
302✔
47
    }
48

49
    /**
50
     * {@inheritdoc}
51
     *
52
     * @throws NotNormalizableValueException
53
     */
54
    public function denormalize(mixed $data, string $class, ?string $format = null, array $context = []): mixed
55
    {
56
        // Avoid issues with proxies if we populated the object
57
        if (isset($data['id']) && !isset($context[self::OBJECT_TO_POPULATE])) {
8✔
58
            if (isset($context['api_allow_update']) && true !== $context['api_allow_update']) {
×
59
                throw new NotNormalizableValueException('Update is not allowed for this operation.');
×
60
            }
61

62
            if (isset($context['resource_class'])) {
×
63
                $this->updateObjectToPopulate($data, $context);
×
64
            } else {
65
                // See https://github.com/api-platform/core/pull/2326 to understand this message.
66
                $this->logger->warning('The "resource_class" key is missing from the context.', [
×
67
                    'context' => $context,
×
68
                ]);
×
69
            }
70
        }
71

72
        return parent::denormalize($data, $class, $format, $context);
8✔
73
    }
74

75
    private function updateObjectToPopulate(array $data, array &$context): void
76
    {
77
        try {
78
            $context[self::OBJECT_TO_POPULATE] = $this->iriConverter->getResourceFromIri((string) $data['id'], $context + ['fetch_data' => true]);
×
79
        } catch (InvalidArgumentException) {
×
80
            $operation = $this->resourceMetadataCollectionFactory?->create($context['resource_class'])->getOperation();
×
81
            if (
82
                !$operation || (
×
83
                    null !== ($context['uri_variables'] ?? null)
×
84
                    && $operation instanceof HttpOperation
×
85
                    && \count($operation->getUriVariables() ?? []) > 1
×
86
                )
87
            ) {
88
                throw new InvalidArgumentException('Cannot find object to populate, use JSON-LD or specify an IRI at path "id".');
×
89
            }
90
            $uriVariables = $this->getContextUriVariables($data, $operation, $context);
×
91
            $iri = $this->iriConverter->getIriFromResource($context['resource_class'], UrlGeneratorInterface::ABS_PATH, $operation, ['uri_variables' => $uriVariables]);
×
92

93
            $context[self::OBJECT_TO_POPULATE] = $this->iriConverter->getResourceFromIri($iri, $context + ['fetch_data' => true]);
×
94
        }
95
    }
96

97
    private function getContextUriVariables(array $data, Operation $operation, array $context): array
98
    {
99
        $uriVariables = $context['uri_variables'] ?? [];
×
100

101
        if ($operation instanceof HttpOperation) {
×
102
            $operationUriVariables = $operation->getUriVariables();
×
103
            if ((null !== $uriVariable = array_shift($operationUriVariables)) && \count($uriVariable->getIdentifiers())) {
×
104
                $identifier = $uriVariable->getIdentifiers()[0];
×
105
                if (isset($data[$identifier])) {
×
106
                    $uriVariables[$uriVariable->getParameterName()] = $data[$identifier];
×
107
                }
108
            }
109
        }
110

111
        return $uriVariables;
×
112
    }
113
}
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