Skip to content

Commit

Permalink
Merge pull request #7 from neofly/neofly
Browse files Browse the repository at this point in the history
Fix the undefined index 'id' in case of error
  • Loading branch information
Christoph Singer committed Jul 15, 2014
2 parents 5e7e143 + 409e147 commit 35b9307
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions Controller/JsonRpcController.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,16 @@ public function execute(Request $httprequest)
{
$json = $httprequest->getContent();
$request = json_decode($json, true);
$requestId = (isset($request['id']) ? $request['id'] : null);

if ($request === null) {
return $this->getErrorResponse(self::PARSE_ERROR, null);
} elseif (!(isset($request['jsonrpc']) && isset($request['method']) && $request['jsonrpc'] == '2.0')) {
return $this->getErrorResponse(self::INVALID_REQUEST, $request['id']);
return $this->getErrorResponse(self::INVALID_REQUEST, $requestId);
}

if (!in_array($request['method'], array_keys($this->config['functions']))) {
return $this->getErrorResponse(self::METHOD_NOT_FOUND, $request['id']);
return $this->getErrorResponse(self::METHOD_NOT_FOUND, $requestId);
}

$service = $this->container->get($this->config['functions'][$request['method']]['service']);
Expand All @@ -94,7 +95,7 @@ public function execute(Request $httprequest)
if (!(count($params) >= $r->getNumberOfRequiredParameters()
&& count($params) <= $r->getNumberOfParameters())
) {
return $this->getErrorResponse(self::INVALID_PARAMS, $request['id'],
return $this->getErrorResponse(self::INVALID_PARAMS, $requestId,
sprintf('Number of given parameters (%d) does not match the number of expected parameters (%d required, %d total)',
count($params), $r->getNumberOfRequiredParameters(), $r->getNumberOfParameters()));
}
Expand All @@ -105,7 +106,7 @@ public function execute(Request $httprequest)
/* @var \ReflectionParameter $rp */
$name = $rp->name;
if (!isset($params->$name) && !$rp->isOptional()) {
return $this->getErrorResponse(self::INVALID_PARAMS, $request['id'],
return $this->getErrorResponse(self::INVALID_PARAMS, $requestId,
sprintf('Parameter %s is missing', $name));
}
if (isset($params->$name)) {
Expand All @@ -120,12 +121,12 @@ public function execute(Request $httprequest)
try {
$result = call_user_func_array(array($service, $method), $params);
} catch (\Exception $e) {
return $this->getErrorResponse(self::INTERNAL_ERROR, $request['id'], $e->getMessage());
return $this->getErrorResponse(self::INTERNAL_ERROR, $requestId, $e->getMessage());
}

$response = array('jsonrpc' => '2.0');
$response['result'] = $result;
$response['id'] = (isset($request['id']) ? $request['id'] : null);
$response['id'] = $requestId;

if ($this->container->has('jms_serializer')) {
$response = $this->container->get('jms_serializer')->serialize($response, 'json', $this->serializationContext);
Expand All @@ -135,7 +136,7 @@ public function execute(Request $httprequest)

return new Response($response, 200, array('Content-Type' => 'application/json'));
} else {
return $this->getErrorResponse(self::METHOD_NOT_FOUND, $request['id']);
return $this->getErrorResponse(self::METHOD_NOT_FOUND, $requestId);
}
}

Expand Down

0 comments on commit 35b9307

Please sign in to comment.