-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
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
JsonResponse fix #16689
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(); | ||
|
@@ -90,24 +90,60 @@ public function setCallback($callback = null) | |
* @param mixed $data | ||
* | ||
* @return JsonResponse | ||
* | ||
* @throws \InvalidArgumentException | ||
*/ | ||
public function setData($data = array()) | ||
{ | ||
$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->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. | ||
|
@@ -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) { | ||
|
@@ -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)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should call |
||
} | ||
|
||
// Only set the header when there is none or when it equals 'text/javascript' (from a previous update with callback) | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
} |
There was a problem hiding this comment.
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