From e02f98611e900c5b8e1b8336b13d6d61b2e718ce Mon Sep 17 00:00:00 2001 From: Lode Claassen Date: Sun, 16 May 2021 16:40:57 +0200 Subject: [PATCH 1/4] add the extension/profile headers to the root self link --- src/Document.php | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Document.php b/src/Document.php index cafa441..372a4a1 100644 --- a/src/Document.php +++ b/src/Document.php @@ -15,6 +15,7 @@ use alsvanzelf\jsonapi\interfaces\ExtensionInterface; use alsvanzelf\jsonapi\interfaces\ProfileInterface; use alsvanzelf\jsonapi\objects\JsonapiObject; +use alsvanzelf\jsonapi\objects\LinkObject; use alsvanzelf\jsonapi\objects\LinksObject; use alsvanzelf\jsonapi\objects\MetaObject; @@ -117,12 +118,24 @@ public function addLink($key, $href, array $meta=[], $level=Document::LEVEL_ROOT /** * set the self link on the document * + * @note a LinkObject is added when extensions or profiles are applied + * * @param string $href * @param array $meta optional, if given a LinkObject is added, otherwise a link string is added * @param string $level one of the Document::LEVEL_* constants, optional, defaults to Document::LEVEL_ROOT */ public function setSelfLink($href, array $meta=[], $level=Document::LEVEL_ROOT) { - $this->addLink('self', $href, $meta, $level); + if ($level === Document::LEVEL_ROOT && ($this->extensions !== [] || $this->profiles !== [])) { + $contentType = Converter::prepareContentType(Document::CONTENT_TYPE_OFFICIAL, $this->extensions, $this->profiles); + + $linkObject = new LinkObject($href, $meta); + $linkObject->setMediaType($contentType); + + $this->addLinkObject('self', $linkObject); + } + else { + $this->addLink('self', $href, $meta, $level); + } } /** From 9c3143b26a807872a76b518ec6243355110151ed Mon Sep 17 00:00:00 2001 From: Lode Claassen Date: Sun, 16 May 2021 16:42:27 +0200 Subject: [PATCH 2/4] improve tests around applying extensions and profiles --- tests/DocumentTest.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/tests/DocumentTest.php b/tests/DocumentTest.php index 30043cb..fd2bbc6 100644 --- a/tests/DocumentTest.php +++ b/tests/DocumentTest.php @@ -237,7 +237,7 @@ public function testAddLinkObject_HappyPath() { public function testApplyExtension_HappyPath() { $extension = new TestExtension(); $extension->setNamespace('test'); - $extension->setOfficialLink('https://jsonapi.org'); + $extension->setOfficialLink('https://jsonapi.org/extension'); $document = new Document(); $document->applyExtension($extension); @@ -245,13 +245,14 @@ public function testApplyExtension_HappyPath() { $array = $document->toArray(); + $this->assertCount(2, $array); $this->assertArrayHasKey('jsonapi', $array); $this->assertCount(2, $array['jsonapi']); $this->assertSame('1.1', $array['jsonapi']['version']); $this->assertArrayHasKey('ext', $array['jsonapi']); $this->assertCount(1, $array['jsonapi']['ext']); $this->assertArrayHasKey(0, $array['jsonapi']['ext']); - $this->assertSame('https://jsonapi.org', $array['jsonapi']['ext'][0]); + $this->assertSame('https://jsonapi.org/extension', $array['jsonapi']['ext'][0]); $this->assertArrayHasKey('test:foo', $array); $this->assertSame('bar', $array['test:foo']); } @@ -298,20 +299,21 @@ public function testApplyExtension_ConflictingNamespace() { */ public function testApplyProfile_HappyPath() { $profile = new TestProfile(); - $profile->setOfficialLink('https://jsonapi.org'); + $profile->setOfficialLink('https://jsonapi.org/profile'); $document = new Document(); $document->applyProfile($profile); $array = $document->toArray(); + $this->assertCount(1, $array); $this->assertArrayHasKey('jsonapi', $array); $this->assertCount(2, $array['jsonapi']); $this->assertSame('1.1', $array['jsonapi']['version']); $this->assertArrayHasKey('profile', $array['jsonapi']); $this->assertCount(1, $array['jsonapi']['profile']); $this->assertArrayHasKey(0, $array['jsonapi']['profile']); - $this->assertSame('https://jsonapi.org', $array['jsonapi']['profile'][0]); + $this->assertSame('https://jsonapi.org/profile', $array['jsonapi']['profile'][0]); } public function testToJson_HappyPath() { From 9afcda0501107fbb77a7d333f1817c00f4f507ee Mon Sep 17 00:00:00 2001 From: Lode Claassen Date: Sun, 16 May 2021 16:43:01 +0200 Subject: [PATCH 3/4] test whether the extended self link works --- tests/DocumentTest.php | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/tests/DocumentTest.php b/tests/DocumentTest.php index fd2bbc6..020a70a 100644 --- a/tests/DocumentTest.php +++ b/tests/DocumentTest.php @@ -242,10 +242,11 @@ public function testApplyExtension_HappyPath() { $document = new Document(); $document->applyExtension($extension); $document->addExtensionMember($extension, 'foo', 'bar'); + $document->setSelfLink('https://jsonapi.org/foo'); $array = $document->toArray(); - $this->assertCount(2, $array); + $this->assertCount(3, $array); $this->assertArrayHasKey('jsonapi', $array); $this->assertCount(2, $array['jsonapi']); $this->assertSame('1.1', $array['jsonapi']['version']); @@ -255,6 +256,14 @@ public function testApplyExtension_HappyPath() { $this->assertSame('https://jsonapi.org/extension', $array['jsonapi']['ext'][0]); $this->assertArrayHasKey('test:foo', $array); $this->assertSame('bar', $array['test:foo']); + $this->assertArrayHasKey('links', $array); + $this->assertCount(1, $array['links']); + $this->assertArrayHasKey('self', $array['links']); + $this->assertCount(2, $array['links']['self']); + $this->assertArrayHasKey('href', $array['links']['self']); + $this->assertArrayHasKey('type', $array['links']['self']); + $this->assertSame('https://jsonapi.org/foo', $array['links']['self']['href']); + $this->assertSame('application/vnd.api+json; ext="https://jsonapi.org/extension"', $array['links']['self']['type']); } /** @@ -303,10 +312,11 @@ public function testApplyProfile_HappyPath() { $document = new Document(); $document->applyProfile($profile); + $document->setSelfLink('https://jsonapi.org/foo'); $array = $document->toArray(); - $this->assertCount(1, $array); + $this->assertCount(2, $array); $this->assertArrayHasKey('jsonapi', $array); $this->assertCount(2, $array['jsonapi']); $this->assertSame('1.1', $array['jsonapi']['version']); @@ -314,6 +324,14 @@ public function testApplyProfile_HappyPath() { $this->assertCount(1, $array['jsonapi']['profile']); $this->assertArrayHasKey(0, $array['jsonapi']['profile']); $this->assertSame('https://jsonapi.org/profile', $array['jsonapi']['profile'][0]); + $this->assertArrayHasKey('links', $array); + $this->assertCount(1, $array['links']); + $this->assertArrayHasKey('self', $array['links']); + $this->assertCount(2, $array['links']['self']); + $this->assertArrayHasKey('href', $array['links']['self']); + $this->assertArrayHasKey('type', $array['links']['self']); + $this->assertSame('https://jsonapi.org/foo', $array['links']['self']['href']); + $this->assertSame('application/vnd.api+json; profile="https://jsonapi.org/profile"', $array['links']['self']['type']); } public function testToJson_HappyPath() { From d3573221297fa2021e246d55383d8528fdedb58a Mon Sep 17 00:00:00 2001 From: Lode Claassen Date: Sun, 16 May 2021 16:45:12 +0200 Subject: [PATCH 4/4] re-use links initialization of the link manager --- src/Document.php | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/Document.php b/src/Document.php index 372a4a1..ddb75d9 100644 --- a/src/Document.php +++ b/src/Document.php @@ -23,7 +23,9 @@ * @see ResourceDocument, CollectionDocument, ErrorsDocument or MetaDocument */ abstract class Document implements DocumentInterface, \JsonSerializable { - use AtMemberManager, ExtensionMemberManager, HttpStatusCodeManager, LinksManager; + use AtMemberManager, ExtensionMemberManager, HttpStatusCodeManager, LinksManager { + LinksManager::addLink as linkManagerAddLink; + } const JSONAPI_VERSION_1_0 = '1.0'; const JSONAPI_VERSION_1_1 = '1.1'; @@ -98,11 +100,7 @@ public function __construct() { */ public function addLink($key, $href, array $meta=[], $level=Document::LEVEL_ROOT) { if ($level === Document::LEVEL_ROOT) { - if ($this->links === null) { - $this->setLinksObject(new LinksObject()); - } - - $this->links->add($key, $href, $meta); + $this->linkManagerAddLink($key, $href, $meta); } elseif ($level === Document::LEVEL_JSONAPI) { throw new InputException('level "jsonapi" can not be used for links');