Skip to content

Commit 9ac2839

Browse files
committed
fix: related relationship responses top level links self link should be the relations related link
1 parent 41a1190 commit 9ac2839

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

src/Core/Document/Links.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,14 @@ public function hasRelated(): bool
191191
return $this->has('related');
192192
}
193193

194+
public function relatedAsSelf(): self
195+
{
196+
if($this->forget('self')->hasRelated()){
197+
$this->push(Link::fromArray('self', $this->getRelated()->toArray()));
198+
}
199+
return $this->forget('related');
200+
}
201+
194202
/**
195203
* Push links into the collection.
196204
*

src/Core/Responses/Internal/RelatedResourceCollectionResponse.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Illuminate\Contracts\Support\Responsable;
1515
use Illuminate\Http\Request;
1616
use Illuminate\Http\Response;
17+
use LaravelJsonApi\Core\Document\Links;
1718
use LaravelJsonApi\Core\Resources\JsonApiResource;
1819
use LaravelJsonApi\Core\Responses\Concerns\HasEncodingParameters;
1920
use LaravelJsonApi\Core\Responses\Concerns\HasRelationship;
@@ -59,7 +60,7 @@ public function toResponse($request)
5960
->withResources($this->related)
6061
->withJsonApi($this->jsonApi())
6162
->withMeta($this->allMeta())
62-
->withLinks($this->allLinks())
63+
->withLinks($this->allLinks()->relatedAsSelf())
6364
->toJson($this->encodeOptions);
6465

6566
return new Response(

src/Core/Responses/Internal/RelatedResourceResponse.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Illuminate\Contracts\Support\Responsable;
1515
use Illuminate\Http\Request;
1616
use Illuminate\Http\Response;
17+
use LaravelJsonApi\Core\Document\Links;
1718
use LaravelJsonApi\Core\Resources\JsonApiResource;
1819
use LaravelJsonApi\Core\Responses\Concerns\HasEncodingParameters;
1920
use LaravelJsonApi\Core\Responses\Concerns\HasRelationship;
@@ -52,14 +53,15 @@ public function toResponse($request)
5253
{
5354
$encoder = $this->server()->encoder();
5455

56+
$links = $this->allLinks();
5557
$document = $encoder
5658
->withRequest($request)
5759
->withIncludePaths($this->includePaths($request))
5860
->withFieldSets($this->sparseFieldSets($request))
5961
->withResource($this->related)
6062
->withJsonApi($this->jsonApi())
6163
->withMeta($this->allMeta())
62-
->withLinks($this->allLinks())
64+
->withLinks($this->allLinks()->relatedAsSelf())
6365
->toJson($this->encodeOptions);
6466

6567
return new Response(

tests/Unit/Document/LinksTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,4 +294,23 @@ public function testOffsetUnset(): void
294294

295295
$this->assertSame(['related' => $related], $links->all());
296296
}
297+
298+
public function testRelatedToSelfWithRelated(): void
299+
{
300+
$links = new Links(
301+
new Link('self', '/api/posts/1/relationships/author'),
302+
new Link('related', '/api/posts/1/author'),
303+
);
304+
305+
$this->assertEquals(['self' => new Link('self', '/api/posts/1/author'),], $links->relatedAsSelf()->all());
306+
}
307+
308+
public function testRelatedToSelfWithoutRelated(): void
309+
{
310+
$links = new Links(
311+
new Link('self', '/api/posts/1/relationships/author'),
312+
);
313+
314+
$this->assertTrue($links->relatedAsSelf()->isEmpty());
315+
}
297316
}

0 commit comments

Comments
 (0)