Skip to content
Snippets Groups Projects
Unverified Commit 90525cf7 authored by Lawxen Liu's avatar Lawxen Liu Committed by Mateu Aguiló Bosch
Browse files

Issue #2998365 by caseylau, e0ipso: jsonrpcmethod's params can't be optional

parent 8a2e5114
Branches
Tags 8.x-1.0-beta3
No related merge requests found
......@@ -20,7 +20,10 @@ use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
* access = {"administer site configuration"},
* params = {
* "page" = @JsonRpcParameterDefinition(factory = "\Drupal\jsonrpc\ParameterFactory\PaginationParameterFactory"),
* "service" = @JsonRpcParameterDefinition(schema={"type"="string"}),
* "service" = @JsonRpcParameterDefinition(
* schema={"type"="string"},
* required=true
* ),
* }
* )
*/
......
......@@ -44,6 +44,13 @@ class JsonRpcParameterDefinition implements ParameterDefinitionInterface {
*/
public $factory;
/**
* Whether the parameter is required.
*
* @var bool
*/
public $required;
/**
* {@inheritdoc}
*/
......@@ -58,6 +65,13 @@ class JsonRpcParameterDefinition implements ParameterDefinitionInterface {
return $this->description;
}
/**
* {@inheritdoc}
*/
public function isRequired() {
return isset($this->required) ? $this->required : FALSE;
}
/**
* {@inheritdoc}
*/
......
......@@ -46,7 +46,7 @@ class ParameterBag {
*/
public function get($key) {
$this->ensure($key);
return $this->parameters[$key];
return isset($this->parameters[$key]) ? $this->parameters[$key] : NULL;
}
/**
......@@ -80,9 +80,7 @@ class ParameterBag {
* When the parameter is not present in the bag.
*/
protected function ensure($key) {
if (!$this->has($key)) {
throw new \InvalidArgumentException('The parameter does not exist.');
}
$this->checkKeyIsValid($key);
}
/**
......
......@@ -115,8 +115,8 @@ class Request {
* The parameter.
*/
public function getParameter($key) {
if ($this->hasParams() && $this->params->has($key)) {
return $this->params->get($key);
if ($this->hasParams() && ($param_value = $this->getParams()->get($key))) {
return $param_value;
}
return NULL;
}
......
......@@ -23,6 +23,14 @@ interface ParameterDefinitionInterface {
*/
public function getDescription();
/**
* Whether the parameter is required.
*
* @return bool
* True if this is a required parameter.
*/
public function isRequired();
/**
* Gets the parameter schema.
*
......
......@@ -140,11 +140,13 @@ class RpcRequestFactory extends TransformationBase {
$arguments = [];
$positional = $method->areParamsPositional();
foreach ($params as $key => $param) {
// TODO: Only force the presence of required parameters.
if (!isset($data['params'][$key])) {
throw $this->newException(Error::invalidParams("Missing parameter: $key"), $context);
if (isset($data['params'][$key])) {
$arguments[$key] = $this->denormalizeParam($data['params'][$key], $param);
}
// Only force the presence of required parameters.
elseif ($param->isRequired()) {
throw $this->newException(Error::invalidParams("Missing required parameter: $key"), $context);
}
$arguments[$key] = $this->denormalizeParam($data['params'][$key], $param);
}
return new ParameterBag($arguments, $positional);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment