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

api-platform / core / 17327553156

29 Aug 2025 03:13PM UTC coverage: 22.588% (-0.03%) from 22.622%
17327553156

push

github

web-flow
fix(laravel): serialization issue with camelCase relation (#7356)

fixes #7344

0 of 76 new or added lines in 9 files covered. (0.0%)

11415 existing lines in 372 files now uncovered.

11181 of 49499 relevant lines covered (22.59%)

23.68 hits per line

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

22.22
/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\Property\Factory\PropertyMetadataFactoryInterface;
20
use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
21
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
22
use ApiPlatform\Metadata\ResourceAccessCheckerInterface;
23
use ApiPlatform\Metadata\ResourceClassResolverInterface;
24
use ApiPlatform\Metadata\UrlGeneratorInterface;
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
 * TODO: do not hardcode "id"
36
 *
37
 * @author Kévin Dunglas <dunglas@gmail.com>
38
 */
39
class ItemNormalizer extends AbstractItemNormalizer
40
{
41
    private readonly LoggerInterface $logger;
42

43
    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)
44
    {
UNCOV
45
        parent::__construct($propertyNameCollectionFactory, $propertyMetadataFactory, $iriConverter, $resourceClassResolver, $propertyAccessor, $nameConverter, $classMetadataFactory, $defaultContext, $resourceMetadataFactory, $resourceAccessChecker, $tagCollector);
594✔
46

UNCOV
47
        $this->logger = $logger ?: new NullLogger();
594✔
48
    }
49

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

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

UNCOV
75
        return parent::denormalize($data, $class, $format, $context);
16✔
76
    }
77

78
    private function updateObjectToPopulate(array $data, array &$context): bool
79
    {
80
        try {
UNCOV
81
            $context[self::OBJECT_TO_POPULATE] = $this->iriConverter->getResourceFromIri((string) $data['id'], $context + ['fetch_data' => true]);
×
82

UNCOV
83
            return true;
×
UNCOV
84
        } catch (InvalidArgumentException) {
×
UNCOV
85
            $operation = $this->resourceMetadataCollectionFactory?->create($context['resource_class'])->getOperation();
×
86
            if (
UNCOV
87
                !$operation || (
×
88
                    null !== ($context['uri_variables'] ?? null)
×
89
                    && $operation instanceof HttpOperation
×
90
                    && \count($operation->getUriVariables() ?? []) > 1
×
91
                )
92
            ) {
93
                throw new InvalidArgumentException('Cannot find object to populate, use JSON-LD or specify an IRI at path "id".');
×
94
            }
95
            $uriVariables = $this->getContextUriVariables($data, $operation, $context);
×
UNCOV
96
            $iri = $this->iriConverter->getIriFromResource($context['resource_class'], UrlGeneratorInterface::ABS_PATH, $operation, ['uri_variables' => $uriVariables]);
×
97

98
            $context[self::OBJECT_TO_POPULATE] = $this->iriConverter->getResourceFromIri($iri, $context + ['fetch_data' => true]);
×
99
        }
100

101
        return false;
×
102
    }
103

104
    private function getContextUriVariables(array $data, $operation, array $context): array
105
    {
UNCOV
106
        $uriVariables = $context['uri_variables'] ?? [];
×
107

UNCOV
108
        $operationUriVariables = $operation->getUriVariables();
×
109
        if ((null !== $uriVariable = array_shift($operationUriVariables)) && \count($uriVariable->getIdentifiers())) {
×
UNCOV
110
            $identifier = $uriVariable->getIdentifiers()[0];
×
111
            if (isset($data[$identifier])) {
×
112
                $uriVariables[$uriVariable->getParameterName()] = $data[$identifier];
×
113
            }
114
        }
115

UNCOV
116
        return $uriVariables;
×
117
    }
118

119
    protected function getAllowedAttributes(string|object $classOrObject, array $context, bool $attributesAsString = false): array|bool
120
    {
UNCOV
121
        $allowedAttributes = parent::getAllowedAttributes($classOrObject, $context, $attributesAsString);
58✔
122
        // id is a special case handled above it causes issues not allowing it
UNCOV
123
        if (\is_array($allowedAttributes) && ($context['api_denormalize'] ?? false) && !\in_array('id', $allowedAttributes, true)) {
58✔
UNCOV
124
            $allowedAttributes[] = 'id';
14✔
125
        }
126

UNCOV
127
        return $allowedAttributes;
58✔
128
    }
129
}
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