Skip to content

Commit 1330662

Browse files
committed
bug symfony#18006 [HttpFoundation] Fix BC break introduced by symfony#17642 (dunglas)
This PR was squashed before being merged into the 3.1-dev branch (closes symfony#18006). Discussion ---------- [HttpFoundation] Fix BC break introduced by symfony#17642 | Q | A | ------------- | --- | Branch | master | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | symfony#17642 | License | MIT | Doc PR | n/a @taylorotwell can you confirm it fixes the issue with Laravel? Commits ------- 73ef995 [HttpFoundation] Fix BC break introduced by symfony#17642
2 parents f5cf886 + 73ef995 commit 1330662

File tree

2 files changed

+44
-34
lines changed

2 files changed

+44
-34
lines changed

src/Symfony/Component/HttpFoundation/JsonResponse.php

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,20 @@ class JsonResponse extends Response
3434
protected $encodingOptions = self::DEFAULT_ENCODING_OPTIONS;
3535

3636
/**
37-
* Constructor.
38-
*
39-
* @param mixed $data The response data
40-
* @param int $status The response status code
41-
* @param array $headers An array of response headers
42-
* @param bool $preEncoded If the data is already a JSON string
37+
* @param mixed $data The response data
38+
* @param int $status The response status code
39+
* @param array $headers An array of response headers
40+
* @param bool $json If the data is already a JSON string
4341
*/
44-
public function __construct($data = null, $status = 200, $headers = array(), $preEncoded = false)
42+
public function __construct($data = null, $status = 200, $headers = array(), $json = false)
4543
{
4644
parent::__construct('', $status, $headers);
4745

4846
if (null === $data) {
4947
$data = new \ArrayObject();
5048
}
5149

52-
$this->setData($data, $preEncoded);
50+
$json ? $this->setJson($data) : $this->setData($data);
5351
}
5452

5553
/**
@@ -87,46 +85,57 @@ public function setCallback($callback = null)
8785
return $this->update();
8886
}
8987

88+
/**
89+
* Sets a raw string containing a JSON document to be sent.
90+
*
91+
* @param string $data
92+
*
93+
* @return JsonResponse
94+
*
95+
* @throws \InvalidArgumentException
96+
*/
97+
public function setJson($json)
98+
{
99+
$this->data = $json;
100+
101+
return $this->update();
102+
}
103+
90104
/**
91105
* Sets the data to be sent as JSON.
92106
*
93107
* @param mixed $data
94-
* @param bool $preEncoded If the data is already a JSON string
95108
*
96109
* @return JsonResponse
97110
*
98111
* @throws \InvalidArgumentException
99112
*/
100-
public function setData($data = array(), $preEncoded = false)
113+
public function setData($data = array())
101114
{
102-
if (!$preEncoded) {
103-
if (defined('HHVM_VERSION')) {
104-
// HHVM does not trigger any warnings and let exceptions
105-
// thrown from a JsonSerializable object pass through.
106-
// If only PHP did the same...
115+
if (defined('HHVM_VERSION')) {
116+
// HHVM does not trigger any warnings and let exceptions
117+
// thrown from a JsonSerializable object pass through.
118+
// If only PHP did the same...
119+
$data = json_encode($data, $this->encodingOptions);
120+
} else {
121+
try {
122+
// PHP 5.4 and up wrap exceptions thrown by JsonSerializable
123+
// objects in a new exception that needs to be removed.
124+
// Fortunately, PHP 5.5 and up do not trigger any warning anymore.
107125
$data = json_encode($data, $this->encodingOptions);
108-
} else {
109-
try {
110-
// PHP 5.4 and up wrap exceptions thrown by JsonSerializable
111-
// objects in a new exception that needs to be removed.
112-
// Fortunately, PHP 5.5 and up do not trigger any warning anymore.
113-
$data = json_encode($data, $this->encodingOptions);
114-
} catch (\Exception $e) {
115-
if ('Exception' === get_class($e) && 0 === strpos($e->getMessage(), 'Failed calling ')) {
116-
throw $e->getPrevious() ?: $e;
117-
}
118-
throw $e;
126+
} catch (\Exception $e) {
127+
if ('Exception' === get_class($e) && 0 === strpos($e->getMessage(), 'Failed calling ')) {
128+
throw $e->getPrevious() ?: $e;
119129
}
120-
}
121-
122-
if (JSON_ERROR_NONE !== json_last_error()) {
123-
throw new \InvalidArgumentException(json_last_error_msg());
130+
throw $e;
124131
}
125132
}
126133

127-
$this->data = $data;
134+
if (JSON_ERROR_NONE !== json_last_error()) {
135+
throw new \InvalidArgumentException(json_last_error_msg());
136+
}
128137

129-
return $this->update();
138+
return $this->setJson($data);
130139
}
131140

132141
/**

src/Symfony/Component/HttpFoundation/Tests/JsonResponseTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,16 @@ public function testConstructorWithCustomContentType()
7575
$this->assertSame('application/vnd.acme.blog-v1+json', $response->headers->get('Content-Type'));
7676
}
7777

78-
public function testConstructorWithPreEncoded()
78+
public function testSetJson()
7979
{
8080
$response = new JsonResponse('1', 200, array(), true);
8181
$this->assertEquals('1', $response->getContent());
8282

8383
$response = new JsonResponse('[1]', 200, array(), true);
8484
$this->assertEquals('[1]', $response->getContent());
8585

86-
$response = new JsonResponse('true', 200, array(), true);
86+
$response = new JsonResponse(null, 200, array());
87+
$response->setJson('true');
8788
$this->assertEquals('true', $response->getContent());
8889
}
8990

0 commit comments

Comments
 (0)