From 18fd37a5c8e88f6871cb4b2d6bb09a031dcfd041 Mon Sep 17 00:00:00 2001 From: kocsismate Date: Thu, 18 Apr 2019 18:35:39 +0200 Subject: [PATCH] Revert "Fix output for ToOneRelationship when the relationship doesn't contain any keys" --- CHANGELOG.md | 6 +----- src/JsonApi/Schema/Relationship/AbstractRelationship.php | 8 ++++---- .../Schema/Relationship/AbstractRelationshipTest.php | 8 ++------ .../Transformer/AbstractResourceTransformerTest.php | 8 +++----- 4 files changed, 10 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 61047062..29596f73 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,17 +10,13 @@ REMOVED: FIXED: -## 3.1.1 - 2019-04-05 +## 3.1.1 - 2019-04-18 DEPRECATED: - `ToOneRelationship::omitWhenNotIncluded()`: Use `ToOneRelationship::omitDataWhenNotIncluded()` - `ToManyRelationship::omitWhenNotIncluded()`: Use `ToManyRelationship::omitDataWhenNotIncluded()` -FIXED: - -- [#82](https://github.com/woohoolabs/yin/pull/82): Fix output for ToOneRelationship when the relationship doesn't contain any keys - ## 3.1.0 - 2019-01-17 This is a release with several deprecations in order to ensure forward compatibility with Yin 4.0. diff --git a/src/JsonApi/Schema/Relationship/AbstractRelationship.php b/src/JsonApi/Schema/Relationship/AbstractRelationship.php index ef050bae..e7004a43 100644 --- a/src/JsonApi/Schema/Relationship/AbstractRelationship.php +++ b/src/JsonApi/Schema/Relationship/AbstractRelationship.php @@ -140,13 +140,11 @@ public function transform( array $defaultRelationships, array $additionalMeta = [] ): ?array { - $relationship = [ - "data" => null, - ]; + $relationship = null; if (( $transformation->fetchedRelationship === $relationshipName && - $this->data !== null && + $this->data && $this->omitDataWhenNotIncluded === false ) || $transformation->request->isIncludedRelationship( @@ -161,6 +159,8 @@ public function transform( } if ($transformation->request->isIncludedField($resourceType, $relationshipName)) { + $relationship = []; + // Links if ($this->links !== null) { $relationship["links"] = $this->links->transform(); diff --git a/tests/JsonApi/Schema/Relationship/AbstractRelationshipTest.php b/tests/JsonApi/Schema/Relationship/AbstractRelationshipTest.php index 4f6b6cc0..1e5f0038 100644 --- a/tests/JsonApi/Schema/Relationship/AbstractRelationshipTest.php +++ b/tests/JsonApi/Schema/Relationship/AbstractRelationshipTest.php @@ -81,12 +81,8 @@ public function transformEmpty() [], [] ); - $this->assertEquals( - [ - "data" => null, - ], - $result - ); + + $this->assertEquals([], $result); } private function createRelationship(): FakeRelationship diff --git a/tests/JsonApi/Transformer/AbstractResourceTransformerTest.php b/tests/JsonApi/Transformer/AbstractResourceTransformerTest.php index 4cc30cda..8f1cb232 100644 --- a/tests/JsonApi/Transformer/AbstractResourceTransformerTest.php +++ b/tests/JsonApi/Transformer/AbstractResourceTransformerTest.php @@ -298,8 +298,7 @@ public function transformToManyEmptyRelationship() $defaultRelationships = ["father"]; $relationships = [ "father" => function () { - $relationship = new ToManyRelationship(); - return $relationship; + return new ToManyRelationship(); } ]; @@ -308,9 +307,8 @@ public function transformToManyEmptyRelationship() $transformer = $this->createTransformer("user", "1", [], null, [], $defaultRelationships, $relationships); $transformation = new Transformation($request, $data, new DefaultExceptionFactory(), ""); $transformedResource = $transformer->transformRelationship("father", $transformation, []); - $this->assertEquals([ - "data" => [], - ], $transformedResource); + + $this->assertEquals([], $transformedResource); } /**