Skip to content

Commit 47efeb8

Browse files
authored
[JSON:API] Fix recursive relationship (#58066)
* [JSON:API] Fix recursive relationship Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> --------- Signed-off-by: Mior Muhammad Zaki <[email protected]>
1 parent 0ad6501 commit 47efeb8

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

src/Illuminate/Http/Resources/JsonApi/Concerns/ResolvesJsonApiElements.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ protected function compileResourceRelationships(JsonApiRequest $request): void
196196
$relatedModels = $relationResolver->handle($this->resource);
197197
$relatedResourceClass = $relationResolver->resourceClass();
198198

199-
if (! is_null($relatedModels)) {
199+
if (! is_null($relatedModels) && $this->includesPreviouslyLoadedRelationships === false) {
200200
$relatedModels->loadMissing($request->sparseIncluded($relationName));
201201
}
202202

tests/Integration/Http/Resources/JsonApi/JsonApiResourceTest.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,66 @@ public function testItCanResolveRelationshipWithNestedRelationship()
405405
->assertJsonMissing(['jsonapi']);
406406
}
407407

408+
public function testItCanResolveRelationshipWithRecursiveNestedRelationship()
409+
{
410+
$now = $this->freezeSecond();
411+
412+
$user = User::factory()->create();
413+
414+
$profile = Profile::factory()->create([
415+
'user_id' => $user->getKey(),
416+
'date_of_birth' => '2011-06-09',
417+
'timezone' => 'America/Chicago',
418+
]);
419+
420+
$this->getJson("/users/{$user->getKey()}?".http_build_query(['include' => 'profile.user.profile']))
421+
->assertHeader('Content-type', 'application/vnd.api+json')
422+
->assertExactJson([
423+
'data' => [
424+
'attributes' => [
425+
'email' => $user->email,
426+
'name' => $user->name,
427+
],
428+
'id' => (string) $user->getKey(),
429+
'type' => 'users',
430+
'relationships' => [
431+
'profile' => [
432+
'data' => ['id' => (string) $profile->getKey(), 'type' => 'profiles'],
433+
],
434+
],
435+
],
436+
'included' => [
437+
[
438+
'attributes' => [
439+
'date_of_birth' => '2011-06-09',
440+
'timezone' => 'America/Chicago',
441+
],
442+
'id' => (string) $profile->getKey(),
443+
'type' => 'profiles',
444+
'relationships' => [
445+
'user' => [
446+
'data' => ['id' => (string) $user->getKey(), 'type' => 'users'],
447+
],
448+
],
449+
],
450+
[
451+
'attributes' => [
452+
'email' => $user->email,
453+
'name' => $user->name,
454+
],
455+
'id' => (string) $user->getKey(),
456+
'type' => 'users',
457+
'relationships' => [
458+
'profile' => [
459+
'data' => ['id' => (string) $profile->getKey(), 'type' => 'profiles'],
460+
],
461+
],
462+
],
463+
],
464+
])
465+
->assertJsonMissing(['jsonapi']);
466+
}
467+
408468
public function testItCanResolveRelationshipWithoutRedundantIncludedRelationship()
409469
{
410470
$now = $this->freezeSecond();

0 commit comments

Comments
 (0)