Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[HttpKernel] [MapQueryString] added key argument to MapQueryString attribute #59157

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function __construct(
public readonly string|GroupSequence|array|null $validationGroups = null,
string $resolver = RequestPayloadValueResolver::class,
public readonly int $validationFailedStatusCode = Response::HTTP_NOT_FOUND,
public readonly ?string $key = null,
) {
parent::__construct($resolver);
}
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/HttpKernel/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.3
---

* Add `$key` argument to `#[MapQueryString]` that allows using a specific key for argument resolving

7.2
---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public static function getSubscribedEvents(): array

private function mapQueryString(Request $request, ArgumentMetadata $argument, MapQueryString $attribute): ?object
{
if (!($data = $request->query->all()) && ($argument->isNullable() || $argument->hasDefaultValue())) {
if (!($data = $request->query->all($attribute->key)) && ($argument->isNullable() || $argument->hasDefaultValue())) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,27 @@ public function testBoolArgumentInJsonBody()

$this->assertTrue($event->getArguments()[0]->value);
}

public function testConfigKeyForQueryString()
{
$serializer = new Serializer([new ObjectNormalizer()]);
$validator = $this->createMock(ValidatorInterface::class);
$resolver = new RequestPayloadValueResolver($serializer, $validator);

$argument = new ArgumentMetadata('filtered', QueryPayload::class, false, false, null, false, [
MapQueryString::class => new MapQueryString(key: 'value'),
]);
$request = Request::create('/', Request::METHOD_GET, ['value' => ['page' => 1.0]]);

$kernel = $this->createMock(HttpKernelInterface::class);
$arguments = $resolver->resolve($request, $argument);
$event = new ControllerArgumentsEvent($kernel, function () {}, $arguments, $request, HttpKernelInterface::MAIN_REQUEST);

$resolver->onKernelControllerArguments($event);

$this->assertInstanceOf(QueryPayload::class, $event->getArguments()[0]);
$this->assertSame(1.0, $event->getArguments()[0]->page);
}
}

class RequestPayload
Expand Down
Loading