-
-
Notifications
You must be signed in to change notification settings - Fork 57
Leverage Request::getPayload()
to populate the parsed body of PSR-7 requests
#117
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
Merged
derrabus
merged 1 commit into
symfony:main
from
AurelienPillevesse:improve-request-server-all
Jul 25, 2023
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
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; | ||
|
@@ -27,6 +28,7 @@ | |
* Builds Psr\HttpMessage instances using a PSR-17 implementation. | ||
* | ||
* @author Antonio J. García Lagar <[email protected]> | ||
* @author Aurélien Pillevesse <[email protected]> | ||
*/ | ||
class PsrHttpFactory implements HttpMessageFactoryInterface | ||
{ | ||
|
@@ -67,12 +69,28 @@ public function createRequest(Request $symfonyRequest) | |
|
||
$body = $this->streamFactory->createStreamFromResource($symfonyRequest->getContent(true)); | ||
|
||
if (method_exists(Request::class, 'getContentTypeFormat')) { | ||
$format = $symfonyRequest->getContentTypeFormat(); | ||
} else { | ||
$format = $symfonyRequest->getContentType(); | ||
} | ||
|
||
if (method_exists(Request::class, 'getPayload') && 'json' === $format) { | ||
try { | ||
$parsedBody = $symfonyRequest->getPayload()->all(); | ||
} catch (JsonException $e) { | ||
$parsedBody = []; | ||
} | ||
} else { | ||
$parsedBody = $symfonyRequest->request->all(); | ||
} | ||
OskarStark marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
$request = $request | ||
->withBody($body) | ||
->withUploadedFiles($this->getFiles($symfonyRequest->files->all())) | ||
->withCookieParams($symfonyRequest->cookies->all()) | ||
->withQueryParams($symfonyRequest->query->all()) | ||
->withParsedBody($symfonyRequest->request->all()) | ||
->withParsedBody($parsedBody) | ||
; | ||
|
||
foreach ($symfonyRequest->attributes->all() as $key => $value) { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ | |
/** | ||
* @author Kévin Dunglas <[email protected]> | ||
* @author Antonio J. García Lagar <[email protected]> | ||
* @author Aurélien Pillevesse <[email protected]> | ||
*/ | ||
class PsrHttpFactoryTest extends TestCase | ||
{ | ||
|
@@ -243,4 +244,52 @@ public function testUploadErrNoFile() | |
$this->assertSame(\UPLOAD_ERR_NO_FILE, $uploadedFiles['f1']->getError()); | ||
$this->assertSame(\UPLOAD_ERR_NO_FILE, $uploadedFiles['f2']->getError()); | ||
} | ||
|
||
public function testJsonContent() | ||
{ | ||
if (!method_exists(Request::class, 'getPayload')) { | ||
$this->markTestSkipped(); | ||
} | ||
|
||
$headers = [ | ||
'HTTP_HOST' => 'http_host.fr', | ||
'CONTENT_TYPE' => 'application/json', | ||
]; | ||
$request = new Request([], [], [], [], [], $headers, '{"city":"Paris","country":"France"}'); | ||
$psrRequest = $this->factory->createRequest($request); | ||
|
||
$this->assertSame(['city' => 'Paris', 'country' => 'France'], $psrRequest->getParsedBody()); | ||
} | ||
|
||
public function testEmptyJsonContent() | ||
{ | ||
if (!method_exists(Request::class, 'getPayload')) { | ||
$this->markTestSkipped(); | ||
} | ||
|
||
$headers = [ | ||
'HTTP_HOST' => 'http_host.fr', | ||
'CONTENT_TYPE' => 'application/json', | ||
]; | ||
$request = new Request([], [], [], [], [], $headers, '{}'); | ||
$psrRequest = $this->factory->createRequest($request); | ||
|
||
$this->assertSame([], $psrRequest->getParsedBody()); | ||
} | ||
|
||
public function testWrongJsonContent() | ||
{ | ||
if (!method_exists(Request::class, 'getPayload')) { | ||
$this->markTestSkipped(); | ||
} | ||
|
||
$headers = [ | ||
'HTTP_HOST' => 'http_host.fr', | ||
'CONTENT_TYPE' => 'application/json', | ||
]; | ||
$request = new Request([], [], [], [], [], $headers, '{"city":"Paris"'); | ||
$psrRequest = $this->factory->createRequest($request); | ||
|
||
$this->assertSame([], $psrRequest->getParsedBody()); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.