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

api-platform / core / 14635100171

24 Apr 2025 06:39AM UTC coverage: 8.271% (+0.02%) from 8.252%
14635100171

Pull #6904

github

web-flow
Merge c9cefd82e into a3e5e53ea
Pull Request #6904: feat(graphql): added support for graphql subscriptions to work for actions

0 of 73 new or added lines in 3 files covered. (0.0%)

1999 existing lines in 144 files now uncovered.

13129 of 158728 relevant lines covered (8.27%)

13.6 hits per line

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

82.76
/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
 * @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 = [], protected ?TagCollectorInterface $tagCollector = null)
42
    {
43
        parent::__construct($propertyNameCollectionFactory, $propertyMetadataFactory, $iriConverter, $resourceClassResolver, $propertyAccessor, $nameConverter, $classMetadataFactory, $defaultContext, $resourceMetadataFactory, $resourceAccessChecker, $tagCollector);
1,160✔
44

45
        $this->logger = $logger ?: new NullLogger();
1,160✔
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])) {
94✔
UNCOV
57
            if (isset($context['api_allow_update']) && true !== $context['api_allow_update']) {
4✔
58
                throw new NotNormalizableValueException('Update is not allowed for this operation.');
×
59
            }
60

UNCOV
61
            if (isset($context['resource_class'])) {
4✔
UNCOV
62
                $this->updateObjectToPopulate($data, $context);
4✔
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.', [
×
66
                    'context' => $context,
×
67
                ]);
×
68
            }
69
        }
70

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

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

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

96
    private function getContextUriVariables(array $data, $operation, array $context): array
97
    {
UNCOV
98
        $uriVariables = $context['uri_variables'] ?? [];
2✔
99

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

UNCOV
108
        return $uriVariables;
2✔
109
    }
110
}
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