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

api-platform / core / 18118486316

30 Sep 2025 04:11AM UTC coverage: 0.0% (-22.0%) from 21.956%
18118486316

Pull #7397

github

web-flow
Merge e92aeff57 into 55fd65795
Pull Request #7397: fix(jsonschema/jsonld): make `@id` and `@type` properties required only in the JSON-LD schema for output

0 of 15 new or added lines in 1 file covered. (0.0%)

12143 existing lines in 402 files now uncovered.

0 of 53916 relevant lines covered (0.0%)

0.0 hits per line

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

0.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
    {
UNCOV
49
        $status = $this->getStatus($request, $operation, $context);
×
UNCOV
50
        $headers = [
×
UNCOV
51
            'Content-Type' => \sprintf('%s; charset=utf-8', $request->getMimeType($request->getRequestFormat())),
×
UNCOV
52
            'Vary' => 'Accept',
×
UNCOV
53
            'X-Content-Type-Options' => 'nosniff',
×
UNCOV
54
            'X-Frame-Options' => 'deny',
×
UNCOV
55
        ];
×
56

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

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

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

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

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

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

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

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

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

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

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

UNCOV
125
        return $headers;
×
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