Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 18 additions & 21 deletions code/serializers/Basic/RESTfulAPI_BasicSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,29 +205,26 @@ protected function formatDataObject(DataObject $dataObject)
//get the DataList for this realtion's name
$dataList = $dataObject->{$relationName}();

//if there actually are objects in the relation
if ( $dataList->count() )
// check if this relation should be embedded
if ( $this->isEmbeddable($dataObject->ClassName, $relationName) )
{
// check if this relation should be embedded
if ( $this->isEmbeddable($dataObject->ClassName, $relationName) )
{
// get the relation's record(s) ready to embed
$embedData = $this->getEmbedData($dataObject, $relationName);
// embed the data if any
if ( $embedData !== null )
{
$serializedColumnName = $this->serializeColumnName( $relationName );
$formattedDataObjectMap[$serializedColumnName] = $embedData;
}
}
else{
// set column value to ID list
$idList = $dataList->map('ID', 'ID')->keys();

$serializedColumnName = $this->serializeColumnName( $relationName );
$formattedDataObjectMap[$serializedColumnName] = $idList;
}
// get the relation's record(s) ready to embed
$embedData = $this->getEmbedData($dataObject, $relationName);
// embed the data if any
if ( $embedData !== null )
{
$serializedColumnName = $this->serializeColumnName( $relationName );
$formattedDataObjectMap[$serializedColumnName] = $embedData;
}
}
else{
// set column value to ID list
$idList = $dataList->map('ID', 'ID')->keys();

$serializedColumnName = $this->serializeColumnName( $relationName );
$formattedDataObjectMap[$serializedColumnName] = $idList;
}

}

if ( $many_many_extraFields )
Expand Down
25 changes: 25 additions & 0 deletions tests/serializers/Basic/RESTfulAPI_BasicSerializer_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ class RESTfulAPI_BasicSerializer_Test extends RESTfulAPI_Tester
'ApiTest_Library'
);

function generateDBEntries()
{
parent::generateDBEntries();
// create an empty library to test empty relations
$london = ApiTest_Library::create(array(
'Name' => 'London'
));
$london->write();
}

protected function getSerializer()
{
$injector = new Injector();
Expand Down Expand Up @@ -89,6 +99,14 @@ public function testSerialize()
is_array($jsonArray),
"Basic Serialize dataObject should return an object"
);

// test empty relation serialization
$dataObject = ApiTest_Library::get()->filter(array('Name' => 'London'))->first();
$result = json_decode($serializer->serialize($dataObject));
$this->assertTrue(
isset($result->Books) && is_array($result->Books),
"Basic Serializer should return an empty array if there are no records present"
);
}


Expand Down Expand Up @@ -125,6 +143,13 @@ public function testEmbeddedRecords()
is_numeric($result->Books[0]->ID),
"Basic Serialize should return a full record for embedded records"
);

$dataObject = ApiTest_Library::get()->filter(array('Name' => 'London'))->first();
$result = json_decode($serializer->serialize($dataObject));
$this->assertTrue(
isset($result->Books) && is_array($result->Books),
"Basic Serializer should return an empty array if there are no records present"
);
}


Expand Down