Skip to content

Commit

Permalink
Merge branch 'develop' into next
Browse files Browse the repository at this point in the history
  • Loading branch information
lindyhopchris committed Aug 21, 2024
2 parents 552d37f + 96bea21 commit 0f63280
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 1 deletion.
20 changes: 20 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,26 @@ All notable changes to this project will be documented in this file. This projec

## Unreleased

## [4.2.0] - 2024-08-21

### Added

- Response classes now have a `withoutHeaders()` method to remove headers from the response.

### Fixed

- [#18](https://github.com/laravel-json-api/core/pull/18) Ensure headers are merged when using the `withHeaders()`
method on the JSON:API response classes. This was previously not merging headers, which was not correct and therefore
this is a bug fix. If you were relying on this behaviour, use the new `withoutHeaders()` method to remove any headers.

## [4.1.0] - 2024-06-26

### Fixed

- [#17](https://github.com/laravel-json-api/core/pull/17) Fix incorrect `self` link in related resource responses, and
remove `related` link that should not exist. This has been incorrect for some time, but is definitely what
the [spec defines here.](https://jsonapi.org/format/1.0/#document-top-level)

## [4.0.0] - 2024-03-12

### Changed
Expand Down
19 changes: 19 additions & 0 deletions src/Core/Document/Links.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,25 @@ public function hasRelated(): bool
return $this->has('related');
}

/**
* @return $this
*/
public function relatedAsSelf(): self
{
$related = $this->getRelated();

if ($related) {
$this->push(new Link(
key: 'self',
href: $related->href(),
meta: $related->meta(),
));
return $this->forget('related');
}

return $this->forget('self');
}

/**
* Push links into the collection.
*
Expand Down
17 changes: 16 additions & 1 deletion src/Core/Responses/Concerns/HasHeaders.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,22 @@ public function withHeader(string $name, string $value = null): static
*/
public function withHeaders(array $headers): static
{
$this->headers = $headers;
$this->headers = [...$this->headers, ...$headers];

return $this;
}

/**
* Remove response headers.
*
* @param string ...$headers
* @return $this
*/
public function withoutHeaders(string ...$headers): static
{
foreach ($headers as $header) {
unset($this->headers[$header]);
}

return $this;
}
Expand Down
14 changes: 14 additions & 0 deletions src/Core/Responses/Internal/RelatedResourceCollectionResponse.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 @@ -68,4 +69,17 @@ public function toResponse($request)
$this->headers()
);
}

/**
* Get all links.
*
* @return Links
*/
private function allLinks(): Links
{
return $this
->linksForRelationship()
->relatedAsSelf()
->merge($this->links());
}
}
14 changes: 14 additions & 0 deletions 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 @@ -68,4 +69,17 @@ public function toResponse($request)
$this->headers()
);
}

/**
* Get all links.
*
* @return Links
*/
private function allLinks(): Links
{
return $this
->linksForRelationship()
->relatedAsSelf()
->merge($this->links());
}
}
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());
}
}

0 comments on commit 0f63280

Please sign in to comment.