-
Notifications
You must be signed in to change notification settings - Fork 273
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Omit params field from requests instead of sending null value #640
base: master
Are you sure you want to change the base?
Conversation
Signed-off-by: Szabolcs Seláf <[email protected]>
Signed-off-by: Szabolcs Seláf <[email protected]>
Signed-off-by: Szabolcs Seláf <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@selu thanks a lot! This looks indeed sensible. Do you mind adding an additional test case to make sure that the server side of the library is correctly converting missing params
into None
?
I think adding a reverse test case (i.e. deserialize without params) would suffice, unless I didn't see we already have a testcase like that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, I agree with your interpretation of the spec.
I suggest additional tests for jsonrpc derive
(if possible):
- a method without params:
fn a() -> BoxedFuture
- a method with optional param:
fn b(p: Option<usize>)
Co-authored-by: Tomasz Drwięga <[email protected]>
@tomusdrw , I found the test case for deserialize without params, it is the last item of |
There is already a test case for empty params in derive/tests/macros.rs #[rpc]
pub trait Rpc {
/// Returns a protocol version.
#[rpc(name = "protocolVersion")]
fn protocol_version(&self) -> Result<String>;
…
fn should_accept_empty_array_as_no_params() {
let mut io = IoHandler::new();
let rpc = RpcImpl::default();
io.extend_with(rpc.to_delegate());
// when
let req1 = r#"{"jsonrpc":"2.0","id":1,"method":"protocolVersion","params":[]}"#;
let req2 = r#"{"jsonrpc":"2.0","id":1,"method":"protocolVersion","params":null}"#;
let req3 = r#"{"jsonrpc":"2.0","id":1,"method":"protocolVersion"}"#; However, this checks another cases too.
I could not find test case for optional param, probably it worth it to add one. Would it be ok in the same file in a similar way? |
Yes, just make a new unit test for it in that file should be fine |
Signed-off-by: Szabolcs Seláf <[email protected]>
@niklasad1 , test is added for the optional parameter, please review. |
io.extend_with(rpc.to_delegate()); | ||
|
||
// when | ||
let req1 = r#"{"jsonrpc":"2.0","id":1,"method":"sqr","params":[2]}"#; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would like to add requests with "params":[]
and "params":null
here otherwise looks great
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added those variations to the test case.
// when | ||
let req1 = r#"{"jsonrpc":"2.0","id":1,"method":"sqr","params":[2]}"#; | ||
let req2 = r#"{"jsonrpc":"2.0","id":1,"method":"sqr","params":[]}"#; | ||
let req3 = r#"{"jsonrpc":"2.0","id":1,"method":"sqr","params":null}"#; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this be rejected as invalid?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a good question, I know only KODI jsonrpc server, that rejects only "params":null
variation, but accepts both "params":[]
and "params":{}
as an empty parameter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd distinguish 3 different cases:
- Method expects positional arguments (either 0 or some being optional)
- Method expects named arguments (either 0 or some being optional)
- Method expects raw arguments
In case (1) IMHO only params: []
should be accepted, similarly in case (2) only params: {}
should be accepted. In both cases omitting params
should be rejected.
In the 3rd case we can accept any combination, since it's up to the method to handle the raw parameters being passed.
Regarding params: null
I'm wondering if that should be forbidden completely, since it's not really part of the spec.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In both cases omitting params should be rejected.
hrm, really? In the client code Params::None
would rejected then?
Regarding params: null I'm wondering if that should be forbidden completely, since it's not really part of the spec.
I'm fine with that and it might better to be explicit, mainly because it might be possible that a request contains the wrong type/format and the default value is then used without that the caller gets informed by that....
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hrm, really? In the client code Params::None would rejected then?
I was checking the code and it might not be what we do now. AFAICT in case of optional parameters we parse None
correctly as well. Anyways, I think this discussion is a bit tangential to that particular PR, so let's open an issue and discuss there.
The code in PR is an improvement for the client side for sure, we might want to follow up on how we want the server to behave separately.
Based on the specification, the request's params must be an array or an object if it exists, so sending a null value is not valid. Actually, it causes errors for KODI JSONRPC server with requests without any parameters. That's why I tried to find a way to fix this issue.
The solution is very simple, just I haven't updated the comments for the params field.