Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make JSON2 the now JSON formatting to fix an issue in formatting associative array #10928

Merged
merged 5 commits into from Dec 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -11,6 +11,7 @@ The Product Changelog at **[piwik.org/changelog](http://piwik.org/changelog)** l
Read more about migrating a plugin from Piwik 2.X to Piwik 3 in [our Migration guide](http://developer.piwik.org/guides/migrate-piwik-2-to-3).

### Breaking Changes
* Reporting API: if you call the Reporting API requesting data in `json` format then you may be affected. The `json` formatting has changed, a bug was fixed so that API methods that return simple associative arrays like `array('name' => 'value', 'name2' => 'value2')` will now appear correctly as `{"name":"value","name2":"value2"}` in JSON API output instead of `[{"name":"value","name2":"value2"}]` (as it used to be in Piwik 2). API methods like **SitesManager.getSiteFromId** & **UsersManager.getUser** and others are affected.
* The menu classes `Piwik\Menu\MenuReporting` and `Piwik\Menu\MenuMain` have been removed
* The class `Piwik\Plugin\Widgets` has been removed and replaced by `Piwik\Widget\Widget`. For each widget one class is needed from now on. You can generate a widget via `./console generate:widget`.
* The class `Piwik\WidgetList` class has been moved to `Piwik\Widget\WidgetsList`.
Expand Down
2 changes: 1 addition & 1 deletion misc/log-analytics
15 changes: 11 additions & 4 deletions plugins/API/Renderer/Json.php
Expand Up @@ -17,9 +17,6 @@
/**
* API output renderer for JSON.
*
* **NOTE: This is the old JSON format. It includes bugs that are fixed in the JSON2 API output
* format. Please use that format instead of this.**
*
* @deprecated
*/
class Json extends ApiRenderer
Expand Down Expand Up @@ -61,7 +58,17 @@ public function renderArray($array)
return $this->applyJsonpIfNeeded($result);
}

return $this->renderDataTable($array);
$result = $this->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);
}

return $result;
}

public function sendHeader()
Expand Down
17 changes: 1 addition & 16 deletions plugins/API/Renderer/Json2.php
Expand Up @@ -11,23 +11,8 @@
use Piwik\Piwik;

/**
* Correct API output renderer for JSON. Includes bug fixes for bugs in the old JSON API
* format.
* Left here for Backward compatibility in Piwik 3.X+ for all users who correctly used format=json2 during Piwik 2.X
*/
class Json2 extends Json
{
public function renderArray($array)
{
$result = parent::renderArray($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);
}

return $result;
}
}
9 changes: 3 additions & 6 deletions plugins/API/tests/Unit/JsonRendererTest.php
Expand Up @@ -356,17 +356,14 @@ public function test_renderArray_ShouldConvertMultiDimensionalMixedArrayToJson()
$this->assertNoJsonError($actual);
}

/**
* backwards compatibility test
*/
public function test_oldJson_renderArray_ShouldConvertSingleDimensionalAssociativeArray()
public function test_json_renderArray_ShouldConvertSingleDimensionalAssociativeArray()
{
$input = array(
"firstElement" => "isFirst",
"secondElement" => "isSecond"
);

$expected = '[{"firstElement":"isFirst","secondElement":"isSecond"}]';
$expected = '{"firstElement":"isFirst","secondElement":"isSecond"}';

$oldJsonBuilder = new Json($input);
$actual = $oldJsonBuilder->renderArray($input);
Expand All @@ -376,7 +373,7 @@ public function test_oldJson_renderArray_ShouldConvertSingleDimensionalAssociati

private function makeBuilder($request)
{
return new Json2($request);
return new Json($request);
}

private function assertNoJsonError($response)
Expand Down
2 changes: 1 addition & 1 deletion plugins/CoreAdminHome/Controller.php
Expand Up @@ -114,7 +114,7 @@ public function setMailSettings()
return '';
}

$response = new ResponseBuilder('json2');
$response = new ResponseBuilder('json');
try {
$this->checkTokenInUrl();

Expand Down
2 changes: 1 addition & 1 deletion plugins/CoreHome/angularjs/common/services/piwik-api.js
Expand Up @@ -249,7 +249,7 @@ var hasBlockedContent = false;
getParams.module = getParams.module || 'API';

if (!getParams.format) {
getParams.format = 'JSON2';
getParams.format = 'JSON';
}

addParams(getParams);
Expand Down
10 changes: 5 additions & 5 deletions plugins/CoreHome/angularjs/common/services/piwik-api.spec.js
Expand Up @@ -55,7 +55,7 @@
piwikApi.fetch({
method: "SomePlugin.action"
}).then(function (response) {
expect(response).to.equal("Request url: index.php?date=&format=JSON2&idSite=1&method=SomePlugin.action&module=API&period=day");
expect(response).to.equal("Request url: index.php?date=&format=JSON&idSite=1&method=SomePlugin.action&module=API&period=day");

done();
}).catch(function (ex) {
Expand Down Expand Up @@ -115,7 +115,7 @@
piwikApi.fetch({
method: "SomePlugin.action"
}).then(function (response) {
expect(response).to.equal("Request url: index.php?date=&format=JSON2&idSite=1&method=SomePlugin.action&module=API&period=day");
expect(response).to.equal("Request url: index.php?date=&format=JSON&idSite=1&method=SomePlugin.action&module=API&period=day");

request1Done = true;

Expand All @@ -127,7 +127,7 @@
piwikApi.fetch({
method: "SomeOtherPlugin.action"
}).then(function (response) {
expect(response).to.equal("Request url: index.php?date=&format=JSON2&idSite=1&method=SomeOtherPlugin.action&module=API&period=day");
expect(response).to.equal("Request url: index.php?date=&format=JSON&idSite=1&method=SomeOtherPlugin.action&module=API&period=day");

request2Done = true;

Expand Down Expand Up @@ -160,7 +160,7 @@
piwikApi.fetch({
method: "SomeOtherPlugin.action"
}).then(function (response) {
expect(response).to.equal("Request url: index.php?date=&format=JSON2&idSite=1&method=SomeOtherPlugin.action&module=API&period=day");
expect(response).to.equal("Request url: index.php?date=&format=JSON&idSite=1&method=SomeOtherPlugin.action&module=API&period=day");

request2Done = true;

Expand Down Expand Up @@ -216,7 +216,7 @@
method: "SomeOtherPlugin.action"
}
]).then(function (response) {
var restOfExpected = "index.php?date=&format=JSON2&idSite=1&method=API.getBulkRequest&" +
var restOfExpected = "index.php?date=&format=JSON&idSite=1&method=API.getBulkRequest&" +
"module=API&period=day - urls%5B%5D=%3Fmethod%3DSomePlugin.action%26param%3D" +
"value&urls%5B%5D=%3Fmethod%3DSomeOtherPlugin.action&token_auth=100bf5eeeed1468f3f9d93750044d3dd";

Expand Down