8000 [HttpKernel] Add support for custom HTTP status code for the MapQuery… · symfony/symfony@454d3aa · GitHub
[go: up one dir, main page]

Skip to content

Commit 454d3aa

Browse files
committed
[HttpKernel] Add support for custom HTTP status code for the MapQueryParameter attribute
1 parent 39e3ba4 commit 454d3aa

File tree

4 files changed

+148
-112
lines changed

4 files changed

+148
-112
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@
1111

1212
namespace Symfony\Component\HttpKernel\Attribute;
1313

14+
use Symfony\Component\HttpFoundation\Response;
1415
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\QueryParameterValueResolver;
1516

1617
/**
1718
* Can be used to pass a query parameter to a controller argument.
1819
*
1920
* @author Ruud Kamphuis <ruud@ticketswap.com>
21+
* @author Ionut Enache <i.ovidiuenache@yahoo.com>
2022
*/
2123
#[\Attribute(\Attribute::TARGET_PARAMETER)]
2224
final class MapQueryParameter extends ValueResolver
@@ -32,6 +34,7 @@ public function __construct(
3234
public int $flags = 0,
3335
public array $options = [],
3436
string $resolver = QueryParameterValueResolver::class,
37+
public readonly int $validationFailedStatusCode = Response::HTTP_NOT_FOUND,
3538
) {
3639
parent::__construct($resolver);
3740
}

src/Symfony/Component/HttpKernel/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
---
66

77
* Add method `isKernelTerminating()` to `ExceptionEvent` that allows to check if an exception was thrown while the kernel is being terminated
8+
* Add `$validationFailedStatusCode` argument to `#[MapQueryParameter]` that allows setting a custom HTTP status code when validation fails
89

910
7.0
1011
---

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@
1515
use Symfony\Component\HttpKernel\Attribute\MapQueryParameter;
1616
use Symfony\Component\HttpKernel\Controller\ValueResolverInterface;
1717
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
18-
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
18+
use Symfony\Component\HttpKernel\Exception\HttpException;
1919

2020
/**
2121
* Resolve arguments of type: array, string, int, float, bool, \BackedEnum from query parameters.
2222
*
2323
* @author Ruud Kamphuis <ruud@ticketswap.com>
2424
* @author Nicolas Grekas <p@tchwork.com>
2525
* @author Mateusz Anders <anders_mateusz@outlook.com>
26+
* @author Ionut Enache <i.ovidiuenache@yahoo.com>
2627
*/
2728
final class QueryParameterValueResolver implements ValueResolverInterface
2829
{
@@ -33,12 +34,14 @@ public function resolve(Request $request, ArgumentMetadata $argument): array
3334
}
3435

3536
$name = $attribute->name ?? $argument->getName();
37+
$validationFailedCode = $attribute->validationFailedStatusCode;
38+
3639
if (!$request->query->has($name)) {
3740
if ($argument->isNullable() || $argument->hasDefaultValue()) {
3841
return [];
3942
}
4043

41-
throw new NotFoundHttpException(sprintf('Missing query parameter "%s".', $name));
44+
throw new HttpException($validationFailedCode, sprintf('Missing query parameter "%s".', $name));
4245
}
4346

4447
$value = $request->query->all()[$name];
@@ -52,7 +55,7 @@ public function resolve(Request $request, ArgumentMetadata $argument): array
5255
$filtered = array_values(array_filter((array) $value, \is_array(...)));
5356

5457
if ($filtered !== $value && !($attribute->flags & \FILTER_NULL_ON_FAILURE)) {
55-
throw new NotFoundHttpException(sprintf('Invalid query parameter "%s".', $name));
58+
throw new HttpException($validationFailedCode, sprintf('Invalid query parameter "%s".', $name));
5659
}
5760

5861
return $filtered;
@@ -103,7 +106,7 @@ public function resolve(Request $request, ArgumentMetadata $argument): array
103106
}
104107

105108
if (null === $value && !($attribute->flags & \FILTER_NULL_ON_FAILURE)) {
106-
throw new NotFoundHttpException(sprintf('Invalid query parameter "%s".', $name));
109+
throw new HttpException($validationFailedCode, sprintf('Invalid query parameter "%s".', $name));
107110
}
108111

109112
if (!\is_array($value)) {
@@ -117,7 +120,7 @@ public function resolve(Request $request, ArgumentMetadata $argument): array
117120
}
118121

119122
if ($filtered !== $value && !($attribute->flags & \FILTER_NULL_ON_FAILURE)) {
120-
throw new NotFoundHttpException(sprintf('Invalid query parameter "%s".', $name));
123+
throw new HttpException($validationFailedCode, sprintf('Invalid query parameter "%s".', $name));
121124
}
122125

123126
return $argument->isVariadic() ? $filtered : [$filtered];

0 commit comments

Comments
 (0)
0