From e8ae0b74e00846a755725f35f5927ec30fd5b966 Mon Sep 17 00:00:00 2001 From: Hugo Sales Date: Mon, 6 Sep 2021 14:29:07 +0100 Subject: [PATCH] [CORE][Controller] Switch order for content negotiation: allow events to take precedence. Bring back default JSON response --- src/Core/Controller.php | 33 +++++++++++++++++++-------------- src/Entity/Note.php | 2 +- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/src/Core/Controller.php b/src/Core/Controller.php index 43abf4b052..874b446d52 100644 --- a/src/Core/Controller.php +++ b/src/Core/Controller.php @@ -37,6 +37,7 @@ use App\Util\Exception\RedirectException; use Exception; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\EventDispatcher\EventSubscriberInterface; +use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RequestStack; @@ -112,22 +113,26 @@ class Controller extends AbstractController implements EventSubscriberInterface // Respond in the most preferred acceptable content type $accept = $request->getAcceptableContentTypes() ?: ['text/html']; $format = $request->getFormat($accept[0]); - switch ($format) { - case 'html': - $event->setResponse($this->render($template, $this->vars)); - break; - default: - $potential_response = null; - if (Event::handle('ControllerResponseInFormat', [ - 'route' => $request->get('_route'), - 'accept_header' => $accept, - 'vars' => $this->vars, - 'response' => &$potential_response, - ])) { + + $potential_response = null; + if (Event::handle('ControllerResponseInFormat', [ + 'route' => $request->get('_route'), + 'accept_header' => $accept, + 'vars' => $this->vars, + 'response' => &$potential_response, + ]) === Event::next) { + switch ($format) { + case 'html': + $event->setResponse($this->render($template, $this->vars)); + break; + case 'json': + $event->setResponse(new JsonResponse($response)); + break; + default: throw new ClientException(_m('Unsupported format: {format}', ['format' => $format]), 406); // 406 Not Acceptable - } else { - $event->setResponse($potential_response); } + } else { + $event->setResponse($potential_response); } Event::handle('CleanupModule'); diff --git a/src/Entity/Note.php b/src/Entity/Note.php index 08c982aa6f..5c5d45f920 100644 --- a/src/Entity/Note.php +++ b/src/Entity/Note.php @@ -38,7 +38,7 @@ use DateTimeInterface; * @copyright 2020-2021 Free Software Foundation, Inc http://www.fsf.org * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later */ -class Note extends Entity +class Note extends Entity implements \JsonSerializable { // {{{ Autocode // @codeCoverageIgnoreStart