From 52342882ccda3b65d6d3d91481480a8d8c8916b9 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Tue, 5 Feb 2019 13:21:54 +0000 Subject: [PATCH] Fix rpc alias(es) key constant (#375) * Fix rpc alias(es) key constant * Bump derive patch --- derive/Cargo.toml | 2 +- derive/src/rpc_attr.rs | 2 +- derive/tests/macros.rs | 32 +++++++++++++++++++++++++++++++- derive/tests/pubsub-macros.rs | 24 +++++++++++++++++++++++- 4 files changed, 56 insertions(+), 4 deletions(-) diff --git a/derive/Cargo.toml b/derive/Cargo.toml index 3e57c2eec..0d2351e4f 100644 --- a/derive/Cargo.toml +++ b/derive/Cargo.toml @@ -7,7 +7,7 @@ homepage = "https://github.com/paritytech/jsonrpc" license = "MIT" name = "jsonrpc-derive" repository = "https://github.com/paritytech/jsonrpc" -version = "10.0.1" +version = "10.0.2" [lib] proc-macro = true diff --git a/derive/src/rpc_attr.rs b/derive/src/rpc_attr.rs index cce176c0b..b4585dff8 100644 --- a/derive/src/rpc_attr.rs +++ b/derive/src/rpc_attr.rs @@ -21,7 +21,7 @@ pub enum PubSubMethodKind { const RPC_ATTR_NAME: &'static str = "rpc"; const RPC_NAME_KEY: &'static str = "name"; const SUBSCRIPTION_NAME_KEY: &'static str = "subscription"; -const ALIASES_KEY: &'static str = "aliases"; +const ALIASES_KEY: &'static str = "alias"; const PUB_SUB_ATTR_NAME: &'static str = "pubsub"; const METADATA_META_WORD: &'static str = "meta"; const SUBSCRIBE_META_WORD: &'static str = "subscribe"; diff --git a/derive/tests/macros.rs b/derive/tests/macros.rs index c0963c3f5..0c119e5af 100644 --- a/derive/tests/macros.rs +++ b/derive/tests/macros.rs @@ -22,7 +22,7 @@ pub trait Rpc { fn neg(&self, _: i64) -> Result; /// Adds two numbers and returns a result - #[rpc(name = "add")] + #[rpc(name = "add", alias("add_alias1", "add_alias2"))] fn add(&self, _: u64, _: u64) -> Result; } @@ -114,3 +114,33 @@ fn should_accept_multiple_params() { "id": 1 }"#).unwrap()); } + +#[test] +fn should_use_method_name_aliases() { + let mut io = IoHandler::new(); + let rpc = RpcImpl::default(); + io.extend_with(rpc.to_delegate()); + + // when + let req1 = r#"{"jsonrpc":"2.0","id":1,"method":"add_alias1","params":[1, 2]}"#; + let req2 = r#"{"jsonrpc":"2.0","id":1,"method":"add_alias2","params":[1, 2]}"#; + + let res1 = io.handle_request_sync(req1); + let res2 = io.handle_request_sync(req2); + + // then + let result1: Response = serde_json::from_str(&res1.unwrap()).unwrap(); + assert_eq!(result1, serde_json::from_str(r#"{ + "jsonrpc": "2.0", + "result": 3, + "id": 1 + }"#).unwrap()); + + let result2: Response = serde_json::from_str(&res2.unwrap()).unwrap(); + assert_eq!(result2, serde_json::from_str(r#"{ + "jsonrpc": "2.0", + "result": 3, + "id": 1 + }"#).unwrap()); +} + diff --git a/derive/tests/pubsub-macros.rs b/derive/tests/pubsub-macros.rs index c7f8a82b5..4f1f05e6e 100644 --- a/derive/tests/pubsub-macros.rs +++ b/derive/tests/pubsub-macros.rs @@ -23,7 +23,7 @@ pub trait Rpc { type Metadata; /// Hello subscription - #[pubsub(subscription = "hello", subscribe, name = "hello_subscribe", alias("hello_sub"))] + #[pubsub(subscription = "hello", subscribe, name = "hello_subscribe", alias("hello_alias"))] fn subscribe(&self, _: Self::Metadata, _: Subscriber, _: u32, _: Option); /// Unsubscribe from hello subscription. @@ -87,3 +87,25 @@ fn test_invalid_trailing_pubsub_params() { let result: jsonrpc_core::Response = serde_json::from_str(&res.unwrap()).unwrap(); assert_eq!(expected, result); } + +#[test] +fn test_subscribe_with_alias() { + let mut io = PubSubHandler::default(); + let rpc = RpcImpl::default(); + io.extend_with(rpc.to_delegate()); + + // when + let meta = Metadata; + let req = r#"{"jsonrpc":"2.0","id":1,"method":"hello_alias","params":[1]}"#; + let res = io.handle_request_sync(req, meta); + let expected = r#"{ + "jsonrpc": "2.0", + "result": 5, + "id": 1 + }"#; + + let expected: jsonrpc_core::Response = serde_json::from_str(expected).unwrap(); + let result: jsonrpc_core::Response = serde_json::from_str(&res.unwrap()).unwrap(); + assert_eq!(expected, result); +} +