Skip to content

Commit

Permalink
Fix circular reference with numeric keys for single item
Browse files Browse the repository at this point in the history
  • Loading branch information
snellingio committed May 27, 2022
1 parent a0bc51a commit 005dc0f
Show file tree
Hide file tree
Showing 2 changed files with 262 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/JsonApiResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ public function with($request): array
{
return [
'included' => $this->included($request)
->uniqueStrict(fn (JsonApiResource $resource): string => $resource->toUniqueResourceIdentifier($request)),
->uniqueStrict(fn (JsonApiResource $resource): string => $resource->toUniqueResourceIdentifier($request))
->values(),
'jsonapi' => self::serverImplementationResolver()($request),
];
}
Expand Down
260 changes: 260 additions & 0 deletions tests/Unit/RelationshipsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1326,6 +1326,266 @@ public function testItFlushesTheRelationshipCache(): void
$this->assertNull($resource->requestedRelationshipsCache());
}

public function testItHasArrayOfIncludesForACollectionOfResourcesWhenHasNumericKeys(): void
{
$users = [
(new BasicModel(
[
'id' => 'user-id-1',
'name' => 'user-name-1',
]
))->setRelation('avatar',
(new BasicModel(
[
'id' => 1,
'url' => 'https://example.com/avatar1.png',
]
))
),
(new BasicModel(
[
'id' => 'user-id-2',
'name' => 'user-name-2',
]
))->setRelation('avatar',
(new BasicModel(
[
'id' => 1,
'url' => 'https://example.com/avatar1.png',
]
))
),
(new BasicModel(
[
'id' => 'user-id-3',
'name' => 'user-name-3',
]
))->setRelation('avatar',
(new BasicModel(
[
'id' => 2,
'url' => 'https://example.com/avatar2.png',
]
))
),
];
Route::get('test-route', fn () => UserResource::collection($users));

$response = $this->getJson('test-route?include=avatar');

$response->assertOk();
$response->assertExactJson(
[
'data' => [
[
'id' => 'user-id-1',
'type' => 'basicModels',
'attributes' => [
'name' => 'user-name-1',
],
'relationships' => [
'avatar' => [
'data' => [
'id' => '1',
'type' => 'basicModels',
'meta' => [],
],
'links' => [],
'meta' => [],
],
],
'meta' => [],
'links' => [],
],
[
'id' => 'user-id-2',
'type' => 'basicModels',
'attributes' => [
'name' => 'user-name-2',
],
'relationships' => [
'avatar' => [
'data' => [
'id' => '1',
'type' => 'basicModels',
'meta' => [],
],
'links' => [],
'meta' => [],
],
],
'meta' => [],
'links' => [],
],
[
'id' => 'user-id-3',
'type' => 'basicModels',
'attributes' => [
'name' => 'user-name-3',
],
'relationships' => [
'avatar' => [
'data' => [
'id' => '2',
'type' => 'basicModels',
'meta' => [],
],
'links' => [],
'meta' => [],
],
],
'meta' => [],
'links' => [],
],
],
'jsonapi' => [
'version' => '1.0',
'meta' => [],
],
'included' => [
[
'id' => '1',
'type' => 'basicModels',
'attributes' => [
'url' => 'https://example.com/avatar1.png',
],
'relationships' => [],
'links' => [],
'meta' => [],
],
[
'id' => '2',
'type' => 'basicModels',
'attributes' => [
'url' => 'https://example.com/avatar2.png',
],
'relationships' => [],
'links' => [],
'meta' => [],
],
],
]
);
}

public function testItHasArrayOfIncludesForASingleResourceWhenHasNumericKeys(): void
{
$user = (new BasicModel(
[
'id' => 1,
'name' => 'user-name-1',
]
));
$user->posts = [
(new BasicModel(
[
'id' => 2,
'title' => 'Title 1',
'content' => 'Content 1',
]
))->setRelation('comments', [
(new BasicModel(
[
'id' => 3,
'content' => 'Comment 1',
]
))->setRelation('author', (new BasicModel(
[
'id' => 1,
'name' => 'user-name-1',
]
))),
]),
(new BasicModel(
[
'id' => 2,
'title' => 'Title 2',
'content' => 'Content 2',
]
))->setRelation('comments', new Collection()),
];

Route::get('test-route', fn () => UserResource::make($user));

$response = $this->getJson('test-route?include=posts,posts.comments,posts.comments.author');

$response->assertOk();
$response->assertExactJson(
[
'data' => [
'id' => '1',
'type' => 'basicModels',
'attributes' => [
'name' => 'user-name-1',
],
'relationships' => [
'posts' => [
[
'data' => [
'id' => '2',
'type' => 'basicModels',
'meta' => [],
],
'meta' => [],
'links' => [],
],
[
'data' => [
'id' => '2',
'type' => 'basicModels',
'meta' => [],
],
'meta' => [],
'links' => [],
],
],
],
'meta' => [],
'links' => [],
],
'included' => [
[
'id' => '2',
'type' => 'basicModels',
'attributes' => [
'title' => 'Title 1',
'content' => 'Content 1',
],
'relationships' => [
'comments' => [
[
'data' => [
'id' => '3',
'type' => 'basicModels',
'meta' => [],
],
'meta' => [],
'links' => [],
],
],
],
'meta' => [],
'links' => [],
],
[
'id' => '3',
'type' => 'basicModels',
'attributes' => [
'content' => 'Comment 1',
],
'relationships' => [],
'meta' => [],
'links' => [],
],
],
'jsonapi' => [
'version' => '1.0',
'meta' => [],
],
]
);
}

public function testItCanHaveANonJsonApiRelationship(): void
{
$user = (new BasicModel([
Expand Down

0 comments on commit 005dc0f

Please sign in to comment.