8000 feature #59157 [HttpKernel] [MapQueryString] added key argument to Ma… · symfony/symfony@bb8fc43 · GitHub
[go: up one dir, main page]

Skip to content

Commit bb8fc43

Browse files
committed
feature #59157 [HttpKernel] [MapQueryString] added key argument to MapQueryString attribute (feymo)
This PR was squashed before being merged into the 7.3 branch. Discussion ---------- [HttpKernel] [MapQueryString] added key argument to MapQueryString attribute | Q | A | ------------- | --- | Branch? | 7.3 | Bug fix? | no | New feature? | yes | Deprecations? | no | Issues | N/A | License | MIT When using `[#MapQueryString]`, the default resolver uses the `$request->query-all()` function but there's currently no way to pass a specific key to this function if needed. This PR add a `$key` argument to the `#[MapQueryString]` attribute that will allow to pass a specific key if needed to the default resolver. **Example :** Given the following query : `https://example.org?search[term]=foo&search[category]=bar`, using current `#[MapQueryString]` implementation will resolve an object with only `$search` property. Change proposed in this PR allows to pass a `key` argument like `#[MapQueryString(key: search)]`. Doing so, the object will be resolved with `$term` and `$category` properties. Commits ------- ac1f54c [HttpKernel] [MapQueryString] added key argument to MapQueryString attribute
2 parents cfe09c2 + ac1f54c commit bb8fc43

File tree

4 files changed

+28
-1
lines changed

4 files changed

+28
-1
lines changed

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

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public function __construct(
3737
public readonly string|GroupSequence|array|null $validationGroups = null,
3838
string $resolver = RequestPayloadValueResolver::class,
3939
public readonly int $validationFailedStatusCode = Response::HTTP_NOT_FOUND,
40+
public readonly ?string $key = null,
4041
) {
4142
parent::__construct($resolver);
4243
}

src/Symfony/Component/HttpKernel/CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
7.3
5+
---
6+
7+
* Add `$key` argument to `#[MapQueryString]` that allows using a specific key for argument resolving
8+
49
7.2
510
---
611

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ public static function getSubscribedEvents(): array
186186

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

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

+21
Original file line numberDiff line numberDiff line change
@@ -874,6 +874,27 @@ public function testBoolArgumentInJsonBody()
874874

875875
$this->assertTrue($event->getArguments()[0]->value);
876876
}
877+
878+
public function testConfigKeyForQueryString()
879+
{
880+
$serializer = new Serializer([new ObjectNormalizer()]);
881+
$validator = $this->createMock(ValidatorInterface::class);
882+
$resolver = new RequestPayloadValueResolver($serializer, $validator);
883+
884+
$argument = new ArgumentMetadata('filtered', QueryPayload::class, false, false, null, false, [
885+
MapQueryString::class => new MapQueryString(key: 'value'),
886+
]);
887+
$request = Request::create('/', Request::METHOD_GET, ['value' => ['page' => 1.0]]);
888+
889+
$kernel = $this->createMock(HttpKernelInterface::class);
890+
$arguments = $resolver->resolve($request, $argument);
891+
$event = new ControllerArgumentsEvent($kernel, function () {}, $arguments, $request, HttpKernelInterface::MAIN_REQUEST);
892+
893+
$resolver->onKernelControllerArguments($event);
894+
895+
$this->assertInstanceOf(QueryPayload::class, $event->getArguments()[0]);
896+
$this->assertSame(1.0, $event->getArguments()[0]->page);
897+
}
877898
}
878899

879900
class RequestPayload

0 commit comments

Comments
 (0)
0