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

api-platform / core / 6067528200

04 Sep 2023 12:12AM UTC coverage: 36.875% (-21.9%) from 58.794%
6067528200

Pull #5791

github

web-flow
Merge 64157e578 into d09cfc9d2
Pull Request #5791: fix: strip down any sql function name

3096 of 3096 new or added lines in 205 files covered. (100.0%)

9926 of 26918 relevant lines covered (36.87%)

6.5 hits per line

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

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

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

43
    public const OPERATION_ATTRIBUTE_KEY = 'serialize';
44

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

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

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

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

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

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

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

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

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

86
            return;
3✔
87
        }
88

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

93
            return;
3✔
94
        }
95

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

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

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

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

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

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

133
        $request->attributes->set('_api_normalization_context', $context);
20✔
134
        $event->setControllerResult($this->serializer->serialize($controllerResult, $request->getRequestFormat(), $context));
20✔
135

136
        $request->attributes->set('_resources', $request->attributes->get('_resources', []) + (array) $resources);
20✔
137
        if (!\count($resourcesToPush)) {
20✔
138
            return;
20✔
139
        }
140

141
        $linkProvider = $request->attributes->get('_links', new GenericLinkProvider());
×
142
        foreach ($resourcesToPush as $resourceToPush) {
×
143
            $linkProvider = $linkProvider->withLink((new Link('preload', $resourceToPush))->withAttribute('as', 'fetch'));
×
144
        }
145
        $request->attributes->set('_links', $linkProvider);
×
146
    }
147

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

158
            return;
×
159
        }
160

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

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

© 2026 Coveralls, Inc