Skip to content

Commit 3d7c1c6

Browse files
committed
Handle missing JSON-RPC params and add test
Add #[serde(default)] to JsonRpcCall.params so requests that omit the params field deserialize to Value::Null instead of failing. Also add a unit test verifying that a JSON-RPC request without params is parsed as a Single JsonRpcRequest and that call.params == Value::Null. This makes the JSON-RPC input handling more robust for requests that don't include params.
1 parent 58d5ec2 commit 3d7c1c6

1 file changed

Lines changed: 15 additions & 0 deletions

File tree

core/apps/dynode/src/jsonrpc_types.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use serde_json::Value;
66
pub struct JsonRpcCall {
77
pub jsonrpc: String,
88
pub method: String,
9+
#[serde(default)]
910
pub params: Value,
1011
pub id: u64,
1112
}
@@ -211,6 +212,20 @@ mod tests {
211212
assert_eq!(key, "example.com:POST:/rpc:eth_blockNumber");
212213
}
213214

215+
#[test]
216+
fn test_request_parsing_without_params() {
217+
let body = br#"{"jsonrpc":"2.0","method":"getSlot","id":1}"#.to_vec();
218+
let request_type = RequestType::from_request("POST", "/solana".to_string(), body);
219+
220+
match request_type {
221+
RequestType::JsonRpc(JsonRpcRequest::Single(call)) => {
222+
assert_eq!(call.method, "getSlot");
223+
assert_eq!(call.params, Value::Null);
224+
}
225+
_ => panic!("Expected JSON-RPC request"),
226+
}
227+
}
228+
214229
#[test]
215230
fn test_batch_request_parsing() {
216231
let batch_json = r#"[

0 commit comments

Comments
 (0)