• 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

58.97
/src/State/Provider/DeserializeProvider.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\Provider;
15

16
use ApiPlatform\Metadata\HttpOperation;
17
use ApiPlatform\Metadata\Operation;
18
use ApiPlatform\Serializer\SerializerContextBuilderInterface;
19
use ApiPlatform\State\ProviderInterface;
20
use ApiPlatform\Symfony\Validator\Exception\ValidationException;
21
use Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException;
22
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
23
use Symfony\Component\Serializer\Exception\PartialDenormalizationException;
24
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
25
use Symfony\Component\Serializer\SerializerInterface;
26
use Symfony\Component\Validator\Constraints\Type;
27
use Symfony\Component\Validator\ConstraintViolation;
28
use Symfony\Component\Validator\ConstraintViolationList;
29
use Symfony\Contracts\Translation\LocaleAwareInterface;
30
use Symfony\Contracts\Translation\TranslatorInterface;
31
use Symfony\Contracts\Translation\TranslatorTrait;
32

33
final class DeserializeProvider implements ProviderInterface
34
{
35
    public function __construct(private readonly ProviderInterface $decorated, private readonly SerializerInterface $serializer, private readonly SerializerContextBuilderInterface $serializerContextBuilder, private ?TranslatorInterface $translator = null)
36
    {
37
        if (null === $this->translator) {
99✔
38
            $this->translator = new class() implements TranslatorInterface, LocaleAwareInterface {
4✔
39
                use TranslatorTrait;
40
            };
4✔
41
            $this->translator->setLocale('en');
4✔
42
        }
43
    }
44

45
    public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
46
    {
47
        $data = $this->decorated->provide($operation, $uriVariables, $context);
99✔
48

49
        // We need request content
50
        if (!$operation instanceof HttpOperation || !($request = $context['request'] ?? null)) {
96✔
51
            return $data;
×
52
        }
53

54
        if (!$operation->canDeserialize()) {
96✔
55
            return $data;
83✔
56
        }
57

58
        $contentType = $request->headers->get('CONTENT_TYPE');
13✔
59
        if (null === $contentType || '' === $contentType) {
13✔
60
            throw new UnsupportedMediaTypeHttpException('The "Content-Type" header must exist.');
4✔
61
        }
62

UNCOV
63
        $serializerContext = $this->serializerContextBuilder->createFromRequest($request, false, [
9✔
UNCOV
64
            'resource_class' => $operation->getClass(),
9✔
UNCOV
65
            'operation' => $operation,
9✔
UNCOV
66
        ]);
9✔
67

UNCOV
68
        $serializerContext['uri_variables'] = $uriVariables;
9✔
69

UNCOV
70
        if (!$format = $request->attributes->get('input_format') ?? null) {
9✔
71
            throw new UnsupportedMediaTypeHttpException('Format not supported.');
×
72
        }
73

UNCOV
74
        $method = $operation->getMethod();
9✔
75

76
        if (
UNCOV
77
            null !== $data
9✔
78
            && (
UNCOV
79
                'POST' === $method
9✔
UNCOV
80
                || 'PATCH' === $method
9✔
UNCOV
81
                || ('PUT' === $method && !($operation->getExtraProperties()['standard_put'] ?? false))
9✔
82
            )
83
        ) {
84
            $serializerContext[AbstractNormalizer::OBJECT_TO_POPULATE] = $data;
×
85
        }
86

87
        try {
UNCOV
88
            return $this->serializer->deserialize((string) $request->getContent(), $operation->getClass(), $format, $serializerContext);
9✔
89
        } catch (PartialDenormalizationException $e) {
×
90
            $violations = new ConstraintViolationList();
×
91
            foreach ($e->getErrors() as $exception) {
×
92
                if (!$exception instanceof NotNormalizableValueException) {
×
93
                    continue;
×
94
                }
95
                $message = (new Type($exception->getExpectedTypes() ?? []))->message;
×
96
                $parameters = [];
×
97
                if ($exception->canUseMessageForUser()) {
×
98
                    $parameters['hint'] = $exception->getMessage();
×
99
                }
100
                $violations->add(new ConstraintViolation($this->translator->trans($message, ['{{ type }}' => implode('|', $exception->getExpectedTypes() ?? [])], 'validators'), $message, $parameters, null, $exception->getPath(), null, null, (string) Type::INVALID_TYPE_ERROR));
×
101
            }
102
            if (0 !== \count($violations)) {
×
103
                throw new ValidationException($violations);
×
104
            }
105
        }
106

107
        return $data;
×
108
    }
109
}
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