Skip to content

fix: related relationship responses top level links self link should be the relations related link #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/Core/Document/Links.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,14 @@ public function hasRelated(): bool
return $this->has('related');
}

public function relatedAsSelf(): self
{
if($this->forget('self')->hasRelated()){
$this->push(Link::fromArray('self', $this->getRelated()->toArray()));
}
return $this->forget('related');
}

/**
* Push links into the collection.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Illuminate\Contracts\Support\Responsable;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use LaravelJsonApi\Core\Document\Links;
use LaravelJsonApi\Core\Resources\JsonApiResource;
use LaravelJsonApi\Core\Responses\Concerns\HasEncodingParameters;
use LaravelJsonApi\Core\Responses\Concerns\HasRelationship;
Expand Down Expand Up @@ -59,7 +60,7 @@ public function toResponse($request)
->withResources($this->related)
->withJsonApi($this->jsonApi())
->withMeta($this->allMeta())
->withLinks($this->allLinks())
->withLinks($this->allLinks()->relatedAsSelf())
->toJson($this->encodeOptions);

return new Response(
Expand Down
4 changes: 3 additions & 1 deletion src/Core/Responses/Internal/RelatedResourceResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Illuminate\Contracts\Support\Responsable;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use LaravelJsonApi\Core\Document\Links;
use LaravelJsonApi\Core\Resources\JsonApiResource;
use LaravelJsonApi\Core\Responses\Concerns\HasEncodingParameters;
use LaravelJsonApi\Core\Responses\Concerns\HasRelationship;
Expand Down Expand Up @@ -52,14 +53,15 @@ public function toResponse($request)
{
$encoder = $this->server()->encoder();

$links = $this->allLinks();
$document = $encoder
->withRequest($request)
->withIncludePaths($this->includePaths($request))
->withFieldSets($this->sparseFieldSets($request))
->withResource($this->related)
->withJsonApi($this->jsonApi())
->withMeta($this->allMeta())
->withLinks($this->allLinks())
->withLinks($this->allLinks()->relatedAsSelf())
->toJson($this->encodeOptions);

return new Response(
Expand Down
19 changes: 19 additions & 0 deletions tests/Unit/Document/LinksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,4 +294,23 @@ public function testOffsetUnset(): void

$this->assertSame(['related' => $related], $links->all());
}

public function testRelatedToSelfWithRelated(): void
{
$links = new Links(
new Link('self', '/api/posts/1/relationships/author'),
new Link('related', '/api/posts/1/author'),
);

$this->assertEquals(['self' => new Link('self', '/api/posts/1/author'),], $links->relatedAsSelf()->all());
}

public function testRelatedToSelfWithoutRelated(): void
{
$links = new Links(
new Link('self', '/api/posts/1/relationships/author'),
);

$this->assertTrue($links->relatedAsSelf()->isEmpty());
}
}