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

api-platform / core / 17487610263

05 Sep 2025 08:12AM UTC coverage: 22.608% (+0.3%) from 22.319%
17487610263

push

github

web-flow
chore: remove @experimental flag from parameters (#7357)

12049 of 53296 relevant lines covered (22.61%)

26.21 hits per line

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

95.83
/src/State/Util/HttpResponseHeadersTrait.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\Util;
15

16
use ApiPlatform\Metadata\Exception\HttpExceptionInterface;
17
use ApiPlatform\Metadata\Exception\InvalidArgumentException;
18
use ApiPlatform\Metadata\Exception\ItemNotFoundException;
19
use ApiPlatform\Metadata\Exception\RuntimeException;
20
use ApiPlatform\Metadata\HttpOperation;
21
use ApiPlatform\Metadata\IriConverterInterface;
22
use ApiPlatform\Metadata\Operation\Factory\OperationMetadataFactoryInterface;
23
use ApiPlatform\Metadata\UrlGeneratorInterface;
24
use ApiPlatform\Metadata\Util\ClassInfoTrait;
25
use ApiPlatform\Metadata\Util\CloneTrait;
26
use Symfony\Component\HttpFoundation\Request;
27
use Symfony\Component\HttpFoundation\Response;
28
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface as SymfonyHttpExceptionInterface;
29

30
/**
31
 * Shares the logic to create API Platform's headers.
32
 *
33
 * @internal
34
 */
35
trait HttpResponseHeadersTrait
36
{
37
    use ClassInfoTrait;
38
    use CloneTrait;
39
    private ?IriConverterInterface $iriConverter;
40
    private ?OperationMetadataFactoryInterface $operationMetadataFactory;
41

42
    /**
43
     * @param array<string, mixed> $context
44
     *
45
     * @return array<string, string|string[]>
46
     */
47
    private function getHeaders(Request $request, HttpOperation $operation, array $context): array
48
    {
49
        $status = $this->getStatus($request, $operation, $context);
562✔
50
        $headers = [
562✔
51
            'Content-Type' => \sprintf('%s; charset=utf-8', $request->getMimeType($request->getRequestFormat())),
562✔
52
            'Vary' => 'Accept',
562✔
53
            'X-Content-Type-Options' => 'nosniff',
562✔
54
            'X-Frame-Options' => 'deny',
562✔
55
        ];
562✔
56

57
        $exception = $request->attributes->get('exception');
562✔
58
        if (($exception instanceof HttpExceptionInterface || $exception instanceof SymfonyHttpExceptionInterface) && $exceptionHeaders = $exception->getHeaders()) {
562✔
59
            $headers = array_merge($headers, $exceptionHeaders);
2✔
60
        }
61

62
        if ($operationHeaders = $operation->getHeaders()) {
562✔
63
            $headers = array_merge($headers, $operationHeaders);
2✔
64
        }
65

66
        if ($sunset = $operation->getSunset()) {
562✔
67
            $headers['Sunset'] = (new \DateTimeImmutable($sunset))->format(\DateTimeInterface::RFC1123);
×
68
        }
69

70
        if ($acceptPatch = $operation->getAcceptPatch()) {
562✔
71
            $headers['Accept-Patch'] = $acceptPatch;
32✔
72
        }
73

74
        $method = $request->getMethod();
562✔
75
        $originalData = $context['original_data'] ?? null;
562✔
76
        $outputMetadata = $operation->getOutput() ?? ['class' => $operation->getClass()];
562✔
77
        $hasOutput = \is_array($outputMetadata) && \array_key_exists('class', $outputMetadata) && null !== $outputMetadata['class'];
562✔
78
        $hasData = !$hasOutput ? false : ($this->resourceClassResolver && $originalData && \is_object($originalData) && $this->resourceClassResolver->isResourceClass($this->getObjectClass($originalData)));
562✔
79

80
        if ($hasData) {
562✔
81
            $isAlternateResourceMetadata = $operation->getExtraProperties()['is_alternate_resource_metadata'] ?? false;
196✔
82
            $canonicalUriTemplate = $operation->getExtraProperties()['canonical_uri_template'] ?? null;
196✔
83

84
            if (
85
                !isset($headers['Location'])
196✔
86
                && 300 <= $status && $status < 400
196✔
87
                && ($isAlternateResourceMetadata || $canonicalUriTemplate)
196✔
88
            ) {
89
                $canonicalOperation = $operation;
2✔
90
                if ($this->operationMetadataFactory && null !== $canonicalUriTemplate) {
2✔
91
                    $canonicalOperation = $this->operationMetadataFactory->create($canonicalUriTemplate, $context);
2✔
92
                }
93

94
                if ($this->iriConverter) {
2✔
95
                    $headers['Location'] = $this->iriConverter->getIriFromResource($originalData, UrlGeneratorInterface::ABS_PATH, $canonicalOperation);
2✔
96
                }
97
            }
98
        }
99

100
        $requestParts = parse_url($request->getRequestUri());
562✔
101
        if ($this->iriConverter && !isset($headers['Content-Location'])) {
562✔
102
            try {
103
                $iri = null;
558✔
104
                if ($hasData) {
558✔
105
                    $iri = $this->iriConverter->getIriFromResource($originalData);
196✔
106
                } elseif ($operation->getClass()) {
383✔
107
                    $iri = $this->iriConverter->getIriFromResource($operation->getClass(), UrlGeneratorInterface::ABS_PATH, $operation);
377✔
108
                }
109

110
                if ($iri && 'GET' !== $method) {
512✔
111
                    $location = \sprintf('%s.%s', $iri, $request->getRequestFormat());
18✔
112
                    if (isset($requestParts['query'])) {
18✔
113
                        $location .= '?'.$requestParts['query'];
×
114
                    }
115

116
                    $headers['Content-Location'] = $location;
18✔
117
                    if ((Response::HTTP_CREATED === $status || (300 <= $status && $status < 400)) && 'POST' === $method && !isset($headers['Location'])) {
18✔
118
                        $headers['Location'] = $iri;
512✔
119
                    }
120
                }
121
            } catch (InvalidArgumentException|ItemNotFoundException|RuntimeException) {
47✔
122
            }
123
        }
124

125
        return $headers;
562✔
126
    }
127
}
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