cranetm / yii2-json-rpc-2.0

Other
26 stars 18 forks source link

Request wih id === 0 and bad response #6

Closed plumbum closed 8 years ago

plumbum commented 8 years ago

Hello.

If i send request with data

{
    "jsonrpc": "2.0",
    "id": 0,
    "method": "update",
    "params": ["world"]
}

I get bad response

{"jsonrpc":"2.0","id":null,"result":{"message":"hello world"}}

This problem located at Controller.php line 90

    return Helper::formatResponse($result, $error, !empty($this->requestObject->id)? $this->requestObject->id : null);

PHP is crazy and he thinking, that number 0 or string "0" is empty.

tyler-ham commented 8 years ago

I believe you are correct that this is a bug at Controller.php line 90.

According to the JSON-RPC 2.0 specs, the id must be a String, Number, or NULL if included, and the server must reply with the same value in the Response object if it was included.

By the looks of it, I think the conditional on Controller.php line 90 is only meant to test whether or not we can safely reference $this->requestObject->id without throwing an error. The !empty might be replaced with isset to fix this:

    return Helper::formatResponse($result, $error, isset($this->requestObject->id) ? $this->requestObject->id : null);
plumbum commented 8 years ago

Thanks you. Yes, i thing, that 'isset' is good idea. I replace it on my code, and it worked correctly.

cranetm commented 8 years ago

Thanks guys, i've changed condition to isset in the latest commit.