-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[HttpFoundation] Add Request::toArray() for JSON content #38224
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
Conversation
src/Symfony/Component/HttpFoundation/Exception/JsonException.php
Outdated
Show resolved
Hide resolved
what about using an InputBag for return? That way we're effectively decoding the request body (payload) as done when using Which raises another question, can we provide decoded payload from |
e.g. if ($_POST) {
$this->request = new InputBag($_POST);
} elseif($this->getContent()) {
$this->request = InputBag::fromString($this->getContent()[, $contentType]);
} |
really, we get HTTP 400 for free this way :) |
I don't have so much to say about the implementation but I have the same use case. I added a trait in my application to solve this issue, but having something native / in core would be really nice. |
cdf866d
to
cf4ed5a
Compare
Thank you for the reviews and feedback.
@ro0NL Im not sure what you mean. How is it lazy and what is the benefit? Note that there is technically a difference between |
cf4ed5a
to
bc52f2d
Compare
e6ae8c8
to
899ef81
Compare
AFAIK the $_POST vars are obtained from the request content. So this is about providing consistent access, to a decoded request body, using The lazy part is about decoding JSON only when needed ;) If $_POST is filled, i think we can assume it always supersedes. |
Tests failures seems related though https://ci.appveyor.com/project/fabpot/symfony/builds/35272847#L1155.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The PR header still uses getContentArray() in the example code, can you please rework this to match toArray() ? Thanks
Oops, Sorry. The code example in the PR header is updated. |
Thank you! 🙏 |
(rebase needed) |
9881df2
to
1a24974
Compare
Thank you for the ping. The PR is rebased |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍🏼
1a24974
to
83c1a26
Compare
Thank you @Nyholm. |
Thank you for merging |
This PR was merged into the master branch. Discussion ---------- Adding docs about Request::toArray() This will fix #14323. The related PR is found here: symfony/symfony#38224 Commits ------- fd03b5e Adding docs about Request::toArray()
…Campbell) This PR was merged into the 5.2-dev branch. Discussion ---------- Remove array return type from Request::toArray() | Q | A | ------------- | --- | Branch? | master | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #38400 | License | MIT | Doc PR | - Laravel already extends Symfony's `Request` class and defines it's own `toArray` method. #38224 added a new `toArray` method to this class with a different signature to the one that is in Laravel, causing fatal errors (laravel/framework#34660). I think the best course of action here is to remove the return type for now, and only add it in Symfony 6. This will allow Symfony 6.0 and Laravel 11 to synchronize adding the return type. Older versions of Laravel can't just change their signature to add an array return type to them, because that would be a breaking change for Laravel users extending Laravel's request class. I'm thinking, in particular, API packages and the like, or just straight up application code. Commits ------- 8b291a4 Remove array return type from Request::toArray()
The past few months I've been working more and more with javascript and APIs. I've written controllers that parse json from the request body. I've found myself copying code between projects so I looked at the possibility to add this code to the
Request
class itself.Usage
I've searched but I have not found that this has been proposed before.. With is strange.. ¯\(ツ)/¯