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

api-platform / core / 15927963363

27 Jun 2025 01:52PM UTC coverage: 22.489% (-0.01%) from 22.5%
15927963363

Pull #7247

github

web-flow
Merge 66de6d385 into d3e73f09a
Pull Request #7247: fix(laravel): decorate error handler

0 of 10 new or added lines in 2 files covered. (0.0%)

8 existing lines in 4 files now uncovered.

11063 of 49194 relevant lines covered (22.49%)

11.03 hits per line

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

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

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

58
        if (!$operation->canDeserialize()) {
8✔
UNCOV
59
            return $data;
×
60
        }
61

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

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

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

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

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

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

91
        try {
92
            return $this->serializer->deserialize((string) $request->getContent(), $serializerContext['deserializer_type'] ?? $operation->getClass(), $format, $serializerContext);
8✔
93
        } catch (PartialDenormalizationException $e) {
1✔
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
                $expectedTypes = $this->normalizeExpectedTypes($exception->getExpectedTypes());
×
104
                $message = (new Type($expectedTypes))->message;
×
105
                $parameters = [];
×
106
                if ($exception->canUseMessageForUser()) {
×
107
                    $parameters['hint'] = $exception->getMessage();
×
108
                }
109
                $violations->add(new ConstraintViolation($this->translator->trans($message, ['{{ type }}' => implode('|', $expectedTypes)], 'validators'), $message, $parameters, null, $exception->getPath(), null, null, (string) Type::INVALID_TYPE_ERROR));
×
110
            }
111
            if (0 !== \count($violations)) {
×
112
                throw new ValidationException($violations);
×
113
            }
114
        }
115

116
        return $data;
×
117
    }
118

119
    private function normalizeExpectedTypes(?array $expectedTypes = null): array
120
    {
121
        $normalizedTypes = [];
×
122

123
        foreach ($expectedTypes ?? [] as $expectedType) {
×
124
            $normalizedType = $expectedType;
×
125

126
            if (class_exists($expectedType) || interface_exists($expectedType)) {
×
127
                $classReflection = new \ReflectionClass($expectedType);
×
128
                $normalizedType = $classReflection->getShortName();
×
129
            }
130

131
            $normalizedTypes[] = $normalizedType;
×
132
        }
133

134
        return $normalizedTypes;
×
135
    }
136
}
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