Skip to content

Commit f92cec8

Browse files
committed
bug #39271 [HttpFoundation] Fix TypeError: Argument 1 passed to JsonResponse::setJson() must be of the type string, object given (sidz)
This PR was squashed before being merged into the 4.4 branch. Discussion ---------- [HttpFoundation] Fix TypeError: Argument 1 passed to JsonResponse::setJson() must be of the type string, object given | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | | License | MIT | Doc PR | This PR fixes the type error when user set `null` as data in the `new JsonResponse(null, 200, [], true)` and true in the fourth argument to mark that value is json. ``` TypeError: Argument 1 passed to Symfony\Component\HttpFoundation\JsonResponse::setJson() must be of the type string, object given, called in /home/projects/symfony/src/Symfony/Component/HttpFoundation/JsonResponse.php on line 52 ``` Commits ------- 7fcba9611d [HttpFoundation] Fix TypeError: Argument 1 passed to JsonResponse::setJson() must be of the type string, object given
2 parents d852399 + 92a50b0 commit f92cec8

File tree

3 files changed

+48
-5
lines changed

3 files changed

+48
-5
lines changed

JsonResponse.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ public function __construct($data = null, int $status = 200, array $headers = []
4343
{
4444
parent::__construct('', $status, $headers);
4545

46+
if ($json && !\is_string($data) && !is_numeric($data) && !\is_callable([$data, '__toString'])) {
47+
throw new \TypeError(sprintf('"%s": If $json is set to true, argument $data must be a string or object implementing __toString(), "%s" given.', __METHOD__, get_debug_type($data)));
48+
}
49+
4650
if (null === $data) {
4751
$data = new \ArrayObject();
4852
}
@@ -77,13 +81,13 @@ public static function create($data = null, $status = 200, $headers = [])
7781
* return JsonResponse::fromJsonString('{"key": "value"}')
7882
* ->setSharedMaxAge(300);
7983
*
80-
* @param string|null $data The JSON response string
81-
* @param int $status The response status code
82-
* @param array $headers An array of response headers
84+
* @param string $data The JSON response string
85+
* @param int $status The response status code
86+
* @param array $headers An array of response headers
8387
*
8488
* @return static
8589
*/
86-
public static function fromJsonString($data = null, $status = 200, $headers = [])
90+
public static function fromJsonString($data, $status = 200, $headers = [])
8791
{
8892
return new static($data, $status, $headers, true);
8993
}

Tests/JsonResponseTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,44 @@ public function testSetComplexCallback()
240240

241241
$this->assertEquals('/**/ಠ_ಠ["foo"].bar[0]({"foo":"bar"});', $response->getContent());
242242
}
243+
244+
public function testConstructorWithNullAsDataThrowsAnUnexpectedValueException()
245+
{
246+
$this->expectException(\TypeError::class);
247+
$this->expectExceptionMessage('If $json is set to true, argument $data must be a string or object implementing __toString(), "null" given.');
248+
249+
new JsonResponse(null, 200, [], true);
250+
}
251+
252+
public function testfromJsonStringConstructorWithNullAsDataThrowsAnUnexpectedValueException()
253+
{
254+
$this->expectException(\TypeError::class);
255+
$this->expectExceptionMessage('If $json is set to true, argument $data must be a string or object implementing __toString(), "null" given.');
256+
257+
JsonResponse::fromJsonString(null);
258+
}
259+
260+
public function testConstructorWithObjectWithToStringMethod()
261+
{
262+
$class = new class() {
263+
public function __toString()
264+
{
265+
return '{}';
266+
}
267+
};
268+
269+
$response = new JsonResponse($class, 200, [], true);
270+
271+
$this->assertSame('{}', $response->getContent());
272+
}
273+
274+
public function testConstructorWithObjectWithoutToStringMethodThrowsAnException()
275+
{
276+
$this->expectException(\TypeError::class);
277+
$this->expectExceptionMessage('If $json is set to true, argument $data must be a string or object implementing __toString(), "stdClass" given.');
278+
279+
new JsonResponse(new \stdClass(), 200, [], true);
280+
}
243281
}
244282

245283
if (interface_exists('JsonSerializable', false)) {

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
"require": {
1919
"php": ">=7.1.3",
2020
"symfony/mime": "^4.3|^5.0",
21-
"symfony/polyfill-mbstring": "~1.1"
21+
"symfony/polyfill-mbstring": "~1.1",
22+
"symfony/polyfill-php80": "^1.15"
2223
},
2324
"require-dev": {
2425
"predis/predis": "~1.0",

0 commit comments

Comments
 (0)