Skip to content

Commit 50f91f2

Browse files
committed
fix: applyWhen correct behavior
1 parent af71524 commit 50f91f2

File tree

6 files changed

+41
-44
lines changed

6 files changed

+41
-44
lines changed

src/Descriptors/Values/ValueRaw.php

Lines changed: 0 additions & 25 deletions
This file was deleted.

src/Resources/Concerns/ConditionallyLoadsAttributes.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Ark4ne\JsonApi\Descriptors\Describer;
66
use Ark4ne\JsonApi\Descriptors\Relations\RelationRaw;
7-
use Ark4ne\JsonApi\Descriptors\Values\ValueRaw;
7+
use Ark4ne\JsonApi\Descriptors\Values\ValueMixed;
88
use Ark4ne\JsonApi\Resources\Relationship;
99
use Ark4ne\JsonApi\Support\Fields;
1010
use Ark4ne\JsonApi\Support\Includes;
@@ -57,13 +57,13 @@ protected function whenIncluded(Request $request, string $type, mixed $value)
5757
*/
5858
protected function applyWhen(bool|Closure $condition, iterable $data): MergeValue
5959
{
60-
return new MergeValue(collect($data)->map(function ($raw, $key) use ($condition) {
60+
return new MergeValue(collect($data)->map(function ($raw) use ($condition) {
6161
if ($raw instanceof Describer) {
6262
$value = $raw;
6363
} elseif ($raw instanceof Relationship) {
6464
$value = RelationRaw::fromRelationship($raw);
6565
} else {
66-
$value = new ValueRaw($key, $raw);
66+
$value = new ValueMixed(is_callable($raw) ? $raw : static fn () => $raw);
6767
}
6868

6969
return $value->when(fn () => value($condition));

tests/Feature/SchemaTest.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,14 @@ class SchemaTest extends FeatureTestCase
1313
{
1414
public function testSchema()
1515
{
16-
$user = new Skeleton(UserResource::class, 'user', ['name', 'email', 'only-with-fields']);
16+
$user = new Skeleton(UserResource::class, 'user', [
17+
'name',
18+
'email',
19+
'only-with-fields',
20+
'with-apply-conditional-raw',
21+
'with-apply-conditional-closure',
22+
'with-apply-conditional-value',
23+
]);
1724
$post = new Skeleton(PostResource::class, 'post', ['state', 'title', 'content']);
1825
$comment = new Skeleton(CommentResource::class, 'comment', ['content']);
1926

tests/Feature/User/ResourceTest.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,16 @@ private function getJsonResult(User $user, ?array $attributes = null, ?array $re
164164
'attributes' => array_filter(array_intersect_key([
165165
'name' => $user->name,
166166
'email' => $user->email,
167-
], array_fill_keys($attributes ?? ['name', 'email'], true))),
167+
"with-apply-conditional-closure" => "huge-data-set",
168+
"with-apply-conditional-raw" => "huge-data-set",
169+
"with-apply-conditional-value" => "huge-data-set"
170+
], array_fill_keys($attributes ?? [
171+
'name',
172+
'email',
173+
"with-apply-conditional-closure",
174+
"with-apply-conditional-raw",
175+
"with-apply-conditional-value",
176+
], true))),
168177
'relationships' => [
169178
'main-post' => [],
170179
'posts' => array_filter([

tests/Unit/Resources/Concerns/ConditionallyLoadsAttributesTest.php

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Ark4ne\JsonApi\Descriptors\Values\ValueMixed;
88
use Ark4ne\JsonApi\Descriptors\Values\ValueRaw;
99
use Ark4ne\JsonApi\Resources\Concerns\ConditionallyLoadsAttributes;
10+
use Ark4ne\JsonApi\Resources\JsonApiResource;
1011
use Ark4ne\JsonApi\Resources\Relationship;
1112
use Illuminate\Http\Request;
1213
use Illuminate\Http\Resources\Json\JsonResource;
@@ -75,16 +76,16 @@ public function testApplyWhen()
7576
'missing.2' => 123,
7677
]);
7778
$this->assertEquals(new MergeValue([
78-
'missing.1' => (new ValueRaw('missing.1', 'abc'))->when(fn () => false),
79-
'missing.2' => (new ValueRaw('missing.2', 123))->when(fn () => false),
79+
'missing.1' => (new ValueMixed(fn() => 'abc'))->when(fn() => false),
80+
'missing.2' => (new ValueMixed(fn() => 123))->when(fn() => false),
8081
]), $actual);
8182
$actual = Reflect::invoke($stub, 'applyWhen', true, [
8283
'present.1' => 'abc',
8384
'present.2' => 123,
8485
]);
8586
$this->assertEquals(new MergeValue([
86-
'present.1' => (new ValueRaw('present.1', 'abc'))->when(fn () => true),
87-
'present.2' => (new ValueRaw('present.2', 123))->when(fn () => true),
87+
'present.1' => (new ValueMixed(fn() => 'abc'))->when(fn() => true),
88+
'present.2' => (new ValueMixed(fn() => 123))->when(fn() => true),
8889
]), $actual);
8990
$actual = Reflect::invoke($stub, 'applyWhen', true, [
9091
'present.1' => (new ValueMixed(fn() => 'abc')),
@@ -94,11 +95,11 @@ public function testApplyWhen()
9495
'present.5' => (new Relationship(UserResource::class, fn() => null)),
9596
]);
9697
$this->assertEquals(new MergeValue([
97-
'present.1' => (new ValueMixed(fn() => 'abc'))->when(fn () => true),
98-
'present.2' => (new ValueMixed(fn() => 123))->when(fn () => true),
99-
'present.3' => (new RelationOne('present', fn() => 'abc'))->when(fn () => true),
100-
'present.4' => (new RelationOne('present', fn() => 123))->when(fn () => true),
101-
'present.5' => RelationRaw::fromRelationship(new Relationship(UserResource::class, fn() => null))->when(fn () => true),
98+
'present.1' => (new ValueMixed(fn() => 'abc'))->when(fn() => true),
99+
'present.2' => (new ValueMixed(fn() => 123))->when(fn() => true),
100+
'present.3' => (new RelationOne('present', fn() => 'abc'))->when(fn() => true),
101+
'present.4' => (new RelationOne('present', fn() => 123))->when(fn() => true),
102+
'present.5' => RelationRaw::fromRelationship(new Relationship(UserResource::class, fn() => null))->when(fn() => true),
102103
]), $actual);
103104
$actual = Reflect::invoke($stub, 'applyWhen', false, [
104105
'missing.1' => (new ValueMixed(fn() => 'abc')),
@@ -108,11 +109,11 @@ public function testApplyWhen()
108109
'missing.5' => (new Relationship(UserResource::class, fn() => null)),
109110
]);
110111
$this->assertEquals(new MergeValue([
111-
'missing.1' => (new ValueMixed(fn() => 'abc'))->when(fn () => false),
112-
'missing.2' => (new ValueMixed(fn() => 123))->when(fn () => false),
113-
'missing.3' => (new RelationOne('present', fn() => 'abc'))->when(fn () => false),
114-
'missing.4' => (new RelationOne('present', fn() => 123))->when(fn () => false),
115-
'missing.5' => RelationRaw::fromRelationship(new Relationship(UserResource::class, fn() => null))->when(fn () => false),
112+
'missing.1' => (new ValueMixed(fn() => 'abc'))->when(fn() => false),
113+
'missing.2' => (new ValueMixed(fn() => 123))->when(fn() => false),
114+
'missing.3' => (new RelationOne('present', fn() => 'abc'))->when(fn() => false),
115+
'missing.4' => (new RelationOne('present', fn() => 123))->when(fn() => false),
116+
'missing.5' => RelationRaw::fromRelationship(new Relationship(UserResource::class, fn() => null))->when(fn() => false),
116117
]), $actual);
117118
}
118119

tests/app/Http/Resources/UserResource.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ protected function toAttributes(Request $request): iterable
2525
'name' => $this->resource->name,
2626
'email' => $this->resource->email,
2727
'only-with-fields' => $this->string(fn() => 'huge-data-set')->whenInFields(),
28+
$this->applyWhen(fn () => true, [
29+
'with-apply-conditional-raw' => 'huge-data-set',
30+
'with-apply-conditional-closure' => fn () => 'huge-data-set',
31+
'with-apply-conditional-value' => $this->string(fn () => 'huge-data-set'),
32+
]),
2833
];
2934
}
3035

0 commit comments

Comments
 (0)