Skip to content

Commit 652a179

Browse files
committed
[Bugfix] Fix passing empty to-one relations in resource object
1 parent c083d16 commit 652a179

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/Document/ResourceObject.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ public function withoutRelationships(): self
351351
public function getRelations(): Collection
352352
{
353353
return $this->getRelationships()->filter(function (array $relation) {
354-
return isset($relation['data']);
354+
return array_key_exists('data', $relation);
355355
})->map(function (array $relation) {
356356
return $relation['data'];
357357
});

tests/lib/Unit/Document/ResourceObjectTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,35 @@ public function testFields(): array
122122
return $expected;
123123
}
124124

125+
public function testFieldsWithEmptyToOne(): void
126+
{
127+
$this->values['relationships']['author']['data'] = null;
128+
129+
$expected = [
130+
'author' => null,
131+
'content' => '...',
132+
'id' => '1',
133+
'published' => null,
134+
'tags' => [
135+
[
136+
'type' => 'tags',
137+
'id' => '4',
138+
],
139+
[
140+
'type' => 'tags',
141+
'id' => '5',
142+
],
143+
],
144+
'title' => 'Hello World',
145+
'type' => 'posts',
146+
];
147+
148+
$resource = ResourceObject::create($this->values);
149+
$this->assertSame($expected, $resource->all());
150+
$this->assertNull($resource['author']);
151+
$this->assertNull($resource->get('author', true));
152+
}
153+
125154
/**
126155
* @param array $expected
127156
* @depends testFields

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy