Skip to content

[9.x] Manually populate POST request body with JSON data only when required #37921

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 5 commits into from
Aug 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/Illuminate/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,9 @@ public static function createFromBase(SymfonyRequest $request)

$newRequest->content = $request->content;

$newRequest->request = $newRequest->getInputSource();
if ($newRequest->isJson()) {
$newRequest->request = $newRequest->json();
}

return $newRequest;
}
Expand Down
25 changes: 24 additions & 1 deletion tests/Http/HttpRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -993,7 +993,12 @@ public function testFingerprintWithoutRoute()
$request->fingerprint();
}

public function testCreateFromBase()
/**
* Ensure JSON GET requests populate $request->request with the JSON content.
*
* @link https://github.com/laravel/framework/pull/7052 Correctly fill the $request->request parameter bag on creation.
*/
public function testJsonRequestFillsRequestBodyParams()
{
$body = [
'foo' => 'bar',
Expand All @@ -1011,6 +1016,24 @@ public function testCreateFromBase()
$this->assertEquals($request->request->all(), $body);
}

/**
* Ensure non-JSON GET requests don't pollute $request->request with the GET parameters.
*
* @link https://github.com/laravel/framework/pull/37921 Manually populate POST request body with JSON data only when required.
*/
public function testNonJsonRequestDoesntFillRequestBodyParams()
{
$params = ['foo' => 'bar'];

$getRequest = Request::create('/', 'GET', $params, [], [], []);
$this->assertEquals($getRequest->request->all(), []);
$this->assertEquals($getRequest->query->all(), $params);

$postRequest = Request::create('/', 'POST', $params, [], [], []);
$this->assertEquals($postRequest->request->all(), $params);
$this->assertEquals($postRequest->query->all(), []);
}

/**
* Tests for Http\Request magic methods `__get()` and `__isset()`.
*
Expand Down