From 9bdf0973b337cbf73287422cc5fdceabc8703e61 Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Tue, 4 Jul 2017 11:31:38 +0200 Subject: API: Fix return type of empty values in file/history If the array is not casted to a object, json_encode will encode it as [] if empty, but {} if it contains data. Always return an object by casting the array to an object if it is empty. Fixes #15 Signed-off-by: Florian Pritz --- application/controllers/api/v2/file.php | 7 +++++++ application/test/Test.php | 6 +++++- application/test/tests/api_v2/common.php | 4 ++-- application/test/tests/api_v2/test_history.php | 10 ++++++++++ doc/api.md | 1 + doc/api/file.md | 5 +++-- 6 files changed, 28 insertions(+), 5 deletions(-) diff --git a/application/controllers/api/v2/file.php b/application/controllers/api/v2/file.php index 6f95d5525..5dedcd508 100644 --- a/application/controllers/api/v2/file.php +++ b/application/controllers/api/v2/file.php @@ -65,6 +65,13 @@ class file extends \controllers\api\api_controller { unset($history['multipaste_items'][$key]['items'][$inner_key]['sort_order']); } } + + foreach (array("items", "multipaste_items") as $key) { + if (empty($history[$key])) { + $history[$key] = (object) array(); + } + } + return $history; } diff --git a/application/test/Test.php b/application/test/Test.php index 33278436b..b8052fbba 100644 --- a/application/test/Test.php +++ b/application/test/Test.php @@ -90,10 +90,14 @@ abstract class Test { // Method: POST, PUT, GET etc // Data: array("param" => "value") ==> index.php?param=value // Source: http://stackoverflow.com/a/9802854/953022 - protected function CallAPI($method, $url, $data = false) + protected function CallAPI($method, $url, $data = false, $return_json = false) { $result = $this->SendHTTPRequest($method, $url, $data); + if ($return_json) { + return $result; + } + $json = json_decode($result, true); if ($json === NULL) { $this->t->fail("json decode"); diff --git a/application/test/tests/api_v2/common.php b/application/test/tests/api_v2/common.php index dbef3cc9c..103e156a8 100644 --- a/application/test/tests/api_v2/common.php +++ b/application/test/tests/api_v2/common.php @@ -53,8 +53,8 @@ class common extends \test\Test { return $this->createApikey($userid, $access_level); } - protected function callEndpoint($verb, $endpoint, $data) + protected function callEndpoint($verb, $endpoint, $data, $return_json = false) { - return $this->CallAPI($verb, "$this->server_url/api/v2.0.0/$endpoint", $data); + return $this->CallAPI($verb, "$this->server_url/api/v2.0.0/$endpoint", $data, $return_json); } } diff --git a/application/test/tests/api_v2/test_history.php b/application/test/tests/api_v2/test_history.php index 3a168cafa..f09aab9bb 100644 --- a/application/test/tests/api_v2/test_history.php +++ b/application/test/tests/api_v2/test_history.php @@ -31,6 +31,16 @@ class test_history extends common { $this->t->is($ret["data"]["total_size"], "0", "total_size = 0 since no uploads"); } + public function test_history_empty_json_structure() + { + $apikey = $this->createUserAndApikey(); + $ret = $this->CallEndpoint("POST", "file/history", array( + "apikey" => $apikey, + ), true); + + $this->t->is($ret, '{"status":"success","data":{"items":{},"multipaste_items":{},"total_size":"0"}}', "empty lists should be json objects, not arrays"); + } + public function test_history_notEmptyAfterUploadSameMD5() { $apikey = $this->createUserAndApikey(); diff --git a/doc/api.md b/doc/api.md index 50b45ca6f..cbdc5bbb9 100644 --- a/doc/api.md +++ b/doc/api.md @@ -147,6 +147,7 @@ These are the most common errors that can be returned by any API call. | Version | Endpoint | Note | | ------- | -------- | ---- | +| 2.1.1 | file/history | Empty objects (values of `items` and `multipaste_items`) are now always returned as {}. Before they were returned as [] | | 2.1.0 | file/history | Add ''item.thumbnail'' | | 2.0.0 | file/history | Add ''multipaste_item.date'' | | 2.0.0 | file/history | Remove private fields in response | diff --git a/doc/api/file.md b/doc/api/file.md index 74120450b..c461caf01 100644 --- a/doc/api/file.md +++ b/doc/api/file.md @@ -125,8 +125,8 @@ multipaste_item = { // Success response responseSuccess.data = { - "items": [item.id: item, ...], - "multipaste_items": [multipaste_item.url_id: multipaste_item, ...], + "items": {item.id: item, ...}, + "multipaste_items": {multipaste_item.url_id: multipaste_item, ...}, "total_size": int, // total size of all files (excluding duplicates) } ``` @@ -187,6 +187,7 @@ Example: | ------- | ------ | | 2.0.0 | Add ''multipaste_item.date''. Remove ''multipaste_item.{multipaste_id,user_id}''. | | 2.1.0 | Add ''item.thumbnail'' | +| 2.1.1 | Empty objects (values of `items` and `multipaste_items`) are now always returned as {}. Before they were returned as [] | ## file/delete -- cgit v1.2.3-24-g4f1b