Skip to content

Commit

Permalink
Merge pull request #52 from tuyakhov/fix_empty_resources
Browse files Browse the repository at this point in the history
Allow empty response
  • Loading branch information
tuyakhov committed Jul 21, 2018
2 parents f763caf + d70a698 commit 75bce87
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 26 deletions.
54 changes: 30 additions & 24 deletions src/JsonApiResponseFormatter.php
Expand Up @@ -10,6 +10,7 @@
use yii\helpers\Json;
use yii\helpers\Url;
use yii\web\ErrorHandler;
use yii\web\Link;
use yii\web\Response;
use yii\web\ResponseFormatterInterface;

Expand Down Expand Up @@ -53,37 +54,42 @@ class JsonApiResponseFormatter extends Component implements ResponseFormatterInt
public function format($response)
{
$response->getHeaders()->set('Content-Type', 'application/vnd.api+json; charset=UTF-8');
$apiDocument = [
'data' => $response->data,
'links' => [
'self' => Url::current([], true)
]
];
$options = $this->encodeOptions;
if ($this->prettyPrint) {
$options |= JSON_PRETTY_PRINT;
}
if ($response->data !== null) {
$apiDocument = $response->data;
if ($response->isClientError || $response->isServerError) {
if (ArrayHelper::isAssociative($response->data)) {
$response->data = [$response->data];
}
$formattedErrors = [];
foreach ($response->data as $error) {
$formattedError = array_intersect_key($error, array_flip(static::ERROR_ALLOWED_MEMBERS));
foreach (static::ERROR_EXCEPTION_MAPPING as $member => $key) {
if (isset($error[$key])) {
$formattedError[$member] = (string) $error[$key];
}
}
if (!empty($formattedError)) {
$formattedErrors[] = $formattedError;

$apiDocument = $response->data;

if (!$response->isEmpty) {
$apiDocument = ['data' => $response->data];
if (\Yii::$app->controller) {
$apiDocument['links'] = Link::serialize([
Link::REL_SELF => Url::current([], true)
]);
}
}

if ($response->isClientError || $response->isServerError) {
if (ArrayHelper::isAssociative($response->data)) {
$response->data = [$response->data];
}
$formattedErrors = [];
foreach ($response->data as $error) {
$formattedError = array_intersect_key($error, array_flip(static::ERROR_ALLOWED_MEMBERS));
foreach (static::ERROR_EXCEPTION_MAPPING as $member => $key) {
if (isset($error[$key])) {
$formattedError[$member] = (string) $error[$key];
}
}
$apiDocument = ['errors' => $formattedErrors];
if (!empty($formattedError)) {
$formattedErrors[] = $formattedError;
}
}
$apiDocument = ['errors' => $formattedErrors];
}
if ($apiDocument !== null) {
$response->content = Json::encode($apiDocument, $options);
}
$response->content = Json::encode($apiDocument, $options);
}
}
29 changes: 27 additions & 2 deletions tests/JsonApiResponseFormatterTest.php
Expand Up @@ -17,7 +17,6 @@ class JsonApiResponseFormatterTest extends TestCase
{
public function testFormatException()
{
\Yii::$app->controller = new Controller('test', \Yii::$app);
$formatter = new JsonApiResponseFormatter();
$exception = new ServerErrorHttpException('Server error');
$response = new Response();
Expand All @@ -44,7 +43,6 @@ public function testFormatException()

public function testFormModelError()
{
\Yii::$app->controller = new Controller('test', \Yii::$app);
$formatter = new JsonApiResponseFormatter();
$exception = new ServerErrorHttpException('Server error');
$response = new Response();
Expand All @@ -71,4 +69,31 @@ public function testFormModelError()
]
]), $response->content);
}

public function testEmptyData()
{
$formatter = new JsonApiResponseFormatter();
$response = new Response();
$response->setStatusCode('200');
$serializer = new Serializer();
$response->data = $serializer->serialize(null);
$formatter->format($response);
$this->assertJson($response->content);
$this->assertSame(Json::encode([
'data' => null
]), $response->content);
\Yii::$app->controller = new Controller('test', \Yii::$app);
$formatter->format($response);
$this->assertJson($response->content);
$this->assertSame(Json::encode([
'data' => null,
'links' => [
'self' => ['href' => '/index.php?r=test']
]
]), $response->content);
$response->clear();
$response->setStatusCode(201);
$formatter->format($response);
$this->assertNull($response->content);
}
}

0 comments on commit 75bce87

Please sign in to comment.