Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[HttpFoundation] Add ParameterBag::getString() and deprecate accepting invalid values #48525

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
Mar 19, 2023

Conversation

GromNaN
Copy link
Member

@GromNaN GromNaN commented Dec 6, 2022

Q A
Branch? 6.3
Bug fix? no
New feature? yes
Deprecations? yes
Tickets Fix #44787 #48219
License MIT
Doc PR symfony/symfony-docs#...

There were a lot of discussions on #44787 regarding the implementation. The main debate is to determine the right behavior when the value is invalid: try to convert the value, throw an exception, return the default value or the falsy?

I added plenty of test cases for the methods getAlpha, getAlnum, getDigits, getInt, getInteger, getString so that we can discuss the expected result for each input value.

My goals:

  • Provide safe methods to get values in the expected type. Example: The parameters being generally of type string, the getString method is useful to be sure we don't get an array.
  • Reduce deprecations on methods that exist since Symfony 2.0, in one of the most popular Symfony component.

How are these getter methods used?

  • Most of the time, parameter values are string (from routing, query string, request payload). get is used but does not validate the return type.
  • getInt is used for pagination (UX Autocomplete) or getting an index (EasyAdmin)
  • I think there was an unidentified breaking change when we introduced return types #42507. Methods getAlpha, getAlnum and getDigits could return an array, but their return type have been modified to string, resulting in a TypeError. example of use

@carsonbot carsonbot changed the title [HttpFoundation] Add ParameterBag::getString and replace getInt with getInteger [HttpFoundation] Add ParameterBag::getString and replace getInt with getInteger Dec 6, 2022
@carsonbot carsonbot added this to the 6.3 milestone Dec 6, 2022
@GromNaN GromNaN force-pushed the parameter-bag-get-string branch from b65b7fc to ce6910d Compare December 6, 2022 23:52
src/Symfony/Component/HttpFoundation/ParameterBag.php Outdated Show resolved Hide resolved
@GromNaN GromNaN force-pushed the parameter-bag-get-string branch 2 times, most recently from 0446360 to 231f96c Compare December 9, 2022 15:38
@GromNaN GromNaN changed the title [HttpFoundation] Add ParameterBag::getString and replace getInt with getInteger [HttpFoundation] Add ParameterBag::getString and add 3rd argument to make getInt more strict Dec 9, 2022
@GromNaN GromNaN force-pushed the parameter-bag-get-string branch 2 times, most recently from 880f74d to 9717f40 Compare December 9, 2022 16:36
src/Symfony/Component/HttpFoundation/InputBag.php Outdated Show resolved Hide resolved
UPGRADE-6.3.md Outdated Show resolved Hide resolved
@GromNaN GromNaN force-pushed the parameter-bag-get-string branch 2 times, most recently from 522a590 to b5c7ee3 Compare December 14, 2022 21:56
src/Symfony/Component/HttpFoundation/ParameterBag.php Outdated Show resolved Hide resolved
src/Symfony/Component/HttpFoundation/ParameterBag.php Outdated Show resolved Hide resolved
src/Symfony/Component/HttpFoundation/InputBag.php Outdated Show resolved Hide resolved
UPGRADE-6.3.md Outdated Show resolved Hide resolved
src/Symfony/Component/HttpFoundation/CHANGELOG.md Outdated Show resolved Hide resolved
src/Symfony/Component/HttpFoundation/CHANGELOG.md Outdated Show resolved Hide resolved
src/Symfony/Component/HttpFoundation/InputBag.php Outdated Show resolved Hide resolved
@GromNaN GromNaN force-pushed the parameter-bag-get-string branch 2 times, most recently from 910e200 to 166e0ad Compare December 15, 2022 19:40
@GromNaN GromNaN requested review from nicolas-grekas, ro0NL and fabpot and removed request for ro0NL December 15, 2022 19:40
nicolas-grekas

This comment was marked as outdated.

@GromNaN
Copy link
Member Author

GromNaN commented Dec 22, 2022

