Skip to content

[HttpFoundation] Adding getString() method to ParameterBag #48219

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

Closed
silasjoisten opened this issue Nov 16, 2022 · 2 comments
Closed

[HttpFoundation] Adding getString() method to ParameterBag #48219

silasjoisten opened this issue Nov 16, 2022 · 2 comments

Comments

@silasjoisten
Copy link
Contributor

silasjoisten commented Nov 16, 2022

Description

I have seen that there are several different ways to get a parameter from the parameter bag. For example

  • getBoolean()
  • getInteger()
  • getAlpha()
  • getAlnum()
  • etc.

It would be a nice small feature to add a function which returns a string. Benefit would be you dont have to cast the type always by yourself and have a strict type

Example

// src/Security/FormAuthenticator.php

class FormAuthenticator extends AbstractLoginFormAuthenticator
{
  // ...
   public function authenticate(Request $request): Passport
      {
         -$username = (string)$request->request->get('username', '');
         +$username = $request->request->getString('username');
         -$password = (string)$request->request->get('password', '');
         +$password = $request->request->getString('password');
         -$csrfToken = (string)$request->request->get('_csrf_token', '');
         +$csrfToken = $request->request->getString('_csrf_token');
  
         return new Passport(
             new UserBadge($username),
             new PasswordCredentials($password),
             [new CsrfTokenBadge('authentication', $csrfToken)],
         );
      }

// ...
}
@GromNaN
Copy link
Member

GromNaN commented Nov 16, 2022

Hello @silasjoisten, we have an open PR for this #44787.

@silasjoisten
Copy link
Contributor Author

@GromNaN Nice thank you i haven't seen the PR.

fabpot added a commit that referenced this issue Mar 19, 2023
…eprecate accepting invalid values (GromNaN)

This PR was merged into the 6.3 branch.

Discussion
----------

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

| 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)](https://github.com/symfony/ux-autocomplete/blob/697f1cb4914480b811d978efe031a6f4a0dc3814/src/Controller/EntityAutocompleteController.php#L41) or [getting an index (EasyAdmin)](https://github.com/EasyCorp/EasyAdminBundle/blob/f210bd1e494b699dec54d2ef29302db1211267b5/src/Context/AdminContext.php#L157-L158)
- I think there was an unidentified breaking change when we introduced return types [#42507](https://github.com/symfony/symfony/pull/42507/files#diff-04f3b8ea71029f48853b804129631aeb9bf3dad7a7a398feb9568bf5397d0e69L117). 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](https://github.com/asaaa1997/Licencjat/blob/8c10abaf87120c904557977ae14284c7d443c9a7/src/Controller/QuizController.php#L164)

Commits
-------

172f0a7 [HttpFoundation] Add `ParameterBag::getString()` and deprecate accepting invalid values
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants