Skip to content

Commit

Permalink
[HttpFoundation] Make Request::getPayload() return an empty InputBag …
Browse files Browse the repository at this point in the history
…if request body is empty
  • Loading branch information
nicolas-grekas committed Jun 21, 2023
1 parent 87b8bfb commit 76002b1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
20 changes: 19 additions & 1 deletion Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -1513,7 +1513,25 @@ public function getContent(bool $asResource = false)
*/
public function getPayload(): InputBag
{
return $this->request->count() ? clone $this->request : new InputBag($this->toArray());
if ($this->request->count()) {
return clone $this->request;
}

if ('' === $content = $this->getContent()) {
return new InputBag([]);
}

try {
$content = json_decode($content, true, 512, \JSON_BIGINT_AS_STRING | \JSON_THROW_ON_ERROR);
} catch (\JsonException $e) {
throw new JsonException('Could not decode request body.', $e->getCode(), $e);
}

if (!\is_array($content)) {
throw new JsonException(sprintf('JSON content was expected to decode to an array, "%s" returned.', get_debug_type($content)));
}

return new InputBag($content);
}

/**
Expand Down
3 changes: 3 additions & 0 deletions Tests/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1309,6 +1309,9 @@ public function testGetPayload()

$req = new Request([], ['foo' => 'bar'], [], [], [], [], json_encode(['baz' => 'qux']));
$this->assertSame(['foo' => 'bar'], $req->getPayload()->all());

$req = new Request([], [], [], [], [], [], '');
$this->assertSame([], $req->getPayload()->all());
}

/**
Expand Down

0 comments on commit 76002b1

Please sign in to comment.