Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Zend\Mvc\Controller\AbstractRestfulController: various fixes to Json handling #3414

Merged
merged 3 commits into from

2 participants

@ralphschindler
Collaborator

Zend\Mvc\Controller\AbstractRestfulController

  • various fixes to Json handling
  • decode() now uses controller property to determine json return type (object/array)
ralphschindler added some commits
@ralphschindler ralphschindler Zend\Mvc\Controller\AbstractRestfulController:
- Fixed AbstractRestfulController to consult Content-type
- Made Json::decode use array notation by default
- Added property to set Json return type in controller
9fbf1a0
@ralphschindler ralphschindler ZendTest\Mvc\Controller\RestfulControllerTest
* reset the shared event manager so that test will pass when run with others
6acf370
@ralphschindler ralphschindler Zend\Mvc\Controller\AbstractRestController:
- fixed content-type header for hal+json
82feaab
@weierophinney weierophinney merged commit 82feaab into from
@cgmartin cgmartin referenced this pull request from a commit in cgmartin/zf2
@weierophinney weierophinney Merge branch 'hotfix/3414' into develop
Close #3414
0822749
@ghost Unknown referenced this pull request from a commit
@weierophinney weierophinney Merge branch 'hotfix/3414' into develop
Close #3414
e1b5fa1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 11, 2013
  1. @ralphschindler

    Zend\Mvc\Controller\AbstractRestfulController:

    ralphschindler authored
    - Fixed AbstractRestfulController to consult Content-type
    - Made Json::decode use array notation by default
    - Added property to set Json return type in controller
  2. @ralphschindler

    ZendTest\Mvc\Controller\RestfulControllerTest

    ralphschindler authored
    * reset the shared event manager so that test will pass when run with others
  3. @ralphschindler

    Zend\Mvc\Controller\AbstractRestController:

    ralphschindler authored
    - fixed content-type header for hal+json
This page is out of date. Refresh to see the latest.
View
18 library/Zend/Mvc/Controller/AbstractRestfulController.php
@@ -38,12 +38,17 @@
*/
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
*
* @var array
@@ -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();
}
@@ -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;
}
}
@@ -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);
View
12 tests/ZendTest/Mvc/Controller/RestfulControllerTest.php
@@ -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);
+ }
}
Something went wrong with that request. Please try again.