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

api-platform / core / 14635100171

24 Apr 2025 06:39AM UTC coverage: 8.271% (+0.02%) from 8.252%
14635100171

Pull #6904

github

web-flow
Merge c9cefd82e into a3e5e53ea
Pull Request #6904: feat(graphql): added support for graphql subscriptions to work for actions

0 of 73 new or added lines in 3 files covered. (0.0%)

1999 existing lines in 144 files now uncovered.

13129 of 158728 relevant lines covered (8.27%)

13.6 hits per line

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

51.22
/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\State\ProviderInterface;
19
use ApiPlatform\State\SerializerContextBuilderInterface;
20
use ApiPlatform\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(
36
        private readonly ?ProviderInterface $decorated,
37
        private readonly SerializerInterface $serializer,
38
        private readonly SerializerContextBuilderInterface $serializerContextBuilder,
39
        private ?TranslatorInterface $translator = null,
40
    ) {
41
        if (null === $this->translator) {
1,004✔
42
            $this->translator = new class implements TranslatorInterface, LocaleAwareInterface {
×
43
                use TranslatorTrait;
44
            };
×
45
            $this->translator->setLocale('en');
×
46
        }
47
    }
48

49
    public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
50
    {
51
        // We need request content
52
        if (!$operation instanceof HttpOperation || !($request = $context['request'] ?? null)) {
787✔
53
            return $this->decorated?->provide($operation, $uriVariables, $context);
×
54
        }
55

56
        $data = $this->decorated ? $this->decorated->provide($operation, $uriVariables, $context) : $request->attributes->get('data');
787✔
57

58
        if (!$operation->canDeserialize()) {
780✔
59
            return $data;
624✔
60
        }
61

62
        $contentType = $request->headers->get('CONTENT_TYPE');
188✔
63
        if (null === $contentType || '' === $contentType) {
188✔
64
            throw new UnsupportedMediaTypeHttpException('The "Content-Type" header must exist.');
×
65
        }
66

67
        $serializerContext = $this->serializerContextBuilder->createFromRequest($request, false, [
188✔
68
            'resource_class' => $operation->getClass(),
188✔
69
            'operation' => $operation,
188✔
70
        ]);
188✔
71

72
        $serializerContext['uri_variables'] = $uriVariables;
188✔
73

74
        if (!$format = $request->attributes->get('input_format') ?? null) {
188✔
75
            throw new UnsupportedMediaTypeHttpException('Format not supported.');
×
76
        }
77

78
        $method = $operation->getMethod();
188✔
79

80
        if (
81
            null !== $data
188✔
82
            && (
83
                'POST' === $method
188✔
84
                || 'PATCH' === $method
188✔
85
                || ('PUT' === $method && !($operation->getExtraProperties()['standard_put'] ?? true))
188✔
86
            )
87
        ) {
88
            $serializerContext[AbstractNormalizer::OBJECT_TO_POPULATE] = $data;
27✔
89
        }
90

91
        try {
92
            return $this->serializer->deserialize((string) $request->getContent(), $serializerContext['deserializer_type'] ?? $operation->getClass(), $format, $serializerContext);
188✔
UNCOV
93
        } catch (PartialDenormalizationException $e) {
20✔
94
            if (!class_exists(ConstraintViolationList::class)) {
×
95
                throw $e;
×
96
            }
97

98
            $violations = new ConstraintViolationList();
×
99
            foreach ($e->getErrors() as $exception) {
×
100
                if (!$exception instanceof NotNormalizableValueException) {
×
101
                    continue;
×
102
                }
103
                $message = (new Type($exception->getExpectedTypes() ?? []))->message;
×
104
                $parameters = [];
×
105
                if ($exception->canUseMessageForUser()) {
×
106
                    $parameters['hint'] = $exception->getMessage();
×
107
                }
108
                $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));
×
109
            }
110
            if (0 !== \count($violations)) {
×
111
                throw new ValidationException($violations);
×
112
            }
113
        }
114

115
        return $data;
×
116
    }
117
}
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