Skip to content
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

[10.x] Migrate JSON data to shared InputBag #47914

Merged
merged 3 commits into from Jul 31, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -1571,4 +1571,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'));
}
}