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

api-platform / core / 7358252084

29 Dec 2023 04:32PM UTC coverage: 37.248% (-0.1%) from 37.395%
7358252084

push

github

soyuka
Merge 3.2

6 of 8 new or added lines in 6 files covered. (75.0%)

44 existing lines in 2 files now uncovered.

10347 of 27779 relevant lines covered (37.25%)

28.62 hits per line

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

48.72
/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) {
89✔
38
            $this->translator = new class() implements TranslatorInterface, LocaleAwareInterface {
×
39
                use TranslatorTrait;
40
            };
×
41
            $this->translator->setLocale('en');
×
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);
89✔
48

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

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

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

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

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

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

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

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

87
        try {
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
                }
NEW
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

© 2025 Coveralls, Inc