Skip to content
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

skip params serializing when it is none #661

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
86 changes: 68 additions & 18 deletions core/src/types/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub struct MethodCall {
/// A Structured value that holds the parameter values to be used
/// during the invocation of the method. This member MAY be omitted.
#[serde(default = "default_params")]
#[serde(skip_serializing_if = "omit_params")]
pub params: Params,
/// An identifier established by the Client that MUST contain a String,
/// Number, or NULL value if included. If it is not included it is assumed
Expand All @@ -31,6 +32,7 @@ pub struct Notification {
/// A Structured value that holds the parameter values to be used
/// during the invocation of the method. This member MAY be omitted.
#[serde(default = "default_params")]
#[serde(skip_serializing_if = "omit_params")]
pub params: Params,
}

Expand All @@ -54,6 +56,10 @@ fn default_params() -> Params {
Params::None
}

fn omit_params(param: &Params) -> bool {
&Params::None == param
}

fn default_id() -> Id {
Id::Null
}
Expand Down Expand Up @@ -91,33 +97,77 @@ mod tests {
use serde_json;
use serde_json::Value;

let m = MethodCall {
jsonrpc: Some(Version::V2),
method: "update".to_owned(),
params: Params::Array(vec![Value::from(1), Value::from(2)]),
id: Id::Num(1),
};
struct Testcase {
name: &'static str,
call: MethodCall,
expect: &'static str,
}

let serialized = serde_json::to_string(&m).unwrap();
assert_eq!(
serialized,
r#"{"jsonrpc":"2.0","method":"update","params":[1,2],"id":1}"#
);
vec![
Testcase {
name: "normal",
call: MethodCall {
jsonrpc: Some(Version::V2),
method: "update".to_owned(),
params: Params::Array(vec![Value::from(1), Value::from(2)]),
id: Id::Num(1),
},
expect: r#"{"jsonrpc":"2.0","method":"update","params":[1,2],"id":1}"#,
},
Testcase {
name: "skip",
call: MethodCall {
jsonrpc: Some(Version::V2),
method: "update".to_owned(),
params: Params::None,
id: Id::Num(1),
},
expect: r#"{"jsonrpc":"2.0","method":"update","id":1}"#,
},
]
.iter()
.for_each(|testcase| {
let serialized = serde_json::to_string(&testcase.call).unwrap();
assert_eq!(testcase.expect, serialized, "{} failed", testcase.name);
});
}

#[test]
fn notification_serialize() {
use serde_json;
use serde_json::Value;

let n = Notification {
jsonrpc: Some(Version::V2),
method: "update".to_owned(),
params: Params::Array(vec![Value::from(1), Value::from(2)]),
};
struct Testcase {
name: &'static str,
notify: Notification,
expect: &'static str,
}

let serialized = serde_json::to_string(&n).unwrap();
assert_eq!(serialized, r#"{"jsonrpc":"2.0","method":"update","params":[1,2]}"#);
vec![
Testcase {
name: "normal",
notify: Notification {
jsonrpc: Some(Version::V2),
method: "update".to_owned(),
params: Params::Array(vec![Value::from(1), Value::from(2)]),
},
expect: r#"{"jsonrpc":"2.0","method":"update","params":[1,2]}"#,
},
Testcase {
name: "skip",
notify: Notification {
jsonrpc: Some(Version::V2),
method: "update".to_owned(),
params: Params::None,
},
expect: r#"{"jsonrpc":"2.0","method":"update"}"#,
},
]
.iter()
.for_each(|testcase| {
let serialized = serde_json::to_string(&testcase.notify).unwrap();
assert_eq!(testcase.expect, serialized, "{} failed", testcase.name);
});
}

#[test]
Expand Down