Skip to content

Commit

Permalink
close paritytech#1142; feat: jsonrpc parameter optional for notification
Browse files Browse the repository at this point in the history
  • Loading branch information
chikko80 committed Jun 17, 2023
1 parent 5ae1083 commit e4e82c1
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions types/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub struct InvalidRequest<'a> {
#[derive(Serialize, Deserialize, Debug)]
pub struct Notification<'a, T> {
/// JSON-RPC version.
pub jsonrpc: TwoPointZero,
pub jsonrpc: Option<TwoPointZero>,
/// Name of the method to be invoked.
#[serde(borrow)]
pub method: Cow<'a, str>,
Expand All @@ -80,8 +80,8 @@ pub struct Notification<'a, T> {

impl<'a, T> Notification<'a, T> {
/// Create a new [`Notification`].
pub fn new(method: Cow<'a, str>, params: T) -> Self {
Self { jsonrpc: TwoPointZero, method, params }
pub fn new(jsonrpc: Option<TwoPointZero>, method: Cow<'a, str>, params: T) -> Self {
Self { jsonrpc, method, params }
}
}

Expand Down Expand Up @@ -192,15 +192,23 @@ mod test {
let ser = r#"{"jsonrpc":"2.0","method":"say_hello","params":[]}"#;
let dsr: Notification<&RawValue> = serde_json::from_str(ser).unwrap();
assert_eq!(dsr.method, "say_hello");
assert_eq!(dsr.jsonrpc, TwoPointZero);
assert_eq!(dsr.jsonrpc, Some(TwoPointZero));
}

#[test]
fn deserialize_valid_notif_escaped_method() {
let ser = r#"{"jsonrpc":"2.0","method":"\"m\"","params":[]}"#;
let dsr: Notification<&RawValue> = serde_json::from_str(ser).unwrap();
assert_eq!(dsr.method, "\"m\"");
assert_eq!(dsr.jsonrpc, TwoPointZero);
assert_eq!(dsr.jsonrpc, Some(TwoPointZero));
}

#[test]
fn deserialize_valid_jsonrpc_none() {
let ser = r#"{"method":"\"m\"","params":[]}"#;
let dsr: Notification<&RawValue> = serde_json::from_str(ser).unwrap();
assert_eq!(dsr.method, "\"m\"");
assert_eq!(dsr.jsonrpc, None);
}

#[test]
Expand Down

0 comments on commit e4e82c1

Please sign in to comment.