Skip to content

Commit 12cdab4

Browse files
authored
Merge pull request #63 from swisnl/bugfix/issue-62-empty-singular-relationship
Fix parsing of empty singular relationships
2 parents cccdee1 + 631aa36 commit 12cdab4

File tree

3 files changed

+103
-2
lines changed

3 files changed

+103
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
66

77
## [Unreleased]
88

9-
* Nothing
9+
### Fixed
10+
11+
* Fixed parsing of empty singular relationships.
1012

1113
## [1.0.0-beta.2] - 2019-09-20
1214

src/Parsers/DocumentParser.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,13 @@ function (ItemInterface $item) {
178178
function (ItemInterface $item) use ($keyedItems) {
179179
foreach ($item->getRelations() as $name => $relation) {
180180
if ($relation instanceof OneRelationInterface) {
181-
/** @var \Swis\JsonApi\Client\Interfaces\ItemInterface $relatedItem */
181+
/** @var \Swis\JsonApi\Client\Interfaces\ItemInterface|null $relatedItem */
182182
$relatedItem = $relation->getIncluded();
183183

184+
if ($relatedItem === null) {
185+
continue;
186+
}
187+
184188
$includedItem = $this->getItem($keyedItems, $relatedItem);
185189
if ($includedItem !== null) {
186190
$relation->associate($includedItem);

tests/Parsers/DocumentParserTest.php

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,49 @@ public function it_links_singular_relations_to_items_from_included()
302302
$this->assertSame($document->getIncluded()->get(0), $document->getData()->child()->getIncluded());
303303
}
304304

305+
/**
306+
* @test
307+
*/
308+
public function it_does_not_link_empty_singular_relations()
309+
{
310+
$typeMapper = new TypeMapper();
311+
$typeMapper->setMapping('master', MasterItem::class);
312+
$typeMapper->setMapping('child', ChildItem::class);
313+
$parser = $this->getDocumentParser($typeMapper);
314+
315+
$document = $parser->parse(
316+
json_encode(
317+
[
318+
'data' => [
319+
'type' => 'master',
320+
'id' => '1',
321+
'attributes' => [
322+
'foo' => 'bar',
323+
],
324+
'relationships' => [
325+
'child' => [
326+
'data' => null,
327+
],
328+
],
329+
],
330+
'included' => [
331+
[
332+
'type' => 'child',
333+
'id' => '1',
334+
'attributes' => [
335+
'foo' => 'baz',
336+
],
337+
],
338+
],
339+
]
340+
)
341+
);
342+
343+
$this->assertInstanceOf(MasterItem::class, $document->getData());
344+
$this->assertNull($document->getData()->child()->getIncluded());
345+
$this->assertInstanceOf(ChildItem::class, $document->getIncluded()->get(0));
346+
}
347+
305348
/**
306349
* @test
307350
*/
@@ -363,6 +406,58 @@ public function it_links_plural_relations_to_items_from_included()
363406
$this->assertSame($document->getIncluded()->get(1), $document->getData()->children()->getIncluded()->get(1));
364407
}
365408

409+
/**
410+
* @test
411+
*/
412+
public function it_does_not_link_empty_plural_relations()
413+
{
414+
$typeMapper = new TypeMapper();
415+
$typeMapper->setMapping('master', MasterItem::class);
416+
$typeMapper->setMapping('child', ChildItem::class);
417+
$parser = $this->getDocumentParser($typeMapper);
418+
419+
$document = $parser->parse(
420+
json_encode(
421+
[
422+
'data' => [
423+
'type' => 'master',
424+
'id' => '1',
425+
'attributes' => [
426+
'foo' => 'bar',
427+
],
428+
'relationships' => [
429+
'children' => [
430+
'data' => [],
431+
],
432+
],
433+
],
434+
'included' => [
435+
[
436+
'type' => 'child',
437+
'id' => '1',
438+
'attributes' => [
439+
'foo' => 'baz',
440+
],
441+
],
442+
[
443+
'type' => 'child',
444+
'id' => '2',
445+
'attributes' => [
446+
'foo' => 'baz',
447+
],
448+
],
449+
],
450+
]
451+
)
452+
);
453+
454+
$this->assertInstanceOf(MasterItem::class, $document->getData());
455+
$this->assertInstanceOf(Collection::class, $document->getData()->children()->getIncluded());
456+
$this->assertEmpty($document->getData()->children()->getIncluded());
457+
$this->assertInstanceOf(ChildItem::class, $document->getIncluded()->get(0));
458+
$this->assertInstanceOf(ChildItem::class, $document->getIncluded()->get(1));
459+
}
460+
366461
/**
367462
* @test
368463
*/

0 commit comments

Comments
 (0)