Skip to content
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

[HttpFoundation] Use InputBag for POST requests too #40315

Merged
merged 1 commit into from Mar 12, 2021

Conversation

acasademont
Copy link
Contributor

@acasademont acasademont commented Feb 26, 2021

Q A
Branch? 5.x
Bug fix? no
New feature? no
Deprecations? no
Tickets Partially revert #37265
License MIT
Doc PR -

#37265 was created as a fix for #37100. However, when #37327 was merged, the original bug was also fixed with a different solution (allowing null values on InputBag::set) and parts of #37265 are not needed anymore. By using only InputBag as the $request bag we can tighten the typehint again and make static analysis a bit more useful.

@acasademont
Copy link
Contributor Author

PS: Also not sure if this should be considered a bugfix and merged into 5.2.x instead of 5.x

@nicolas-grekas
Copy link
Member

nicolas-grekas commented Feb 26, 2021

Thinking a bit more about this, there might still be an issue when using JSON payloads: the InputBag::get() returns only string|null. That means booleans, integers, floats found in JSONs will be lost.

@nicolas-grekas nicolas-grekas added this to the 5.x milestone Feb 26, 2021
@acasademont
Copy link
Contributor Author

acasademont commented Feb 26, 2021

@nicolas-grekas that's only because of the phpdoc typehint, there's nothing preventing an int|bool|float from being returned. Maybe the typehint of InputBag should be string|int|bool|float|null?

@nicolas-grekas
Copy link
Member

Can you update the type hints so that we can discuss the full change that would be needed?

@nicolas-grekas
Copy link
Member

(and add related tests btw)

@acasademont
Copy link
Contributor Author

Added the missing scalar type hints and some testing

$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->assertFalse($bag->get('bool'), '->get() gets the value of a bool parameter');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps assert eg. $default=true is never casted to string

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, sorry I didn't see your comment!

Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM thanks.
@ro0NL's suggestion is a good one, can you update the PR if you agree?

@fabpot
Copy link
Member

fabpot commented Mar 12, 2021

Thank you @acasademont.

@fabpot fabpot merged commit 7754bed into symfony:5.x Mar 12, 2021
@acasademont acasademont deleted the use_input_bag branch March 12, 2021 09:17
@acasademont
Copy link
Contributor Author

Ah! Thx @fabpot ! I was planning to update the tests this weekend, I'll submit another PR for them.

@dvdknaap
Copy link

dvdknaap commented Jun 11, 2021

Why are arrays not allowed anymore ?

This means if you got an form with checkboxes it's not allowed anymore.

Edit: @acasademont @fabpot Why is the set accepting arrays but the get not could this be fixed asap please?

@olsavmic
Copy link
Contributor

olsavmic commented Jul 8, 2021

Why are arrays not allowed anymore ?

This means if you got an form with checkboxes it's not allowed anymore.

Edit: @acasademont @fabpot Why is the set accepting arrays but the get not could this be fixed asap please?

I guess you're supposed to access those nested arrays by calling $inputBag->all('key'). It's slightly misleading though now, I also stumbled upon this.
See: #41766

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants