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

[2.x] Call toArray() on Arrayable props resolved from the Container #696

Open
wants to merge 1 commit into
base: 2.x
Choose a base branch
from
Open
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: 4 additions & 0 deletions src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,10 @@ public function resolvePropertyInstances(array $props, Request $request): array
$value = App::call($value);
}

if ($value instanceof Arrayable) {
$value = $value->toArray();
}

if ($value instanceof PromiseInterface) {
$value = $value->wait();
}
Expand Down
26 changes: 26 additions & 0 deletions tests/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Inertia\Tests;

use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
Expand Down Expand Up @@ -584,6 +585,31 @@ public function test_lazy_props_are_included_in_partial_reload(): void
$this->assertSame('A lazy value', $page->props->lazy);
}

public function test_defer_arrayable_props_are_resolved_in_partial_reload(): void
{
$request = Request::create('/users', 'GET');
$request->headers->add(['X-Inertia' => 'true']);
$request->headers->add(['X-Inertia-Partial-Component' => 'Users']);
$request->headers->add(['X-Inertia-Partial-Data' => 'defer']);

$deferProp = new DeferProp(function () {
return new class implements Arrayable
{
public function toArray()
{
return ['foo' => 'bar'];
}
};
});

$response = new Response('Users', ['users' => [], 'defer' => $deferProp], 'app', '123');
$response = $response->toResponse($request);
$page = $response->getData();

$this->assertFalse(property_exists($page->props, 'users'));
$this->assertEquals((object) ['foo' => 'bar'], $page->props->defer);
}

public function test_always_props_are_included_on_partial_reload(): void
{
$request = Request::create('/user/123', 'GET');
Expand Down