Skip to content

Commit

Permalink
Merge branch 'hotfix/3414' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
weierophinney committed Jan 11, 2013
2 parents 874ac52 + 6e031e1 commit 0822749
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
18 changes: 12 additions & 6 deletions library/Zend/Mvc/Controller/AbstractRestfulController.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,16 @@ abstract class AbstractRestfulController extends AbstractController
*/
protected $contentTypes = array(
self::CONTENT_TYPE_JSON => array(
'application/application/hal+json',
'application/hal+json',
'application/json'
)
);

/**
* @var int From Zend\Json\Json
*/
protected $jsonDecodeType = Json::TYPE_ARRAY;

/**
* Map of custom HTTP methods and their handlers
*
Expand Down Expand Up @@ -301,7 +306,7 @@ public function onDispatch(MvcEvent $e)
public function processPostData(Request $request)
{
if ($this->requestHasContentType($request, self::CONTENT_TYPE_JSON)) {
$data = Json::decode($request->getContent());
$data = Json::decode($request->getContent(), $this->jsonDecodeType);
} else {
$data = $request->getPost()->toArray();
}
Expand Down Expand Up @@ -336,14 +341,15 @@ public function processPutData(Request $request, $routeMatch)
*/
public function requestHasContentType(Request $request, $contentType = '')
{
$acceptHeaders = $request->getHeaders()->get('Accept');
if (!$acceptHeaders) {
/** @var $headerContentType \Zend\Http\Header\ContentType */
$headerContentType = $request->getHeaders()->get('content-type');
if (!$headerContentType) {
return false;
}

if (array_key_exists($contentType, $this->contentTypes)) {
foreach ($this->contentTypes[$contentType] as $contentTypeValue) {
if ($acceptHeaders->match($contentTypeValue) !== false) {
if (stripos($contentTypeValue, $headerContentType->getFieldValue()) === 0) {
return true;
}
}
Expand Down Expand Up @@ -434,7 +440,7 @@ protected function processBodyContent($request)

// JSON content? decode and return it.
if ($this->requestHasContentType($request, self::CONTENT_TYPE_JSON)) {
return Json::decode($content);
return Json::decode($content, $this->jsonDecodeType);
}

parse_str($content, $parsedParams);
Expand Down
12 changes: 12 additions & 0 deletions tests/ZendTest/Mvc/Controller/RestfulControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,4 +293,16 @@ public function testMethodOverloadingShouldInvokePluginAsFunctorIfPossible()
$this->controller->layout('alternate/layout');
$this->assertEquals('alternate/layout', $model->getTemplate());
}

public function testParsingDataAsJsonWillReturnAsArray()
{
$this->request->setMethod('POST');
$this->request->getHeaders()->addHeaderLine('Content-type', 'application/json');
$this->request->setContent('{"foo":"bar"}');
$this->controller->getEventManager()->setSharedManager(new SharedEventManager());

$result = $this->controller->dispatch($this->request, $this->response);
$this->assertInternalType('array', $result);
$this->assertEquals(array('entity' => array('foo' => 'bar')), $result);
}
}

0 comments on commit 0822749

Please sign in to comment.