Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

BUG FIX: AbstractRestfulController does not allow xml, yml(or any string) as body #43

Merged
merged 2 commits into from
Mar 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions src/Controller/AbstractRestfulController.php
Original file line number Diff line number Diff line change
Expand Up @@ -560,8 +560,8 @@ protected function getIdentifier($routeMatch, $request)
*
* If the content-type indicates a JSON payload, the payload is immediately
* decoded and the data returned. Otherwise, the data is passed to
* parse_str(). If that function returns a single-member array with a key
* of "0", the method assumes that we have non-urlencoded content and
* parse_str(). If that function returns a single-member array with a empty
* value, the method assumes that we have non-urlencoded content and
* returns the raw content; otherwise, the array created is returned.
*
* @param mixed $request
Expand All @@ -578,10 +578,9 @@ protected function processBodyContent($request)

parse_str($content, $parsedParams);

// If parse_str fails to decode, or we have a single element with key
// 0, return the raw content.
if (!is_array($parsedParams)
|| (1 == count($parsedParams) && isset($parsedParams[0]))
// If parse_str fails to decode, or we have a single element with empty value
if (!is_array($parsedParams) || empty($parsedParams)
|| (1 == count($parsedParams) && '' === reset($parsedParams))
) {
return $content;
}
Expand Down
17 changes: 17 additions & 0 deletions test/Controller/RestfulControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Zend\Http\Response;
use Zend\Mvc\MvcEvent;
use Zend\Mvc\Router\RouteMatch;
use ZendTest\Mvc\Controller\TestAsset\RestfulContentTypeTestController;

class RestfulControllerTest extends TestCase
{
Expand Down Expand Up @@ -76,6 +77,22 @@ public function testDispatchInvokesCreateMethodWhenNoActionPresentAndPostInvoked
$this->assertEquals('create', $this->routeMatch->getParam('action'));
}

public function testCanReceiveStringAsRequestContent()
{
$string = "any content";
$this->request->setMethod('PUT');
$this->request->setContent($string);
$this->routeMatch->setParam('id', $id = 1);

$controller = new RestfulContentTypeTestController();
$controller->setEvent($this->event);
$result = $controller->dispatch($this->request, $this->response);

$this->assertEquals($id, $result['id']);
$this->assertEquals($string, $result['data']);
$this->assertEquals('update', $this->routeMatch->getParam('action'));
}

public function testDispatchInvokesUpdateMethodWhenNoActionPresentAndPutInvokedWithIdentifier()
{
$entity = ['name' => __FUNCTION__];
Expand Down
30 changes: 30 additions & 0 deletions test/Controller/TestAsset/RestfulContentTypeTestController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace ZendTest\Mvc\Controller\TestAsset;

use Zend\Mvc\Controller\AbstractRestfulController;

class RestfulContentTypeTestController extends AbstractRestfulController
{
/**
* Update an existing resource
*
* @param mixed $id
* @param mixed $data
* @return array
*/
public function update($id, $data)
{
return [
'id' => $id,
'data' => $data
];
}
}