Skip to content

Commit 9379ad5

Browse files
authored
Merge pull request #71 from swisnl/bugfix/item-hydrator-unset-relation-fix
Do not break out of loop when hydrating relations
2 parents 53f5d74 + b1bc23b commit 9379ad5

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/ItemHydrator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ protected function fillRelations(ItemInterface $item, array $attributes): void
8585
) {
8686
$relation->dissociate();
8787

88-
return;
88+
continue;
8989
}
9090

9191
// It is a valid relation

tests/ItemHydratorTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,33 @@ public function it_hydrates_nested_relationship_items()
523523
$this->assertEquals('item-with-relationship', $hasOneParent->getIncluded()->getType());
524524
}
525525

526+
/**
527+
* @test
528+
*/
529+
public function it_dissociates_and_hydrates_relationships_in_one_call()
530+
{
531+
$data = [
532+
'hasone_relation' => null,
533+
'morphto_relation' => [
534+
'id' => 1,
535+
'type' => 'related-item',
536+
],
537+
];
538+
539+
$item = new WithRelationshipItem();
540+
$item = $this->getItemHydrator()->hydrate($item, $data);
541+
542+
/** @var \Swis\JsonApi\Client\Relations\MorphToRelation $morphTo */
543+
$morphTo = $item->getRelation('morphto_relation');
544+
$this->assertInstanceOf(MorphToRelation::class, $morphTo);
545+
546+
$this->assertEquals($data['morphto_relation']['id'], $morphTo->getIncluded()->getId());
547+
$this->assertEquals($data['morphto_relation']['type'], $morphTo->getIncluded()->getType());
548+
$this->assertArrayNotHasKey('type', $morphTo->getIncluded()->getAttributes());
549+
550+
$this->assertArrayHasKey('morphto_relation', $item->toJsonApiArray()['relationships']);
551+
}
552+
526553
/**
527554
* @test
528555
* @dataProvider provideIdArguments

0 commit comments

Comments
 (0)