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

api-platform / core / 13724522058

07 Mar 2025 04:04PM UTC coverage: 8.175% (-0.3%) from 8.518%
13724522058

Pull #7005

github

web-flow
Merge 322407532 into 1e0bc9dc8
Pull Request #7005: fix(validation): deprecate string message for ValidationException con…

4 of 6 new or added lines in 1 file covered. (66.67%)

159 existing lines in 24 files now uncovered.

12839 of 157045 relevant lines covered (8.18%)

13.55 hits per line

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

84.62
/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
    }
981✔
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)) {
974✔
48
            return $this->processor ? $this->processor->process($data, $operation, $uriVariables, $context) : $data;
24✔
49
        }
50

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

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

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

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

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

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

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

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