Skip to content

Commit 4aa004b

Browse files
authored
Merge pull request #6 from undabot/v2.1.3
Fix relationships key to always be object
2 parents fe5f815 + 48fbc2d commit 4aa004b

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

src/Implementation/Encoding/ResourceToPhpArrayEncoder.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,15 @@ public function encode(ResourceInterface $resource): array
5959
}
6060

6161
if (null !== $resource->getRelationships()) {
62-
$serializedResource['relationships'] = $this->relationshipCollectionEncoder->encode($resource->getRelationships());
62+
$encodedRelationships = $this->relationshipCollectionEncoder->encode($resource->getRelationships());
63+
// relationships key must be object
64+
// if it's empty, we'll get empty array and it will be encoded to array
65+
// so in that case we'll create php object so json encode will return object
66+
if (true === empty($encodedRelationships)) {
67+
$encodedRelationships = new \stdClass();
68+
}
69+
70+
$serializedResource['relationships'] = $encodedRelationships;
6371
}
6472

6573
return $serializedResource;

0 commit comments

Comments
 (0)