Skip to content

[HttpFoundation] Fix TypeError: Argument 1 passed to JsonResponse::setJson() must be of the type string, object given #39271

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 7, 2020
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
12 changes: 8 additions & 4 deletions src/Symfony/Component/HttpFoundation/JsonResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public function __construct($data = null, int $status = 200, array $headers = []
{
parent::__construct('', $status, $headers);

if ($json && !\is_string($data) && !is_numeric($data) && !\is_callable([$data, '__toString'])) {
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)));
}

if (null === $data) {
$data = new \ArrayObject();
}
Expand Down Expand Up @@ -77,13 +81,13 @@ public static function create($data = null, $status = 200, $headers = [])
* return JsonResponse::fromJsonString('{"key": "value"}')
* ->setSharedMaxAge(300);
*
* @param string|null $data The JSON response string
* @param int $status The response status code
* @param array $headers An array of response headers
* @param string $data The JSON response string
* @param int $status The response status code
* @param array $headers An array of response headers
*
* @return static
*/
public static function fromJsonString($data = null, $status = 200, $headers = [])
public static function fromJsonString($data, $status = 200, $headers = [])
{
return new static($data, $status, $headers, true);
}
Expand Down
38 changes: 38 additions & 0 deletions src/Symfony/Component/HttpFoundation/Tests/JsonResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,44 @@ public function testSetComplexCallback()

$this->assertEquals('/**/ಠ_ಠ["foo"].bar[0]({"foo":"bar"});', $response->getContent());
}

public function testConstructorWithNullAsDataThrowsAnUnexpectedValueException()
{
$this->expectException(\TypeError::class);
$this->expectExceptionMessage('If $json is set to true, argument $data must be a string or object implementing __toString(), "null" given.');

new JsonResponse(null, 200, [], true);
}

public function testfromJsonStringConstructorWithNullAsDataThrowsAnUnexpectedValueException()
{
$this->expectException(\TypeError::class);
$this->expectExceptionMessage('If $json is set to true, argument $data must be a string or object implementing __toString(), "null" given.');

JsonResponse::fromJsonString(null);
}

public function testConstructorWithObjectWithToStringMethod()
{
$class = new class() {
public function __toString()
{
return '{}';
}
};

$response = new JsonResponse($class, 200, [], true);

$this->assertSame('{}', $response->getContent());
}

public function testConstructorWithObjectWithoutToStringMethodThrowsAnException()
{
$this->expectException(\TypeError::class);
$this->expectExceptionMessage('If $json is set to true, argument $data must be a string or object implementing __toString(), "stdClass" given.');

new JsonResponse(new \stdClass(), 200, [], true);
}
}

if (interface_exists('JsonSerializable', false)) {
Expand Down
3 changes: 2 additions & 1 deletion src/Symfony/Component/HttpFoundation/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"require": {
"php": ">=7.1.3",
"symfony/mime": "^4.3|^5.0",
"symfony/polyfill-mbstring": "~1.1"
"symfony/polyfill-mbstring": "~1.1",
"symfony/polyfill-php80": "^1.15"
},
"require-dev": {
"predis/predis": "~1.0",
Expand Down