Skip to content

Commit

Permalink
Merge branch 'release/4.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
lindyhopchris committed Jun 26, 2024
2 parents 5dbf042 + 7e4e547 commit 5b3d39c
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 5 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ All notable changes to this project will be documented in this file. This projec

## Unreleased

## [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
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());
}
}
15 changes: 15 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 @@ -52,6 +53,7 @@ public function toResponse($request)
{
$encoder = $this->server()->encoder();

$links = $this->allLinks();
$document = $encoder
->withRequest($request)
->withIncludePaths($this->includePaths($request))
Expand All @@ -68,4 +70,17 @@ public function toResponse($request)
$this->headers()
);
}

/**
* Get all links.
*
* @return Links
*/
private function allLinks(): Links
{
return $this
->linksForRelationship()
->relatedAsSelf()
->merge($this->links());
}
}
9 changes: 4 additions & 5 deletions src/Core/Schema/Concerns/MatchesIds.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

trait MatchesIds
{

/**
* @var string
*/
Expand All @@ -39,7 +38,7 @@ public function pattern(): string
*
* @return $this
*/
public function uuid(): self
public function uuid(): static
{
return $this->matchAs('[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}');
}
Expand All @@ -49,7 +48,7 @@ public function uuid(): self
*
* @return $this
*/
public function ulid(): self
public function ulid(): static
{
return $this->matchAs('[0-7][0-9a-hjkmnp-tv-zA-HJKMNP-TV-Z]{25}');
}
Expand All @@ -60,7 +59,7 @@ public function ulid(): self
* @param string $pattern
* @return $this
*/
public function matchAs(string $pattern): self
public function matchAs(string $pattern): static
{
$this->pattern = $pattern;

Expand All @@ -72,7 +71,7 @@ public function matchAs(string $pattern): self
*
* @return $this
*/
public function matchCase(): self
public function matchCase(): static
{
$this->flags = 'D';

Expand Down
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());
}
}
82 changes: 82 additions & 0 deletions tests/Unit/Schema/Concerns/MatchesIdsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php
/*
* Copyright 2024 Cloud Creativity Limited
*
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE file or at
* https://opensource.org/licenses/MIT.
*/

declare(strict_types=1);

namespace LaravelJsonApi\Core\Tests\Unit\Schema\Concerns;

use LaravelJsonApi\Core\Schema\Concerns\MatchesIds;
use PHPUnit\Framework\TestCase;

class MatchesIdsTest extends TestCase
{
/**
* @return void
*/
public function testItIsNumeric(): void
{
$id = new class () {
use MatchesIds;
};

$this->assertSame('[0-9]+', $id->pattern());
$this->assertTrue($id->match('1234'));
$this->assertFalse($id->match('123A45'));
}

/**
* @return void
*/
public function testItIsUuid(): void
{
$id = new class () {
use MatchesIds;
};

$this->assertSame($id, $id->uuid());
$this->assertSame('[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}', $id->pattern());
$this->assertTrue($id->match('fca1509e-9178-45fd-8a2b-ae819d34f7e6'));
$this->assertFalse($id->match('fca1509e917845fd8a2bae819d34f7e6'));
}

/**
* @return void
*/
public function testItIsUlid(): void
{
$id = new class () {
use MatchesIds;
};

$this->assertSame($id, $id->ulid());
$this->assertSame('[0-7][0-9a-hjkmnp-tv-zA-HJKMNP-TV-Z]{25}', $id->pattern());
$this->assertTrue($id->match('01HT4PA8AZC8Q30ZGC5PEWZP0E'));
$this->assertFalse($id->match('01HT4PA8AZC8Q30ZGC5PEWZP0'));
}

/**
* @return void
*/
public function testItIsCustom(): void
{
$id = new class () {
use MatchesIds;
};

$this->assertSame($id, $id->matchAs('[A-D]+'));
$this->assertSame('[A-D]+', $id->pattern());
$this->assertTrue($id->match('abcd'));
$this->assertTrue($id->match('ABCD'));
$this->assertFalse($id->match('abcde'));

$this->assertSame($id, $id->matchCase());
$this->assertTrue($id->match('ABCD'));
$this->assertFalse($id->match('abcd'));
}
}

0 comments on commit 5b3d39c

Please sign in to comment.