Skip to content

Commit 778d6bc

Browse files
committed
Merge branch '5.1' into 5.2
* 5.1: [HttpFoundation] Removed obsolete test. [HttpFoundation] Fix TypeError: Argument 1 passed to JsonResponse::setJson() must be of the type string, object given [Security][Validator] Add missing Portuguese translations Add Croatian (hr) translations
2 parents e8e1083 + fc088b8 commit 778d6bc

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
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
}
@@ -81,13 +85,13 @@ public static function create($data = null, int $status = 200, array $headers =
8185
* return JsonResponse::fromJsonString('{"key": "value"}')
8286
* ->setSharedMaxAge(300);
8387
*
84-
* @param string|null $data The JSON response string
85-
* @param int $status The response status code
86-
* @param array $headers An array of response headers
88+
* @param string $data The JSON response string
89+
* @param int $status The response status code
90+
* @param array $headers An array of response headers
8791
*
8892
* @return static
8993
*/
90-
public static function fromJsonString(string $data = null, int $status = 200, array $headers = [])
94+
public static function fromJsonString(string $data, int $status = 200, array $headers = [])
9195
{
9296
return new static($data, $status, $headers, true);
9397
}

Tests/JsonResponseTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,36 @@ public function testSetComplexCallback()
267267

268268
$this->assertEquals('/**/ಠ_ಠ["foo"].bar[0]({"foo":"bar"});', $response->getContent());
269269
}
270+
271+
public function testConstructorWithNullAsDataThrowsAnUnexpectedValueException()
272+
{
273+
$this->expectException(\TypeError::class);
274+
$this->expectExceptionMessage('If $json is set to true, argument $data must be a string or object implementing __toString(), "null" given.');
275+
276+
new JsonResponse(null, 200, [], true);
277+
}
278+
279+
public function testConstructorWithObjectWithToStringMethod()
280+
{
281+
$class = new class() {
282+
public function __toString()
283+
{
284+
return '{}';
285+
}
286+
};
287+
288+
$response = new JsonResponse($class, 200, [], true);
289+
290+
$this->assertSame('{}', $response->getContent());
291+
}
292+
293+
public function testConstructorWithObjectWithoutToStringMethodThrowsAnException()
294+
{
295+
$this->expectException(\TypeError::class);
296+
$this->expectExceptionMessage('If $json is set to true, argument $data must be a string or object implementing __toString(), "stdClass" given.');
297+
298+
new JsonResponse(new \stdClass(), 200, [], true);
299+
}
270300
}
271301

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

0 commit comments

Comments
 (0)