From 9da63bd46f6acdd6b51c6d361397300850309b2f Mon Sep 17 00:00:00 2001 From: Agrim Rai Date: Wed, 19 Nov 2025 11:20:38 +0530 Subject: [PATCH] Fix #1200: ensure v1 JSON data/tag errors return JSON instead of XML --- openml_OS/controllers/Api_new.php | 12 ++++++------ openml_OS/core/MY_Api_Model.php | 9 ++++++++- .../pages/api_new/v1/json/error-message.tpl.php | 12 ++++++++++++ 3 files changed, 26 insertions(+), 7 deletions(-) create mode 100644 openml_OS/views/pages/api_new/v1/json/error-message.tpl.php diff --git a/openml_OS/controllers/Api_new.php b/openml_OS/controllers/Api_new.php index 437603f3..c3518cc6 100644 --- a/openml_OS/controllers/Api_new.php +++ b/openml_OS/controllers/Api_new.php @@ -141,23 +141,23 @@ private function bootstrap($version) { // TODO: very important (for future versions of the API)! if ($this->database_connection_error) { - $this->Api_data->returnError(107, $this->version); + $this->Api_data->returnError(107, $this->version, 412, null, false, false, $outputFormat); return; } $request_type = strtolower($_SERVER['REQUEST_METHOD']); if ($this->authenticated == false && $request_type != 'get') { if ($this->provided_hash) { - $this->Api_data->returnError(103, $this->version); + $this->Api_data->returnError(103, $this->version, 412, null, false, false, $outputFormat); } else { - $this->Api_data->returnError(102, $this->version); + $this->Api_data->returnError(102, $this->version, 412, null, false, false, $outputFormat); } } else if ($this->authenticated == false && $this->provided_hash) { - $this->Api_data->returnError(103, $this->version); + $this->Api_data->returnError(103, $this->version, 412, null, false, false, $outputFormat); } else if ($this->user_has_writing_rights == false && $request_type != 'get') { - $this->Api_data->returnError(104, $this->version, $this->openmlGeneralErrorCode, 'API calls of the read-only user can only be of type GET. '); + $this->Api_data->returnError(104, $this->version, $this->openmlGeneralErrorCode, 'API calls of the read-only user can only be of type GET. ', false, false, $outputFormat); } else if (file_exists(APPPATH.'models/api/' . $this->version . '/Api_' . $type . '.php') == false && $type != 'xsd' && $type != 'xml_example' && $type != 'arff_example') { - $this->Api_data->returnError(100, $this->version); + $this->Api_data->returnError(100, $this->version, 412, null, false, false, $outputFormat); } else if($type == 'xsd') { $this->server_document('xsd', $segs[0] . '.xsd', 'v1', 'Content-type: text/xml; charset=utf-8'); } else if($type == 'xml_example') { diff --git a/openml_OS/core/MY_Api_Model.php b/openml_OS/core/MY_Api_Model.php index f7b4c45c..0dc45d02 100644 --- a/openml_OS/core/MY_Api_Model.php +++ b/openml_OS/core/MY_Api_Model.php @@ -95,14 +95,21 @@ function xmlToArray($xml, $options = array()) { ); } - public function returnError($code, $version, $httpErrorCode = 412, $additionalInfo = null, $emailLog = false, $supress_output = false) { + public function returnError($code, $version, $httpErrorCode = 412, $additionalInfo = null, $emailLog = false, $supress_output = false, $outputFormat = null) { $this->Log->api_error('error', $_SERVER['REMOTE_ADDR'], $code, $_SERVER['QUERY_STRING'], $this->load->apiErrors[$code] . (($additionalInfo == null)?'':$additionalInfo) ); $error['code'] = $code; $error['message'] = htmlentities( $this->load->apiErrors[$code] ); $error['additional'] = htmlentities( $additionalInfo ); if (!$supress_output) { http_response_code($httpErrorCode); + $originalFormat = $this->outputFormat; + if ($outputFormat !== null) { + $this->outputFormat = $outputFormat; + } $this->xmlContents('error-message', $version, $error); + if ($outputFormat !== null) { + $this->outputFormat = $originalFormat; + } } if ($emailLog && defined('EMAIL_API_LOG')) { $to = EMAIL_API_LOG; diff --git a/openml_OS/views/pages/api_new/v1/json/error-message.tpl.php b/openml_OS/views/pages/api_new/v1/json/error-message.tpl.php new file mode 100644 index 00000000..1d405a16 --- /dev/null +++ b/openml_OS/views/pages/api_new/v1/json/error-message.tpl.php @@ -0,0 +1,12 @@ + array( + 'code' => $code, + 'message' => $message + ) +); +if ($additional != null) { + $error_response['error']['additional_information'] = $additional; +} +echo json_encode($error_response); +?> \ No newline at end of file