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

api-platform / core / 17562281609

08 Sep 2025 07:47PM UTC coverage: 0.0% (-22.6%) from 22.604%
17562281609

Pull #7374

github

web-flow
Merge 0c2b4a90a into 6db55be8c
Pull Request #7374: fix(jsonld): various json streamer fixes

0 of 33 new or added lines in 2 files covered. (0.0%)

12082 existing lines in 401 files now uncovered.

0 of 52792 relevant lines covered (0.0%)

0.0 hits per line

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

0.0
/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
 * TODO: do not hardcode "id"
37
 *
38
 * @author Kévin Dunglas <dunglas@gmail.com>
39
 */
40
class ItemNormalizer extends AbstractItemNormalizer
41
{
42
    private readonly LoggerInterface $logger;
43

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

UNCOV
48
        $this->logger = $logger ?: new NullLogger();
×
49
    }
50

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

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

UNCOV
76
        return parent::denormalize($data, $class, $format, $context);
×
77
    }
78

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

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

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

102
        return false;
×
103
    }
104

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

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

119
        return $uriVariables;
×
120
    }
121

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

UNCOV
130
        return $allowedAttributes;
×
131
    }
132
}
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