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

api-platform / core / 14854964964

06 May 2025 08:21AM UTC coverage: 6.873% (-1.6%) from 8.459%
14854964964

Pull #7122

github

web-flow
Merge c6177db2c into f55606b01
Pull Request #7122: Cherry picks from main (deprecations)

0 of 35 new or added lines in 9 files covered. (0.0%)

2031 existing lines in 148 files now uncovered.

10887 of 158407 relevant lines covered (6.87%)

6.19 hits per line

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

80.77
/src/State/Processor/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\State\Processor;
15

16
use ApiPlatform\Metadata\Operation;
17
use ApiPlatform\State\ProcessorInterface;
18
use ApiPlatform\State\ResourceList;
19
use ApiPlatform\State\SerializerContextBuilderInterface;
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
 * @template T1
30
 * @template T2
31
 *
32
 * @implements ProcessorInterface<T1, T2>
33
 *
34
 * @author Kévin Dunglas <dunglas@gmail.com>
35
 */
36
final class SerializeProcessor implements ProcessorInterface
37
{
38
    /**
39
     * @param ProcessorInterface<mixed, mixed>|null $processor
40
     */
41
    public function __construct(private readonly ?ProcessorInterface $processor, private readonly SerializerInterface $serializer, private readonly SerializerContextBuilderInterface $serializerContextBuilder)
42
    {
43
    }
416✔
44

45
    public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = [])
46
    {
47
        if ($data instanceof Response || !$operation->canSerialize() || !($request = $context['request'] ?? null)) {
413✔
48
            return $this->processor ? $this->processor->process($data, $operation, $uriVariables, $context) : $data;
13✔
49
        }
50

51
        // @see ApiPlatform\State\Processor\RespondProcessor
52
        $context['original_data'] = $data;
402✔
53

54
        $class = $operation->getClass();
402✔
55
        $serializerContext = $this->serializerContextBuilder->createFromRequest($request, true, [
402✔
56
            'resource_class' => $class,
402✔
57
            'operation' => $operation,
402✔
58
        ]);
402✔
59

60
        $serializerContext['uri_variables'] = $uriVariables;
402✔
61

62
        if (isset($serializerContext['output']) && \array_key_exists('class', $serializerContext['output']) && null === $serializerContext['output']['class']) {
402✔
UNCOV
63
            return $this->processor ? $this->processor->process(null, $operation, $uriVariables, $context) : null;
×
64
        }
65

66
        $resources = new ResourceList();
402✔
67
        $serializerContext['resources'] = &$resources;
402✔
68
        $serializerContext[AbstractObjectNormalizer::EXCLUDE_FROM_CACHE_KEY][] = 'resources';
402✔
69

70
        $resourcesToPush = new ResourceList();
402✔
71
        $serializerContext['resources_to_push'] = &$resourcesToPush;
402✔
72
        $serializerContext[AbstractObjectNormalizer::EXCLUDE_FROM_CACHE_KEY][] = 'resources_to_push';
402✔
73

74
        $serialized = $this->serializer->serialize($data, $request->getRequestFormat(), $serializerContext);
402✔
75
        $request->attributes->set('_resources', $request->attributes->get('_resources', []) + (array) $resources);
402✔
76
        if (\count($resourcesToPush)) {
402✔
77
            $linkProvider = $request->attributes->get('_api_platform_links', new GenericLinkProvider());
×
78
            foreach ($resourcesToPush as $resourceToPush) {
×
79
                $linkProvider = $linkProvider->withLink((new Link('preload', $resourceToPush))->withAttribute('as', 'fetch'));
×
80
            }
81
            $request->attributes->set('_api_platform_links', $linkProvider);
×
82
        }
83

84
        return $this->processor ? $this->processor->process($serialized, $operation, $uriVariables, $context) : $serialized;
402✔
85
    }
86
}
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