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

api-platform / core / 15133993414

20 May 2025 09:30AM UTC coverage: 26.313% (-1.2%) from 27.493%
15133993414

Pull #7161

github

web-flow
Merge e2c03d45f into 5459ba375
Pull Request #7161: fix(metadata): infer parameter string type from schema

0 of 2 new or added lines in 1 file covered. (0.0%)

11019 existing lines in 363 files now uncovered.

12898 of 49018 relevant lines covered (26.31%)

34.33 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
    ) {
UNCOV
41
        if (null === $this->translator) {
799✔
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
UNCOV
52
        if (!$operation instanceof HttpOperation || !($request = $context['request'] ?? null)) {
799✔
53
            return $this->decorated?->provide($operation, $uriVariables, $context);
×
54
        }
55

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

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

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

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

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

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

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

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

91
        try {
UNCOV
92
            return $this->serializer->deserialize((string) $request->getContent(), $serializerContext['deserializer_type'] ?? $operation->getClass(), $format, $serializerContext);
185✔
UNCOV
93
        } catch (PartialDenormalizationException $e) {
21✔
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