Skip to content
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

[HttpFoundation] Add Request::toArray() for JSON content #38224

Merged
merged 1 commit into from Oct 3, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Component/HttpFoundation/CHANGELOG.md
Expand Up @@ -8,6 +8,7 @@ CHANGELOG
* added `HeaderUtils::parseQuery()`: it does the same as `parse_str()` but preserves dots in variable names
* added `File::getContent()`
* added ability to use comma separated ip addresses for `RequestMatcher::matchIps()`
* added `Request::toArray()` to parse a JSON request body to an array
* added `RateLimiter\RequestRateLimiterInterface` and `RateLimiter\AbstractRequestRateLimiter`

5.1.0
Expand Down
21 changes: 21 additions & 0 deletions src/Symfony/Component/HttpFoundation/Exception/JsonException.php
@@ -0,0 +1,21 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\HttpFoundation\Exception;

/**
* Thrown by Request::toArray() when the content cannot be JSON-decoded.
*
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
final class JsonException extends \UnexpectedValueException implements RequestExceptionInterface
{
}
29 changes: 29 additions & 0 deletions src/Symfony/Component/HttpFoundation/Request.php
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\HttpFoundation;

use Symfony\Component\HttpFoundation\Exception\ConflictingHeadersException;
use Symfony\Component\HttpFoundation\Exception\JsonException;
use Symfony\Component\HttpFoundation\Exception\SuspiciousOperationException;
use Symfony\Component\HttpFoundation\Session\SessionInterface;

Expand Down Expand Up @@ -1569,6 +1570,34 @@ public function getContent(bool $asResource = false)
return $this->content;
}

/**
* Gets the request body decoded as array, typically from a JSON payload.
*
* @throws JsonException When the body cannot be decoded to an array
*/
public function toArray(): array
{
if ('' === $content = $this->getContent()) {
throw new JsonException('Response body is empty.');
}

try {
$content = json_decode($content, true, 512, \JSON_BIGINT_AS_STRING | (\PHP_VERSION_ID >= 70300 ? \JSON_THROW_ON_ERROR : 0));
yceruto marked this conversation as resolved.
Show resolved Hide resolved
} catch (\JsonException $e) {
throw new JsonException('Could not decode request body.', $e->getCode(), $e);
}

if (\PHP_VERSION_ID < 70300 && \JSON_ERROR_NONE !== json_last_error()) {
throw new JsonException('Could not decode request body: '.json_last_error_msg(), json_last_error());
}

if (!\is_array($content)) {
lyrixx marked this conversation as resolved.
Show resolved Hide resolved
throw new JsonException(sprintf('JSON content was expected to decode to an array, "%s" returned.', get_debug_type($content)));
}

return $content;
}

/**
* Gets the Etags.
*
Expand Down
25 changes: 25 additions & 0 deletions src/Symfony/Component/HttpFoundation/Tests/RequestTest.php
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\HttpFoundation\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Exception\JsonException;
use Symfony\Component\HttpFoundation\Exception\SuspiciousOperationException;
use Symfony\Component\HttpFoundation\InputBag;
use Symfony\Component\HttpFoundation\ParameterBag;
Expand Down Expand Up @@ -1250,6 +1251,30 @@ public function provideOverloadedMethods()
];
}

public function testToArrayEmpty()
{
$req = new Request();
$this->expectException(JsonException::class);
$this->expectExceptionMessage('Response body is empty.');
$req->toArray();
}

public function testToArrayNonJson()
{
$req = new Request([], [], [], [], [], [], 'foobar');
$this->expectException(JsonException::class);
$this->expectExceptionMessageMatches('|Could not decode request body.+|');
$req->toArray();
}

public function testToArray()
{
$req = new Request([], [], [], [], [], [], json_encode([]));
$this->assertEquals([], $req->toArray());
$req = new Request([], [], [], [], [], [], json_encode(['foo' => 'bar']));
$this->assertEquals(['foo' => 'bar'], $req->toArray());
}

/**
* @dataProvider provideOverloadedMethods
*/
Expand Down