From 010f0b933cb2510544572d2019cf04e72b91accd Mon Sep 17 00:00:00 2001 From: Tito Duarte Date: Tue, 15 Aug 2023 19:21:10 +0100 Subject: [PATCH] [BUGFIX] - Partial-Data; Fixed correct handling of header data [BUGFIX] - Partial-Data; check component for valid partial request --- src/Service/Inertia.php | 4 ++-- test/Service/InertiaTest.php | 10 ++++------ 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/Service/Inertia.php b/src/Service/Inertia.php index ae4bb50..ec25876 100644 --- a/src/Service/Inertia.php +++ b/src/Service/Inertia.php @@ -38,8 +38,8 @@ public function render(string $component, array $props = []): ResponseInterface ->withUrl((string)$this->request->getUri()); if ($this->request->hasHeader('X-Inertia-Partial-Data')) { - $only = $this->request->getHeader('X-Inertia-Partial-Data'); - $props = ($only && $this->request->getHeaderLine('X-Inertia-Partial-Component')) + $only = explode(',', $this->request->getHeaderLine('X-Inertia-Partial-Data')); + $props = ($only && $this->request->getHeaderLine('X-Inertia-Partial-Component') === $component) ? array_intersect_key($props, array_flip((array) $only)) : $props; } diff --git a/test/Service/InertiaTest.php b/test/Service/InertiaTest.php index b800c2c..ff7aa1d 100644 --- a/test/Service/InertiaTest.php +++ b/test/Service/InertiaTest.php @@ -98,11 +98,9 @@ public function testRenderReturnPartialDataWhenHeaderContainsPartialData() $request = $this->prophesize(ServerRequestInterface::class); $request->hasHeader('X-Inertia')->willReturn(true); $request->hasHeader('X-Inertia-Partial-Data')->willReturn(true); - $request->getHeaderLine('X-Inertia-Partial-Component')->willReturn(Argument::type('string')); - $request->getHeader('X-Inertia-Partial-Data')->willReturn([ - 'key2' - ]); - $json = '{"component":"type(string)","props":{"key2":"value2"},"url":"callback()","version":null}'; + $request->getHeaderLine('X-Inertia-Partial-Component')->willReturn('component'); + $request->getHeaderLine('X-Inertia-Partial-Data')->willReturn('key2'); + $json = '{"component":"component","props":{"key2":"value2"},"url":"callback()","version":null}'; $jsonResponse = null; $uri = $this->prophesize(UriInterface::class); @@ -133,7 +131,7 @@ public function testRenderReturnPartialDataWhenHeaderContainsPartialData() ); $returnedResponse = $inertia->render( - Argument::type('string'), + 'component', [ 'key1' => fn() => 'value1', 'key2' => fn() => 'value2'