Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Commit

Permalink
bug #55: prevent request from serializing ZSTs as null (#57)
Browse files Browse the repository at this point in the history
* bug #55: prevent request from serializing ZSTs as null

* chore: cargo fmt

Co-authored-by: Georgios Konstantopoulos <[email protected]>
  • Loading branch information
prestwich and gakonst authored Aug 18, 2020
1 parent 516b431 commit ca2ec0a
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion ethers-providers/src/transports/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,17 @@ impl fmt::Display for JsonRpcError {
}
}

fn is_zst<T>(_t: &T) -> bool {
std::mem::size_of::<T>() == 0
}

#[derive(Serialize, Deserialize, Debug)]
/// A JSON-RPC request
pub struct Request<'a, T> {
id: u64,
jsonrpc: &'a str,
method: &'a str,
#[serde(skip_serializing_if = "is_zst")]
params: T,
}

Expand Down Expand Up @@ -76,10 +81,25 @@ mod tests {
use super::*;

#[test]
fn response() {
fn deser_response() {
let response: Response<u64> =
serde_json::from_str(r#"{"jsonrpc": "2.0", "result": 19, "id": 1}"#).unwrap();
assert_eq!(response.id, 1);
assert_eq!(response.data.into_result().unwrap(), 19);
}

#[test]
fn ser_request() {
let request: Request<()> = Request::new(300, "method_name", ());
assert_eq!(
&serde_json::to_string(&request).unwrap(),
r#"{"id":300,"jsonrpc":"2.0","method":"method_name"}"#
);

let request: Request<u32> = Request::new(300, "method_name", 1);
assert_eq!(
&serde_json::to_string(&request).unwrap(),
r#"{"id":300,"jsonrpc":"2.0","method":"method_name","params":1}"#
);
}
}

0 comments on commit ca2ec0a

Please sign in to comment.