Skip to content
This repository has been archived by the owner on Dec 29, 2022. It is now read-only.

Commit

Permalink
Merge pull request #1041 from alexheretic/permissive-void-param-requests
Browse files Browse the repository at this point in the history
Avoid void-params deserialize errors
  • Loading branch information
nrc authored Sep 5, 2018
2 parents 502a46e + 25bcb21 commit 4b61cf0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/server/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,10 +269,18 @@ impl RawMessage {
Id::Null => None,
};

let params = R::Params::deserialize(&self.params).map_err(|e| {
debug!("error when parsing as request: {}", e);
jsonrpc::Error::invalid_params(format!("{}", e))
})?;
let params = R::Params::deserialize(&self.params)
.or_else(|e| {
// Avoid tedious type errors trying to deserialize `()`
if std::mem::size_of::<R::Params>() == 0 {
R::Params::deserialize(&serde_json::Value::Null).map_err(|_| e)
} else {
Err(e)
}
}).map_err(|e| {
debug!("error when parsing as request: {}", e);
jsonrpc::Error::invalid_params(format!("{}", e))
})?;

match parsed_id {
Some(id) => Ok(Request {
Expand Down
14 changes: 14 additions & 0 deletions src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,4 +465,18 @@ mod test {

assert_eq!(get_root_path(&params), root_path);
}

/// Some clients send empty object params for void params requests (see #1038)
#[test]
fn parse_shutdown_object_params() {
let raw = RawMessage::try_parse(
r#"{"jsonrpc": "2.0", "id": 2, "method": "shutdown", "params": {}}"#,
).ok()
.and_then(|x| x)
.expect("raw parse failed");

let _request: Request<ShutdownRequest> = raw
.parse_as_request()
.expect("Boring validation is happening");
}
}

0 comments on commit 4b61cf0

Please sign in to comment.