Skip to content

Commit

Permalink
fix: use serde_json Value for req first
Browse files Browse the repository at this point in the history
  • Loading branch information
whalelephant committed Jan 9, 2022
1 parent ca18b07 commit 348ab33
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ features = [ "http1", "runtime", "server", "tcp" ]

[dependencies.json-rpc-types]
git = "https://github.com/whalelephant/json-rpc-types"
branch = "deserialise-id-with-map"
branch = "deserialize-version-with-str"
version = "1.0"

[dependencies.jsonrpc-core]
Expand Down
36 changes: 25 additions & 11 deletions src/rpc/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,18 +141,32 @@ async fn handle_rpc<N: Network, E: Environment>(
};

// Deserialize the JSON-RPC request.
let req: jrt::Request<Params> = match serde_json::from_slice(&data) {
Ok(req) => req,
Err(_) => {
let resp = jrt::Response::<(), ()>::error(
jrt::Version::V2,
jrt::Error::with_custom_msg(jrt::ErrorCode::ParseError, "Couldn't parse the RPC body"),
None,
);
let body = serde_json::to_vec(&resp).unwrap_or_default();

return Ok(hyper::Response::new(body.into()));
// First deserialize into serde_json::Value - a workaround https://github.com/serde-rs/json/issues/505
// for https://github.com/AleoHQ/snarkOS/issues/1369
let req = if let Ok(req_data) = serde_json::from_slice::<serde_json::Value>(&data) {
match serde_json::from_value::<jrt::Request<Params>>(req_data) {
Ok(req) => req,
Err(e) => {
debug!("Request Parsing Error: {:?}", e);
let resp = jrt::Response::<(), ()>::error(
jrt::Version::V2,
jrt::Error::with_custom_msg(jrt::ErrorCode::ParseError, "Couldn't parse the RPC body"),
None,
);
let body = serde_json::to_vec(&resp).unwrap_or_default();

return Ok(hyper::Response::new(body.into()));
}
}
} else {
let resp = jrt::Response::<(), ()>::error(
jrt::Version::V2,
jrt::Error::with_custom_msg(jrt::ErrorCode::ParseError, "Couldn't parse the RPC body into Value"),
None,
);
let body = serde_json::to_vec(&resp).unwrap_or_default();

return Ok(hyper::Response::new(body.into()));
};

debug!("Received '{}' RPC request from {}: {:?}", &*req.method, caller, headers);
Expand Down

0 comments on commit 348ab33

Please sign in to comment.