8000 JsonResponse fix by kanariezwart · Pull Request #16689 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

JsonResponse fix #16689

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

Closed
wants to merge 3 commits into from
Closed
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
102 changes: 58 additions & 44 deletions src/Symfony/Component/HttpFoundation/JsonResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class JsonResponse extends Response
*/
public function __construct($data = null, $status = 200, $headers = array())
{
parent::__construct('', $status, $headers);
parent::__construct('{}', $status, $headers);

if (null === $data) {
$data = new \ArrayObject();
Expand Down Expand Up @@ -90,24 +90,60 @@ public function setCallback($callback = null)
* @param mixed $data
*
* @return JsonResponse
*
* @throws \InvalidArgumentException
*/
public function setData($data = array())
{
$this->data = $data;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are still breaking BC by storing the array/object/scalar instead of storing the JSON string.

And it might even break things in case your data is a mutable object, as your data could be mutated before you trigger another update


return $this->update();
}

/**
* Returns options used while encoding data to JSON.
*
* @return int
*/
public function getEncodingOptions()
{
return $this->encodingOptions;
}

/**
* Sets options used while encoding data to JSON.
*
* @param int $encodingOptions
*
* @return JsonResponse
*/
public function setEncodingOptions($encodingOptions)
{
$this->encodingOptions = (int) $encodingOptions;

return $this->update();
}

/**
* Updates the content and headers according to the JSON data and callback.
*
* @return JsonResponse
*
* @throws \Exception
*/
protected function update()
{
if (defined('HHVM_VERSION')) {
// HHVM does not trigger any warnings and let exceptions
// thrown from a JsonSerializable object pass through.
// If only PHP did the same...
$data = json_encode($data, $this->encodingOptions);
$serializedData = json_encode($this->data, $this->encodingOptions);
} else {
try {
if (PHP_VERSION_ID < 50400) {
// PHP 5.3 triggers annoying warnings for some
// types that can't be serialized as JSON (INF, resources, etc.)
// but doesn't provide the JsonSerializable interface.
set_error_handler('var_dump', 0);
$data = @json_encode($data, $this->encodingOptions);
$serializedData = @json_encode($this->data, $this->encodingOptions);
} else {
// PHP 5.4 and up wrap exceptions thrown by JsonSerializable
// objects in a new exception that needs to be removed.
Expand All @@ -121,10 +157,12 @@ public function setData($data = array())
if (JSON_ERROR_NONE === json_last_error()) {
return $errorHandler && false !== call_user_func_array($errorHandler, func_get_args());
}

return;
});
}

$data = json_encode($data, $this->encodingOptions);
$serializedData = json_encode($this->data, $this->encodingOptions);
}

if (PHP_VERSION_ID < 50500) {
Expand All @@ -145,47 +183,11 @@ public function setData($data = array())
throw new \InvalidArgumentException(json_last_error_msg());
}

$this->data = $data;

return $this->update();
}

/**
* Returns options used while encoding data to JSON.
*
* @return int
*/
public function getEncodingOptions()
{
return $this->encodingOptions;
}

/**
* Sets options used while encoding data to JSON.
*
* @param int $encodingOptions
*
* @return JsonResponse
*/
public function setEncodingOptions($encodingOptions)
{
$this->encodingOptions = (int) $encodingOptions;

return $this->setData(json_decode($this->data));
}

/**
* Updates the content and headers according to the JSON data and callback.
*
* @return JsonResponse
*/
protected function update()
{
if (null !== $this->callback) {
// Not using application/javascript for compatibility reasons with older browsers.
$this->headers->set('Content-Type', 'text/javascript');

return $this->setContent(sprintf('/**/%s(%s);', $this->callback, $this->data));
return $this->setContent(sprintf('/**/%s(%s);', $this->callback, $serializedData));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should call parent::setContent to avoid redoing the work of synchronizing things

}

// Only set the header when there is none or when it equals 'text/javascript' (from a previous update with callback)
Expand All @@ -194,6 +196,18 @@ protected function update()
$this->headers->set('Content-Type', 'application/json');
}

return $this->setContent($this->data);
return $this->setContent($serializedData);
}

/**
* @param mixed $content
*
* @return Response
*/
public function setContent($content)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

public methods must go before the protected ones

{
$this->data = json_decode($content);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is broken in case the content is not a JSON string (which is the case for JSONP cases in your code currently btw)


return parent::setContent($content);
}
}
16 changes: 16 additions & 0 deletions src/Symfony/Component/HttpFoundation/Tests/JsonResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,22 @@ public function testSetEncodingOptions()
$this->assertEquals('{"0":{"0":1,"1":2,"2":3}}', $response->getContent());
}

public function testSettingContentWithoutSettingData()
{
$response = new JsonResponse();
$this->assertEquals('{}', $response->getContent());
}

public function testContentAfterSettingDataAndEncodingOptions()
{
$response = new JsonResponse();
$response->setData(array(array(1, 2, 3)));
$response->setContent('{"different":{"key":"value"}}');
$response->setEncodingOptions(JSON_OBJECT_AS_ARRAY);

$this->assertEquals('{"different":{"key":"value"}}', $response->getContent());
}

/**
* @expectedException \InvalidArgumentException
*/
Expand Down
0