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

Omit params field from requests instead of sending null value #640

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions core/src/types/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ impl Params {
p => Err(Error::invalid_params_with_details("No parameters were expected", p)),
}
}

/// Check for None
pub fn is_none(&self) -> bool {
matches!(*self, Params::None)
}
}

impl From<Params> for Value {
Expand Down Expand Up @@ -110,4 +115,12 @@ mod tests {
let params: (u64,) = Params::Array(vec![Value::from(1)]).parse().unwrap();
assert_eq!(params, (1,));
}

#[test]
fn detect_none() {
let none = Params::None;
assert!(none.is_none());
let some = Params::Array(vec![]);
assert!(!some.is_none());
}
}
17 changes: 16 additions & 1 deletion core/src/types/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub struct MethodCall {
pub method: String,
/// 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(default = "default_params", skip_serializing_if = "Params::is_none")]
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 Down Expand Up @@ -105,6 +105,21 @@ mod tests {
);
}

#[test]
fn call_serizalize_without_params() {
selu marked this conversation as resolved.
Show resolved Hide resolved
use serde_json;

let m = MethodCall {
jsonrpc: Some(Version::V2),
method: "status".to_owned(),
params: Params::None,
id: Id::Num(1),
};

let serialized = serde_json::to_string(&m).unwrap();
assert_eq!(serialized, r#"{"jsonrpc":"2.0","method":"status","id":1}"#);
}

#[test]
fn notification_serialize() {
use serde_json;
Expand Down