Skip to content

Commit ad1d591

Browse files
committed
fix: add some changes to fixing self link on related responses
1 parent 9ac2839 commit ad1d591

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

src/Core/Document/Links.php

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

194+
/**
195+
* @return $this
196+
*/
194197
public function relatedAsSelf(): self
195198
{
196-
if($this->forget('self')->hasRelated()){
197-
$this->push(Link::fromArray('self', $this->getRelated()->toArray()));
199+
$related = $this->getRelated();
200+
201+
if ($related) {
202+
$this->push(new Link(
203+
key: 'self',
204+
href: $related->href(),
205+
meta: $related->meta(),
206+
));
207+
return $this->forget('related');
198208
}
199-
return $this->forget('related');
209+
210+
return $this->forget('self');
200211
}
201212

202213
/**

src/Core/Responses/Internal/RelatedResourceCollectionResponse.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function toResponse($request)
6060
->withResources($this->related)
6161
->withJsonApi($this->jsonApi())
6262
->withMeta($this->allMeta())
63-
->withLinks($this->allLinks()->relatedAsSelf())
63+
->withLinks($this->allLinks())
6464
->toJson($this->encodeOptions);
6565

6666
return new Response(
@@ -69,4 +69,17 @@ public function toResponse($request)
6969
$this->headers()
7070
);
7171
}
72+
73+
/**
74+
* Get all links.
75+
*
76+
* @return Links
77+
*/
78+
private function allLinks(): Links
79+
{
80+
return $this
81+
->linksForRelationship()
82+
->relatedAsSelf()
83+
->merge($this->links());
84+
}
7285
}

src/Core/Responses/Internal/RelatedResourceResponse.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function toResponse($request)
6161
->withResource($this->related)
6262
->withJsonApi($this->jsonApi())
6363
->withMeta($this->allMeta())
64-
->withLinks($this->allLinks()->relatedAsSelf())
64+
->withLinks($this->allLinks())
6565
->toJson($this->encodeOptions);
6666

6767
return new Response(
@@ -70,4 +70,17 @@ public function toResponse($request)
7070
$this->headers()
7171
);
7272
}
73+
74+
/**
75+
* Get all links.
76+
*
77+
* @return Links
78+
*/
79+
private function allLinks(): Links
80+
{
81+
return $this
82+
->linksForRelationship()
83+
->relatedAsSelf()
84+
->merge($this->links());
85+
}
7386
}

0 commit comments

Comments
 (0)