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

api-platform / core / 3711198772

pending completion
3711198772

push

github

GitHub
fix(jsonschema): remove @id @type @context from input schema  (#5267)

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

7447 of 12203 relevant lines covered (61.03%)

82.13 hits per line

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

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

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

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

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

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

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

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

73
    private function updateObjectToPopulate(array $data, array &$context): void
74
    {
75
        try {
76
            $context[self::OBJECT_TO_POPULATE] = $this->iriConverter->getResourceFromIri((string) $data['id'], $context + ['fetch_data' => true]);
5✔
77
        } catch (InvalidArgumentException) {
2✔
78
            $operation = $this->resourceMetadataCollectionFactory->create($context['resource_class'])->getOperation();
2✔
79
            // todo: we could guess uri variables with the operation and the data instead of hardcoding id
80
            $iri = $this->iriConverter->getIriFromResource($context['resource_class'], UrlGeneratorInterface::ABS_PATH, $operation, ['uri_variables' => ['id' => $data['id']]]);
2✔
81

82
            $context[self::OBJECT_TO_POPULATE] = $this->iriConverter->getResourceFromIri($iri, ['fetch_data' => true]);
2✔
83
        }
84
    }
85
}
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