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

api-platform / core / 7930644932

16 Feb 2024 12:36PM UTC coverage: 66.683% (+0.005%) from 66.678%
7930644932

push

github

web-flow
fix: return null instead of exception for GraphQL Query operation (#6118)

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

53 existing lines in 19 files now uncovered.

16304 of 24450 relevant lines covered (66.68%)

37.74 hits per line

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

92.68
/src/State/Processor/RespondProcessor.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\Processor;
15

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

30
/**
31
 * Serializes data.
32
 *
33
 * @author Kévin Dunglas <dunglas@gmail.com>
34
 */
35
final class RespondProcessor implements ProcessorInterface
36
{
37
    use ClassInfoTrait;
38
    use CloneTrait;
39

40
    public const METHOD_TO_CODE = [
41
        'POST' => Response::HTTP_CREATED,
42
        'DELETE' => Response::HTTP_NO_CONTENT,
43
    ];
44

45
    public function __construct(
46
        private ?IriConverterInterface $iriConverter = null,
47
        private readonly ?ResourceClassResolverInterface $resourceClassResolver = null,
48
        private readonly ?OperationMetadataFactoryInterface $operationMetadataFactory = null,
49
    ) {
50
    }
103✔
51

52
    public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = [])
53
    {
54
        if ($data instanceof Response || !$operation instanceof HttpOperation) {
100✔
55
            return $data;
8✔
56
        }
57

58
        if (!($request = $context['request'] ?? null)) {
92✔
59
            return $data;
×
60
        }
61

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

69
        $exception = $request->attributes->get('exception');
92✔
70
        if (($exception instanceof HttpExceptionInterface || $exception instanceof SymfonyHttpExceptionInterface) && $exceptionHeaders = $exception->getHeaders()) {
92✔
71
            $headers = array_merge($headers, $exceptionHeaders);
4✔
72
        }
73

74
        $status = $operation->getStatus();
92✔
75

76
        if ($sunset = $operation->getSunset()) {
92✔
77
            $headers['Sunset'] = (new \DateTimeImmutable($sunset))->format(\DateTimeInterface::RFC1123);
×
78
        }
79

80
        if ($acceptPatch = $operation->getAcceptPatch()) {
92✔
UNCOV
81
            $headers['Accept-Patch'] = $acceptPatch;
15✔
82
        }
83

84
        $method = $request->getMethod();
92✔
85
        $originalData = $context['original_data'] ?? null;
92✔
86

87
        if ($hasData = ($this->resourceClassResolver && $originalData && \is_object($originalData) && $this->resourceClassResolver->isResourceClass($this->getObjectClass($originalData))) && $this->iriConverter) {
92✔
88
            if (
89
                300 <= $status && $status < 400
32✔
90
                && (($operation->getExtraProperties()['is_alternate_resource_metadata'] ?? false) || ($operation->getExtraProperties()['canonical_uri_template'] ?? null))
32✔
91
            ) {
92
                $canonicalOperation = $operation;
4✔
93
                if ($this->operationMetadataFactory && null !== ($operation->getExtraProperties()['canonical_uri_template'] ?? null)) {
4✔
94
                    $canonicalOperation = $this->operationMetadataFactory->create($operation->getExtraProperties()['canonical_uri_template'], $context);
4✔
95
                }
96

97
                $headers['Location'] = $this->iriConverter->getIriFromResource($originalData, UrlGeneratorInterface::ABS_PATH, $canonicalOperation);
4✔
98
            } elseif ('PUT' === $method && !$request->attributes->get('previous_data') && null === $status && ($operation instanceof Put && ($operation->getAllowCreate() ?? false))) {
32✔
99
                $status = 201;
×
100
            }
101
        }
102

103
        $status ??= self::METHOD_TO_CODE[$method] ?? 200;
92✔
104

105
        if ($hasData && $this->iriConverter) {
92✔
106
            $iri = $this->iriConverter->getIriFromResource($originalData);
32✔
107
            $headers['Content-Location'] = $iri;
32✔
108

109
            if ((201 === $status || (300 <= $status && $status < 400)) && 'POST' === $method) {
32✔
UNCOV
110
                $headers['Location'] = $iri;
9✔
111
            }
112
        }
113

114
        return new Response(
92✔
115
            $data,
92✔
116
            $status,
92✔
117
            $headers
92✔
118
        );
92✔
119
    }
120
}
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