Skip to content

Commit 78f744e

Browse files
authored
Merge pull request #5 from DirectoryTree/fix-json-resource-nesting
Fix nested `JsonResource` anonymization
2 parents 9eac630 + 45e2e07 commit 78f744e

File tree

6 files changed

+91
-8
lines changed

6 files changed

+91
-8
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,12 +201,12 @@ class UserResource extends JsonResource implements Anonymizable
201201

202202
public function toArray(Request $request): array
203203
{
204-
return [
204+
return $this->toAnonymized([
205205
'id' => $this->id,
206206
'name' => $this->name,
207207
'email' => $this->email,
208208
'phone' => $this->phone,
209-
];
209+
]);
210210
}
211211

212212
public function getAnonymizedAttributes(Generator $faker): array

src/AnonymizedResource.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,13 @@ trait AnonymizedResource
77
use AnonymizesAttributes;
88

99
/**
10-
* Transform the resource into an array.
10+
* Anonymize the given attributes.
1111
*
12-
* @param \Illuminate\Http\Request|null $request
12+
* @param array<string, mixed> $attributes
1313
* @return array<string, mixed>
1414
*/
15-
public function resolve($request = null): array
15+
public function toAnonymized(array $attributes): array
1616
{
17-
$attributes = parent::resolve($request);
18-
1917
if (! $this->anonymizeEnabled || ! static::getAnonymizeManager()->isEnabled()) {
2018
return $attributes;
2119
}

src/AnonymizesAttributes.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,13 @@ protected function addAnonymizedAttributesToArray(array $attributes): array
7171
continue;
7272
}
7373

74-
$attributes[$key] = $value;
74+
if (! is_array($attributes[$key])) {
75+
$attributes[$key] = $value;
76+
77+
continue;
78+
}
79+
80+
$attributes[$key] = $this->addAnonymizedAttributesToArray($value) + $attributes[$key];
7581
}
7682

7783
return $attributes;

tests/AnonymizedJsonResource.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,32 @@
55
use DirectoryTree\Anonymize\Anonymizable;
66
use DirectoryTree\Anonymize\AnonymizedResource;
77
use Faker\Generator;
8+
use Illuminate\Http\Request;
89
use Illuminate\Http\Resources\Json\JsonResource;
910

1011
class AnonymizedJsonResource extends JsonResource implements Anonymizable
1112
{
1213
use AnonymizedResource;
1314

15+
public function toArray(Request $request): array
16+
{
17+
return $this->toAnonymized($this->resource);
18+
}
19+
1420
public function getAnonymizedAttributes(Generator $faker): array
1521
{
1622
return [
1723
'name' => $faker->name(),
1824
'address' => $faker->address(),
25+
'role' => [
26+
'name' => $faker->word(),
27+
'permissions' => [
28+
['name' => $faker->word()],
29+
],
30+
],
31+
'nested' => [
32+
'name' => $faker->name(),
33+
],
1934
];
2035
}
2136

tests/NestedAnonymizedResource.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace DirectoryTree\Anonymize\Tests;
4+
5+
use DirectoryTree\Anonymize\Anonymizable;
6+
use DirectoryTree\Anonymize\AnonymizedResource;
7+
use Faker\Generator;
8+
use Illuminate\Http\Request;
9+
use Illuminate\Http\Resources\Json\JsonResource;
10+
11+
class NestedAnonymizedResource extends JsonResource implements Anonymizable
12+
{
13+
use AnonymizedResource;
14+
15+
public function toArray(Request $request): array
16+
{
17+
return $this->toAnonymized($this->resource);
18+
}
19+
20+
public function getAnonymizedAttributes(Generator $faker): array
21+
{
22+
return [
23+
'name' => $faker->name(),
24+
];
25+
}
26+
27+
public function getAnonymizableKey(): string
28+
{
29+
return 1;
30+
}
31+
}

tests/Unit/AnonymizedJsonResourceTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use DirectoryTree\Anonymize\Facades\Anonymize;
44
use DirectoryTree\Anonymize\Tests\AnonymizedJsonResource;
5+
use DirectoryTree\Anonymize\Tests\NestedAnonymizedResource;
56

67
it('anonymizes json resource when anonymization is enabled', function () {
78
Anonymize::enable();
@@ -26,3 +27,35 @@
2627
expect($resource->resolve())->toHaveKey('name', 'Foo Bar')
2728
->and($resource->resolve())->toHaveKey('address', '1600 Pennsylvania Avenue');
2829
});
30+
31+
it('anonymizes nested arrays and resources', function () {
32+
Anonymize::enable();
33+
34+
$resource = new AnonymizedJsonResource([
35+
'name' => 'Foo Bar',
36+
'address' => '1600 Pennsylvania Avenue',
37+
'role' => [
38+
'name' => 'Admin',
39+
'permissions' => [
40+
['name' => 'View Users'],
41+
['name' => 'View Roles'],
42+
],
43+
'created_at' => '2023-01-01 00:00:00',
44+
],
45+
'nested' => new NestedAnonymizedResource([
46+
'name' => 'Foo Bar',
47+
]),
48+
]);
49+
50+
$attributes = $resource->resolve();
51+
52+
expect($attributes['role'])->toHaveKey('created_at');
53+
expect($attributes['role']['name'])->not->toBe('Admin');
54+
55+
expect($attributes['role']['permissions'])->toHaveCount(1);
56+
expect($attributes['role']['permissions'][0])->toHaveKey('name');
57+
expect($attributes['role']['permissions'][0]['name'])->not->toBe('View Users');
58+
59+
expect($attributes['nested'])->toHaveKey('name');
60+
expect($attributes['nested']['name'])->not->toBe('Foo Bar');
61+
});

0 commit comments

Comments
 (0)