Skip to content

Commit

Permalink
Do not add relations without data to JSON API array
Browse files Browse the repository at this point in the history
If the relation does not have any data, we should not serialize it as having empty data (i.e. null or empty array).
  • Loading branch information
JaZo committed Aug 5, 2020
1 parent c14fd7d commit 609c49a
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 11 deletions.
23 changes: 12 additions & 11 deletions src/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,26 +130,27 @@ public function getRelationships(): array
$relationships = [];

foreach ($this->getRelations() as $name => $relation) {
if (!$relation->hasIncluded()) {
continue;
}

if ($relation instanceof OneRelationInterface) {
$relationships[$name] = ['data' => null];
$relationships[$name]['data'] = null;

if ($relation->getIncluded() !== null) {
$relationships[$name] = [
'data' => [
'type' => $relation->getIncluded()->getType(),
'id' => $relation->getIncluded()->getId(),
],
$relationships[$name]['data'] = [
'type' => $relation->getIncluded()->getType(),
'id' => $relation->getIncluded()->getId(),
];
}
} elseif ($relation instanceof ManyRelationInterface) {
$relationships[$name]['data'] = [];

foreach ($relation->getIncluded() as $item) {
$relationships[$name]['data'][] =
[
'type' => $item->getType(),
'id' => $item->getId(),
];
$relationships[$name]['data'][] = [
'type' => $item->getType(),
'id' => $item->getId(),
];
}
}
}
Expand Down
72 changes: 72 additions & 0 deletions tests/ItemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,24 @@ public function is_adds_empty_hasone_relation_in_to_json_api_array()
);
}

/**
* @test
*/
public function is_does_not_add_hasone_relation_without_data_in_to_json_api_array()
{
$item = new WithRelationshipItem();
$item->setId('1234');
$item->hasoneRelation();

$this->assertSame(
[
'type' => 'item-with-relationship',
'id' => '1234',
],
$item->toJsonApiArray()
);
}

/**
* @test
*/
Expand Down Expand Up @@ -170,6 +188,24 @@ public function is_adds_empty_hasmany_relation_in_to_json_api_array()
);
}

/**
* @test
*/
public function is_does_not_add_hasmany_relation_without_data_in_to_json_api_array()
{
$item = new WithRelationshipItem();
$item->setId('1234');
$item->hasmanyRelation();

$this->assertSame(
[
'type' => 'item-with-relationship',
'id' => '1234',
],
$item->toJsonApiArray()
);
}

/**
* @test
*/
Expand Down Expand Up @@ -219,6 +255,24 @@ public function is_adds_empty_morphto_relation_in_to_json_api_array()
);
}

/**
* @test
*/
public function is_does_not_add_morphto_relation_without_data_in_to_json_api_array()
{
$item = new WithRelationshipItem();
$item->setId('1234');
$item->morphtoRelation();

$this->assertSame(
[
'type' => 'item-with-relationship',
'id' => '1234',
],
$item->toJsonApiArray()
);
}

/**
* @test
*/
Expand Down Expand Up @@ -270,6 +324,24 @@ public function is_adds_empty_morphtomany_relation_in_to_json_api_array()
);
}

/**
* @test
*/
public function is_does_not_add_morphtomany_relation_without_data_in_to_json_api_array()
{
$item = new WithRelationshipItem();
$item->setId('1234');
$item->morphtomanyRelation();

$this->assertSame(
[
'type' => 'item-with-relationship',
'id' => '1234',
],
$item->toJsonApiArray()
);
}

/**
* @test
*/
Expand Down

0 comments on commit 609c49a

Please sign in to comment.