diff --git a/src/Illuminate/Http/Request.php b/src/Illuminate/Http/Request.php index 2da2efd95d16..624607035905 100644 --- a/src/Illuminate/Http/Request.php +++ b/src/Illuminate/Http/Request.php @@ -12,7 +12,6 @@ use RuntimeException; use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException; use Symfony\Component\HttpFoundation\InputBag; -use Symfony\Component\HttpFoundation\ParameterBag; use Symfony\Component\HttpFoundation\Request as SymfonyRequest; use Symfony\Component\HttpFoundation\Session\SessionInterface; @@ -404,7 +403,7 @@ public function get(string $key, mixed $default = null): mixed public function json($key = null, $default = null) { if (! isset($this->json)) { - $this->json = new ParameterBag((array) json_decode($this->getContent(), true)); + $this->json = new InputBag((array) json_decode($this->getContent(), true)); } if (is_null($key)) { @@ -488,7 +487,7 @@ public static function createFromBase(SymfonyRequest $request) $newRequest->content = $request->content; if ($newRequest->isJson()) { - $newRequest->request = new InputBag($newRequest->json()->all()); + $newRequest->request = $newRequest->json(); } return $newRequest; diff --git a/tests/Http/HttpRequestTest.php b/tests/Http/HttpRequestTest.php index c1c9aef8bd09..67090376e8b6 100644 --- a/tests/Http/HttpRequestTest.php +++ b/tests/Http/HttpRequestTest.php @@ -1574,4 +1574,34 @@ public function testGeneratingJsonRequestFromParentRequestUsesCorrectType() $this->assertInstanceOf(InputBag::class, $request->getPayload()); $this->assertSame('world', $request->getPayload()->get('hello')); } + + public function testJsonRequestsCanMergeDataIntoJsonRequest() + { + if (! method_exists(SymfonyRequest::class, 'getPayload')) { + return; + } + + $base = SymfonyRequest::create('/', 'POST', server: ['CONTENT_TYPE' => 'application/json'], content: '{"first":"Taylor","last":"Otwell"}'); + $request = Request::createFromBase($base); + + $request->merge([ + 'name' => $request->get('first').' '.$request->get('last'), + ]); + + $this->assertSame('Taylor Otwell', $request->get('name')); + } + + public function testItCanHaveObjectsInJsonPayload() + { + if (! method_exists(SymfonyRequest::class, 'getPayload')) { + return; + } + + $base = SymfonyRequest::create('/', 'POST', server: ['CONTENT_TYPE' => 'application/json'], content: '{"framework":{"name":"Laravel"}}'); + $request = Request::createFromBase($base); + + $value = $request->get('framework'); + + $this->assertSame(['name' => 'Laravel'], $request->get('framework')); + } }