Skip to content

Commit

Permalink
fix: backported #47914 (#47919)
Browse files Browse the repository at this point in the history
  • Loading branch information
ImJustToNy committed Aug 1, 2023
1 parent aeb8205 commit 4655805
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
5 changes: 2 additions & 3 deletions src/Illuminate/Http/Request.php
Expand Up @@ -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;

Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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;
Expand Down
30 changes: 30 additions & 0 deletions tests/Http/HttpRequestTest.php
Expand Up @@ -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'));
}
}

0 comments on commit 4655805

Please sign in to comment.