Skip to content

Commit

Permalink
Don't rely on Request::getPayload() to populate the parsed body
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed Jul 26, 2023
1 parent 3c62b81 commit ef03b6d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
11 changes: 5 additions & 6 deletions Factory/PsrHttpFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
use Psr\Http\Message\UploadedFileInterface;
use Symfony\Bridge\PsrHttpMessage\HttpMessageFactoryInterface;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\Exception\JsonException;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand Down Expand Up @@ -79,11 +78,11 @@ public function createRequest(Request $symfonyRequest)
$format = $symfonyRequest->getContentType();
}

if (method_exists(Request::class, 'getPayload') && 'json' === $format) {
try {
$parsedBody = $symfonyRequest->getPayload()->all();
} catch (JsonException $e) {
$parsedBody = [];
if ('json' === $format) {
$parsedBody = json_decode($symfonyRequest->getContent(), true, 512, \JSON_BIGINT_AS_STRING);

if (!\is_array($parsedBody)) {
$parsedBody = null;
}
} else {
$parsedBody = $symfonyRequest->request->all();
Expand Down
2 changes: 1 addition & 1 deletion Tests/Factory/PsrHttpFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,6 @@ public function testWrongJsonContent()
$request = new Request([], [], [], [], [], $headers, '{"city":"Paris"');
$psrRequest = $this->factory->createRequest($request);

$this->assertSame([], $psrRequest->getParsedBody());
$this->assertNull($psrRequest->getParsedBody());
}
}

0 comments on commit ef03b6d

Please sign in to comment.