diff --git a/src/Core/Document/Links.php b/src/Core/Document/Links.php index 1d07f50..fb3147a 100644 --- a/src/Core/Document/Links.php +++ b/src/Core/Document/Links.php @@ -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. * diff --git a/src/Core/Responses/Internal/RelatedResourceCollectionResponse.php b/src/Core/Responses/Internal/RelatedResourceCollectionResponse.php index db33285..986046c 100644 --- a/src/Core/Responses/Internal/RelatedResourceCollectionResponse.php +++ b/src/Core/Responses/Internal/RelatedResourceCollectionResponse.php @@ -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; @@ -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( diff --git a/src/Core/Responses/Internal/RelatedResourceResponse.php b/src/Core/Responses/Internal/RelatedResourceResponse.php index d03b896..6c1fc17 100644 --- a/src/Core/Responses/Internal/RelatedResourceResponse.php +++ b/src/Core/Responses/Internal/RelatedResourceResponse.php @@ -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; @@ -52,6 +53,7 @@ public function toResponse($request) { $encoder = $this->server()->encoder(); + $links = $this->allLinks(); $document = $encoder ->withRequest($request) ->withIncludePaths($this->includePaths($request)) @@ -59,7 +61,7 @@ public function toResponse($request) ->withResource($this->related) ->withJsonApi($this->jsonApi()) ->withMeta($this->allMeta()) - ->withLinks($this->allLinks()) + ->withLinks($this->allLinks()->relatedAsSelf()) ->toJson($this->encodeOptions); return new Response( diff --git a/tests/Unit/Document/LinksTest.php b/tests/Unit/Document/LinksTest.php index 0fe8ead..c23073d 100644 --- a/tests/Unit/Document/LinksTest.php +++ b/tests/Unit/Document/LinksTest.php @@ -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()); + } }