diff options
author | Justin Velluppillai <justin@innocraft.com> | 2021-10-13 01:12:40 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-10-13 01:12:40 +0300 |
commit | f1cba6a5f168bdb3faf64e812b44d591d8afce08 (patch) | |
tree | 06018a3435f45ec6f1607d2cf54b7a835a2a4c97 /plugins/API | |
parent | 4632b2cf89b241789dea66a5e0593aa20a1453ad (diff) |
Remove root array correctly for jsonp API requests (#18136)
Diffstat (limited to 'plugins/API')
-rw-r--r-- | plugins/API/Renderer/Json.php | 18 | ||||
-rw-r--r-- | plugins/API/tests/Unit/JsonRendererTest.php | 20 |
2 files changed, 27 insertions, 11 deletions
diff --git a/plugins/API/Renderer/Json.php b/plugins/API/Renderer/Json.php index 5531c94537..3bd9fd2a69 100644 --- a/plugins/API/Renderer/Json.php +++ b/plugins/API/Renderer/Json.php @@ -59,20 +59,16 @@ class Json extends ApiRenderer $jsonRenderer = Renderer::factory('json'); $jsonRenderer->setTable($array); $result = $jsonRenderer->render(); - return $this->applyJsonpIfNeeded($result); - } - - $result = $this->renderDataTable($array); + } else { + $result = parent::renderDataTable($array); - // if $array is a simple associative array, remove the JSON root array that is added by renderDataTable - if (!empty($array) - && Piwik::isAssociativeArray($array) - && !Piwik::isMultiDimensionalArray($array) - ) { - $result = substr($result, 1, strlen($result) - 2); + // if $array is a simple associative array, remove the JSON root array that is added by renderDataTable + if (!empty($array) && Piwik::isAssociativeArray($array)) { + $result = substr($result, 1, strlen($result) - 2); + } } - return $result; + return $this->applyJsonpIfNeeded($result); } public function sendHeader() diff --git a/plugins/API/tests/Unit/JsonRendererTest.php b/plugins/API/tests/Unit/JsonRendererTest.php index 07981a5b07..2cbcde0a86 100644 --- a/plugins/API/tests/Unit/JsonRendererTest.php +++ b/plugins/API/tests/Unit/JsonRendererTest.php @@ -238,6 +238,26 @@ class JsonRendererTest extends \PHPUnit\Framework\TestCase $this->assertEquals('myName([1,2,5,"string",10])', $response); } + public function test_renderArray_withAssociativeArrayJsonpCorrectlyFormatted() + { + $input = array('key' => 'value'); + $renderer = $this->makeBuilder(array('callback' => '__myfunc', 'jsoncallback' => '__myfunc')); + $result = $renderer->renderArray($input); + + $this->assertEquals('__myfunc({"key":"value"})', $result); + $this->assertNoJsonError($result); + } + + public function test_renderArray_withMultidimensionalArrayJsonpCorrectlyFormatted() + { + $input = array('key' => 'value', 'deepKey' => array('deeper' => 'deepValue')); + $renderer = $this->makeBuilder(array('callback' => '__myfunc', 'jsoncallback' => '__myfunc')); + $result = $renderer->renderArray($input); + + $this->assertEquals('__myfunc({"key":"value","deepKey":{"deeper":"deepValue"}})', $result); + $this->assertNoJsonError($result); + } + public function test_renderArray_ShouldRenderAnEmptyArray() { $response = $this->jsonBuilder->renderArray(array()); |