Skip to content

Commit

Permalink
Fix: Dot props should be removed after unpacking
Browse files Browse the repository at this point in the history
  • Loading branch information
claudiodekker committed Jan 18, 2022
1 parent add5a92 commit 507b0a0
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 10 deletions.
13 changes: 10 additions & 3 deletions src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Response as ResponseFactory;
use Illuminate\Support\Str;
use Illuminate\Support\Traits\Macroable;

class Response implements Responsable
Expand Down Expand Up @@ -118,9 +119,10 @@ public function toResponse($request)
*
* @param array $props
* @param \Illuminate\Http\Request $request
* @param bool $unpackDotProps
* @return array
*/
public function resolvePropertyInstances(array $props, Request $request): array
public function resolvePropertyInstances(array $props, Request $request, bool $unpackDotProps = true): array
{
foreach ($props as $key => $value) {
if ($value instanceof Closure) {
Expand All @@ -144,10 +146,15 @@ public function resolvePropertyInstances(array $props, Request $request): array
}

if (is_array($value)) {
$value = $this->resolvePropertyInstances($value, $request);
$value = $this->resolvePropertyInstances($value, $request, false);
}

Arr::set($props, $key, $value);
if ($unpackDotProps && str_contains($key, '.')) {
Arr::set($props, $key, $value);
unset($props[$key]);
} else {
$props[$key] = $value;
}
}

return $props;
Expand Down
46 changes: 39 additions & 7 deletions tests/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,26 +292,58 @@ public function test_lazy_props_are_included_in_partial_reload(): void
$this->assertSame('A lazy value', $page->props->lazy);
}

public function test_can_nest_props_using_dot_notation(): void
public function test_top_level_dot_props_get_unpacked(): void
{
$props = [
'auth' => [
'user' => [
'name' => 'Jonathan Reinink',
],
],
'auth.user.can' => [
'do.stuff' => true,
],
'product' => ['name' => 'My example product'],
];

$request = Request::create('/products/123', 'GET');
$request->headers->add(['X-Inertia' => 'true']);

$response = new Response('User/Edit',$props, 'app', '123');
$response = $response->toResponse($request);
$page = $response->getData(true);

$user = $page['props']['auth']['user'];
$this->assertSame('Jonathan Reinink', $user['name']);
$this->assertTrue($user['can']['do.stuff']);
$this->assertFalse(array_key_exists('auth.user.can', $page['props']));
}

public function test_nested_dot_props_do_not_get_unpacked(): void
{
$props = [
'auth' => [
'user.can' => [
'do.stuff' => true,
],
'user' => [
'name' => 'Jonathan Reinink',
],
],
'auth.user.can.deleteProducts' => true,
'product' => ['name' => 'My example product'],
];
$response = new Response('Products/Edit', $props, 'app', '123');

$request = Request::create('/products/123', 'GET');
$request->headers->add(['X-Inertia' => 'true']);

$response = new Response('User/Edit',$props, 'app', '123');
$response = $response->toResponse($request);
$view = $response->getOriginalContent();
$user = $view->getData()['page']['props']['auth']['user'];
$page = $response->getData(true);

$this->assertSame('Jonathan Reinink', $user['name']);
$this->assertTrue($user['can']['deleteProducts']);
$auth = $page['props']['auth'];
$this->assertSame('Jonathan Reinink', $auth['user']['name']);
$this->assertTrue($auth['user.can']['do.stuff']);
$this->assertFalse(array_key_exists('can', $auth));
}

public function test_responsable_with_invalid_key(): void
Expand Down

0 comments on commit 507b0a0

Please sign in to comment.