Skip to content

Commit e251ad7

Browse files
committed
Do not add relations without data to JSON API array
If the relation does not have any data, we should not serialize it as having empty data (i.e. null or empty array).
1 parent 4f8d6b3 commit e251ad7

File tree

2 files changed

+84
-11
lines changed

2 files changed

+84
-11
lines changed

src/Item.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -130,26 +130,27 @@ public function getRelationships(): array
130130
$relationships = [];
131131

132132
foreach ($this->getRelations() as $name => $relation) {
133+
if (!$relation->hasIncluded()) {
134+
continue;
135+
}
136+
133137
if ($relation instanceof OneRelationInterface) {
134-
$relationships[$name] = ['data' => null];
138+
$relationships[$name]['data'] = null;
135139

136140
if ($relation->getIncluded() !== null) {
137-
$relationships[$name] = [
138-
'data' => [
139-
'type' => $relation->getIncluded()->getType(),
140-
'id' => $relation->getIncluded()->getId(),
141-
],
141+
$relationships[$name]['data'] = [
142+
'type' => $relation->getIncluded()->getType(),
143+
'id' => $relation->getIncluded()->getId(),
142144
];
143145
}
144146
} elseif ($relation instanceof ManyRelationInterface) {
145147
$relationships[$name]['data'] = [];
146148

147149
foreach ($relation->getIncluded() as $item) {
148-
$relationships[$name]['data'][] =
149-
[
150-
'type' => $item->getType(),
151-
'id' => $item->getId(),
152-
];
150+
$relationships[$name]['data'][] = [
151+
'type' => $item->getType(),
152+
'id' => $item->getId(),
153+
];
153154
}
154155
}
155156
}

tests/ItemTest.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,24 @@ public function is_adds_empty_hasone_relation_in_to_json_api_array()
119119
);
120120
}
121121

122+
/**
123+
* @test
124+
*/
125+
public function is_does_not_add_hasone_relation_without_data_in_to_json_api_array()
126+
{
127+
$item = new WithRelationshipItem();
128+
$item->setId('1234');
129+
$item->hasoneRelation();
130+
131+
$this->assertSame(
132+
[
133+
'type' => 'item-with-relationship',
134+
'id' => '1234',
135+
],
136+
$item->toJsonApiArray()
137+
);
138+
}
139+
122140
/**
123141
* @test
124142
*/
@@ -170,6 +188,24 @@ public function is_adds_empty_hasmany_relation_in_to_json_api_array()
170188
);
171189
}
172190

191+
/**
192+
* @test
193+
*/
194+
public function is_does_not_add_hasmany_relation_without_data_in_to_json_api_array()
195+
{
196+
$item = new WithRelationshipItem();
197+
$item->setId('1234');
198+
$item->hasmanyRelation();
199+
200+
$this->assertSame(
201+
[
202+
'type' => 'item-with-relationship',
203+
'id' => '1234',
204+
],
205+
$item->toJsonApiArray()
206+
);
207+
}
208+
173209
/**
174210
* @test
175211
*/
@@ -219,6 +255,24 @@ public function is_adds_empty_morphto_relation_in_to_json_api_array()
219255
);
220256
}
221257

258+
/**
259+
* @test
260+
*/
261+
public function is_does_not_add_morphto_relation_without_data_in_to_json_api_array()
262+
{
263+
$item = new WithRelationshipItem();
264+
$item->setId('1234');
265+
$item->morphtoRelation();
266+
267+
$this->assertSame(
268+
[
269+
'type' => 'item-with-relationship',
270+
'id' => '1234',
271+
],
272+
$item->toJsonApiArray()
273+
);
274+
}
275+
222276
/**
223277
* @test
224278
*/
@@ -270,6 +324,24 @@ public function is_adds_empty_morphtomany_relation_in_to_json_api_array()
270324
);
271325
}
272326

327+
/**
328+
* @test
329+
*/
330+
public function is_does_not_add_morphtomany_relation_without_data_in_to_json_api_array()
331+
{
332+
$item = new WithRelationshipItem();
333+
$item->setId('1234');
334+
$item->morphtomanyRelation();
335+
336+
$this->assertSame(
337+
[
338+
'type' => 'item-with-relationship',
339+
'id' => '1234',
340+
],
341+
$item->toJsonApiArray()
342+
);
343+
}
344+
273345
/**
274346
* @test
275347
*/

0 commit comments

Comments
 (0)