-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[HttpKernel] Adding new #[MapRequestHeader]
attribute and resolver
#51379
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
Open
StevenRenaux
wants to merge
1
commit into
symfony:7.4
Choose a base branch
from
StevenRenaux:map-request-header-attribute
base: 7.4
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+301
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/Symfony/Component/HttpKernel/Attribute/MapRequestHeader.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\HttpKernel\Attribute; | ||
|
||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\RequestHeaderValueResolver; | ||
|
||
#[\Attribute(\Attribute::TARGET_PARAMETER)] | ||
class MapRequestHeader extends ValueResolver | ||
{ | ||
/** | ||
* @param string|null $name The name of the header parameter; if null, the name of the argument in the controller will be used | ||
* @param string $resolver The class name of the resolver to use | ||
* @param int $validationFailedStatusCode The HTTP code to return if the validation fails | ||
*/ | ||
public function __construct( | ||
public readonly ?string $name = null, | ||
string $resolver = RequestHeaderValueResolver::class, | ||
public readonly int $validationFailedStatusCode = Response::HTTP_BAD_REQUEST, | ||
) { | ||
parent::__construct($resolver); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
src/Symfony/Component/HttpKernel/Controller/ArgumentResolver/RequestHeaderValueResolver.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,62 @@ | ||||||
<?php | ||||||
|
||||||
/* | ||||||
* This file is part of the Symfony package. | ||||||
* | ||||||
* (c) Fabien Potencier <fabien@symfony.com> | ||||||
* | ||||||
* For the full copyright and license information, please view the LICENSE | ||||||
* file that was distributed with this source code. | ||||||
*/ | ||||||
|
||||||
namespace Symfony\Component\HttpKernel\Controller\ArgumentResolver; | ||||||
|
||||||
use Symfony\Component\HttpFoundation\AcceptHeader; | ||||||
use Symfony\Component\HttpFoundation\Request; | ||||||
use Symfony\Component\HttpKernel\Attribute\MapRequestHeader; | ||||||
use Symfony\Component\HttpKernel\Controller\ValueResolverInterface; | ||||||
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata; | ||||||
use Symfony\Component\HttpKernel\Exception\HttpException; | ||||||
|
||||||
class RequestHeaderValueResolver implements ValueResolverInterface | ||||||
{ | ||||||
public function resolve(Request $request, ArgumentMetadata $argument): iterable | ||||||
OskarStark marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
{ | ||||||
if (!$attribute = $argument->getAttributesOfType(MapRequestHeader::class)[0] ?? null) { | ||||||
return []; | ||||||
} | ||||||
|
||||||
$type = $argument->getType(); | ||||||
|
||||||
if (!\in_array($type, ['string', 'array', AcceptHeader::class])) { | ||||||
throw new \LogicException(\sprintf('Could not resolve the argument typed "%s". Valid values types are "array", "string" or "%s".', $type, AcceptHeader::class)); | ||||||
} | ||||||
|
||||||
$name = $attribute->name ?? $argument->getName(); | ||||||
$value = null; | ||||||
|
||||||
if ($request->headers->has($name)) { | ||||||
$value = match ($type) { | ||||||
'string' => $request->headers->get($name), | ||||||
'array' => match (strtolower($name)) { | ||||||
'accept' => $request->getAcceptableContentTypes(), | ||||||
'accept-charset' => $request->getCharsets(), | ||||||
'accept-language' => $request->getLanguages(), | ||||||
'accept-encoding' => $request->getEncodings(), | ||||||
default => [$request->headers->get($name)], | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
}, | ||||||
default => AcceptHeader::fromString($request->headers->get($name)), | ||||||
}; | ||||||
} | ||||||
|
||||||
if (null === $value && $argument->hasDefaultValue()) { | ||||||
$value = $argument->getDefaultValue(); | ||||||
} | ||||||
|
||||||
if (null === $value && !$argument->isNullable()) { | ||||||
throw new HttpException($attribute->validationFailedStatusCode, \sprintf('Missing header "%s".', $name)); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think if type is array we could return an empty array |
||||||
} | ||||||
|
||||||
return [$value]; | ||||||
} | ||||||
} |
202 changes: 202 additions & 0 deletions
202
...Component/HttpKernel/Tests/Controller/ArgumentResolver/RequestHeaderValueResolverTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,202 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\HttpKernel\Tests\Controller\ArgumentResolver; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\HttpFoundation\AcceptHeader; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpKernel\Attribute\MapRequestHeader; | ||
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\RequestHeaderValueResolver; | ||
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata; | ||
use Symfony\Component\HttpKernel\Exception\HttpException; | ||
|
||
class RequestHeaderValueResolverTest extends TestCase | ||
{ | ||
public static function provideHeaderValueWithStringType(): iterable | ||
{ | ||
yield 'with accept' => ['accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8']; | ||
yield 'with accept-language' => ['accept-language', 'en-us,en;q=0.5']; | ||
yield 'with host' => ['host', 'localhost']; | ||
yield 'with user-agent' => ['user-agent', 'Symfony']; | ||
} | ||
|
||
public static function provideHeaderValueWithArrayType(): iterable | ||
{ | ||
yield 'with accept' => [ | ||
'accept', | ||
'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', | ||
[ | ||
[ | ||
'text/html', | ||
'application/xhtml+xml', | ||
'application/xml', | ||
'*/*', | ||
], | ||
], | ||
]; | ||
yield 'with accept-language' => [ | ||
'accept-language', | ||
'en-us,en;q=0.5', | ||
[ | ||
[ | ||
'en_US', | ||
'en', | ||
], | ||
], | ||
]; | ||
yield 'with host' => [ | ||
'host', | ||
'localhost', | ||
[ | ||
['localhost'], | ||
], | ||
]; | ||
yield 'with user-agent' => [ | ||
'user-agent', | ||
'Symfony', | ||
[ | ||
['Symfony'], | ||
], | ||
]; | ||
} | ||
|
||
public static function provideHeaderValueWithAcceptHeaderType(): iterable | ||
{ | ||
yield 'with accept' => [ | ||
'accept', | ||
'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', | ||
[AcceptHeader::fromString('text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8')], | ||
]; | ||
yield 'with accept-language' => [ | ||
'accept-language', | ||
'en-us,en;q=0.5', | ||
[AcceptHeader::fromString('en-us,en;q=0.5')], | ||
]; | ||
yield 'with host' => [ | ||
'host', | ||
'localhost', | ||
[AcceptHeader::fromString('localhost')], | ||
]; | ||
yield 'with user-agent' => [ | ||
'user-agent', | ||
'Symfony', | ||
[AcceptHeader::fromString('Symfony')], | ||
]; | ||
} | ||
|
||
public static function provideHeaderValueWithDefaultAndNull(): iterable | ||
{ | ||
yield 'with hasDefaultValue' => [true, 'foo', false, 'foo']; | ||
yield 'with no isNullable' => [false, null, true, null]; | ||
} | ||
|
||
public function testWrongType() | ||
{ | ||
$this->expectException(\LogicException::class); | ||
|
||
$metadata = new ArgumentMetadata('accept', 'int', false, false, null, false, [ | ||
MapRequestHeader::class => new MapRequestHeader(), | ||
]); | ||
|
||
$request = Request::create('/'); | ||
|
||
$resolver = new RequestHeaderValueResolver(); | ||
$resolver->resolve($request, $metadata); | ||
} | ||
|
||
/** | ||
* @dataProvider provideHeaderValueWithStringType | ||
*/ | ||
public function testWithStringType(string $parameter, string $value) | ||
{ | ||
$resolver = new RequestHeaderValueResolver(); | ||
|
||
$metadata = new ArgumentMetadata('variableName', 'string', false, false, null, false, [ | ||
MapRequestHeader::class => new MapRequestHeader($parameter), | ||
]); | ||
|
||
$request = Request::create('/'); | ||
$request->headers->set($parameter, $value); | ||
|
||
$arguments = $resolver->resolve($request, $metadata); | ||
|
||
self::assertEquals([$value], $arguments); | ||
} | ||
|
||
/** | ||
* @dataProvider provideHeaderValueWithArrayType | ||
*/ | ||
public function testWithArrayType(string $parameter, string $value, array $expected) | ||
{ | ||
$resolver = new RequestHeaderValueResolver(); | ||
|
||
$metadata = new ArgumentMetadata('variableName', 'array', false, false, null, false, [ | ||
MapRequestHeader::class => new MapRequestHeader($parameter), | ||
]); | ||
|
||
$request = Request::create('/'); | ||
$request->headers->set($parameter, $value); | ||
|
||
$arguments = $resolver->resolve($request, $metadata); | ||
|
||
self::assertEquals($expected, $arguments); | ||
} | ||
|
||
/** | ||
* @dataProvider provideHeaderValueWithAcceptHeaderType | ||
*/ | ||
public function testWithAcceptHeaderType(string $parameter, string $value, array $expected) | ||
{ | ||
$resolver = new RequestHeaderValueResolver(); | ||
|
||
$metadata = new ArgumentMetadata('variableName', AcceptHeader::class, false, false, null, false, [ | ||
MapRequestHeader::class => new MapRequestHeader($parameter), | ||
]); | ||
|
||
$request = Request::create('/'); | ||
$request->headers->set($parameter, $value); | ||
|
||
$arguments = $resolver->resolve($request, $metadata); | ||
|
||
self::assertEquals($expected, $arguments); | ||
} | ||
|
||
/** | ||
* @dataProvider provideHeaderValueWithDefaultAndNull | ||
*/ | ||
public function testWithDefaultValueAndNull(bool $hasDefaultValue, ?string $defaultValue, bool $isNullable, ?string $expected) | ||
{ | ||
$metadata = new ArgumentMetadata('wrong-header', 'string', false, $hasDefaultValue, $defaultValue, $isNullable, [ | ||
MapRequestHeader::class => new MapRequestHeader(), | ||
]); | ||
|
||
$request = Request::create('/'); | ||
|
||
$resolver = new RequestHeaderValueResolver(); | ||
$arguments = $resolver->resolve($request, $metadata); | ||
|
||
self::assertEquals([$expected], $arguments); | ||
} | ||
|
||
public function testWithNoDefaultAndNotNullable() | ||
{ | ||
$this->expectException(HttpException::class); | ||
$this->expectExceptionMessage('Missing header "variableName".'); | ||
|
||
$metadata = new ArgumentMetadata('variableName', 'string', false, false, null, false, [ | ||
MapRequestHeader::class => new MapRequestHeader(), | ||
]); | ||
|
||
$resolver = new RequestHeaderValueResolver(); | ||
$resolver->resolve(Request::create('/'), $metadata); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.