From 38287e76ed0a88b4fb3eb24584afec97749c53f6 Mon Sep 17 00:00:00 2001 From: Joseph Bielawski Date: Tue, 31 Dec 2013 00:31:44 +0100 Subject: [PATCH] [HttpFoundation] Throw proper exception when invalid data is passed to JsonResponse class --- .../Component/HttpFoundation/JsonResponse.php | 33 +++++++++++++++++++ .../HttpFoundation/Tests/JsonResponseTest.php | 25 +++++++++----- 2 files changed, 50 insertions(+), 8 deletions(-) diff --git a/src/Symfony/Component/HttpFoundation/JsonResponse.php b/src/Symfony/Component/HttpFoundation/JsonResponse.php index eafccaa844..4a96d082d5 100644 --- a/src/Symfony/Component/HttpFoundation/JsonResponse.php +++ b/src/Symfony/Component/HttpFoundation/JsonResponse.php @@ -85,12 +85,18 @@ class JsonResponse extends Response * @param mixed $data * * @return JsonResponse + * + * @throws \InvalidArgumentException */ public function setData($data = array()) { // Encode <, >, ', &, and " for RFC4627-compliant JSON, which may also be embedded into HTML. $this->data = json_encode($data, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT); + if (JSON_ERROR_NONE !== json_last_error()) { + throw new \InvalidArgumentException($this->transformJsonError()); + } + return $this->update(); } @@ -116,4 +122,31 @@ class JsonResponse extends Response return $this->setContent($this->data); } + + private function transformJsonError() + { + if (function_exists('json_last_error_msg')) { + return json_last_error_msg(); + } + + switch (json_last_error()) { + case JSON_ERROR_DEPTH: + return 'Maximum stack depth exceeded.'; + + case JSON_ERROR_STATE_MISMATCH: + return 'Underflow or the modes mismatch.'; + + case JSON_ERROR_CTRL_CHAR: + return 'Unexpected control character found.'; + + case JSON_ERROR_SYNTAX: + return 'Syntax error, malformed JSON.'; + + case JSON_ERROR_UTF8: + return 'Malformed UTF-8 characters, possibly incorrectly encoded.'; + + default: + return 'Unknown error.'; + } + } } diff --git a/src/Symfony/Component/HttpFoundation/Tests/JsonResponseTest.php b/src/Symfony/Component/HttpFoundation/Tests/JsonResponseTest.php index 8f1383fba9..ef392ca59d 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/JsonResponseTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/JsonResponseTest.php @@ -159,18 +159,27 @@ class JsonResponseTest extends \PHPUnit_Framework_TestCase $this->assertEquals('text/javascript', $response->headers->get('Content-Type')); } - public function testSetCallbackInvalidIdentifier() - { - $response = new JsonResponse('foo'); - - $this->setExpectedException('InvalidArgumentException'); - $response->setCallback('+invalid'); - } - public function testJsonEncodeFlags() { $response = new JsonResponse('<>\'&"'); $this->assertEquals('"\u003C\u003E\u0027\u0026\u0022"', $response->getContent()); } + + /** + * @expectedException \InvalidArgumentException + */ + public function testSetCallbackInvalidIdentifier() + { + $response = new JsonResponse('foo'); + $response->setCallback('+invalid'); + } + + /** + * @expectedException \InvalidArgumentException + */ + public function testSetContent() + { + JsonResponse::create("\xB1\x31"); + } }