Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8199380

Browse files
bug#50730 [HttpFoundation] Make Request::getPayload() return an empty InputBag if request body is empty (nicolas-grekas)
This PR was merged into the 6.3 branch.Discussion----------[HttpFoundation] Make Request::getPayload() return an empty InputBag if request body is empty| Q | A| ------------- | ---| Branch? | 6.3| Bug fix? | yes| New feature? | no| Deprecations? | no| Tickets |Fix#50647| License | MIT| Doc PR | -Not calling toArray() because we don't want to call getContent() twice.Commits-------7d9541d [HttpFoundation] Make Request::getPayload() return an empty InputBag if request body is empty
2 parentsb7980f3 +7d9541d commit8199380

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

‎src/Symfony/Component/HttpFoundation/Request.php‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1513,7 +1513,25 @@ public function getContent(bool $asResource = false)
15131513
*/
15141514
publicfunctiongetPayload():InputBag
15151515
{
1516-
return$this->request->count() ?clone$this->request :newInputBag($this->toArray());
1516+
if ($this->request->count()) {
1517+
returnclone$this->request;
1518+
}
1519+
1520+
if ('' ===$content =$this->getContent()) {
1521+
returnnewInputBag([]);
1522+
}
1523+
1524+
try {
1525+
$content =json_decode($content,true,512, \JSON_BIGINT_AS_STRING | \JSON_THROW_ON_ERROR);
1526+
}catch (\JsonException$e) {
1527+
thrownewJsonException('Could not decode request body.',$e->getCode(),$e);
1528+
}
1529+
1530+
if (!\is_array($content)) {
1531+
thrownewJsonException(sprintf('JSON content was expected to decode to an array, "%s" returned.',get_debug_type($content)));
1532+
}
1533+
1534+
returnnewInputBag($content);
15171535
}
15181536

15191537
/**

‎src/Symfony/Component/HttpFoundation/Tests/RequestTest.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,6 +1309,9 @@ public function testGetPayload()
13091309

13101310
$req =newRequest([], ['foo' =>'bar'], [], [], [], [],json_encode(['baz' =>'qux']));
13111311
$this->assertSame(['foo' =>'bar'],$req->getPayload()->all());
1312+
1313+
$req =newRequest([], [], [], [], [], [],'');
1314+
$this->assertSame([],$req->getPayload()->all());
13121315
}
13131316

13141317
/**

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp