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

Commit f1509af

Browse files
committed
Merge pull request #43 from svycka/issue-42
BUG FIX: AbstractRestfulController does not allow xml, yml(or any string) as body
2 parents 9b44720 + cdea8bd commit f1509af

File tree

3 files changed

+52
-6
lines changed

3 files changed

+52
-6
lines changed

Diff for: src/Controller/AbstractRestfulController.php

+5-6
Original file line numberDiff line numberDiff line change
@@ -560,8 +560,8 @@ protected function getIdentifier($routeMatch, $request)
560560
*
561561
* If the content-type indicates a JSON payload, the payload is immediately
562562
* decoded and the data returned. Otherwise, the data is passed to
563-
* parse_str(). If that function returns a single-member array with a key
564-
* of "0", the method assumes that we have non-urlencoded content and
563+
* parse_str(). If that function returns a single-member array with a empty
564+
* value, the method assumes that we have non-urlencoded content and
565565
* returns the raw content; otherwise, the array created is returned.
566566
*
567567
* @param mixed $request
@@ -578,10 +578,9 @@ protected function processBodyContent($request)
578578

579579
parse_str($content, $parsedParams);
580580

581-
// If parse_str fails to decode, or we have a single element with key
582-
// 0, return the raw content.
583-
if (!is_array($parsedParams)
584-
|| (1 == count($parsedParams) && isset($parsedParams[0]))
581+
// If parse_str fails to decode, or we have a single element with empty value
582+
if (!is_array($parsedParams) || empty($parsedParams)
583+
|| (1 == count($parsedParams) && '' === reset($parsedParams))
585584
) {
586585
return $content;
587586
}

Diff for: test/Controller/RestfulControllerTest.php

+17
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Zend\Http\Response;
1919
use Zend\Mvc\MvcEvent;
2020
use Zend\Mvc\Router\RouteMatch;
21+
use ZendTest\Mvc\Controller\TestAsset\RestfulContentTypeTestController;
2122

2223
class RestfulControllerTest extends TestCase
2324
{
@@ -101,6 +102,22 @@ public function testDispatchInvokesCreateMethodWhenNoActionPresentAndPostInvoked
101102
$this->assertEquals('create', $this->routeMatch->getParam('action'));
102103
}
103104

105+
public function testCanReceiveStringAsRequestContent()
106+
{
107+
$string = "any content";
108+
$this->request->setMethod('PUT');
109+
$this->request->setContent($string);
110+
$this->routeMatch->setParam('id', $id = 1);
111+
112+
$controller = new RestfulContentTypeTestController();
113+
$controller->setEvent($this->event);
114+
$result = $controller->dispatch($this->request, $this->response);
115+
116+
$this->assertEquals($id, $result['id']);
117+
$this->assertEquals($string, $result['data']);
118+
$this->assertEquals('update', $this->routeMatch->getParam('action'));
119+
}
120+
104121
public function testDispatchInvokesUpdateMethodWhenNoActionPresentAndPutInvokedWithIdentifier()
105122
{
106123
$entity = ['name' => __FUNCTION__];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
/**
3+
* Zend Framework (http://framework.zend.com/)
4+
*
5+
* @link http://github.com/zendframework/zf2 for the canonical source repository
6+
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7+
* @license http://framework.zend.com/license/new-bsd New BSD License
8+
*/
9+
10+
namespace ZendTest\Mvc\Controller\TestAsset;
11+
12+
use Zend\Mvc\Controller\AbstractRestfulController;
13+
14+
class RestfulContentTypeTestController extends AbstractRestfulController
15+
{
16+
/**
17+
* Update an existing resource
18+
*
19+
* @param mixed $id
20+
* @param mixed $data
21+
* @return array
22+
*/
23+
public function update($id, $data)
24+
{
25+
return [
26+
'id' => $id,
27+
'data' => $data
28+
];
29+
}
30+
}

0 commit comments

Comments
 (0)