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

api-platform / core / 14008635868

22 Mar 2025 12:39PM UTC coverage: 8.52% (+0.005%) from 8.515%
14008635868

Pull #7042

github

web-flow
Merge fdd88ef56 into 47a6dffbb
Pull Request #7042: Purge parent collections in inheritance cases

4 of 9 new or added lines in 1 file covered. (44.44%)

540 existing lines in 57 files now uncovered.

13394 of 157210 relevant lines covered (8.52%)

22.93 hits per line

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

78.05
/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,711✔
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)) {
1,498✔
53
            return $this->decorated?->provide($operation, $uriVariables, $context);
×
54
        }
55

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

58
        if (!$operation->canDeserialize()) {
1,487✔
59
            return $data;
1,142✔
60
        }
61

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

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

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

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

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

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

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

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