8000 [Bugfix] Fix passing empty to-one relations in resource object · sablesoft/laravel-json-api@652a179 · GitHub
[go: up one dir, main page]

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+
publi EB0B c 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)
0