From 34356a95370e47a2651f48a3631c2653ab01aeb6 Mon Sep 17 00:00:00 2001 From: Riccardo Casatta Date: Wed, 8 Nov 2023 22:42:56 +0100 Subject: [PATCH] Accept also None or Objects as params according to the jsonrpc spec params can be missing, or an object too --- README.md | 2 +- fuzz/fuzz_targets/minreq_http.rs | 2 +- fuzz/fuzz_targets/simple_http.rs | 2 +- integration_test/src/main.rs | 2 +- src/client.rs | 8 ++++---- src/http/simple_http.rs | 4 ++-- src/lib.rs | 2 +- src/simple_tcp.rs | 2 +- src/simple_uds.rs | 2 +- 9 files changed, 13 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index ece3c413..1b6ebe98 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ fn client(url: &str, user: &str, pass: &str) -> Result(&self, method: &'a str, params: &'a [Box]) -> Request<'a> { + pub fn build_request<'a>(&self, method: &'a str, params: Option<&'a RawValue>) -> Request<'a> { let nonce = self.nonce.fetch_add(1, atomic::Ordering::Relaxed); Request { method, @@ -115,7 +115,7 @@ impl Client { pub fn call serde::de::Deserialize<'a>>( &self, method: &str, - args: &[Box], + args: Option<&RawValue>, ) -> Result { let request = self.build_request(method, args); let id = request.id.clone(); @@ -224,9 +224,9 @@ mod tests { fn sanity() { let client = Client::with_transport(DummyTransport); assert_eq!(client.nonce.load(sync::atomic::Ordering::Relaxed), 1); - let req1 = client.build_request("test", &[]); + let req1 = client.build_request("test", None); assert_eq!(client.nonce.load(sync::atomic::Ordering::Relaxed), 2); - let req2 = client.build_request("test", &[]); + let req2 = client.build_request("test", None); assert_eq!(client.nonce.load(sync::atomic::Ordering::Relaxed), 3); assert!(req1.id != req2.id); } diff --git a/src/http/simple_http.rs b/src/http/simple_http.rs index 5b216710..e143a62f 100644 --- a/src/http/simple_http.rs +++ b/src/http/simple_http.rs @@ -818,11 +818,11 @@ mod tests { let port = rx.recv().unwrap(); let client = Client::simple_http(format!("localhost:{}", port).as_str(), None, None).unwrap(); - let request = client.build_request("test_request", &[]); + let request = client.build_request("test_request", None); let result = client.send_request(request).unwrap(); assert_eq!(result.id, Value::Number(Number::from(0))); thread::sleep(Duration::from_secs(1)); - let request = client.build_request("test_request2", &[]); + let request = client.build_request("test_request2", None); let result2 = client.send_request(request) .expect("This second request should not be an Err like `Err(Transport(HttpResponseTooShort { actual: 0, needed: 12 }))`"); assert_eq!(result2.id, Value::Number(Number::from(1))); diff --git a/src/lib.rs b/src/lib.rs index 73d19726..90888ed6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -70,7 +70,7 @@ pub struct Request<'a> { /// The name of the RPC call. pub method: &'a str, /// Parameters to the RPC call. - pub params: &'a [Box], + pub params: Option<&'a RawValue>, /// Identifier for this request, which should appear in the response. pub id: serde_json::Value, /// jsonrpc field, MUST be "2.0". diff --git a/src/simple_tcp.rs b/src/simple_tcp.rs index 27130e4e..f36600b0 100644 --- a/src/simple_tcp.rs +++ b/src/simple_tcp.rs @@ -134,7 +134,7 @@ mod tests { let addr = server.local_addr().unwrap(); let dummy_req = Request { method: "arandommethod", - params: &[], + params: None, id: serde_json::Value::Number(4242242.into()), jsonrpc: Some("2.0"), }; diff --git a/src/simple_uds.rs b/src/simple_uds.rs index a1afdf58..d05c48fd 100644 --- a/src/simple_uds.rs +++ b/src/simple_uds.rs @@ -137,7 +137,7 @@ mod tests { let server = UnixListener::bind(&socket_path).unwrap(); let dummy_req = Request { method: "getinfo", - params: &[], + params: None, id: serde_json::Value::Number(111.into()), jsonrpc: Some("2.0"), };