MVC: Routing - remove setJsonContent and make sure Response->send() handles array types properly. closes https://github.com/opnsense/core/issues/7757

Since we already handle stream output in send() as well, it makes sense to push the render decision to that spot in full.
This commit is contained in:
Ad Schellevis 2024-08-11 15:21:52 +02:00
parent 3e99b87cdf
commit f3669a58d7
2 changed files with 8 additions and 4 deletions

View File

@ -268,7 +268,7 @@ class ApiControllerBase extends ControllerRoot
// not authenticated
$this->response->setStatusCode(403, "Forbidden");
$this->response->setContentType('application/json', 'UTF-8');
$this->response->setJsonContent(['status' => 403,'message' => 'Forbidden']);
$this->response->setContent(['status' => 403,'message' => 'Forbidden']);
$this->response->send();
return false;
} else {
@ -279,7 +279,7 @@ class ApiControllerBase extends ControllerRoot
if ($dispatchError != null) {
$this->response->setStatusCode(400, "Bad Request");
$this->response->setContentType('application/json', 'UTF-8');
$this->response->setJsonContent(['status' => 400, 'message' => $dispatchError]);
$this->response->setContent(['status' => 400, 'message' => $dispatchError]);
$this->response->send();
return false;
}
@ -301,7 +301,7 @@ class ApiControllerBase extends ControllerRoot
// not authenticated
$this->response->setStatusCode(401, "Unauthorized");
$this->response->setContentType('application/json', 'UTF-8');
$this->response->setJsonContent(['status' => 401, 'message' => 'Authentication Failed']);
$this->response->setContent(['status' => 401, 'message' => 'Authentication Failed']);
$this->response->send();
return false;
} else {

View File

@ -107,7 +107,11 @@ class Response
fpassthru($this->content);
@fclose($this->content);
} elseif (!empty($this->content)) {
echo $this->content;
if (is_array($this->content)) {
echo json_encode($this->content);
} else {
echo $this->content;
}
}
$this->sent = true;