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

api-platform / core / 6054263056

01 Sep 2023 09:47PM UTC coverage: 58.794% (-0.09%) from 58.879%
6054263056

push

github

web-flow
fix(serializer): retrieve only first uriVariable from operation (#5788)

* fix(serializer): retrieve only first uriVariable from operation

* array default

---------

Co-authored-by: Antoine Bluchet <soyuka@users.noreply.github.com>

10 of 10 new or added lines in 1 file covered. (100.0%)

10861 of 18473 relevant lines covered (58.79%)

11.3 hits per line

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

42.86
/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\Api\IriConverterInterface;
17
use ApiPlatform\Api\ResourceClassResolverInterface;
18
use ApiPlatform\Api\UrlGeneratorInterface;
19
use ApiPlatform\Exception\InvalidArgumentException;
20
use ApiPlatform\Metadata\HttpOperation;
21
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
22
use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
23
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
24
use ApiPlatform\Symfony\Security\ResourceAccessCheckerInterface;
25
use Psr\Log\LoggerInterface;
26
use Psr\Log\NullLogger;
27
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
28
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
29
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
30
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
31

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

41
    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 = [])
42
    {
43
        parent::__construct($propertyNameCollectionFactory, $propertyMetadataFactory, $iriConverter, $resourceClassResolver, $propertyAccessor, $nameConverter, $classMetadataFactory, $defaultContext, $resourceMetadataFactory, $resourceAccessChecker);
78✔
44

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

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

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

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

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

90
            $context[self::OBJECT_TO_POPULATE] = $this->iriConverter->getResourceFromIri($iri, ['fetch_data' => true]);
×
91
        }
92
    }
93

94
    private function getContextUriVariables(array $data, $operation, array $context): array
95
    {
96
        $uriVariables = $context['uri_variables'] ?? [];
×
97

98
        $operationUriVariables = $operation->getUriVariables();
×
99
        if ((null !== $uriVariable = array_shift($operationUriVariables)) && \count($uriVariable->getIdentifiers())) {
×
100
            $identifier = $uriVariable->getIdentifiers()[0];
×
101
            if (isset($data[$identifier])) {
×
102
                $uriVariables[$uriVariable->getParameterName()] = $data[$identifier];
×
103
            }
104
        }
105

106
        return $uriVariables;
×
107
    }
108
}
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