Skip to content

Navigation Menu

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 9fd614a

Browse filesBrowse files
seb-jeanfabpot
authored andcommitted
[HttpKernel] Support Uid in #[MapQueryParameter]
1 parent cf015b8 commit 9fd614a
Copy full SHA for 9fd614a

File tree

3 files changed

+24
-4
lines changed
Filter options

3 files changed

+24
-4
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpKernel/CHANGELOG.md
+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
---
66

77
* Add `$key` argument to `#[MapQueryString]` that allows using a specific key for argument resolving
8+
* Support `Uid` in `#[MapQueryParameter]`
89

910
7.2
1011
---

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpKernel/Controller/ArgumentResolver/QueryParameterValueResolver.php
+13-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\HttpKernel\Controller\ValueResolverInterface;
1717
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
1818
use Symfony\Component\HttpKernel\Exception\HttpException;
19+
use Symfony\Component\Uid\AbstractUid;
1920

2021
/**
2122
* Resolve arguments of type: array, string, int, float, bool, \BackedEnum from query parameters.
@@ -73,17 +74,24 @@ public function resolve(Request $request, ArgumentMetadata $argument): array
7374
$options['flags'] |= \FILTER_REQUIRE_SCALAR;
7475
}
7576

77+
$uidType = null;
78+
if (is_subclass_of($type, AbstractUid::class)) {
79+
$uidType = $type;
80+
$type = 'uid';
81+
}
82+
7683
$enumType = null;
7784
$filter = match ($type) {
7885
'array' => \FILTER_DEFAULT,
7986
'string' => isset($attribute->options['regexp']) ? \FILTER_VALIDATE_REGEXP : \FILTER_DEFAULT,
8087
'int' => \FILTER_VALIDATE_INT,
8188
'float' => \FILTER_VALIDATE_FLOAT,
8289
'bool' => \FILTER_VALIDATE_BOOL,
90+
'uid' => \FILTER_DEFAULT,
8391
default => match ($enumType = is_subclass_of($type, \BackedEnum::class) ? (new \ReflectionEnum($type))->getBackingType()->getName() : null) {
8492
'int' => \FILTER_VALIDATE_INT,
8593
'string' => \FILTER_DEFAULT,
86-
default => throw new \LogicException(\sprintf('#[MapQueryParameter] cannot be used on controller argument "%s$%s" of type "%s"; one of array, string, int, float, bool or \BackedEnum should be used.', $argument->isVariadic() ? '...' : '', $argument->getName(), $type ?? 'mixed')),
94+
default => throw new \LogicException(\sprintf('#[MapQueryParameter] cannot be used on controller argument "%s$%s" of type "%s"; one of array, string, int, float, bool, uid or \BackedEnum should be used.', $argument->isVariadic() ? '...' : '', $argument->getName(), $type ?? 'mixed')),
8795
},
8896
};
8997

@@ -105,6 +113,10 @@ public function resolve(Request $request, ArgumentMetadata $argument): array
105113
$value = \is_array($value) ? array_map($enumFrom, $value) : $enumFrom($value);
106114
}
107115

116+
if (null !== $uidType) {
117+
$value = \is_array($value) ? array_map([$uidType, 'fromString'], $value) : $uidType::fromString($value);
118+
}
119+
108120
if (null === $value && !($attribute->flags & \FILTER_NULL_ON_FAILURE)) {
109121
throw HttpException::fromStatusCode($validationFailedCode, \sprintf('Invalid query parameter "%s".', $name));
110122
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpKernel/Tests/Controller/ArgumentResolver/QueryParameterValueResolverTest.php
+10-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Symfony\Component\HttpKernel\Exception\HttpException;
2323
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
2424
use Symfony\Component\HttpKernel\Tests\Fixtures\Suit;
25+
use Symfony\Component\Uid\Ulid;
2526

2627
class QueryParameterValueResolverTest extends TestCase
2728
{
@@ -44,7 +45,7 @@ public function testSkipWhenNoAttribute()
4445
*/
4546
public function testResolvingSuccessfully(Request $request, ArgumentMetadata $metadata, array $expected)
4647
{
47-
$this->assertSame($expected, $this->resolver->resolve($request, $metadata));
48+
$this->assertEquals($expected, $this->resolver->resolve($request, $metadata));
4849
}
4950

5051
/**
@@ -231,6 +232,12 @@ public static function validDataProvider(): iterable
231232
new ArgumentMetadata('firstName', 'string', false, true, false, attributes: [new MapQueryParameter()]),
232233
[],
233234
];
235+
236+
yield 'parameter found and ULID' => [
237+
Request::create('/', 'GET', ['groupId' => '01E439TP9XJZ9RPFH3T1PYBCR8']),
238+
new ArgumentMetadata('groupId', Ulid::class, false, true, false, attributes: [new MapQueryParameter()]),
239+
[Ulid::fromString('01E439TP9XJZ9RPFH3T1PYBCR8')],
240+
];
234241
}
235242

236243
/**
@@ -245,13 +252,13 @@ public static function invalidArgumentTypeProvider(): iterable
245252
yield 'unsupported type' => [
246253
Request::create('/', 'GET', ['standardClass' => 'test']),
247254
new ArgumentMetadata('standardClass', \stdClass::class, false, false, false, attributes: [new MapQueryParameter()]),
248-
'#[MapQueryParameter] cannot be used on controller argument "$standardClass" of type "stdClass"; one of array, string, int, float, bool or \BackedEnum should be used.',
255+
'#[MapQueryParameter] cannot be used on controller argument "$standardClass" of type "stdClass"; one of array, string, int, float, bool, uid or \BackedEnum should be used.',
249256
];
250257

251258
yield 'unsupported type variadic' => [
252259
Request::create('/', 'GET', ['standardClass' => 'test']),
253260
new ArgumentMetadata('standardClass', \stdClass::class, true, false, false, attributes: [new MapQueryParameter()]),
254-
'#[MapQueryParameter] cannot be used on controller argument "...$standardClass" of type "stdClass"; one of array, string, int, float, bool or \BackedEnum should be used.',
261+
'#[MapQueryParameter] cannot be used on controller argument "...$standardClass" of type "stdClass"; one of array, string, int, float, bool, uid or \BackedEnum should be used.',
255262
];
256263
}
257264

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.