Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commit24f3cc7

Browse files
committed
map a list of items with MapRequestPayload attribute
1 parentb5ee977 commit24f3cc7

File tree

4 files changed

+47
-1
lines changed

4 files changed

+47
-1
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,15 @@ class MapRequestPayload extends ValueResolver
3232
* @param string|GroupSequence|array<string>|null $validationGroups The validation groups to use when validating the query string mapping
3333
* @param class-string $resolver The class name of the resolver to use
3434
* @param int $validationFailedStatusCode The HTTP code to return if the validation fails
35+
* @param class-string|string|null $itemsType The element type for array deserialization.
3536
*/
3637
publicfunction__construct(
3738
publicreadonlyarray|string|null$acceptFormat =null,
3839
publicreadonlyarray$serializationContext = [],
3940
publicreadonlystring|GroupSequence|array|null$validationGroups =null,
4041
string$resolver = RequestPayloadValueResolver::class,
4142
publicreadonlyint$validationFailedStatusCode = Response::HTTP_UNPROCESSABLE_ENTITY,
43+
publicreadonly ?string$itemsType =null,
4244
) {
4345
parent::__construct($resolver);
4446
}

‎src/Symfony/Component/HttpKernel/CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ CHANGELOG
88
* Add`HttpException::fromStatusCode()`
99
* Add`$validationFailedStatusCode` argument to`#[MapQueryParameter]` that allows setting a custom HTTP status code when validation fails
1010
* Add`NearMissValueResolverException` to let value resolvers report when an argument could be under their watch but failed to be resolved
11+
* Add`$itemsType` argument to`#[MapRequestPayload]` that allows mapping a list of items
1112

1213
7.0
1314
---

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ private function mapQueryString(Request $request, string $type, MapQueryString $
170170
return$this->serializer->denormalize($data,$type,null,$attribute->serializationContext +self::CONTEXT_DENORMALIZE + ['filter_bool' =>true]);
171171
}
172172

173-
privatefunctionmapRequestPayload(Request$request,string$type,MapRequestPayload$attribute):?object
173+
privatefunctionmapRequestPayload(Request$request,string$type,MapRequestPayload$attribute):object|array|null
174174
{
175175
if (null ===$format =$request->getContentTypeFormat()) {
176176
thrownewUnsupportedMediaTypeHttpException('Unsupported format.');
@@ -180,6 +180,14 @@ private function mapRequestPayload(Request $request, string $type, MapRequestPay
180180
thrownewUnsupportedMediaTypeHttpException(sprintf('Unsupported format, expects "%s", but "%s" given.',implode('", "', (array)$attribute->acceptFormat),$format));
181181
}
182182

183+
if ('array' ===$type) {
184+
if (null ===$attribute->itemsType) {
185+
thrownew \LogicException(sprintf('For proper deserialization, the %s::$itemsType value is required when the argument type is "array".', MapRequestPayload::class));
186+
}
187+
188+
$type =$attribute->itemsType.'[]';
189+
}
190+
183191
if ($data =$request->request->all()) {
184192
return$this->serializer->denormalize($data,$type,null,$attribute->serializationContext +self::CONTEXT_DENORMALIZE + ('form' ===$format ? ['filter_bool' =>true] : []));
185193
}

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
useSymfony\Component\Serializer\Encoder\XmlEncoder;
2626
useSymfony\Component\Serializer\Exception\NotNormalizableValueException;
2727
useSymfony\Component\Serializer\Exception\PartialDenormalizationException;
28+
useSymfony\Component\Serializer\Normalizer\ArrayDenormalizer;
2829
useSymfony\Component\Serializer\Normalizer\DenormalizerInterface;
2930
useSymfony\Component\Serializer\Normalizer\ObjectNormalizer;
3031
useSymfony\Component\Serializer\Serializer;
@@ -421,6 +422,40 @@ public function testRequestInputValidationPassed()
421422
$this->assertEquals([$payload],$event->getArguments());
422423
}
423424

425+
publicfunctiontestRequestArrayDenormalization()
426+
{
427+
$input = [
428+
['price' =>'50'],
429+
['price' =>'23'],
430+
];
431+
$payload = [
432+
newRequestPayload(50),
433+
newRequestPayload(23),
434+
];
435+
436+
$serializer =newSerializer([newArrayDenormalizer(),newObjectNormalizer()], ['json' =>newJsonEncoder()]);
437+
438+
$validator =$this->createMock(ValidatorInterface::class);
439+
$validator->expects($this->once())
440+
->method('validate')
441+
->willReturn(newConstraintViolationList());
442+
443+
$resolver =newRequestPayloadValueResolver($serializer,$validator);
444+
445+
$argument =newArgumentMetadata('prices','array',false,false,null,false, [
446+
MapRequestPayload::class =>newMapRequestPayload(itemsType: RequestPayload::class),
447+
]);
448+
$request = Request::create('/','POST',$input);
449+
450+
$kernel =$this->createMock(HttpKernelInterface::class);
451+
$arguments =$resolver->resolve($request,$argument);
452+
$event =newControllerArgumentsEvent($kernel,function () {},$arguments,$request, HttpKernelInterface::MAIN_REQUEST);
453+
454+
$resolver->onKernelControllerArguments($event);
455+
456+
$this->assertEquals([$payload],$event->getArguments());
457+
}
458+
424459
publicfunctiontestItThrowsOnVariadicArgument()
425460
{
426461
$serializer =newSerializer();

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp