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

api-platform / core / 3713134090

pending completion
3713134090

Pull #5254

github

GitHub
Merge b2ec54b3c into ac711530f
Pull Request #5254: [OpenApi] Add ApiResource::openapi and deprecate openapiContext

197 of 197 new or added lines in 5 files covered. (100.0%)

10372 of 12438 relevant lines covered (83.39%)

11.97 hits per line

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

94.44
/src/Symfony/EventListener/RespondListener.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\Api\IriConverterInterface;
17
use ApiPlatform\Api\UrlGeneratorInterface;
18
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
19
use ApiPlatform\Util\OperationRequestInitiatorTrait;
20
use ApiPlatform\Util\RequestAttributesExtractor;
21
use Symfony\Component\HttpFoundation\Response;
22
use Symfony\Component\HttpKernel\Event\ViewEvent;
23

24
/**
25
 * Builds the response object.
26
 *
27
 * @author Kévin Dunglas <dunglas@gmail.com>
28
 */
29
final class RespondListener
30
{
31
    use OperationRequestInitiatorTrait;
32

33
    public const METHOD_TO_CODE = [
34
        'POST' => Response::HTTP_CREATED,
35
        'DELETE' => Response::HTTP_NO_CONTENT,
36
    ];
37

38
    public function __construct(ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory = null, private readonly ?IriConverterInterface $iriConverter = null)
39
    {
40
        $this->resourceMetadataCollectionFactory = $resourceMetadataFactory;
32✔
41
    }
42

43
    /**
44
     * Creates a Response to send to the client according to the requested format.
45
     */
46
    public function onKernelView(ViewEvent $event): void
47
    {
48
        $controllerResult = $event->getControllerResult();
32✔
49
        $request = $event->getRequest();
32✔
50
        $operation = $this->initializeOperation($request);
32✔
51

52
        $attributes = RequestAttributesExtractor::extractAttributes($request);
32✔
53

54
        if ($controllerResult instanceof Response && ($attributes['respond'] ?? false)) {
32✔
55
            $event->setResponse($controllerResult);
1✔
56

57
            return;
1✔
58
        }
59

60
        if ($controllerResult instanceof Response || !($attributes['respond'] ?? $request->attributes->getBoolean('_api_respond'))) {
31✔
61
            return;
2✔
62
        }
63

64
        $headers = [
29✔
65
            'Content-Type' => sprintf('%s; charset=utf-8', $request->getMimeType($request->getRequestFormat())),
29✔
66
            'Vary' => 'Accept',
29✔
67
            'X-Content-Type-Options' => 'nosniff',
29✔
68
            'X-Frame-Options' => 'deny',
29✔
69
        ];
29✔
70

71
        $status = $operation?->getStatus();
29✔
72

73
        if ($sunset = $operation?->getSunset()) {
29✔
74
            $headers['Sunset'] = (new \DateTimeImmutable($sunset))->format(\DateTime::RFC1123);
1✔
75
        }
76

77
        if ($acceptPatch = $operation?->getAcceptPatch()) {
29✔
78
            $headers['Accept-Patch'] = $acceptPatch;
4✔
79
        }
80

81
        if (
82
            $this->iriConverter &&
29✔
83
            $operation &&
84
            ($operation->getExtraProperties()['is_alternate_resource_metadata'] ?? false)
29✔
85
            && 301 === $operation->getStatus()
29✔
86
        ) {
87
            $status = 301;
×
88
            $headers['Location'] = $this->iriConverter->getIriFromResource($request->attributes->get('data'), UrlGeneratorInterface::ABS_PATH, $operation);
×
89
        }
90

91
        $status ??= self::METHOD_TO_CODE[$request->getMethod()] ?? Response::HTTP_OK;
29✔
92

93
        if ($request->attributes->has('_api_write_item_iri')) {
29✔
94
            $headers['Content-Location'] = $request->attributes->get('_api_write_item_iri');
6✔
95

96
            if ((Response::HTTP_CREATED === $status || (300 <= $status && $status < 400)) && $request->isMethod('POST')) {
6✔
97
                $headers['Location'] = $request->attributes->get('_api_write_item_iri');
5✔
98
            }
99
        }
100

101
        $event->setResponse(new Response(
29✔
102
            $controllerResult,
29✔
103
            $status,
29✔
104
            $headers
29✔
105
        ));
29✔
106
    }
107
}
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