From e44e06f91754f15dd3a75fa8338f6d06da8d4b5a Mon Sep 17 00:00:00 2001 From: Bohan Yang Date: Wed, 10 Jan 2024 02:45:56 +0000 Subject: [PATCH] Feature: use other argument as object to populate --- .../HttpKernel/MapRequestPayloadListener.php | 32 ++++++++++++------- 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/packages/mango/HttpKernel/MapRequestPayloadListener.php b/packages/mango/HttpKernel/MapRequestPayloadListener.php index dcfbc5c..f815764 100644 --- a/packages/mango/HttpKernel/MapRequestPayloadListener.php +++ b/packages/mango/HttpKernel/MapRequestPayloadListener.php @@ -29,10 +29,6 @@ public function onKernelControllerArguments(ControllerArgumentsEvent $event): vo { $request = $event->getRequest(); - if (! $routeParams = $request->attributes->get('_route_params', [])) { - return; - } - $arguments = $event->getArguments(); foreach ($arguments as $i => $argument) { @@ -40,17 +36,29 @@ public function onKernelControllerArguments(ControllerArgumentsEvent $event): vo continue; } - $groups = $argument->serializationContext['groups'] ?? []; - - if ($groups !== [] && ! in_array('route', $groups, true)) { + if ($argument->disabled) { continue; } - $object = $this->denormalizer->denormalize( - $routeParams, - $argument->metadata->getType(), - context: ['groups' => ['route']] + $argument->serializationContext, - ); + if (str_ends_with($name = $argument->metadata->getName(), 'Payload')) { + $object = $event->getNamedArguments()[substr($name, 0, -7)] ?? null; + } else if ($routeParams = $request->attributes->get('_route_params', [])) { + $groups = $argument->serializationContext['groups'] ?? []; + + if ($groups !== [] && ! in_array('route', $groups, true)) { + continue; + } + + $object = $this->denormalizer->denormalize( + $routeParams, + $argument->metadata->getType(), + context: ['groups' => ['route']] + $argument->serializationContext, + ); + } + + if (!isset($object)) { + continue; + } $attribute = new MapRequestPayload( $argument->acceptFormat,