Skip to content

Commit

Permalink
Call toArray() on Arrayable props resolved from the Container
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalbaljet committed Dec 17, 2024
1 parent 0259e37 commit 443c78b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
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

0 comments on commit 443c78b

Please sign in to comment.