Skip to content

Commit 3f82571

Browse files
authored
Merge pull request #28 from swisnl/bugfix/do-not-fill-type-in-unmapped-morph-relation
Do not fill type attribute in unmapped items in morph(to) relationships
2 parents e0cbd84 + 88f2af8 commit 3f82571

File tree

2 files changed

+61
-3
lines changed

2 files changed

+61
-3
lines changed

src/ItemHydrator.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,12 @@ protected function hydrateHasManyRelation(array $attributes, string $availableRe
152152
*/
153153
protected function hydrateMorphToRelation(array $attributes, MorphToRelation $relation, string $availableRelation)
154154
{
155-
if (!array_key_exists('type', $attributes[$availableRelation])) {
155+
$relationData = $attributes[$availableRelation];
156+
if (!array_key_exists('type', $relationData)) {
156157
throw new \InvalidArgumentException('Always provide a "type" attribute in a morphTo relationship');
157158
}
159+
$relationItem = $this->buildRelationItem($relation, array_diff_key($relationData, ['type' => 'type']), $relationData['type']);
158160

159-
$relationItem = $this->buildRelationItem($relation, $attributes[$availableRelation], $attributes[$availableRelation]['type']);
160161
$relation->associate($relationItem);
161162
}
162163

@@ -173,7 +174,7 @@ protected function hydrateMorphToManyRelation(array $attributes, MorphToManyRela
173174
if (!array_key_exists('type', $relationData)) {
174175
throw new \InvalidArgumentException('Always provide a "type" attribute in a morphToMany relationship entry');
175176
}
176-
$relationItem = $this->buildRelationItem($relation, $relationData, $relationData['type']);
177+
$relationItem = $this->buildRelationItem($relation, array_diff_key($relationData, ['type' => 'type']), $relationData['type']);
177178

178179
$relation->associate($relation->getIncluded()->push($relationItem));
179180
}

tests/ItemHydratorTest.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,13 +185,38 @@ public function it_hydrates_items_with_morphto_relationship()
185185
$this->assertEquals($data['testattribute1'], $item->getAttribute('testattribute1'));
186186
$this->assertEquals($data['testattribute2'], $item->getAttribute('testattribute2'));
187187
$this->assertEquals('related-item', $morphTo->getType());
188+
$this->assertEquals('related-item', $morphTo->getIncluded()->getType());
188189
$this->assertEquals(
189190
$data['morphto_relation']['test_related_attribute1'],
190191
$morphTo->getIncluded()->getAttribute('test_related_attribute1')
191192
);
192193
$this->assertArrayHasKey('morphto_relation', $item->toJsonApiArray()['relationships']);
193194
}
194195

196+
/**
197+
* @test
198+
*/
199+
public function it_hydrates_unmapped_items_with_morphto_relationship()
200+
{
201+
$data = [
202+
'morphto_relation' => [
203+
'id' => 1,
204+
'type' => 'unmapped-item',
205+
'test_related_attribute1' => 'test',
206+
],
207+
];
208+
209+
$item = new WithRelationshipItem();
210+
$item = $this->getItemHydrator()->hydrate($item, $data);
211+
212+
/** @var \Swis\JsonApi\Client\Relations\MorphToRelation $morphTo */
213+
$morphTo = $item->getRelationship('morphto_relation');
214+
215+
$this->assertEquals('unmapped-item', $morphTo->getType());
216+
$this->assertEquals('unmapped-item', $morphTo->getIncluded()->getType());
217+
$this->assertArrayNotHasKey('type', $morphTo->getIncluded()->getAttributes());
218+
}
219+
195220
/**
196221
* @test
197222
*/
@@ -265,6 +290,38 @@ public function it_hydrates_items_with_morphtomany_relationship()
265290
$this->assertArrayHasKey('morphtomany_relation', $item->toJsonApiArray()['relationships']);
266291
}
267292

293+
/**
294+
* @test
295+
*/
296+
public function it_hydrates_unmapped_items_with_morphtomany_relationship()
297+
{
298+
$data = [
299+
'morphtomany_relation' => [
300+
[
301+
'id' => 1,
302+
'type' => 'unmapped-item',
303+
'test_related_attribute1' => 'test1',
304+
],
305+
[
306+
'id' => 2,
307+
'type' => 'unmapped-item',
308+
'test_related_attribute1' => 'test2',
309+
],
310+
],
311+
];
312+
313+
$item = new WithRelationshipItem();
314+
$item = $this->getItemHydrator()->hydrate($item, $data);
315+
316+
/** @var \Swis\JsonApi\Client\Relations\MorphToManyRelation $morphToMany */
317+
$morphToMany = $item->getRelationship('morphtomany_relation');
318+
319+
$this->assertEquals('unmapped-item', $morphToMany->getIncluded()[0]->getType());
320+
$this->assertEquals('unmapped-item', $morphToMany->getIncluded()[1]->getType());
321+
$this->assertArrayNotHasKey('type', $morphToMany->getIncluded()[0]->getAttributes());
322+
$this->assertArrayNotHasKey('type', $morphToMany->getIncluded()[1]->getAttributes());
323+
}
324+
268325
/**
269326
* @test
270327
*/

0 commit comments

Comments
 (0)