From 919ce2113f7b588640215656072b516eb00fbfac Mon Sep 17 00:00:00 2001 From: Rob Allen Date: Sat, 5 Sep 2015 22:40:31 +0100 Subject: [PATCH 1/2] Return arrays where possible for getParsedBody() This fixes the weird case that if you POST, then getParsedBody() will have an array in it ($_POST), but if you PUT or have json, then getParsedBody() will have an object. Even more weirdly, if you POST with the hidden _METHOD=PUT, you get an array in your put() handler, but if you PUT to it you get an object. It's much more consistent to try to always make it an array and ensures that we can also handle keys with hyphens much more sanely as they are horrible to deal with when in a stdClass. --- Slim/Http/Request.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Slim/Http/Request.php b/Slim/Http/Request.php index 1d9b89537..1b54f5c9d 100644 --- a/Slim/Http/Request.php +++ b/Slim/Http/Request.php @@ -182,7 +182,7 @@ public function __construct($method, UriInterface $uri, HeadersInterface $header } $this->registerMediaTypeParser('application/json', function ($input) { - return json_decode($input); + return json_decode($input, true); }); $this->registerMediaTypeParser('application/xml', function ($input) { @@ -191,7 +191,7 @@ public function __construct($method, UriInterface $uri, HeadersInterface $header $this->registerMediaTypeParser('application/x-www-form-urlencoded', function ($input) { parse_str($input, $data); - return (object)$data; + return $data; }); } From 3fb0cd4db9b4d00ad498bf91f150a02aa86eaeaa Mon Sep 17 00:00:00 2001 From: Rob Allen Date: Sat, 5 Sep 2015 22:43:31 +0100 Subject: [PATCH 2/2] Update unit tests to expect arrays from getParsedBody() --- tests/Http/RequestTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Http/RequestTest.php b/tests/Http/RequestTest.php index db02b82a8..dced78bb4 100644 --- a/tests/Http/RequestTest.php +++ b/tests/Http/RequestTest.php @@ -694,7 +694,7 @@ public function testGetParsedBodyForm() $body = new RequestBody(); $body->write('foo=bar'); $request = new Request($method, $uri, $headers, $cookies, $serverParams, $body); - $this->assertEquals((object)['foo' => 'bar'], $request->getParsedBody()); + $this->assertEquals(['foo' => 'bar'], $request->getParsedBody()); } public function testGetParsedBodyJson() @@ -709,7 +709,7 @@ public function testGetParsedBodyJson() $body->write('{"foo":"bar"}'); $request = new Request($method, $uri, $headers, $cookies, $serverParams, $body); - $this->assertEquals((object)['foo' => 'bar'], $request->getParsedBody()); + $this->assertEquals(['foo' => 'bar'], $request->getParsedBody()); } public function testGetParsedBodyXml()