Skip to content

[HttpFoundation] Fix \Stringable support in InputBag::get() #46957

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

Merged
merged 1 commit into from
Jul 22, 2022
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
2 changes: 1 addition & 1 deletion src/Symfony/Component/HttpFoundation/InputBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function get(string $key, mixed $default = null): string|int|float|bool|n

$value = parent::get($key, $this);

if (null !== $value && $this !== $value && !\is_scalar($value)) {
if (null !== $value && $this !== $value && !\is_scalar($value) && !$value instanceof \Stringable) {
throw new BadRequestException(sprintf('Input value "%s" contains a non-scalar value.', $key));
}

Expand Down
8 changes: 7 additions & 1 deletion src/Symfony/Component/HttpFoundation/Tests/InputBagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,19 @@ class InputBagTest extends TestCase
{
public function testGet()
{
$bag = new InputBag(['foo' => 'bar', 'null' => null, 'int' => 1, 'float' => 1.0, 'bool' => false]);
$bag = new InputBag(['foo' => 'bar', 'null' => null, 'int' => 1, 'float' => 1.0, 'bool' => false, 'stringable' => new class() implements \Stringable {
public function __toString(): string
{
return 'strval';
}
}]);

$this->assertSame('bar', $bag->get('foo'), '->get() gets the value of a string parameter');
$this->assertSame('default', $bag->get('unknown', 'default'), '->get() returns second argument as default if a parameter is not defined');
$this->assertNull($bag->get('null', 'default'), '->get() returns null if null is set');
$this->assertSame(1, $bag->get('int'), '->get() gets the value of an int parameter');
$this->assertSame(1.0, $bag->get('float'), '->get() gets the value of a float parameter');
$this->assertSame('strval', $bag->get('stringable'), '->get() gets the string value of a \Stringable parameter');
$this->assertFalse($bag->get('bool'), '->get() gets the value of a bool parameter');
}

Expand Down