I don't really like the extra argument to handle the BC layer because it means we're going to have to deprecate it in 7.1 + remove it in 8.0. Looks like a costly change. But I don't have a better idea other than just changing the behavior on 6.3 without this layer...

I'll take care of this process 😃.

But I am also tempted to change the behavior directly in 6.3. The modification to keep the current behavior is to replace $parameters->getInt('page') by (int) $parameters->get('page'). This change in user code is compatible with all previous version of Symfony and could be applied as a bugfix in all existing codebase that want to fallback to 0 in case of invalid value.

@GromNaN GromNaN force-pushed the parameter-bag-get-string branch from 166e0ad to 8862249 Compare December 23, 2022 19:04
UPGRADE-6.3.md Outdated Show resolved Hide resolved
UPGRADE-6.3.md Show resolved Hide resolved
src/Symfony/Component/HttpFoundation/CHANGELOG.md Outdated Show resolved Hide resolved
@GromNaN GromNaN force-pushed the parameter-bag-get-string branch 2 times, most recently from c5b893e to bb1bf04 Compare March 16, 2023 15:05
src/Symfony/Component/HttpFoundation/ParameterBag.php Outdated Show resolved Hide resolved
@GromNaN GromNaN force-pushed the parameter-bag-get-string branch from ccea2a3 to 70261b3 Compare March 16, 2023 16:39
@GromNaN
Copy link
Member Author

GromNaN commented Mar 16, 2023

or better: we (plan to) throw unless FILTER_NULL_ON_FAILURE is explicitly set, and we drop the error_on_failure option

This is a brilliant idea, let me explain it for the readers.
Historically, filter_var returns false in case of invalid data. But this is a design problem because you can't tell a false from FILTER_VALIDATE_BOOLEAN from a false from an invalid value. So FILTER_NULL_ON_FAILURE was introduced in PHP 5.2 to allow differentiating errors. So we should always use this flag.
We will use the absence of this flag to define our default behavior in Symfony 7.0: throw an exception in case of invalid data.

@nicolas-grekas nicolas-grekas changed the title [HttpFoundation] Add ParameterBag::getString and add 3rd argument to make getInt more strict [HttpFoundation] Add ParameterBag::getString() and deprecate accepting invalid values Mar 16, 2023
@nicolas-grekas nicolas-grekas force-pushed the parameter-bag-get-string branch 2 times, most recently from ce97141 to 2fff7e5 Compare March 17, 2023 09:51
@nicolas-grekas nicolas-grekas force-pushed the parameter-bag-get-string branch from 2fff7e5 to 172f0a7 Compare March 17, 2023 10:04
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.

(I improved the deprecation message)

src/Symfony/Component/HttpFoundation/InputBag.php Outdated Show resolved Hide resolved
@fabpot
Copy link
Member

fabpot commented Mar 19, 2023

Thank you @GromNaN.

@fabpot fabpot merged commit 6adba59 into symfony:6.3 Mar 19, 2023
@GromNaN GromNaN deleted the parameter-bag-get-string branch March 20, 2023 07:51
nicolas-grekas added a commit that referenced this pull request Jun 30, 2023
… behaviors (GromNaN)

This PR was squashed before being merged into the 7.0 branch.

Discussion
----------

[HttpFoundation] Remove deprecated classes, method and behaviors

| Q             | A
| ------------- | ---
| Branch?       | 7.0
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Tickets       | -
| License       | MIT
| Doc PR        | n/a

Clean `symfony/http-foundation` from all its legacy.

- Remove `RequestMatcher` and `ExpressionRequestMatcher`,
  deprecated since #47595
- Remove `Request::getContentType()`,
  deprecated since #45034
- Throw a `UnexpectedValueException` or `BadRequestException` when `ParameterBag::filter()` or `InputBag::filter()` reads an invalid value and the flag `FILTER_NULL_ON_FAILURE` is not set.
  new behavior announced since #48525
- Throw a `InvalidArgumentException` when calling `Request::create()` with a malformed URI,
  deprecated since #49376

Commits
-------

665a775 [HttpFoundation] Remove deprecated classes, method and behaviors
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.

7 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.