• 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

62.3
/src/Symfony/EventListener/SerializeListener.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\Doctrine\Odm\State\Options as ODMOptions;
17
use ApiPlatform\Doctrine\Orm\State\Options;
18
use ApiPlatform\Exception\RuntimeException;
19
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
20
use ApiPlatform\Serializer\ResourceList;
21
use ApiPlatform\Serializer\SerializerContextBuilderInterface;
22
use ApiPlatform\State\Util\OperationRequestInitiatorTrait;
23
use ApiPlatform\Symfony\Util\RequestAttributesExtractor;
24
use ApiPlatform\Util\ErrorFormatGuesser;
25
use ApiPlatform\Validator\Exception\ValidationException;
26
use Symfony\Component\HttpFoundation\Request;
27
use Symfony\Component\HttpFoundation\Response;
28
use Symfony\Component\HttpKernel\Event\ViewEvent;
29
use Symfony\Component\Serializer\Encoder\EncoderInterface;
30
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
31
use Symfony\Component\Serializer\SerializerInterface;
32
use Symfony\Component\WebLink\GenericLinkProvider;
33
use Symfony\Component\WebLink\Link;
34

35
/**
36
 * Serializes data.
37
 *
38
 * @author Kévin Dunglas <dunglas@gmail.com>
39
 */
40
final class SerializeListener
41
{
42
    use OperationRequestInitiatorTrait;
43

44
    public const OPERATION_ATTRIBUTE_KEY = 'serialize';
45

46
    public function __construct(
47
        private readonly SerializerInterface $serializer,
48
        private readonly SerializerContextBuilderInterface $serializerContextBuilder,
49
        ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory = null,
50
        private readonly array $errorFormats = [],
51
        private readonly bool $debug = false,
52
    ) {
53
        $this->resourceMetadataCollectionFactory = $resourceMetadataFactory;
35✔
54
    }
55

56
    /**
57
     * Serializes the data to the requested format.
58
     */
59
    public function onKernelView(ViewEvent $event): void
60
    {
61
        $controllerResult = $event->getControllerResult();
35✔
62
        $request = $event->getRequest();
35✔
63

64
        if ($controllerResult instanceof Response) {
35✔
65
            return;
×
66
        }
67

68
        $attributes = RequestAttributesExtractor::extractAttributes($request);
35✔
69

70
        if (!($attributes['respond'] ?? $request->attributes->getBoolean('_api_respond', false))) {
35✔
71
            return;
6✔
72
        }
73

74
        $operation = $this->initializeOperation($request);
29✔
75

76
        if ('api_platform.symfony.main_controller' === $operation?->getController()) {
29✔
77
            return;
×
78
        }
79

80
        if (!($operation?->canSerialize() ?? true)) {
29✔
81
            return;
3✔
82
        }
83

84
        if (!$attributes) {
26✔
85
            $this->serializeRawData($event, $request, $controllerResult);
3✔
86

87
            return;
3✔
88
        }
89

90
        $context = $this->serializerContextBuilder->createFromRequest($request, true, $attributes);
23✔
91
        if (isset($context['output']) && \array_key_exists('class', $context['output']) && null === $context['output']['class']) {
23✔
92
            $event->setControllerResult(null);
3✔
93

94
            return;
3✔
95
        }
96

97
        if ($controllerResult instanceof ValidationException) {
20✔
98
            $format = ErrorFormatGuesser::guessErrorFormat($request, $this->errorFormats);
×
99
            $previousOperation = $request->attributes->get('_api_previous_operation');
×
100
            if (!($previousOperation?->getExtraProperties()['rfc_7807_compliant_errors'] ?? false)) {
×
101
                $context['groups'] = ['legacy_'.$format['key']];
×
102
                $context['force_iri_generation'] = false;
×
103
            }
104
        }
105

106
        if ($request->get('_api_error', false)) {
20✔
107
            $context['skip_deprecated_exception_normalizers'] = true;
×
108

109
            if ($this->debug) {
×
110
                $groups = $context['groups'] ?? [];
×
111
                if (!\is_array($groups)) {
×
112
                    $groups = [$groups];
×
113
                }
114

115
                $groups[] = 'trace';
×
116
                $context['groups'] = $groups;
×
117
            }
118
        }
119

120
        if ($included = $request->attributes->get('_api_included')) {
20✔
121
            $context['api_included'] = $included;
×
122
        }
123
        $resources = new ResourceList();
20✔
124
        $context['resources'] = &$resources;
20✔
125
        $context[AbstractObjectNormalizer::EXCLUDE_FROM_CACHE_KEY][] = 'resources';
20✔
126

127
        $resourcesToPush = new ResourceList();
20✔
128
        $context['resources_to_push'] = &$resourcesToPush;
20✔
129
        $context[AbstractObjectNormalizer::EXCLUDE_FROM_CACHE_KEY][] = 'resources_to_push';
20✔
130
        if (($options = $operation?->getStateOptions()) && (
20✔
131
            ($options instanceof Options && $options->getEntityClass())
20✔
132
            || ($options instanceof ODMOptions && $options->getDocumentClass())
20✔
133
        )) {
134
            $context['force_resource_class'] = $operation->getClass();
×
135
        }
136

137
        $request->attributes->set('_api_normalization_context', $context);
20✔
138
        $event->setControllerResult($this->serializer->serialize($controllerResult, $request->getRequestFormat(), $context));
20✔
139

140
        $request->attributes->set('_resources', $request->attributes->get('_resources', []) + (array) $resources);
20✔
141
        if (!\count($resourcesToPush)) {
20✔
142
            return;
20✔
143
        }
144

145
        $linkProvider = $request->attributes->get('_api_platform_links', new GenericLinkProvider());
×
146
        foreach ($resourcesToPush as $resourceToPush) {
×
147
            $linkProvider = $linkProvider->withLink((new Link('preload', $resourceToPush))->withAttribute('as', 'fetch'));
×
148
        }
149
        $request->attributes->set('_api_platform_links', $linkProvider);
×
150
    }
151

152
    /**
153
     * Tries to serialize data that are not API resources (e.g. the entrypoint or data returned by a custom controller).
154
     *
155
     * @throws RuntimeException
156
     */
157
    private function serializeRawData(ViewEvent $event, Request $request, $controllerResult): void
158
    {
159
        if (\is_object($controllerResult)) {
3✔
160
            $event->setControllerResult($this->serializer->serialize($controllerResult, $request->getRequestFormat(), $request->attributes->get('_api_normalization_context', [])));
×
161

162
            return;
×
163
        }
164

165
        if (!$this->serializer instanceof EncoderInterface) {
3✔
166
            throw new RuntimeException(sprintf('The serializer must implement the "%s" interface.', EncoderInterface::class));
×
167
        }
168

169
        $event->setControllerResult($this->serializer->encode($controllerResult, $request->getRequestFormat()));
3✔
170
    }
171
}
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