Skip to content

Commit 21248fd

Browse files
committed
Fix support for nested includes on polymorphic relationship
Closes #99
1 parent 1e09fa5 commit 21248fd

File tree

2 files changed

+99
-4
lines changed

2 files changed

+99
-4
lines changed

src/Endpoint/Concerns/IncludesData.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,16 @@ private function validateInclude(
7676
continue;
7777
}
7878

79-
$types = $field->collections;
80-
81-
$relatedResources = $types
82-
? array_map(fn($type) => $context->api->getResource($type), $types)
79+
$relatedResources = $field->collections
80+
? array_merge(
81+
...array_map(
82+
fn($collection) => array_map(
83+
fn($resource) => $context->api->getResource($resource),
84+
$context->api->getCollection($collection)->resources(),
85+
),
86+
$field->collections,
87+
),
88+
)
8389
: array_values($context->api->resources);
8490

8591
$this->validateInclude($context, $relatedResources, $nested, $name . '.');

tests/specification/InclusionOfRelatedResourcesTest.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Tobyz\JsonApiServer\Schema\Field\ToMany;
1010
use Tobyz\JsonApiServer\Schema\Field\ToOne;
1111
use Tobyz\Tests\JsonApiServer\AbstractTestCase;
12+
use Tobyz\Tests\JsonApiServer\MockCollection;
1213
use Tobyz\Tests\JsonApiServer\MockResource;
1314

1415
/**
@@ -182,4 +183,92 @@ public function test_relationship_inclusion_on_list_endpoint()
182183
$response->getBody(),
183184
);
184185
}
186+
187+
public function test_relationship_inclusion_for_polymorphic_relationship()
188+
{
189+
$api = new JsonApi();
190+
191+
$api->resource(
192+
new MockResource(
193+
'users',
194+
models: [($user1 = (object) ['id' => '1', 'name' => 'Toby'])],
195+
fields: [Attribute::make('name')],
196+
),
197+
);
198+
199+
$api->resource(
200+
new MockResource(
201+
'posts',
202+
models: [($post1 = (object) ['id' => '1', 'author' => $user1])],
203+
fields: [
204+
ToOne::make('author')
205+
->type('users')
206+
->includable(),
207+
],
208+
),
209+
);
210+
211+
$api->collection(
212+
new MockCollection('subjects', [
213+
'users' => [$user1],
214+
'posts' => [$post1],
215+
]),
216+
);
217+
218+
$api->resource(
219+
new MockResource(
220+
'notifications',
221+
models: [
222+
((object) ['id' => '1', 'subject' => $post1]),
223+
((object) ['id' => '2', 'subject' => $user1]),
224+
],
225+
endpoints: [Index::make()],
226+
fields: [
227+
ToOne::make('subject')
228+
->collection('subjects')
229+
->includable(),
230+
],
231+
),
232+
);
233+
234+
$response = $api->handle(
235+
$this->buildRequest('GET', '/notifications?include=subject.author'),
236+
);
237+
238+
$this->assertJsonApiDocumentSubset(
239+
[
240+
'data' => [
241+
[
242+
'type' => 'notifications',
243+
'id' => '1',
244+
'relationships' => [
245+
'subject' => [
246+
'data' => ['type' => 'posts', 'id' => '1'],
247+
],
248+
],
249+
],
250+
[
251+
'type' => 'notifications',
252+
'id' => '2',
253+
'relationships' => [
254+
'subject' => [
255+
'data' => ['type' => 'users', 'id' => '1'],
256+
],
257+
],
258+
],
259+
],
260+
'included' => [
261+
[
262+
'type' => 'posts',
263+
'id' => '1',
264+
],
265+
[
266+
'type' => 'users',
267+
'id' => '1',
268+
],
269+
],
270+
],
271+
$response->getBody(),
272+
);
273+
}
185274
}

0 commit comments

Comments
 (0)