Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[HttpFoundation] Throw proper exception when invalid data is passed to JsonResponse class #9908

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

Merged
merged 1 commit into from
Dec 31, 2013
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/Symfony/Component/HttpFoundation/JsonResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,18 @@ public function setCallback($callback = null)
* @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();
}

Expand All @@ -116,4 +122,31 @@ protected function update()

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.';
}
}
}
21 changes: 15 additions & 6 deletions src/Symfony/Component/HttpFoundation/Tests/JsonResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,18 +159,27 @@ public function testSetCallback()
$this->assertEquals('text/javascript', $response->headers->get('Content-Type'));
}

public function testJsonEncodeFlags()
{
$response = new JsonResponse('<>\'&"');

$this->assertEquals('"\u003C\u003E\u0027\u0026\u0022"', $response->getContent());
}

/**
* @expectedException \InvalidArgumentException
*/
public function testSetCallbackInvalidIdentifier()
{
$response = new JsonResponse('foo');

$this->setExpectedException('InvalidArgumentException');
$response->setCallback('+invalid');
}

public function testJsonEncodeFlags()
/**
* @expectedException \InvalidArgumentException
*/
public function testSetContent()
{
$response = new JsonResponse('<>\'&"');

$this->assertEquals('"\u003C\u003E\u0027\u0026\u0022"', $response->getContent());
JsonResponse::create("\xB1\x31");
}
}