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

api-platform / core / 6250499992

20 Sep 2023 03:04PM UTC coverage: 36.863% (-0.2%) from 37.089%
6250499992

push

github

web-flow
fix: errors without compatibility flag (#5841)

24 of 24 new or added lines in 8 files covered. (100.0%)

10081 of 27347 relevant lines covered (36.86%)

13.36 hits per line

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

80.0
/src/Symfony/State/SerializeProcessor.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\State;
15

16
use ApiPlatform\Metadata\Operation;
17
use ApiPlatform\Serializer\ResourceList;
18
use ApiPlatform\Serializer\SerializerContextBuilderInterface;
19
use ApiPlatform\State\ProcessorInterface;
20
use Symfony\Component\HttpFoundation\Response;
21
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
22
use Symfony\Component\Serializer\SerializerInterface;
23
use Symfony\Component\WebLink\GenericLinkProvider;
24
use Symfony\Component\WebLink\Link;
25

26
/**
27
 * Serializes data.
28
 *
29
 * @author Kévin Dunglas <dunglas@gmail.com>
30
 */
31
final class SerializeProcessor implements ProcessorInterface
32
{
33
    public function __construct(private readonly ProcessorInterface $processor, private readonly SerializerInterface $serializer, private readonly SerializerContextBuilderInterface $serializerContextBuilder)
34
    {
35
    }
58✔
36

37
    public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = [])
38
    {
39
        if ($data instanceof Response || !$operation->canSerialize() || !($request = $context['request'] ?? null)) {
58✔
40
            return $this->processor->process($data, $operation, $uriVariables, $context);
9✔
41
        }
42

43
        // @see ApiPlatform\State\Processor\RespondProcessor
44
        $context['original_data'] = $data;
49✔
45

46
        $serializerContext = $this->serializerContextBuilder->createFromRequest($request, true, [
49✔
47
            'resource_class' => $operation->getClass(),
49✔
48
            'operation' => $operation,
49✔
49
        ]);
49✔
50

51
        $serializerContext['uri_variables'] = $uriVariables;
49✔
52

53
        if (isset($serializerContext['output']) && \array_key_exists('class', $serializerContext['output']) && null === $serializerContext['output']['class']) {
49✔
54
            return $this->processor->process(null, $operation, $uriVariables, $context);
×
55
        }
56

57
        $resources = new ResourceList();
49✔
58
        $serializerContext['resources'] = &$resources;
49✔
59
        $serializerContext[AbstractObjectNormalizer::EXCLUDE_FROM_CACHE_KEY][] = 'resources';
49✔
60

61
        $resourcesToPush = new ResourceList();
49✔
62
        $serializerContext['resources_to_push'] = &$resourcesToPush;
49✔
63
        $serializerContext[AbstractObjectNormalizer::EXCLUDE_FROM_CACHE_KEY][] = 'resources_to_push';
49✔
64

65
        $serialized = $this->serializer->serialize($data, $request->getRequestFormat(), $serializerContext);
49✔
66
        $request->attributes->set('_resources', $request->attributes->get('_resources', []) + (array) $resources);
49✔
67
        if (\count($resourcesToPush)) {
49✔
68
            $linkProvider = $request->attributes->get('_api_platform_links', new GenericLinkProvider());
×
69
            foreach ($resourcesToPush as $resourceToPush) {
×
70
                $linkProvider = $linkProvider->withLink((new Link('preload', $resourceToPush))->withAttribute('as', 'fetch'));
×
71
            }
72
            $request->attributes->set('_api_platform_links', $linkProvider);
×
73
        }
74

75
        return $this->processor->process($serialized, $operation, $uriVariables, $context);
49✔
76
    }
77
}
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