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

api-platform / core / 18089958695

29 Sep 2025 07:57AM UTC coverage: 21.569% (-0.2%) from 21.797%
18089958695

push

github

web-flow
fix(openapi): define items type for HydraCollectionBaseSchema hydra:member (#7419)

Co-authored-by: Thibaut Cholley <thibaut.cholley@elsie-sante.fr>

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

12063 existing lines in 401 files now uncovered.

11765 of 54545 relevant lines covered (21.57%)

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

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

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

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

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

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

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

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

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

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

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

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

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