Skip to content

Commit 507b0a0

Browse files
committed
Fix: Dot props should be removed after unpacking
1 parent add5a92 commit 507b0a0

File tree

2 files changed

+49
-10
lines changed

2 files changed

+49
-10
lines changed

src/Response.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Illuminate\Support\Arr;
1414
use Illuminate\Support\Facades\App;
1515
use Illuminate\Support\Facades\Response as ResponseFactory;
16+
use Illuminate\Support\Str;
1617
use Illuminate\Support\Traits\Macroable;
1718

1819
class Response implements Responsable
@@ -118,9 +119,10 @@ public function toResponse($request)
118119
*
119120
* @param array $props
120121
* @param \Illuminate\Http\Request $request
122+
* @param bool $unpackDotProps
121123
* @return array
122124
*/
123-
public function resolvePropertyInstances(array $props, Request $request): array
125+
public function resolvePropertyInstances(array $props, Request $request, bool $unpackDotProps = true): array
124126
{
125127
foreach ($props as $key => $value) {
126128
if ($value instanceof Closure) {
@@ -144,10 +146,15 @@ public function resolvePropertyInstances(array $props, Request $request): array
144146
}
145147

146148
if (is_array($value)) {
147-
$value = $this->resolvePropertyInstances($value, $request);
149+
$value = $this->resolvePropertyInstances($value, $request, false);
148150
}
149151

150-
Arr::set($props, $key, $value);
152+
if ($unpackDotProps && str_contains($key, '.')) {
153+
Arr::set($props, $key, $value);
154+
unset($props[$key]);
155+
} else {
156+
$props[$key] = $value;
157+
}
151158
}
152159

153160
return $props;

tests/ResponseTest.php

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -292,26 +292,58 @@ public function test_lazy_props_are_included_in_partial_reload(): void
292292
$this->assertSame('A lazy value', $page->props->lazy);
293293
}
294294

295-
public function test_can_nest_props_using_dot_notation(): void
295+
public function test_top_level_dot_props_get_unpacked(): void
296296
{
297+
$props = [
298+
'auth' => [
299+
'user' => [
300+
'name' => 'Jonathan Reinink',
301+
],
302+
],
303+
'auth.user.can' => [
304+
'do.stuff' => true,
305+
],
306+
'product' => ['name' => 'My example product'],
307+
];
308+
297309
$request = Request::create('/products/123', 'GET');
310+
$request->headers->add(['X-Inertia' => 'true']);
311+
312+
$response = new Response('User/Edit',$props, 'app', '123');
313+
$response = $response->toResponse($request);
314+
$page = $response->getData(true);
315+
316+
$user = $page['props']['auth']['user'];
317+
$this->assertSame('Jonathan Reinink', $user['name']);
318+
$this->assertTrue($user['can']['do.stuff']);
319+
$this->assertFalse(array_key_exists('auth.user.can', $page['props']));
320+
}
298321

322+
public function test_nested_dot_props_do_not_get_unpacked(): void
323+
{
299324
$props = [
300325
'auth' => [
326+
'user.can' => [
327+
'do.stuff' => true,
328+
],
301329
'user' => [
302330
'name' => 'Jonathan Reinink',
303331
],
304332
],
305-
'auth.user.can.deleteProducts' => true,
306333
'product' => ['name' => 'My example product'],
307334
];
308-
$response = new Response('Products/Edit', $props, 'app', '123');
335+
336+
$request = Request::create('/products/123', 'GET');
337+
$request->headers->add(['X-Inertia' => 'true']);
338+
339+
$response = new Response('User/Edit',$props, 'app', '123');
309340
$response = $response->toResponse($request);
310-
$view = $response->getOriginalContent();
311-
$user = $view->getData()['page']['props']['auth']['user'];
341+
$page = $response->getData(true);
312342

313-
$this->assertSame('Jonathan Reinink', $user['name']);
314-
$this->assertTrue($user['can']['deleteProducts']);
343+
$auth = $page['props']['auth'];
344+
$this->assertSame('Jonathan Reinink', $auth['user']['name']);
345+
$this->assertTrue($auth['user.can']['do.stuff']);
346+
$this->assertFalse(array_key_exists('can', $auth));
315347
}
316348

317349
public function test_responsable_with_invalid_key(): void

0 commit comments

Comments
 (0)