diff --git a/CHANGELOG-3.1.md b/CHANGELOG-3.1.md index 53c0e38b4..4b865b957 100644 --- a/CHANGELOG-3.1.md +++ b/CHANGELOG-3.1.md @@ -1,5 +1,9 @@ # v3.1.12 - TBD +## Fixed + +- [#6566](https://github.com/hyperf/hyperf/pull/6566) Fixed bug that the numeric keys will be reset when using `$request->all()`. + # v3.1.11 - 2024-03-01 ## Fixed diff --git a/src/http-server/src/Request.php b/src/http-server/src/Request.php index 1dbe73c51..c3fec5d58 100644 --- a/src/http-server/src/Request.php +++ b/src/http-server/src/Request.php @@ -566,7 +566,7 @@ class Request implements RequestInterface $data = []; } - return array_merge($data, $request->getQueryParams()); + return $request->getQueryParams() + $data; }); } diff --git a/src/http-server/tests/RequestTest.php b/src/http-server/tests/RequestTest.php index a884085b0..44ddd573a 100644 --- a/src/http-server/tests/RequestTest.php +++ b/src/http-server/tests/RequestTest.php @@ -80,12 +80,32 @@ class RequestTest extends TestCase public function testRequestAll() { $psrRequest = Mockery::mock(ServerRequestPlusInterface::class); - $psrRequest->shouldReceive('getParsedBody')->andReturn(['id' => 1]); + $psrRequest->shouldReceive('getParsedBody')->andReturn(['id' => 1, '123' => '123']); $psrRequest->shouldReceive('getQueryParams')->andReturn(['name' => 'Hyperf']); RequestContext::set($psrRequest); $psrRequest = new Request(); - $this->assertSame(['id' => 1, 'name' => 'Hyperf'], $psrRequest->all()); + $this->assertSame(['name' => 'Hyperf', 'id' => 1, 123 => '123'], $psrRequest->all()); + + $psrRequest = Mockery::mock(ServerRequestPlusInterface::class); + $psrRequest->shouldReceive('getParsedBody')->andReturn(['name' => 'Hyperf']); + $psrRequest->shouldReceive('getQueryParams')->andReturn(['id' => 1, '123' => '123']); + RequestContext::set($psrRequest); + + $psrRequest = new Request(); + + $this->assertSame(['name' => 'Hyperf', 'id' => 1, 123 => '123'], $psrRequest->all()); + } + + public function testRequestAllByReplace() + { + $psrRequest = Mockery::mock(ServerRequestPlusInterface::class); + $psrRequest->shouldReceive('getParsedBody')->andReturn(['id' => 1, 'data' => ['id' => 2]]); + $psrRequest->shouldReceive('getQueryParams')->andReturn(['data' => 'Hyperf']); + RequestContext::set($psrRequest); + + $psrRequest = new Request(); + $this->assertEquals(['id' => 1, 'data' => 'Hyperf'], $psrRequest->all()); } public function testRequestInputs()