Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[HttpFoundation] Add InputBag #13543

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
fabpot opened this issue Apr 13, 2020 · 7 comments
Closed

[HttpFoundation] Add InputBag #13543

fabpot opened this issue Apr 13, 2020 · 7 comments
Labels
help wanted Issues and PRs which are looking for volunteers to complete them. HttpFoundation Stalled
Milestone

Comments

@fabpot
Copy link
Member

fabpot commented Apr 13, 2020

Q A
Feature PR symfony/symfony#34363
PR author(s) @azjezz
Merged in 5.1-dev
@xabbuh xabbuh added this to the 5.1 milestone Apr 14, 2020
@javiereguiluz
Copy link
Member

We should take care of this as soon as possible. We're getting some issues from people who is confused about this: symfony/symfony#37029, symfony/symfony#36725, symfony/symfony#37229, etc.

@azjezz explained to me that this is how the new code works:

// new method -> equivalent code
get('foo') is getString('foo')
all('foo') is getArray('foo')
all() is getAll()
all()['foo'] ?? null is getStringOrArrayOrNull('foo')

Any volunteer to provide these docs?

@javiereguiluz javiereguiluz added the help wanted Issues and PRs which are looking for volunteers to complete them. label Jun 12, 2020
@javiereguiluz
Copy link
Member

javiereguiluz commented Jun 12, 2020

Some more information:

The problem solved by InputBag is that when the URL is https://example.com/something?q[]=faa if you do a ->get('q') you get a 500 error because the query param is an array and not a string (it should be q=faa not q[]=faa). The expected behavior is not a 500 error but a 400 error.

Thanks to InputBag this now shows a deprecation, but in Symfony 6 it will generate the expected 400 error.

@azjezz
Copy link
Contributor

azjezz commented Jun 12, 2020

The problem solved by InputBag is that when the URL is https://example.com/something?q[]=faa if you do a ->get('q') you get a 500 error because the query param is an array and not a string (it should be q=faa not q[]=faa). The expected behavior is not a 500 error but a 400 error.

To explain more, the 500 error doesn't result from get('q') itself, but the return value will be an array, the developer might think that the return value is always a string as that's what they are expecting, but it's not. as soon as that variable is passed to a function/method that expects a string, a 500 error will raise.

e.g:

class FooRepository
{
  // code ..

  /**
   * Search for all the foos containing the given keyword.
   */
  public function search(string $keyword): iterable
  {

  }

  // code ...
}
final class FooController extends AbstractController
{
  private FooRepository $foos;

  public function __construct(FooRepository $foos)
  {
    $this->foos = $foos;
  }

  /**
   * @Route('/foo/search', methods={'GET'}, name='foo_search')
   */
  public function search(Request $request): Response
  {
    $keyword = $request->request->get('q', null);
    if (null === $keyword) {
      $foos = [];
    } else {
      $foos = $this->foos->search($keyword);
    }

    // render foos
  }

}

in the example above, the expected value for q( $keyword ), is string ( q=bar ), however, passing an array ( q[]=bar ) will result in a 500 server error.

ÌnputBag is the first step toward fixing this, by triggering a deprecation when an array is encountered, this tells the developer :

if you are expecting this array, use all('q'), if you are expecting a string, ignore this deprecation, Symfony 6 will throw an exception with status code 400 back to the client.

@azjezz
Copy link
Contributor

azjezz commented Jun 12, 2020

if you are expecting this array, use all('q')

Note: all('q') is another new feature in InputBag if the value of q is a string, all('q') will not trigger a deprecation, instead it will throw an exception with status code 400 :)

@carsonbot
Copy link
Collaborator

Thank you for this issue.
There has not been a lot of activity here for a while. Has this been resolved?

@carsonbot
Copy link
Collaborator

Friendly reminder that this issue exists. If I don't hear anything I'll close this.

@carsonbot
Copy link
Collaborator

Hey,

I didn't hear anything so I'm going to close it. Feel free to comment if this is still relevant, I can always reopen!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Issues and PRs which are looking for volunteers to complete them. HttpFoundation Stalled
Projects
None yet
Development

No branches or pull requests

5 participants