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

api-platform / core / 14969517179

12 May 2025 10:08AM UTC coverage: 7.243% (-1.2%) from 8.418%
14969517179

push

github

web-flow
feat(serializer): (un)set object-to-populate through denormalization context (#7124)

14 of 95 new or added lines in 4 files covered. (14.74%)

1 existing line in 1 file now uncovered.

11006 of 151963 relevant lines covered (7.24%)

6.55 hits per line

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

100.0
/src/Symfony/EventListener/DeserializeListener.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\Symfony\EventListener;
15

16
use ApiPlatform\Metadata\HttpOperation;
17
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
18
use ApiPlatform\State\ProviderInterface;
19
use ApiPlatform\State\SerializerContextBuilderInterface;
20
use ApiPlatform\State\Util\OperationRequestInitiatorTrait;
21
use ApiPlatform\State\Util\RequestAttributesExtractor;
22
use Symfony\Component\HttpKernel\Event\RequestEvent;
23
use Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException;
24

25
/**
26
 * Updates the entity retrieved by the data provider with data contained in the request body.
27
 *
28
 * @author Kévin Dunglas <dunglas@gmail.com>
29
 */
30
final class DeserializeListener
31
{
32
    use OperationRequestInitiatorTrait;
33

34
    public const OPERATION_ATTRIBUTE_KEY = 'deserialize';
35

36
    /**
37
     * @param ProviderInterface<object> $provider
38
     */
39
    public function __construct(
40
        private ProviderInterface $provider,
41
        ?ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory = null,
42
    ) {
43
        $this->resourceMetadataCollectionFactory = $resourceMetadataFactory;
225✔
44
    }
45

46
    /**
47
     * Deserializes the data sent in the requested format.
48
     *
49
     * @throws UnsupportedMediaTypeHttpException
50
     */
51
    public function onKernelRequest(RequestEvent $event): void
52
    {
53
        $request = $event->getRequest();
223✔
54
        $method = $request->getMethod();
223✔
55
        $operation = $this->initializeOperation($request);
223✔
56

57
        if (
58
            !($attributes = RequestAttributesExtractor::extractAttributes($request))
223✔
59
            || !$attributes['receive']
223✔
60
            || !$operation
223✔
61
        ) {
62
            return;
29✔
63
        }
64

65
        if (null === $operation->canDeserialize() && $operation instanceof HttpOperation) {
196✔
66
            $operation = $operation->withDeserialize(\in_array($method, ['POST', 'PUT', 'PATCH'], true));
196✔
67
        }
68

NEW
69
        $denormalizationContext = $operation->getDenormalizationContext() ?? [];
196✔
NEW
70
        if ($operation->canDeserialize() && !isset($denormalizationContext[SerializerContextBuilderInterface::ASSIGN_OBJECT_TO_POPULATE])) {
196✔
NEW
71
            $method = $operation->getMethod();
6✔
NEW
72
            $assignObjectToPopulate = 'POST' === $method
6✔
NEW
73
                || 'PATCH' === $method
6✔
NEW
74
                || ('PUT' === $method && !($operation->getExtraProperties()['standard_put'] ?? true));
6✔
75

NEW
76
            $operation = $operation->withDenormalizationContext($denormalizationContext + [SerializerContextBuilderInterface::ASSIGN_OBJECT_TO_POPULATE => $assignObjectToPopulate]);
6✔
77
        }
78

79
        if (!$operation->canDeserialize()) {
196✔
80
            return;
192✔
81
        }
82

83
        $data = $this->provider->provide($operation, $request->attributes->get('_api_uri_variables') ?? [], [
6✔
84
            'request' => $request,
6✔
85
            'uri_variables' => $request->attributes->get('_api_uri_variables') ?? [],
6✔
86
            'resource_class' => $operation->getClass(),
6✔
87
        ]);
6✔
88

89
        $request->attributes->set('data', $data);
5✔
90
    }
91
}
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