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

Fallback to JSON renderer if no valid format is given #15590

Merged
merged 3 commits into from Feb 19, 2020
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
7 changes: 6 additions & 1 deletion core/API/Request.php
Expand Up @@ -215,6 +215,8 @@ private function sanitizeRequest()
*/
public function process()
{
$shouldReloadAuth = false;

try {
++self::$nestedApiInvocationCount;

Expand All @@ -233,7 +235,6 @@ public function process()
$corsHandler->handle();

$tokenAuth = Common::getRequestVar('token_auth', '', 'string', $this->request);
$shouldReloadAuth = false;

// IP check is needed here as we cannot listen to API.Request.authenticate as it would then not return proper API format response.
// We can also not do it by listening to API.Request.dispatch as by then the user is already authenticated and we want to make sure
Expand Down Expand Up @@ -276,6 +277,10 @@ public function process()
'ignoreInScreenWriter' => true,
]);

if (empty($response)) {
$response = new ResponseBuilder('console', $this->request);
}

$toReturn = $response->getResponseException($e);
} finally {
--self::$nestedApiInvocationCount;
Expand Down
8 changes: 8 additions & 0 deletions tests/PHPUnit/System/ApiGetReportMetadataTest.php
Expand Up @@ -75,6 +75,14 @@ public function getApiForTesting()
array('Actions.getPageTitles', array('idSite' => $idSite, 'date' => $dateTime,
'testSuffix' => '_pageTitleZeroString')),

// Test w/ no format, should default to format=json
['Actions.getPageTitles', [
'idSite' => $idSite,
'date' => $dateTime,
'testSuffix' => '_defaultFormatValue',
'format' => 'asldjkf',
]],

// test php renderer w/ array data
array('API.getDefaultMetricTranslations', array('idSite' => $idSite, 'date' => $dateTime,
'format' => 'php', 'testSuffix' => '_phpRenderer')),
Expand Down
@@ -0,0 +1,3 @@
Error: Renderer format 'asldjkf' not valid. Try any of the following instead: console, csv, html, json, json2, original, php, rss, tsv, xml.

--> To temporarily debug this error further, set const PIWIK_PRINT_ERROR_BACKTRACE=true; in index.php