diff --git a/src/PropertyContext.php b/src/PropertyContext.php new file mode 100644 index 00000000..8375850e --- /dev/null +++ b/src/PropertyContext.php @@ -0,0 +1,16 @@ + $value) { $resolveViaApp = collect([ @@ -270,6 +270,12 @@ public function resolvePropertyInstances(array $props, Request $request): array $value = App::call($value); } + $currentKey = $parentKey ? $parentKey.'.'.$key : $key; + + if ($value instanceof ProvidesInertiaProperty) { + $value = $value->toInertiaProperty(new PropertyContext($currentKey, $props, $request)); + } + if ($value instanceof Arrayable) { $value = $value->toArray(); } @@ -287,7 +293,7 @@ public function resolvePropertyInstances(array $props, Request $request): array } if (is_array($value)) { - $value = $this->resolvePropertyInstances($value, $request); + $value = $this->resolvePropertyInstances($value, $request, $currentKey); } $props[$key] = $value; diff --git a/tests/ResponseTest.php b/tests/ResponseTest.php index 9763386a..c10c6dc8 100644 --- a/tests/ResponseTest.php +++ b/tests/ResponseTest.php @@ -14,10 +14,12 @@ use Illuminate\View\View; use Inertia\AlwaysProp; use Inertia\DeferProp; +use Inertia\Inertia; use Inertia\LazyProp; use Inertia\MergeProp; use Inertia\Response; use Inertia\Tests\Stubs\FakeResource; +use Inertia\Tests\Stubs\MergeWithSharedProp; use Mockery; class ResponseTest extends TestCase @@ -820,6 +822,29 @@ public function test_always_props_are_included_on_partial_reload(): void $this->assertFalse(isset($page->props->user)); } + public function test_inertia_response_type_prop(): void + { + $request = Request::create('/user/123', 'GET'); + + Inertia::share('items', ['foo']); + Inertia::share('deep.foo.bar', ['foo']); + + $response = new Response('User/Edit', [ + 'items' => new MergeWithSharedProp(['bar']), + 'deep' => [ + 'foo' => [ + 'bar' => new MergeWithSharedProp(['baz']), + ], + ], + ], 'app', '123'); + $response = $response->toResponse($request); + $view = $response->getOriginalContent(); + $page = $view->getData()['page']; + + $this->assertSame(['foo', 'bar'], $page['props']['items']); + $this->assertSame(['foo', 'baz'], $page['props']['deep']['foo']['bar']); + } + public function test_top_level_dot_props_get_unpacked(): void { $props = [ diff --git a/tests/Stubs/MergeWithSharedProp.php b/tests/Stubs/MergeWithSharedProp.php new file mode 100644 index 00000000..a4121b92 --- /dev/null +++ b/tests/Stubs/MergeWithSharedProp.php @@ -0,0 +1,17 @@ +key, []), $this->items); + } +}