Skip to content

Commit

Permalink
Fix serialization of JSON-RPC types
Browse files Browse the repository at this point in the history
  • Loading branch information
efoerster committed May 9, 2019
1 parent db96b9e commit cba8df5
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 18 deletions.
4 changes: 2 additions & 2 deletions jsonrpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ where
Ok(Message::Notification(notification)) => {
self.server.handle_notification(notification);
}
Ok(Message::Response(_)) => unimplemented!(),
Ok(Message::Response(response)) => unimplemented!("{:?}", response),
Err(why) => {
let response = Response::new(serde_json::Value::Null, Some(why), None);
let response = Response::error(why, None);
let json = serde_json::to_string(&response).unwrap();
let mut output = await!(self.output.lock());
await!(output.send(json));
Expand Down
13 changes: 6 additions & 7 deletions jsonrpc/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ where
};

match await!(handle(request.params)) {
Ok(result) => Response::new(json!(result), None, Some(request.id)),
Err(error) => Response::new(serde_json::Value::Null, Some(error), Some(request.id)),
Ok(result) => Response::result(json!(result), request.id),
Err(error) => Response::error(error, Some(request.id)),
}
}

Expand All @@ -58,7 +58,6 @@ mod tests {
use super::*;
use futures::executor::block_on;

const JSONRPC_VERSION: &str = "2.0";
const METHOD_NAME: &str = "foo";

async fn increment(i: i32) -> Result<i32> {
Expand All @@ -71,7 +70,7 @@ mod tests {

fn setup_request<T: Serialize>(value: T) -> Request {
Request {
jsonrpc: JSONRPC_VERSION.to_owned(),
jsonrpc: PROTOCOL_VERSION.to_owned(),
params: json!(value),
method: METHOD_NAME.to_owned(),
id: 0,
Expand All @@ -80,7 +79,7 @@ mod tests {

fn setup_notification() -> Notification {
Notification {
jsonrpc: JSONRPC_VERSION.to_owned(),
jsonrpc: PROTOCOL_VERSION.to_owned(),
method: METHOD_NAME.to_owned(),
params: json!(()),
}
Expand All @@ -94,7 +93,7 @@ mod tests {
let response = block_on(handle_request(request.clone(), increment));
let expected = Response {
jsonrpc: request.jsonrpc,
result: json!(block_on(increment(value)).unwrap()),
result: Some(json!(block_on(increment(value)).unwrap())),
error: None,
id: Some(request.id),
};
Expand All @@ -109,7 +108,7 @@ mod tests {
let response = block_on(handle_request(request.clone(), increment));
let expected = Response {
jsonrpc: request.jsonrpc.clone(),
result: serde_json::Value::Null,
result: None,
error: Some(Error {
code: ErrorCode::InvalidParams,
message: DESERIALIZE_OBJECT_ERROR.to_owned(),
Expand Down
25 changes: 18 additions & 7 deletions jsonrpc/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use serde::{Deserialize, Serialize};
use serde_repr::*;

pub const PROTOCOL_VERSION: &str = "2.0";

#[derive(Clone, Copy, Debug, Eq, PartialEq, Deserialize_repr, Serialize_repr)]
#[repr(i32)]
pub enum ErrorCode {
Expand Down Expand Up @@ -35,8 +37,8 @@ pub struct Request {
pub struct Response {
pub jsonrpc: String,

#[serde(skip_serializing_if = "serde_json::Value::is_null")]
pub result: serde_json::Value,
#[serde(skip_serializing_if = "Option::is_none")]
pub result: Option<serde_json::Value>,

#[serde(skip_serializing_if = "Option::is_none")]
pub error: Option<Error>,
Expand All @@ -45,11 +47,20 @@ pub struct Response {
}

impl Response {
pub fn new(result: serde_json::Value, error: Option<Error>, id: Option<i32>) -> Self {
pub fn result(result: serde_json::Value, id: i32) -> Self {
Response {
jsonrpc: String::from("2.0"),
result,
error,
jsonrpc: PROTOCOL_VERSION.to_owned(),
result: Some(result),
error: None,
id: Some(id),
}
}

pub fn error(error: Error, id: Option<i32>) -> Self {
Response {
jsonrpc: PROTOCOL_VERSION.to_owned(),
result: None,
error: Some(error),
id,
}
}
Expand All @@ -66,8 +77,8 @@ pub struct Notification {
#[serde(untagged)]
pub enum Message {
Request(Request),
Response(Response),
Notification(Notification),
Response(Response),
}

pub type Result<T> = std::result::Result<T, String>;
4 changes: 2 additions & 2 deletions jsonrpc_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ pub fn jsonrpc_server(
_ => {
let error = jsonrpc::Error {
code: jsonrpc::ErrorCode::MethodNotFound,
message: String::from("Method not found"),
message: "Method not found".to_owned(),
data: serde_json::Value::Null,
};

jsonrpc::Response::new(serde_json::Value::Null, Some(error), Some(request.id))
jsonrpc::Response::error(error, Some(request.id))
}
}
};
Expand Down

0 comments on commit cba8df5

Please sign in to comment.