Skip to content

Commit 0f63280

Browse files
committed
Merge branch 'develop' into next
2 parents 552d37f + 96bea21 commit 0f63280

File tree

6 files changed

+102
-1
lines changed

6 files changed

+102
-1
lines changed

CHANGELOG.md

+20
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,26 @@ All notable changes to this project will be documented in this file. This projec
1818

1919
## Unreleased
2020

21+
## [4.2.0] - 2024-08-21
22+
23+
### Added
24+
25+
- Response classes now have a `withoutHeaders()` method to remove headers from the response.
26+
27+
### Fixed
28+
29+
- [#18](https://github.com/laravel-json-api/core/pull/18) Ensure headers are merged when using the `withHeaders()`
30+
method on the JSON:API response classes. This was previously not merging headers, which was not correct and therefore
31+
this is a bug fix. If you were relying on this behaviour, use the new `withoutHeaders()` method to remove any headers.
32+
33+
## [4.1.0] - 2024-06-26
34+
35+
### Fixed
36+
37+
- [#17](https://github.com/laravel-json-api/core/pull/17) Fix incorrect `self` link in related resource responses, and
38+
remove `related` link that should not exist. This has been incorrect for some time, but is definitely what
39+
the [spec defines here.](https://jsonapi.org/format/1.0/#document-top-level)
40+
2141
## [4.0.0] - 2024-03-12
2242

2343
### Changed

src/Core/Document/Links.php

+19
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,25 @@ public function hasRelated(): bool
191191
return $this->has('related');
192192
}
193193

194+
/**
195+
* @return $this
196+
*/
197+
public function relatedAsSelf(): self
198+
{
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');
208+
}
209+
210+
return $this->forget('self');
211+
}
212+
194213
/**
195214
* Push links into the collection.
196215
*

src/Core/Responses/Concerns/HasHeaders.php

+16-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,22 @@ public function withHeader(string $name, string $value = null): static
4040
*/
4141
public function withHeaders(array $headers): static
4242
{
43-
$this->headers = $headers;
43+
$this->headers = [...$this->headers, ...$headers];
44+
45+
return $this;
46+
}
47+
48+
/**
49+
* Remove response headers.
50+
*
51+
* @param string ...$headers
52+
* @return $this
53+
*/
54+
public function withoutHeaders(string ...$headers): static
55+
{
56+
foreach ($headers as $header) {
57+
unset($this->headers[$header]);
58+
}
4459

4560
return $this;
4661
}

src/Core/Responses/Internal/RelatedResourceCollectionResponse.php

+14
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;
@@ -68,4 +69,17 @@ public function toResponse($request)
6869
$this->headers()
6970
);
7071
}
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+
}
7185
}

src/Core/Responses/Internal/RelatedResourceResponse.php

+14
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;
@@ -68,4 +69,17 @@ public function toResponse($request)
6869
$this->headers()
6970
);
7071
}
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+
}
7185
}

tests/Unit/Document/LinksTest.php

+19
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)