@@ -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