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

api-platform / core / 20745180982

06 Jan 2026 10:14AM UTC coverage: 28.884% (+0.04%) from 28.843%
20745180982

Pull #7647

github

web-flow
Merge cc84ab376 into d23ab4301
Pull Request #7647: fix(doctrine): fix partial fetch with same entity included multiple time with different fields

31 of 116 new or added lines in 2 files covered. (26.72%)

378 existing lines in 45 files now uncovered.

16832 of 58274 relevant lines covered (28.88%)

78.61 hits per line

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

100.0
/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);
1,910✔
50
        $headers = [
1,910✔
51
            'Content-Type' => \sprintf('%s; charset=utf-8', $request->getMimeType($request->getRequestFormat())),
1,910✔
52
            'Vary' => 'Accept',
1,910✔
53
            'X-Content-Type-Options' => 'nosniff',
1,910✔
54
            'X-Frame-Options' => 'deny',
1,910✔
55
        ];
1,910✔
56

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

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

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

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

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

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

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

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

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

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

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

125
        return $headers;
1,910✔
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

© 2026 Coveralls, Inc