Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 820d1b1

Browse filesBrowse files
committed
[HttpKernel] Enhance MapRequestPayload adding format and validation group
apply feedbacks revert argument type better properties name
1 parent 03ca033 commit 820d1b1
Copy full SHA for 820d1b1

File tree

3 files changed

+23
-8
lines changed
Filter options

3 files changed

+23
-8
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpKernel/Attribute/MapRequestPayload.php
+4-1Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpKernel/Controller/ArgumentResolver/RequestPayloadValueResolver.php
+11-7Lines changed: 11 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,28 @@ 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+
if ($attribute->acceptFormat && !\in_array($format, (array) $attribute->acceptFormat, true)) {
133+
throw new HttpException(Response::HTTP_UNSUPPORTED_MEDIA_TYPE, sprintf('Unsupported format, expects "%s", but "%s" given.', implode('", "', (array) $attribute->acceptFormat), $format));
134+
}
135+
128136
if ($data = $request->request->all()) {
129-
return $this->serializer->denormalize($data, $type, null, self::CONTEXT_DENORMALIZE + $attribute->context);
137+
return $this->serializer->denormalize($data, $type, null, self::CONTEXT_DENORMALIZE + $attribute->serializationContext);
130138
}
131139

132140
if ('' === $data = $request->getContent()) {
133141
return null;
134142
}
135143

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

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

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpKernel/Tests/Controller/ArgumentResolver/RequestPayloadValueResolverTest.php
+8Lines 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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.