diff --git a/src/Illuminate/Routing/RoutingServiceProvider.php b/src/Illuminate/Routing/RoutingServiceProvider.php index 0221839fc8df..aef743021378 100755 --- a/src/Illuminate/Routing/RoutingServiceProvider.php +++ b/src/Illuminate/Routing/RoutingServiceProvider.php @@ -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.'); diff --git a/tests/Integration/Foundation/RoutingServiceProviderTest.php b/tests/Integration/Foundation/RoutingServiceProviderTest.php index 859b348eb774..c08526231879 100644 --- a/tests/Integration/Foundation/RoutingServiceProviderTest.php +++ b/tests/Integration/Foundation/RoutingServiceProviderTest.php @@ -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) {