Skip to content

Commit

Permalink
[10.x] fix type error registering PSR Request
Browse files Browse the repository at this point in the history
  • Loading branch information
kpicaza committed Oct 25, 2023
1 parent 98ffb75 commit 97a09bb
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/Illuminate/Routing/RoutingServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,13 @@ protected function registerPsrRequest()
if (class_exists(Psr17Factory::class) && class_exists(PsrHttpFactory::class)) {
$psr17Factory = new Psr17Factory;

return with((new PsrHttpFactory($psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory))
->createRequest($illuminateRequest = $app->make('request')), fn ($request) => $request->withParsedBody(
array_merge($request->getParsedBody(), $illuminateRequest->getPayload()->all())
));
return with(
(new PsrHttpFactory($psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory))
->createRequest($illuminateRequest = $app->make('request')),
static fn ($request) => $request->withParsedBody(
array_merge($request->getParsedBody() ?? [], $illuminateRequest->getPayload()->all())
)
);
}

throw new BindingResolutionException('Unable to resolve PSR request. Please install the symfony/psr-http-message-bridge and nyholm/psr7 packages.');
Expand Down
38 changes: 38 additions & 0 deletions tests/Integration/Foundation/RoutingServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,44 @@ public function testItIncludesMergedDataInServerRequestInterfaceInstancesUsingGe
]);
}

public function testItWorksNormallyWithoutMergeDataMiddlewareWithEmptyRequests()
{
Route::get('test-route', function (ServerRequestInterface $request) {
return $request->getParsedBody();
});

$response = $this->withoutExceptionHandling()->get('test-route', [
'content-type' => 'application/json',
]);

$response->assertOk();
$response->assertExactJson([]);
}

public function testItIncludesMergedDataInServerRequestInterfaceInstancesUsingGetJsonRequestsWithContentTypeHeader()
{
Route::get('test-route', function (ServerRequestInterface $request) {
return $request->getParsedBody();
})->middleware(MergeDataMiddleware::class);

$response = $this->getJson(
'test-route?' . http_build_query([
'sent' => 'sent-data',
'overridden' => 'overriden-sent-data',
]), [
'content-type' => 'application/json',
]
);

$response->assertOk();
$response->assertExactJson([
'json-data' => 'json-data',
'merged' => 'replaced-merged-data',
'overridden' => 'overriden-merged-data',
'request-data' => 'request-data',
]);
}

public function testItIncludesMergedDataInServerRequestInterfaceInstancesUsingGetJsonRequests()
{
Route::get('test-route', function (ServerRequestInterface $request) {
Expand Down

0 comments on commit 97a09bb

Please sign in to comment.