8000 [HttpKernel] Enhance MapRequestPayload adding format and validation g… · symfony/symfony@ba63b5b · GitHub
[go: up one dir, main page]

Skip to content

Commit ba63b5b

Browse files
author
Renan
committed
[HttpKernel] Enhance MapRequestPayload adding format and validation group
apply feedbacks revert argument type better properties name
1 parent 03ca033 commit ba63b5b

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

src/Symfony/Component/HttpKernel/Attribute/MapRequestPayload.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\HttpKernel\Attribute;
1313

1414
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\RequestPayloadValueResolver;
15+
use Symfony\Component\Validator\Constraints\GroupSequence;
1516

1617
/**
1718
* Controller parameter tag to map the request content to typed object and validate it.
@@ -22,7 +23,9 @@
2223
class MapRequestPayload extends ValueResolver
2324
{
2425
public function __construct(
25-
public readonly array $context = [],
26+
public readonly array|string|null $acceptFormat = null,
27+
public readonly array $serializationContext = [],
28+
public readonly string|GroupSequence|array|null $validationGroups = null,
2629
string $resolver = RequestPayloadValueResolver::class,
2730
) {
2831
parent::__construct($resolver);

src/Symfony/Component/HttpKernel/Controller/ArgumentResolver/RequestPayloadValueResolver.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public function resolve(Request $request, ArgumentMetadata $argument): iterable
9292
}
9393

9494
if (null !== $payload) {
95-
$violations->addAll($this->validator->validate($payload));
95+
$violations->addAll($this->validator->validate($payload, null, $attributes[0]->validationGroups ?? null));
9696
}
9797

9898
if (\count($violations)) {
@@ -125,24 +125,29 @@ private function mapQueryString(Request $request, string $type, MapQueryString $
125125

126126
private function mapRequestPayload(Request $request, string $type, MapRequestPayload $attribute): ?object
127127
{
128+
if (null === $format = $request->getContentTypeFormat()) {
129+
throw new HttpException(Response::HTTP_UNSUPPORTED_MEDIA_TYPE, 'Unsupported format.');
130+
}
131+
132+
$acceptFormat = \is_string($attribute->acceptFormat) ? [$attribute->acceptFormat] : $attribute->acceptFormat;
133+
if ($attribute->acceptFormat && !\in_array($format, $acceptFormat, true)) {
134+
throw new HttpException(Response::HTTP_UNSUPPORTED_MEDIA_TYPE, sprintf('Unsupported format, expects "%s", but "%s" given.', implode('", "', $acceptFormat), $format));
135+
}
136+
128137
if ($data = $request->request->all()) {
129-
return $this->serializer->denormalize($data, $type, null, self::CONTEXT_DENORMALIZE + $attribute->context);
138+
return $this->serializer->denormalize($data, $type, null, self::CONTEXT_DENORMALIZE + $attribute->serializationContext);
130139
}
131140

132141
if ('' === $data = $request->getContent()) {
133142
return null;
134143
}
135144

136-
if (null === $format = $request->getContentTypeFormat()) {
137-
throw new HttpException(Response::HTTP_UNSUPPORTED_MEDIA_TYPE, 'Unsupported format.');
138-
}
139-
140145
if ('form' === $format) {
141146
throw new HttpException(Response::HTTP_BAD_REQUEST, 'Request payload contains invalid "form" data.');
142147
}
143148

144149
try {
145-
return $this->serializer->deserialize($data, $type, $format, self::CONTEXT_DESERIALIZE + $attribute->context);
150+
return $this->serializer->deserialize($data, $type, $format, self::CONTEXT_DESERIALIZE + $attribute->serializationContext);
146151
} catch (UnsupportedFormatException $e) {
147152
throw new HttpException(Response::HTTP_UNSUPPORTED_MEDIA_TYPE, sprintf('Unsupported format: "%s".', $format), $e);
148153
} catch (NotEncodableValueException $e) {

src/Symfony/Component/HttpKernel/Tests/Controller/ArgumentResolver/RequestPayloadValueResolverTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,14 @@ public function testRequestInputValidationPassed()
181181

182182
$this->assertEquals($payload, $resolver->resolve($request, $argument)[0]);
183183
}
184+
185+
public function testNonSupportedRestrictedFormatMustThrowUnsupportedMediaTypeException()
186+
{
187+
}
188+
189+
public function testRestrictGroupValidation()
190+
{
191+
}
184192
}
185193

186194
class RequestPayload

0 commit comments

Comments
 (0)
0