Skip to content
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
1 change: 1 addition & 0 deletions src/Symfony/Component/HttpKernel/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
---

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

7.2
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\HttpKernel\Controller\ValueResolverInterface;
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\Uid\AbstractUid;

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

$uidType = null;
if (is_subclass_of($type, AbstractUid::class)) {
$uidType = $type;
$type = 'uid';
}

$enumType = null;
$filter = match ($type) {
'array' => \FILTER_DEFAULT,
'string' => isset($attribute->options['regexp']) ? \FILTER_VALIDATE_REGEXP : \FILTER_DEFAULT,
'int' => \FILTER_VALIDATE_INT,
'float' => \FILTER_VALIDATE_FLOAT,
'bool' => \FILTER_VALIDATE_BOOL,
'uid' => \FILTER_DEFAULT,
default => match ($enumType = is_subclass_of($type, \BackedEnum::class) ? (new \ReflectionEnum($type))->getBackingType()->getName() : null) {
'int' => \FILTER_VALIDATE_INT,
'string' => \FILTER_DEFAULT,
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')),
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')),
},
};

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

if (null !== $uidType) {
$value = \is_array($value) ? array_map([$uidType, 'fromString'], $value) : $uidType::fromString($value);
}

if (null === $value && !($attribute->flags & \FILTER_NULL_ON_FAILURE)) {
throw HttpException::fromStatusCode($validationFailedCode, \sprintf('Invalid query parameter "%s".', $name));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Tests\Fixtures\Suit;
use Symfony\Component\Uid\Ulid;

class QueryParameterValueResolverTest extends TestCase
{
Expand All @@ -44,7 +45,7 @@ public function testSkipWhenNoAttribute()
*/
public function testResolvingSuccessfully(Request $request, ArgumentMetadata $metadata, array $expected)
{
$this->assertSame($expected, $this->resolver->resolve($request, $metadata));
$this->assertEquals($expected, $this->resolver->resolve($request, $metadata));
}

/**
Expand Down Expand Up @@ -231,6 +232,12 @@ public static function validDataProvider(): iterable
new ArgumentMetadata('firstName', 'string', false, true, false, attributes: [new MapQueryParameter()]),
[],
];

yield 'parameter found and ULID' => [
Request::create('/', 'GET', ['groupId' => '01E439TP9XJZ9RPFH3T1PYBCR8']),
new ArgumentMetadata('groupId', Ulid::class, false, true, false, attributes: [new MapQueryParameter()]),
[Ulid::fromString('01E439TP9XJZ9RPFH3T1PYBCR8')],
];
}

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

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

Expand Down
Loading