Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(types): add lenient feature #1446

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions jsonrpsee/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ http-client = ["jsonrpsee-http-client", "jsonrpsee-types", "jsonrpsee-core/clien
wasm-client = ["jsonrpsee-wasm-client", "jsonrpsee-types", "jsonrpsee-core/client"]
ws-client = ["jsonrpsee-ws-client", "jsonrpsee-types", "jsonrpsee-core/client"]
macros = ["jsonrpsee-proc-macros", "jsonrpsee-types", "tracing"]
lenient = ["jsonrpsee-types/lenient"]

client = ["http-client", "ws-client", "wasm-client", "client-ws-transport-tls", "client-web-transport", "async-client", "async-wasm-client", "client-core"]
client-core = ["jsonrpsee-core/client"]
Expand Down
1 change: 1 addition & 0 deletions jsonrpsee/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
//! - **`client-ws-transport`** - Enables `ws` transport with TLS.
//! - **`client-ws-transport-no-tls`** - Enables `ws` transport without TLS.
//! - **`client-web-transport`** - Enables `websys` transport.
//! - **`lenient`** - Enables lenient request parsing, allowing requests without a `jsonrpc` field.

#![warn(missing_docs, missing_debug_implementations, missing_copy_implementations, unreachable_pub)]
#![cfg_attr(not(test), warn(unused_crate_dependencies))]
Expand Down
3 changes: 3 additions & 0 deletions types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@ serde = { version = "1", default-features = false, features = ["derive"] }
serde_json = { version = "1", default-features = false, features = ["alloc", "raw_value", "std"] }
thiserror = "1.0"
http = "1"

[features]
lenient = []
12 changes: 12 additions & 0 deletions types/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ use serde_json::value::RawValue;
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Request<'a> {
/// JSON-RPC version.
#[cfg_attr(feature = "lenient", serde(default))]
pub jsonrpc: TwoPointZero,
/// Request ID
#[serde(borrow)]
Expand Down Expand Up @@ -101,6 +102,7 @@ pub struct InvalidRequest<'a> {
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Notification<'a, T> {
/// JSON-RPC version.
#[cfg_attr(feature = "lenient", serde(default))]
pub jsonrpc: TwoPointZero,
/// Name of the method to be invoked.
#[serde(borrow)]
Expand Down Expand Up @@ -240,6 +242,16 @@ mod test {
assert!(serde_json::from_str::<Request>(ser).is_err());
}

#[test]
fn deserialize_missing_jsonrpc_field() {
let ser = r#"{"method":"say_hello","params":[],"id":1}"#;
#[cfg(feature = "lenient")]
assert!(serde_json::from_str::<Request>(ser).is_ok());

#[cfg(not(feature = "lenient"))]
assert!(serde_json::from_str::<Request>(ser).is_err());
}

#[test]
fn deserialize_invalid_request() {
let s = r#"{"id":120,"method":"my_method","params":["foo", "bar"],"extra_field":[]}"#;
Expand Down
Loading