Skip to content

Commit 3a8caad

Browse files
AurelienPillevessederrabus
authored andcommitted
Leverage Request::getPayload() to populate the parsed body of PSR-7 requests
1 parent 28a732c commit 3a8caad

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

Factory/PsrHttpFactory.php

+19-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Psr\Http\Message\UploadedFileInterface;
1919
use Symfony\Bridge\PsrHttpMessage\HttpMessageFactoryInterface;
2020
use Symfony\Component\HttpFoundation\BinaryFileResponse;
21+
use Symfony\Component\HttpFoundation\Exception\JsonException;
2122
use Symfony\Component\HttpFoundation\File\UploadedFile;
2223
use Symfony\Component\HttpFoundation\Request;
2324
use Symfony\Component\HttpFoundation\Response;
@@ -27,6 +28,7 @@
2728
* Builds Psr\HttpMessage instances using a PSR-17 implementation.
2829
*
2930
* @author Antonio J. García Lagar <[email protected]>
31+
* @author Aurélien Pillevesse <[email protected]>
3032
*/
3133
class PsrHttpFactory implements HttpMessageFactoryInterface
3234
{
@@ -67,12 +69,28 @@ public function createRequest(Request $symfonyRequest)
6769

6870
$body = $this->streamFactory->createStreamFromResource($symfonyRequest->getContent(true));
6971

72+
if (method_exists(Request::class, 'getContentTypeFormat')) {
73+
$format = $symfonyRequest->getContentTypeFormat();
74+
} else {
75+
$format = $symfonyRequest->getContentType();
76+
}
77+
78+
if (method_exists(Request::class, 'getPayload') && 'json' === $format) {
79+
try {
80+
$parsedBody = $symfonyRequest->getPayload()->all();
81+
} catch (JsonException $e) {
82+
$parsedBody = [];
83+
}
84+
} else {
85+
$parsedBody = $symfonyRequest->request->all();
86+
}
87+
7088
$request = $request
7189
->withBody($body)
7290
->withUploadedFiles($this->getFiles($symfonyRequest->files->all()))
7391
->withCookieParams($symfonyRequest->cookies->all())
7492
->withQueryParams($symfonyRequest->query->all())
75-
->withParsedBody($symfonyRequest->request->all())
93+
->withParsedBody($parsedBody)
7694
;
7795

7896
foreach ($symfonyRequest->attributes->all() as $key => $value) {

Tests/Factory/PsrHttpFactoryTest.php

+49
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
/**
2626
* @author Kévin Dunglas <[email protected]>
2727
* @author Antonio J. García Lagar <[email protected]>
28+
* @author Aurélien Pillevesse <[email protected]>
2829
*/
2930
class PsrHttpFactoryTest extends TestCase
3031
{
@@ -243,4 +244,52 @@ public function testUploadErrNoFile()
243244
$this->assertSame(\UPLOAD_ERR_NO_FILE, $uploadedFiles['f1']->getError());
244245
$this->assertSame(\UPLOAD_ERR_NO_FILE, $uploadedFiles['f2']->getError());
245246
}
247+
248+
public function testJsonContent()
249+
{
250+
if (!method_exists(Request::class, 'getPayload')) {
251+
$this->markTestSkipped();
252+
}
253+
254+
$headers = [
255+
'HTTP_HOST' => 'http_host.fr',
256+
'CONTENT_TYPE' => 'application/json',
257+
];
258+
$request = new Request([], [], [], [], [], $headers, '{"city":"Paris","country":"France"}');
259+
$psrRequest = $this->factory->createRequest($request);
260+
261+
$this->assertSame(['city' => 'Paris', 'country' => 'France'], $psrRequest->getParsedBody());
262+
}
263+
264+
public function testEmptyJsonContent()
265+
{
266+
if (!method_exists(Request::class, 'getPayload')) {
267+
$this->markTestSkipped();
268+
}
269+
270+
$headers = [
271+
'HTTP_HOST' => 'http_host.fr',
272+
'CONTENT_TYPE' => 'application/json',
273+
];
274+
$request = new Request([], [], [], [], [], $headers, '{}');
275+
$psrRequest = $this->factory->createRequest($request);
276+
277+
$this->assertSame([], $psrRequest->getParsedBody());
278+
}
279+
280+
public function testWrongJsonContent()
281+
{
282+
if (!method_exists(Request::class, 'getPayload')) {
283+
$this->markTestSkipped();
284+
}
285+
286+
$headers = [
287+
'HTTP_HOST' => 'http_host.fr',
288+
'CONTENT_TYPE' => 'application/json',
289+
];
290+
$request = new Request([], [], [], [], [], $headers, '{"city":"Paris"');
291+
$psrRequest = $this->factory->createRequest($request);
292+
293+
$this->assertSame([], $psrRequest->getParsedBody());
294+
}
246295
}

0 commit comments

Comments
 (0)