diff --git a/src/Illuminate/Http/Request.php b/src/Illuminate/Http/Request.php index 06f143c6020f..b0f59fe7d175 100644 --- a/src/Illuminate/Http/Request.php +++ b/src/Illuminate/Http/Request.php @@ -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; } diff --git a/tests/Http/HttpRequestTest.php b/tests/Http/HttpRequestTest.php index c019499419f6..36435677b73c 100644 --- a/tests/Http/HttpRequestTest.php +++ b/tests/Http/HttpRequestTest.php @@ -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', @@ -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()`. *