Skip to content

Commit 5234288

Browse files
ascjonestomusdrw
authored andcommitted
Fix rpc alias(es) key constant (#375)
* Fix rpc alias(es) key constant * Bump derive patch
1 parent 5e82ae0 commit 5234288

File tree

4 files changed

+56
-4
lines changed

4 files changed

+56
-4
lines changed

derive/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ homepage = "https://github.com/paritytech/jsonrpc"
77
license = "MIT"
88
name = "jsonrpc-derive"
99
repository = "https://github.com/paritytech/jsonrpc"
10-
version = "10.0.1"
10+
version = "10.0.2"
1111

1212
[lib]
1313
proc-macro = true

derive/src/rpc_attr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub enum PubSubMethodKind {
2121
const RPC_ATTR_NAME: &'static str = "rpc";
2222
const RPC_NAME_KEY: &'static str = "name";
2323
const SUBSCRIPTION_NAME_KEY: &'static str = "subscription";
24-
const ALIASES_KEY: &'static str = "aliases";
24+
const ALIASES_KEY: &'static str = "alias";
2525
const PUB_SUB_ATTR_NAME: &'static str = "pubsub";
2626
const METADATA_META_WORD: &'static str = "meta";
2727
const SUBSCRIBE_META_WORD: &'static str = "subscribe";

derive/tests/macros.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub trait Rpc {
2222
fn neg(&self, _: i64) -> Result<i64>;
2323

2424
/// Adds two numbers and returns a result
25-
#[rpc(name = "add")]
25+
#[rpc(name = "add", alias("add_alias1", "add_alias2"))]
2626
fn add(&self, _: u64, _: u64) -> Result<u64>;
2727
}
2828

@@ -114,3 +114,33 @@ fn should_accept_multiple_params() {
114114
"id": 1
115115
}"#).unwrap());
116116
}
117+
118+
#[test]
119+
fn should_use_method_name_aliases() {
120+
let mut io = IoHandler::new();
121+
let rpc = RpcImpl::default();
122+
io.extend_with(rpc.to_delegate());
123+
124+
// when
125+
let req1 = r#"{"jsonrpc":"2.0","id":1,"method":"add_alias1","params":[1, 2]}"#;
126+
let req2 = r#"{"jsonrpc":"2.0","id":1,"method":"add_alias2","params":[1, 2]}"#;
127+
128+
let res1 = io.handle_request_sync(req1);
129+
let res2 = io.handle_request_sync(req2);
130+
131+
// then
132+
let result1: Response = serde_json::from_str(&res1.unwrap()).unwrap();
133+
assert_eq!(result1, serde_json::from_str(r#"{
134+
"jsonrpc": "2.0",
135+
"result": 3,
136+
"id": 1
137+
}"#).unwrap());
138+
139+
let result2: Response = serde_json::from_str(&res2.unwrap()).unwrap();
140+
assert_eq!(result2, serde_json::from_str(r#"{
141+
"jsonrpc": "2.0",
142+
"result": 3,
143+
"id": 1
144+
}"#).unwrap());
145+
}
146+

derive/tests/pubsub-macros.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub trait Rpc {
2323
type Metadata;
2424

2525
/// Hello subscription
26-
#[pubsub(subscription = "hello", subscribe, name = "hello_subscribe", alias("hello_sub"))]
26+
#[pubsub(subscription = "hello", subscribe, name = "hello_subscribe", alias("hello_alias"))]
2727
fn subscribe(&self, _: Self::Metadata, _: Subscriber<String>, _: u32, _: Option<u64>);
2828

2929
/// Unsubscribe from hello subscription.
@@ -87,3 +87,25 @@ fn test_invalid_trailing_pubsub_params() {
8787
let result: jsonrpc_core::Response = serde_json::from_str(&res.unwrap()).unwrap();
8888
assert_eq!(expected, result);
8989
}
90+
91+
#[test]
92+
fn test_subscribe_with_alias() {
93+
let mut io = PubSubHandler::default();
94+
let rpc = RpcImpl::default();
95+
io.extend_with(rpc.to_delegate());
96+
97+
// when
98+
let meta = Metadata;
99+
let req = r#"{"jsonrpc":"2.0","id":1,"method":"hello_alias","params":[1]}"#;
100+
let res = io.handle_request_sync(req, meta);
101+
let expected = r#"{
102+
"jsonrpc": "2.0",
103+
"result": 5,
104+
"id": 1
105+
}"#;
106+
107+
let expected: jsonrpc_core::Response = serde_json::from_str(expected).unwrap();
108+
let result: jsonrpc_core::Response = serde_json::from_str(&res.unwrap()).unwrap();
109+
assert_eq!(expected, result);
110+
}
111+

0 commit comments

Comments
 (0)