diff --git a/README.md b/README.md index 8a231a5..e3df353 100644 --- a/README.md +++ b/README.md @@ -415,15 +415,10 @@ GET /posts?include=author&fields[posts]=title,excerpt&fields[users]=name "author": { "data": { "type": "users", - "id": "74812", - "meta": {} - }, - "meta": {}, - "links": {} + "id": "74812" + } } - }, - "meta": {}, - "links": {} + } }, { "id": "39974", @@ -436,15 +431,10 @@ GET /posts?include=author&fields[posts]=title,excerpt&fields[users]=name "author": { "data": { "type": "users", - "id": "74812", - "meta": {} - }, - "meta": {}, - "links": {} + "id": "74812" + } } - }, - "meta": {}, - "links": {} + } } ], "included": [ @@ -453,10 +443,7 @@ GET /posts?include=author&fields[posts]=title,excerpt&fields[users]=name "id": "74812", "attributes": { "name": "Tim" - }, - "relationships": {}, - "meta": {}, - "links": {} + } } ] } diff --git a/src/JsonApiResource.php b/src/JsonApiResource.php index 9f5031d..ef7e784 100644 --- a/src/JsonApiResource.php +++ b/src/JsonApiResource.php @@ -118,17 +118,19 @@ public function toResourceIdentifier(Request $request) * @api * * @param Request $request - * @return array{id: string, type: string, attributes: stdClass, relationships: stdClass, meta: stdClass, links: stdClass} + * @return array{id: string, type: string, attributes?: stdClass, relationships?: stdClass, meta?: stdClass, links?: stdClass} */ public function toArray($request) { return [ 'id' => $this->resolveId($request), 'type' => $this->resolveType($request), - 'attributes' => (object) $this->requestedAttributes($request)->all(), - 'relationships' => (object) $this->requestedRelationshipsAsIdentifiers($request)->all(), - 'meta' => (object) array_merge($this->toMeta($request), $this->meta), - 'links' => (object) self::parseLinks(array_merge($this->toLinks($request), $this->links)), + ...Collection::make([ + 'attributes' => $this->requestedAttributes($request)->all(), + 'relationships' => $this->requestedRelationshipsAsIdentifiers($request)->all(), + 'links' => self::parseLinks(array_merge($this->toLinks($request), $this->links)), + 'meta' => array_merge($this->toMeta($request), $this->meta), + ])->filter()->map(fn ($value) => (object) $value)->all(), ]; } @@ -141,9 +143,10 @@ public function toArray($request) public function with($request) { return [ - 'included' => $this->included($request) + ...$included = $this->included($request) ->uniqueStrict(fn (JsonApiResource $resource): string => $resource->toUniqueResourceIdentifier($request)) - ->values(), + ->values() + ->all() ? ['included' => $included] : [], 'jsonapi' => self::serverImplementationResolver()($request), ]; } diff --git a/src/JsonApiServerImplementation.php b/src/JsonApiServerImplementation.php index c571c84..5f12115 100644 --- a/src/JsonApiServerImplementation.php +++ b/src/JsonApiServerImplementation.php @@ -32,7 +32,7 @@ public function jsonSerialize(): array { return [ 'version' => $this->version, - 'meta' => (object) $this->meta, + ...$this->meta ? ['meta' => (object) $this->meta] : [], ]; } } diff --git a/src/Link.php b/src/Link.php index 15ec8b2..ebb842c 100644 --- a/src/Link.php +++ b/src/Link.php @@ -66,7 +66,7 @@ public function jsonSerialize(): array { return [ 'href' => $this->href, - 'meta' => (object) $this->meta, + ...$this->meta ? ['meta' => (object) $this->meta] : [], ]; } } diff --git a/src/RelationshipObject.php b/src/RelationshipObject.php index ecb336a..2c148bd 100644 --- a/src/RelationshipObject.php +++ b/src/RelationshipObject.php @@ -70,8 +70,8 @@ public function jsonSerialize(): array { return [ 'data' => $this->data, - 'meta' => (object) $this->meta, - 'links' => (object) self::parseLinks($this->links), + ...$this->meta ? ['meta' => (object) $this->meta] : [], + ...$this->links ? ['links' => (object) self::parseLinks($this->links)] : [], ]; } } diff --git a/src/ResourceIdentifier.php b/src/ResourceIdentifier.php index 67b1908..f2bd148 100644 --- a/src/ResourceIdentifier.php +++ b/src/ResourceIdentifier.php @@ -45,7 +45,7 @@ public function jsonSerialize(): array return [ 'type' => $this->type, 'id' => $this->id, - 'meta' => (object) $this->meta, + ...$this->meta ? ['meta' => (object) $this->meta] : [], ]; } } diff --git a/tests/Feature/AttributesTest.php b/tests/Feature/AttributesTest.php index 12d1713..46e88c4 100644 --- a/tests/Feature/AttributesTest.php +++ b/tests/Feature/AttributesTest.php @@ -41,13 +41,9 @@ public function toAttributes($request): array 'name' => 'Tim', 'email' => 'tim@example.com', ], - 'relationships' => [], - 'meta' => [], - 'links' => [], ], 'jsonapi' => [ 'version' => '1.0', - 'meta' => [], ], 'included' => [], ]); @@ -84,13 +80,9 @@ public function toAttributes($request): array 'name' => 'Tim', 'location' => 'Melbourne', ], - 'relationships' => [], - 'links' => [], - 'meta' => [], ], 'jsonapi' => [ 'version' => '1.0', - 'meta' => [], ], 'included' => [], ]); @@ -123,14 +115,9 @@ public function toAttributes($request): array 'data' => [ 'id' => 'expected-id', 'type' => 'basicModels', - 'attributes' => [], - 'relationships' => [], - 'meta' => [], - 'links' => [], ], 'jsonapi' => [ 'version' => '1.0', - 'meta' => [], ], 'included' => [], ]); @@ -162,13 +149,9 @@ public function toAttributes($request): array 'attributes' => [ 'location' => 'Melbourne', ], - 'relationships' => [], - 'links' => [], - 'meta' => [], ], 'jsonapi' => [ 'version' => '1.0', - 'meta' => [], ], 'included' => [], ]); @@ -196,14 +179,9 @@ public function toAttributes($request): array 'data' => [ 'id' => 'expected-id', 'type' => 'basicModels', - 'attributes' => [], - 'relationships' => [], - 'links' => [], - 'meta' => [], ], 'jsonapi' => [ 'version' => '1.0', - 'meta' => [], ], 'included' => [], ]); @@ -252,14 +230,9 @@ public function testItCanSpecifyMinimalAttributes(): void 'data' => [ 'type' => 'basicModels', 'id' => 'user-id', - 'attributes' => [], - 'relationships' => [], - 'meta' => [], - 'links' => [], ], 'jsonapi' => [ 'version' => '1.0', - 'meta' => [], ], 'included' => [], ]); @@ -287,13 +260,9 @@ public function testItCanRequestAttributesWhenUsingMinimalAttributes() 'attributes' => [ 'name' => 'user-name', ], - 'relationships' => [], - 'meta' => [], - 'links' => [], ], 'jsonapi' => [ 'version' => '1.0', - 'meta' => [], ], 'included' => [], ]); @@ -327,31 +296,23 @@ public function testItCanUseSparseFieldsetsWithIncludedCollections(): void 'data' => [ 'id' => 'user-id', 'type' => 'basicModels', - 'attributes' => [], 'relationships' => [ 'posts' => [ 'data' => [ [ 'id' => 'post-id-1', - 'meta' => [], 'type' => 'basicModels', ], [ 'id' => 'post-id-2', - 'meta' => [], 'type' => 'basicModels', ], ], - 'links' => [], - 'meta' => [], ], ], - 'meta' => [], - 'links' => [], ], 'jsonapi' => [ 'version' => '1.0', - 'meta' => [], ], 'included' => [ [ @@ -360,9 +321,6 @@ public function testItCanUseSparseFieldsetsWithIncludedCollections(): void 'attributes' => [ 'title' => 'post-title-1', ], - 'relationships' => [], - 'links' => [], - 'meta' => [], ], [ 'id' => 'post-id-2', @@ -370,9 +328,6 @@ public function testItCanUseSparseFieldsetsWithIncludedCollections(): void 'attributes' => [ 'title' => 'post-title-2', ], - 'relationships' => [], - 'links' => [], - 'meta' => [], ], ], ]); @@ -408,13 +363,9 @@ public function toAttributes($request): array 'attributes' => [ 'email' => 'tim@example.com', ], - 'relationships' => [], - 'meta' => [], - 'links' => [], ], 'jsonapi' => [ 'version' => '1.0', - 'meta' => [], ], 'included' => [], ]); @@ -452,13 +403,9 @@ public function toAttributes($request): array 'email' => 'tim@example.com', 'address' => '123 fake street', ], - 'relationships' => [], - 'meta' => [], - 'links' => [], ], 'jsonapi' => [ 'version' => '1.0', - 'meta' => [], ], 'included' => [], ]); @@ -497,13 +444,9 @@ public function toAttributes($request): array 'email' => 'tim@example.com', 'address' => '123 fake street', ], - 'relationships' => [], - 'meta' => [], - 'links' => [], ], 'jsonapi' => [ 'version' => '1.0', - 'meta' => [], ], 'included' => [], ]); diff --git a/tests/Feature/FeatureTest.php b/tests/Feature/FeatureTest.php index 218c1d2..230479b 100644 --- a/tests/Feature/FeatureTest.php +++ b/tests/Feature/FeatureTest.php @@ -42,9 +42,6 @@ public function testItCanPaginate(): void 'attributes' => [ 'name' => 'name-0', ], - 'relationships' => [], - 'links' => [], - 'meta' => [], ], [ 'id' => '2', @@ -52,9 +49,6 @@ public function testItCanPaginate(): void 'attributes' => [ 'name' => 'name-1', ], - 'relationships' => [], - 'links' => [], - 'meta' => [], ], ], 'included' => [], @@ -100,7 +94,6 @@ public function testItCanPaginate(): void ], ], 'jsonapi' => [ - 'meta' => [], 'version' => '1.0', ], ]); diff --git a/tests/Feature/JsonApiTest.php b/tests/Feature/JsonApiTest.php index 698d8ae..823e17c 100644 --- a/tests/Feature/JsonApiTest.php +++ b/tests/Feature/JsonApiTest.php @@ -36,14 +36,10 @@ public function testItCanReturnASingleResource(): void 'attributes' => [ 'name' => 'user-name', ], - 'relationships' => [], - 'meta' => [], - 'links' => [], ], 'included' => [], 'jsonapi' => [ 'version' => '1.0', - 'meta' => [], ], ]); $this->assertValidJsonApi($response); @@ -74,9 +70,6 @@ public function testItCanReturnACollection(): void 'attributes' => [ 'name' => 'user-name-1', ], - 'relationships' => [], - 'meta' => [], - 'links' => [], ], [ 'id' => 'user-id-2', @@ -84,27 +77,26 @@ public function testItCanReturnACollection(): void 'attributes' => [ 'name' => 'user-name-2', ], - 'relationships' => [], - 'meta' => [], - 'links' => [], ], ], 'included' => [], 'jsonapi' => [ 'version' => '1.0', - 'meta' => [], ], ]); $this->assertValidJsonApi($response); } - public function testItCastsEmptyAttributesAndRelationshipsToAnObject(): void + public function testItExcludesEmptyAttributesAndRelationships(): void { Route::get('test-route', fn () => UserResource::make((new BasicModel(['id' => 'user-id'])))); $response = $this->getJson('test-route?fields[basicModels]='); - self::assertStringContainsString('"attributes":{},"relationships":{},"meta":{},"links":{}', $response->content()); + self::assertStringNotContainsString('"attributes"', $response->content()); + self::assertStringNotContainsString('"relationships"', $response->content()); + self::assertStringNotContainsString('"meta"', $response->content()); + self::assertStringNotContainsString('"links"', $response->content()); $this->assertValidJsonApi($response); } @@ -126,17 +118,13 @@ public function toMeta($request): array 'data' => [ 'id' => 'expected-id', 'type' => 'basicModels', - 'attributes' => [], - 'relationships' => [], 'meta' => [ 'meta-key' => 'meta-value', ], - 'links' => [], ], 'included' => [], 'jsonapi' => [ 'version' => '1.0', - 'meta' => [], ], ]); $this->assertValidJsonApi($response); @@ -164,9 +152,6 @@ public function toLinks($request): array 'data' => [ 'id' => 'expected-id', 'type' => 'basicModels', - 'attributes' => [], - 'relationships' => [], - 'meta' => [], 'links' => [ 'self' => [ 'href' => 'https://example.test/self', @@ -176,18 +161,15 @@ public function toLinks($request): array ], 'related' => [ 'href' => 'https://example.test/related', - 'meta' => [], ], 'home' => [ 'href' => 'https://example.test', - 'meta' => [], ], ], ], 'included' => [], 'jsonapi' => [ 'version' => '1.0', - 'meta' => [], ], ]); $this->assertValidJsonApi($response); @@ -224,15 +206,10 @@ public function testItCanCustomiseTheTypeResolution(): void 'data' => [ 'id' => 'expected-id', 'type' => 'Tests\\Models\\BasicModel', - 'relationships' => [], - 'attributes' => [], - 'meta' => [], - 'links' => [], ], 'included' => [], 'jsonapi' => [ 'version' => '1.0', - 'meta' => [], ], ]); $this->assertValidJsonApi($response); @@ -251,15 +228,10 @@ public function testItCanCustomiseTheIdResolution(): void 'data' => [ 'id' => 'expected-id', 'type' => 'basicModels', - 'relationships' => [], - 'attributes' => [], - 'meta' => [], - 'links' => [], ], 'included' => [], 'jsonapi' => [ 'version' => '1.0', - 'meta' => [], ], ]); $this->assertValidJsonApi($response); @@ -267,31 +239,31 @@ public function testItCanCustomiseTheIdResolution(): void JsonApiResource::resolveIdNormally(); } - public function testItCastsEmptyResourceIdentifierMetaToObject(): void + public function testItExcludesEmptyResourceIdentifierMeta(): void { $relationship = new ResourceIdentifier('users', '5'); $json = json_encode($relationship); - self::assertSame('{"type":"users","id":"5","meta":{}}', $json); + self::assertSame('{"type":"users","id":"5"}', $json); } - public function testItCastsEmptyLinksMetaToObject(): void + public function testItExcludesEmptyLinksMeta(): void { $link = Link::self('https://timacdonald.me', []); $json = json_encode($link); - self::assertSame('{"href":"https:\/\/timacdonald.me","meta":{}}', $json); + self::assertSame('{"href":"https:\/\/timacdonald.me"}', $json); } - public function testItCastsEmptyImplementationMetaToObject(): void + public function testItExcludesEmptyImplementationMeta(): void { $implementation = new JsonApiServerImplementation('1.5', []); $json = json_encode($implementation); - self::assertSame('{"version":"1.5","meta":{}}', $json); + self::assertSame('{"version":"1.5"}', $json); } public function testItCanSpecifyAnImplementation(): void @@ -315,9 +287,6 @@ public function testItCanSpecifyAnImplementation(): void 'attributes' => [ 'name' => 'user-name', ], - 'relationships' => [], - 'meta' => [], - 'links' => [], ], 'included' => [], 'jsonapi' => [ @@ -332,7 +301,7 @@ public function testItCanSpecifyAnImplementation(): void BasicJsonApiResource::resolveServerImplementationNormally(); } - public function testItCastsEmptyRelationshipLinkMetaToJsonObject() + public function testItExcludesEmptyRelationshipLinkMeta() { $resourceLink = RelationshipObject::toOne( new ResourceIdentifier('expected-type', 'expected-id') @@ -340,7 +309,7 @@ public function testItCastsEmptyRelationshipLinkMetaToJsonObject() $json = json_encode($resourceLink); - self::assertSame('{"data":{"type":"expected-type","id":"expected-id","meta":{}},"meta":{},"links":{}}', $json); + self::assertSame('{"data":{"type":"expected-type","id":"expected-id"}}', $json); } public function testItCanPopulateAllTheMetasAndAllTheLinks() @@ -581,7 +550,6 @@ public static function collection($resource): JsonApiResourceCollection 'data' => [ 'id' => 'user-id', 'type' => 'basicModels', - 'attributes' => [], 'relationships' => [ 'profile' => [ 'data' => null, @@ -696,8 +664,6 @@ public static function collection($resource): JsonApiResourceCollection [ 'id' => 'avatar-id', 'type' => 'basicModels', - 'attributes' => [], - 'relationships' => [], 'meta' => [ 'avatar-internal' => 'meta', 'avatar-external' => 'meta', @@ -720,8 +686,6 @@ public static function collection($resource): JsonApiResourceCollection [ 'id' => 'post-id-1', 'type' => 'basicModels', - 'attributes' => [], - 'relationships' => [], 'meta' => [ 'posts-internal' => 'meta', 'posts-internal-collection' => 'meta', @@ -742,15 +706,12 @@ public static function collection($resource): JsonApiResourceCollection ], 'external' => [ 'href' => 'posts.com', - 'meta' => [], ], ], ], [ 'id' => 'post-id-2', 'type' => 'basicModels', - 'attributes' => [], - 'relationships' => [], 'meta' => [ 'posts-internal' => 'meta', 'posts-internal-collection' => 'meta', @@ -771,7 +732,6 @@ public static function collection($resource): JsonApiResourceCollection ], 'external' => [ 'href' => 'posts.com', - 'meta' => [], ], ], ], diff --git a/tests/Feature/RelationshipsTest.php b/tests/Feature/RelationshipsTest.php index d59b089..5976aa3 100644 --- a/tests/Feature/RelationshipsTest.php +++ b/tests/Feature/RelationshipsTest.php @@ -56,14 +56,10 @@ public function toRelationships($request): array 'title' => 'post-title', 'content' => 'post-content', ], - 'relationships' => [], - 'links' => [], - 'meta' => [], ], 'included' => [], 'jsonapi' => [ 'version' => '1.0', - 'meta' => [], ], ]); $this->assertValidJsonApi($response); @@ -98,18 +94,12 @@ public function testItCanIncludeASingleToOneResourceForASingleResource(): void 'data' => [ 'id' => 'author-id', 'type' => 'basicModels', - 'meta' => [], ], - 'links' => [], - 'meta' => [], ], ], - 'links' => [], - 'meta' => [], ], 'jsonapi' => [ 'version' => '1.0', - 'meta' => [], ], 'included' => [ [ @@ -118,9 +108,6 @@ public function testItCanIncludeASingleToOneResourceForASingleResource(): void 'attributes' => [ 'name' => 'author-name', ], - 'relationships' => [], - 'links' => [], - 'meta' => [], ], ], ]); @@ -168,27 +155,18 @@ public function testItCanIncludeNestedToOneResourcesForASingleResource(): void 'data' => [ 'id' => 'author-id', 'type' => 'basicModels', - 'meta' => [], ], - 'links' => [], - 'meta' => [], ], 'featureImage' => [ 'data' => [ 'id' => 'feature-image-id', 'type' => 'basicModels', - 'meta' => [], ], - 'links' => [], - 'meta' => [], ], ], - 'links' => [], - 'meta' => [], ], 'jsonapi' => [ 'version' => '1.0', - 'meta' => [], ], 'included' => [ [ @@ -202,23 +180,15 @@ public function testItCanIncludeNestedToOneResourcesForASingleResource(): void 'data' => [ 'id' => 'avatar-id', 'type' => 'basicModels', - 'meta' => [], ], - 'links' => [], - 'meta' => [], ], 'license' => [ 'data' => [ 'id' => 'license-id', 'type' => 'basicModels', - 'meta' => [], ], - 'links' => [], - 'meta' => [], ], ], - 'links' => [], - 'meta' => [], ], [ 'id' => 'feature-image-id', @@ -226,9 +196,6 @@ public function testItCanIncludeNestedToOneResourcesForASingleResource(): void 'attributes' => [ 'url' => 'https://example.com/doggo.png', ], - 'relationships' => [], - 'links' => [], - 'meta' => [], ], [ 'id' => 'license-id', @@ -236,9 +203,6 @@ public function testItCanIncludeNestedToOneResourcesForASingleResource(): void 'attributes' => [ 'key' => 'license-key', ], - 'relationships' => [], - 'links' => [], - 'meta' => [], ], [ 'id' => 'avatar-id', @@ -246,9 +210,6 @@ public function testItCanIncludeNestedToOneResourcesForASingleResource(): void 'attributes' => [ 'url' => 'https://example.com/avatar.png', ], - 'relationships' => [], - 'links' => [], - 'meta' => [], ], ], ]); @@ -287,51 +248,34 @@ public function toRelationships($request): array 'data' => [ 'id' => 'parent-id', 'type' => 'basicModels', - 'attributes' => [], 'relationships' => [ 'child' => [ 'data' => [ 'id' => 'child-id-1', 'type' => 'basicModels', - 'meta' => [], ], - 'links' => [], - 'meta' => [], ], ], - 'links' => [], - 'meta' => [], ], 'jsonapi' => [ 'version' => '1.0', - 'meta' => [], ], 'included' => [ [ 'id' => 'child-id-1', 'type' => 'basicModels', - 'attributes' => [], 'relationships' => [ 'child' => [ 'data' => [ 'id' => 'child-id-2', 'type' => 'basicModels', - 'meta' => [], ], - 'links' => [], - 'meta' => [], ], ], - 'links' => [], - 'meta' => [], ], [ 'id' => 'child-id-2', 'type' => 'basicModels', - 'attributes' => [], - 'relationships' => [], - 'links' => [], - 'meta' => [], ], ], ]); @@ -371,66 +315,44 @@ public function toRelationships($request): array 'data' => [ 'id' => 'parent-id', 'type' => 'basicModels', - 'attributes' => [], 'relationships' => [ 'child' => [ 'data' => [ 'id' => 'child-id-1', 'type' => 'basicModels', - 'meta' => [], ], - 'links' => [], - 'meta' => [], ], ], - 'meta' => [], - 'links' => [], ], 'jsonapi' => [ 'version' => '1.0', - 'meta' => [], ], 'included' => [ [ 'id' => 'child-id-1', 'type' => 'basicModels', - 'attributes' => [], 'relationships' => [ 'child' => [ 'data' => [ [ 'id' => 'child-id-2', 'type' => 'basicModels', - 'meta' => [], ], [ 'id' => 'child-id-3', 'type' => 'basicModels', - 'meta' => [], ], ], - 'links' => [], - 'meta' => [], ], ], - 'meta' => [], - 'links' => [], ], [ 'id' => 'child-id-2', 'type' => 'basicModels', - 'attributes' => [], - 'relationships'=> [], - 'links' => [], - 'meta' => [], ], [ 'id' => 'child-id-3', 'type' => 'basicModels', - 'attributes' => [], - 'relationships'=> [], - 'links' => [], - 'meta' => [], ], ], ]); @@ -476,14 +398,9 @@ public function testItCanIncludeToOneResourcesForACollectionOfResources(): void 'data' => [ 'id' => 'author-id-1', 'type' => 'basicModels', - 'meta' => [], ], - 'links' => [], - 'meta' => [], ], ], - 'meta' => [], - 'links' => [], ], [ 'id' => 'post-id-2', @@ -497,19 +414,13 @@ public function testItCanIncludeToOneResourcesForACollectionOfResources(): void 'data' => [ 'id' => 'author-id-2', 'type' => 'basicModels', - 'meta' => [], ], - 'links' => [], - 'meta' => [], ], ], - 'meta' => [], - 'links' => [], ], ], 'jsonapi' => [ 'version' => '1.0', - 'meta' => [], ], 'included' => [ [ @@ -518,9 +429,6 @@ public function testItCanIncludeToOneResourcesForACollectionOfResources(): void 'attributes' => [ 'name' => 'author-name-1', ], - 'relationships' => [], - 'meta' => [], - 'links' => [], ], [ 'id' => 'author-id-2', @@ -528,9 +436,6 @@ public function testItCanIncludeToOneResourcesForACollectionOfResources(): void 'attributes' => [ 'name' => 'author-name-2', ], - 'relationships' => [], - 'meta' => [], - 'links' => [], ], ], ]); @@ -573,24 +478,17 @@ public function testItCanIncludeACollectionOfResourcesForASingleResource(): void [ 'id' => 'post-id-1', 'type' => 'basicModels', - 'meta' => [], ], [ 'id' => 'post-id-2', 'type' => 'basicModels', - 'meta' => [], ], ], - 'links' => [], - 'meta' => [], ], ], - 'meta' => [], - 'links' => [], ], 'jsonapi' => [ 'version' => '1.0', - 'meta' => [], ], 'included' => [ [ @@ -600,9 +498,6 @@ public function testItCanIncludeACollectionOfResourcesForASingleResource(): void 'title' => 'post-title-1', 'content' => 'post-content-1', ], - 'relationships' => [], - 'meta' => [], - 'links' => [], ], [ 'id' => 'post-id-2', @@ -611,9 +506,6 @@ public function testItCanIncludeACollectionOfResourcesForASingleResource(): void 'title' => 'post-title-2', 'content' => 'post-content-2', ], - 'relationships' => [], - 'meta' => [], - 'links' => [], ], ], ]); @@ -700,20 +592,14 @@ public function testItCanIncludeAManyManyManyRelationship(): void [ 'id' => 'comment-id-1', 'type' => 'basicModels', - 'meta' => [], ], [ 'id' => 'comment-id-2', 'type' => 'basicModels', - 'meta' => [], ], ], - 'links' => [], - 'meta' => [], ], ], - 'meta' => [], - 'links' => [], ], [ 'id' => 'post-id-2', @@ -728,25 +614,18 @@ public function testItCanIncludeAManyManyManyRelationship(): void [ 'id' => 'comment-id-3', 'type' => 'basicModels', - 'meta' => [], ], [ 'id' => 'comment-id-4', 'type' => 'basicModels', - 'meta' => [], ], ], - 'links' => [], - 'meta' => [], ], ], - 'meta' => [], - 'links' => [], ], ], 'jsonapi' => [ 'version' => '1.0', - 'meta' => [], ], 'included' => [ [ @@ -761,20 +640,14 @@ public function testItCanIncludeAManyManyManyRelationship(): void [ 'id' => 'like-id-1', 'type' => 'basicModels', - 'meta' => [], ], [ 'id' => 'like-id-2', 'type' => 'basicModels', - 'meta' => [], ], ], - 'links' => [], - 'meta' => [], ], ], - 'meta' => [], - 'links' => [], ], [ 'id' => 'comment-id-2', @@ -788,52 +661,30 @@ public function testItCanIncludeAManyManyManyRelationship(): void [ 'id' => 'like-id-3', 'type' => 'basicModels', - 'meta' => [], ], [ 'id' => 'like-id-4', 'type' => 'basicModels', - 'meta' => [], ], ], - 'links' => [], - 'meta' => [], ], ], - 'meta' => [], - 'links' => [], ], [ 'id' => 'like-id-1', 'type' => 'basicModels', - 'attributes' => [], - 'relationships' => [], - 'links' => [], - 'meta' => [], ], [ 'id' => 'like-id-2', 'type' => 'basicModels', - 'attributes' => [], - 'relationships' => [], - 'links' => [], - 'meta' => [], ], [ 'id' => 'like-id-3', 'type' => 'basicModels', - 'attributes' => [], - 'relationships' => [], - 'links' => [], - 'meta' => [], ], [ 'id' => 'like-id-4', 'type' => 'basicModels', - 'attributes' => [], - 'relationships' => [], - 'links' => [], - 'meta' => [], ], [ 'id' => 'comment-id-3', @@ -847,20 +698,14 @@ public function testItCanIncludeAManyManyManyRelationship(): void [ 'id' => 'like-id-5', 'type' => 'basicModels', - 'meta' => [], ], [ 'id' => 'like-id-6', 'type' => 'basicModels', - 'meta' => [], ], ], - 'links' => [], - 'meta' => [], ], ], - 'links' => [], - 'meta' => [], ], [ 'id' => 'comment-id-4', @@ -874,52 +719,30 @@ public function testItCanIncludeAManyManyManyRelationship(): void [ 'id' => 'like-id-7', 'type' => 'basicModels', - 'meta' => [], ], [ 'id' => 'like-id-8', 'type' => 'basicModels', - 'meta' => [], ], ], - 'links' => [], - 'meta' => [], ], ], - 'links' => [], - 'meta' => [], ], [ 'id' => 'like-id-5', 'type' => 'basicModels', - 'attributes' => [], - 'relationships' => [], - 'links' => [], - 'meta' => [], ], [ 'id' => 'like-id-6', 'type' => 'basicModels', - 'attributes' => [], - 'relationships' => [], - 'links' => [], - 'meta' => [], ], [ 'id' => 'like-id-7', 'type' => 'basicModels', - 'attributes' => [], - 'relationships' => [], - 'links' => [], - 'meta' => [], ], [ 'id' => 'like-id-8', 'type' => 'basicModels', - 'attributes' => [], - 'relationships' => [], - 'links' => [], - 'meta' => [], ], ], ]); @@ -966,24 +789,17 @@ public function toAttributes($request): array 'data' => [ 'id' => 'post-id', 'type' => 'basicModels', - 'attributes' => [], 'relationships' => [ 'relation' => [ 'data' => [ 'id' => 'relation-id', 'type' => 'relation-type', - 'meta' => [], ], - 'links' => [], - 'meta' => [], ], ], - 'meta' => [], - 'links' => [], ], 'jsonapi' => [ 'version' => '1.0', - 'meta' => [], ], 'included' => [ [ @@ -992,9 +808,6 @@ public function toAttributes($request): array 'attributes' => [ 'name' => 'expected name', ], - 'relationships' => [], - 'links' => [], - 'meta' => [], ], ], ]); @@ -1037,14 +850,9 @@ public function testItFiltersOutDuplicateIncludesForACollectionOfResources(): vo 'data' => [ 'id' => 'avatar-id', 'type' => 'basicModels', - 'meta' => [], ], - 'links' => [], - 'meta' => [], ], ], - 'meta' => [], - 'links' => [], ], [ 'id' => 'user-id-2', @@ -1057,19 +865,13 @@ public function testItFiltersOutDuplicateIncludesForACollectionOfResources(): vo 'data' => [ 'id' => 'avatar-id', 'type' => 'basicModels', - 'meta' => [], ], - 'links' => [], - 'meta' => [], ], ], - 'meta' => [], - 'links' => [], ], ], 'jsonapi' => [ 'version' => '1.0', - 'meta' => [], ], 'included' => [ [ @@ -1078,9 +880,6 @@ public function testItFiltersOutDuplicateIncludesForACollectionOfResources(): vo 'attributes' => [ 'url' => 'https://example.com/avatar.png', ], - 'relationships' => [], - 'links' => [], - 'meta' => [], ], ], ]); @@ -1122,19 +921,13 @@ public function testItFiltersOutDuplicateResourceObjectsIncludesForASingleResour [ 'id' => 'post-id', 'type' => 'basicModels', - 'meta' => [], ], ], - 'links' => [], - 'meta' => [], ], ], - 'meta' => [], - 'links' =>[], ], 'jsonapi' => [ 'version' => '1.0', - 'meta' => [], ], 'included' => [ [ @@ -1144,9 +937,6 @@ public function testItFiltersOutDuplicateResourceObjectsIncludesForASingleResour 'title' => 'post-title', 'content' => 'post-content', ], - 'relationships' => [], - 'meta' => [], - 'links' =>[], ], ], ]); @@ -1173,13 +963,9 @@ public function testItHasIncludedArrayWhenIncludeParameterIsPresentForASingleRes 'attributes' => [ 'name' => 'user-name', ], - 'relationships' => [], - 'meta' => [], - 'links' =>[], ], 'jsonapi' => [ 'version' => '1.0', - 'meta' => [], ], 'included' => [], ]); @@ -1207,14 +993,10 @@ public function testItHasIncludedArrayWhenIncludeParameterIsPresentForACollectio 'attributes' => [ 'name' => 'user-name', ], - 'relationships' => [], - 'meta' => [], - 'links' => [], ], ], 'jsonapi' => [ 'version' => '1.0', - 'meta' => [], ], 'included' => [], ]); @@ -1241,16 +1023,11 @@ public function testItCanReturnNullForEmptyToOneRelationships(): void 'relationships' => [ 'avatar' => [ 'data' => null, - 'links' => [], - 'meta' => [], ], ], - 'meta' => [], - 'links' => [], ], 'jsonapi' => [ 'version' => '1.0', - 'meta' => [], ], 'included' => [], ]); @@ -1277,16 +1054,11 @@ public function testItCanReturnAnEmptyArrayForEmptyToManyRelationships(): void 'relationships' => [ 'posts' => [ "data" => [], - "links" => [], - "meta" => [], ], ], - 'meta' => [], - 'links' => [], ], 'jsonapi' => [ 'version' => '1.0', - 'meta' => [], ], 'included' => [], ]); @@ -1349,14 +1121,9 @@ public function testCollectionIncludesDoesntBecomeNumericKeyedObjectAfterFilteri 'data' => [ 'id' => '1', 'type' => 'basicModels', - 'meta' => [], ], - 'links' => [], - 'meta' => [], ], ], - 'meta' => [], - 'links' => [], ], [ 'id' => 'user-id-2', @@ -1369,14 +1136,9 @@ public function testCollectionIncludesDoesntBecomeNumericKeyedObjectAfterFilteri 'data' => [ 'id' => '1', 'type' => 'basicModels', - 'meta' => [], ], - 'links' => [], - 'meta' => [], ], ], - 'meta' => [], - 'links' => [], ], [ 'id' => 'user-id-3', @@ -1389,19 +1151,13 @@ public function testCollectionIncludesDoesntBecomeNumericKeyedObjectAfterFilteri 'data' => [ 'id' => '2', 'type' => 'basicModels', - 'meta' => [], ], - 'links' => [], - 'meta' => [], ], ], - 'meta' => [], - 'links' => [], ], ], 'jsonapi' => [ 'version' => '1.0', - 'meta' => [], ], 'included' => [ [ @@ -1410,9 +1166,6 @@ public function testCollectionIncludesDoesntBecomeNumericKeyedObjectAfterFilteri 'attributes' => [ 'url' => 'https://example.com/avatar1.png', ], - 'relationships' => [], - 'links' => [], - 'meta' => [], ], [ 'id' => '2', @@ -1420,9 +1173,6 @@ public function testCollectionIncludesDoesntBecomeNumericKeyedObjectAfterFilteri 'attributes' => [ 'url' => 'https://example.com/avatar2.png', ], - 'relationships' => [], - 'links' => [], - 'meta' => [], ], ], ]); @@ -1473,15 +1223,10 @@ public function testSingleResourceIncludesDoesntBecomeNumericKeyedObjectAfterFil [ 'id' => '2', 'type' => 'basicModels', - 'meta' => [], ], ], - 'meta' => [], - 'links' => [], ], ], - 'meta' => [], - 'links' => [], ], 'included' => [ [ @@ -1497,15 +1242,10 @@ public function testSingleResourceIncludesDoesntBecomeNumericKeyedObjectAfterFil [ 'id' => '3', 'type' => 'basicModels', - 'meta' => [], ], ], - 'meta' => [], - 'links' => [], ], ], - 'meta' => [], - 'links' => [], ], [ 'id' => '3', @@ -1513,14 +1253,10 @@ public function testSingleResourceIncludesDoesntBecomeNumericKeyedObjectAfterFil 'attributes' => [ 'content' => 'Comment 1', ], - 'relationships' => [], - 'meta' => [], - 'links' => [], ], ], 'jsonapi' => [ 'version' => '1.0', - 'meta' => [], ], ] ); @@ -1552,13 +1288,9 @@ public function toRelationships($request): array 'attributes' => [ 'name' => 'user-name', ], - 'relationships' => [], - 'meta' => [], - 'links' => [], ], 'jsonapi' => [ 'version' => '1.0', - 'meta' => [], ], 'included' => [], ]); @@ -1600,18 +1332,12 @@ public function toRelationships($request): array 'data' => [ 'type' => 'basicModels', 'id' => '2', - 'meta' => [], ], - 'links' => [], - 'meta' => [], ], ], - 'meta' => [], - 'links' => [], ], 'jsonapi' => [ 'version' => '1.0', - 'meta' => [], ], 'included' => [ [ @@ -1620,9 +1346,6 @@ public function toRelationships($request): array 'attributes' => [ 'name' => 'relation-name', ], - 'relationships' => [], - 'links' => [], - 'meta' => [], ], ], ]); @@ -1665,18 +1388,12 @@ public function toRelationships($request): array 'data' => [ 'type' => 'basicModels', 'id' => '2', - 'meta' => [], ], - 'links' => [], - 'meta' => [], ], ], - 'meta' => [], - 'links' => [], ], 'jsonapi' => [ 'version' => '1.0', - 'meta' => [], ], 'included' => [ [ @@ -1685,9 +1402,6 @@ public function toRelationships($request): array 'attributes' => [ 'name' => 'relation-name', ], - 'relationships' => [], - 'links' => [], - 'meta' => [], ], ], ]); diff --git a/tests/Feature/ResourceIdentificationTest.php b/tests/Feature/ResourceIdentificationTest.php index 1952012..8e8c755 100644 --- a/tests/Feature/ResourceIdentificationTest.php +++ b/tests/Feature/ResourceIdentificationTest.php @@ -29,15 +29,10 @@ public function testIt() "author": { "data": { "type": "users", - "id": "74812", - "meta": {} - }, - "meta": {}, - "links": {} + "id": "74812" + } } - }, - "meta": {}, - "links": {} + } }, { "id": "39974", @@ -50,15 +45,10 @@ public function testIt() "author": { "data": { "type": "users", - "id": "74812", - "meta": {} - }, - "meta": {}, - "links": {} + "id": "74812" + } } - }, - "meta": {}, - "links": {} + } } ], "included": [ @@ -67,10 +57,7 @@ public function testIt() "id": "74812", "attributes": { "name": "Tim" - }, - "relationships": {}, - "meta": {}, - "links": {} + } } ] } @@ -90,14 +77,9 @@ public function testItResolvesTheIdAndTypeOfAModel(): void 'data' => [ 'id' => 'user-id', 'type' => 'basicModels', - 'attributes' => [], - 'relationships' => [], - 'links' => [], - 'meta' => [], ], 'jsonapi' => [ 'version' => '1.0', - 'meta' => [], ], 'included' => [], ]); @@ -120,14 +102,9 @@ public function testItCastsAModelsIntegerIdToAString(): void 'data' => [ 'id' => '55', 'type' => 'basicModels', - 'attributes' => [], - 'relationships' => [], - 'links' => [], - 'meta' => [], ], 'jsonapi' => [ 'version' => '1.0', - 'meta' => [], ], 'included' => [], ]); diff --git a/tests/Unit/AttributesAsPropertiesTest.php b/tests/Unit/AttributesAsPropertiesTest.php index 98d375b..306abfd 100644 --- a/tests/Unit/AttributesAsPropertiesTest.php +++ b/tests/Unit/AttributesAsPropertiesTest.php @@ -41,9 +41,6 @@ public function toAttributes($request) 'attributes' => [ 'content' => 'post-content', ], - 'relationships' => [], - 'meta' => [], - 'links' => [], ], $response->getData(true)['data']); } @@ -76,9 +73,6 @@ public function toAttributes($request) 'attributes' => [ 'title' => 'expected-title', ], - 'relationships' => [], - 'meta' => [], - 'links' => [], ], $response->getData(true)['data']); } @@ -97,6 +91,6 @@ public function getAttributesAttribute() $response = $resource->toResponse(Request::create('https://timacdonald.me')); $this->assertValidJsonApi($response->content()); - $this->assertSame([], $response->getData(true)['data']['attributes']); + $this->assertSame([], $response->getData(true)['data']['attributes'] ?? []); } } diff --git a/tests/Unit/LinkTest.php b/tests/Unit/LinkTest.php index 4af0b66..7011f5c 100644 --- a/tests/Unit/LinkTest.php +++ b/tests/Unit/LinkTest.php @@ -21,22 +21,22 @@ public function testItSerializes(): void $this->assertSame('{"href":"https:\/\/related.com","meta":{"expected":"meta"}}', $serialized); } - public function testEmptyMetaIsObject(): void + public function testEmptyMetaIsExluded(): void { $link = Link::related('https://related.com', []); $serialized = json_encode($link); - $this->assertSame('{"href":"https:\/\/related.com","meta":{}}', $serialized); + $this->assertSame('{"href":"https:\/\/related.com"}', $serialized); } - public function testMissingMetaIsObject(): void + public function testMissingMetaIsExcluded(): void { $link = Link::related('https://related.com'); $serialized = json_encode($link); - $this->assertSame('{"href":"https:\/\/related.com","meta":{}}', $serialized); + $this->assertSame('{"href":"https:\/\/related.com"}', $serialized); } public function testMetaCanBeAppended(): void @@ -70,7 +70,7 @@ public function testItCanUseHash() $links = json_encode($resource->toArray($request)['links']); - $this->assertSame('{"foo":{"href":"http:\/\/foo.com","meta":{}}}', $links); + $this->assertSame('{"foo":{"href":"http:\/\/foo.com"}}', $links); JsonApiResource::resolveIdNormally(); JsonApiResource::resolveTypeNormally(); diff --git a/tests/Unit/RelationshipObjectTest.php b/tests/Unit/RelationshipObjectTest.php index be5632e..0f29ef0 100644 --- a/tests/Unit/RelationshipObjectTest.php +++ b/tests/Unit/RelationshipObjectTest.php @@ -17,25 +17,25 @@ public function testItSerializes(): void $serialized = json_encode($link); - $this->assertSame('{"data":{"type":"expected-type","id":"expected-id","meta":{}},"meta":{"expected":"meta"},"links":{"expected":{"href":"link","meta":{}}}}', $serialized); + $this->assertSame('{"data":{"type":"expected-type","id":"expected-id"},"meta":{"expected":"meta"},"links":{"expected":{"href":"link"}}}', $serialized); } - public function testEmptyMetaAndLinksIsObject(): void + public function testEmptyMetaAndLinksIsExcluded(): void { $link = RelationshipObject::toOne(new ResourceIdentifier('expected-type', 'expected-id'), [], []); $serialized = json_encode($link); - $this->assertSame('{"data":{"type":"expected-type","id":"expected-id","meta":{}},"meta":{},"links":{}}', $serialized); + $this->assertSame('{"data":{"type":"expected-type","id":"expected-id"}}', $serialized); } - public function testMissingMetaAndLinksIsObject(): void + public function testMissingMetaAndLinksIsExcluded(): void { $link = RelationshipObject::toOne(new ResourceIdentifier('expected-type', 'expected-id')); $serialized = json_encode($link); - $this->assertSame('{"data":{"type":"expected-type","id":"expected-id","meta":{}},"meta":{},"links":{}}', $serialized); + $this->assertSame('{"data":{"type":"expected-type","id":"expected-id"}}', $serialized); } public function testMetaAndLinksCanBeAppended(): void @@ -47,6 +47,6 @@ public function testMetaAndLinksCanBeAppended(): void ->withLinks([Link::self('self.com')]) ); - $this->assertSame('{"data":[{"type":"expected-type","id":"expected-id","meta":{}}],"meta":{"original":"meta","expected":"meta","another":"one"},"links":{"related":{"href":"related.com","meta":{}},"self":{"href":"self.com","meta":{}}}}', $serialized); + $this->assertSame('{"data":[{"type":"expected-type","id":"expected-id"}],"meta":{"original":"meta","expected":"meta","another":"one"},"links":{"related":{"href":"related.com"},"self":{"href":"self.com"}}}', $serialized); } } diff --git a/tests/Unit/RelationshipsAsPropertiesTest.php b/tests/Unit/RelationshipsAsPropertiesTest.php index b171684..e623d42 100644 --- a/tests/Unit/RelationshipsAsPropertiesTest.php +++ b/tests/Unit/RelationshipsAsPropertiesTest.php @@ -47,10 +47,7 @@ public function toRelationships($request) 'data' => [ 'type' => 'basicModels', 'id' => 'author-id', - 'meta' => [], ], - 'meta' => [], - 'links' => [], ], ], $response->getData(true)['data']['relationships']); $this->assertSame([ @@ -60,9 +57,6 @@ public function toRelationships($request) 'attributes' => [ 'name' => 'author-name', ], - 'relationships' => [], - 'meta' => [], - 'links' => [], ], ], $response->getData(true)['included']); } @@ -96,10 +90,7 @@ public function toRelationships($request) 'data' => [[ 'type' => 'basicModels', 'id' => 'comment-id', - 'meta' => [], ]], - 'meta' => [], - 'links' => [], ], ], $response->getData(true)['data']['relationships']); $this->assertSame([ @@ -109,9 +100,6 @@ public function toRelationships($request) 'attributes' => [ 'content' => 'comment-content', ], - 'relationships' => [], - 'meta' => [], - 'links' => [], ], ], $response->getData(true)['included']); } @@ -147,10 +135,7 @@ public function toRelationships($request) 'data' => [[ 'type' => 'basicModels', 'id' => 'comment-id', - 'meta' => [], ]], - 'meta' => [], - 'links' => [], ], ], $response->getData(true)['data']['relationships']); $this->assertSame([ @@ -160,9 +145,6 @@ public function toRelationships($request) 'attributes' => [ 'content' => 'comment-content', ], - 'relationships' => [], - 'meta' => [], - 'links' => [], ], ], $response->getData(true)['included']); } @@ -202,11 +184,8 @@ public function toRelationships($request) [ 'type' => 'basicModels', 'id' => 'post-id', - 'meta' => [], ], ], - 'meta' => [], - 'links' => [], ], ], $response->getData(true)['data']['relationships']); $this->assertSame([ @@ -217,9 +196,6 @@ public function toRelationships($request) 'title' => 'post-title', 'content' => 'post-content', ], - 'relationships' => [], - 'meta' => [], - 'links' => [], ], ], $response->getData(true)['included']); JsonApiResource::guessRelationshipResourceUsing(null); @@ -240,6 +216,6 @@ public function getRelationshipsAttribute() $response = $resource->toResponse(Request::create('https://timacdonald.me')); $this->assertValidJsonApi($response->content()); - $this->assertSame([], $response->getData(true)['data']['relationships']); + $this->assertSame([], $response->getData(true)['data']['relationships'] ?? []); } } diff --git a/tests/Unit/ResourceIdentifierTest.php b/tests/Unit/ResourceIdentifierTest.php index 4ba6b99..f627512 100644 --- a/tests/Unit/ResourceIdentifierTest.php +++ b/tests/Unit/ResourceIdentifierTest.php @@ -18,22 +18,22 @@ public function testItSerializes(): void $this->assertSame('{"type":"expected-type","id":"expected-id","meta":{"expected":"meta"}}', $serialized); } - public function testEmptyMetaIsObject(): void + public function testEmptyMetaIsExcluded(): void { $identifier = new ResourceIdentifier('expected-type', 'expected-id', []); $serialized = json_encode($identifier); - $this->assertSame('{"type":"expected-type","id":"expected-id","meta":{}}', $serialized); + $this->assertSame('{"type":"expected-type","id":"expected-id"}', $serialized); } - public function testMissingMetaIsObject(): void + public function testMissingMetaIsExcluded(): void { $identifier = new ResourceIdentifier('expected-type', 'expected-id'); $serialized = json_encode($identifier); - $this->assertSame('{"type":"expected-type","id":"expected-id","meta":{}}', $serialized); + $this->assertSame('{"type":"expected-type","id":"expected-id"}', $serialized); } public function testMetaCanBeAppended(): void diff --git a/tests/Unit/ServerApiImplementationTest.php b/tests/Unit/ServerApiImplementationTest.php index 5349157..aea6717 100644 --- a/tests/Unit/ServerApiImplementationTest.php +++ b/tests/Unit/ServerApiImplementationTest.php @@ -22,21 +22,21 @@ public function testItSerializes(): void self::assertSame('{"version":"5.0","meta":{"expected":"meta","more":"meta"}}', $json); } - public function testEmptyMetaIsObject(): void + public function testEmptyMetaIsExcluded(): void { $instance = new JsonApiServerImplementation('5.0', []); $json = json_encode($instance); - $this->assertSame('{"version":"5.0","meta":{}}', $json); + $this->assertSame('{"version":"5.0"}', $json); } - public function testMissingMetaIsObject(): void + public function testMissingMetaIsExcluded(): void { $instance = new JsonApiServerImplementation('5.0'); $json = json_encode($instance); - $this->assertSame('{"version":"5.0","meta":{}}', $json); + $this->assertSame('{"version":"5.0"}', $json); } }