Skip to content

Commit 374e165

Browse files
authored
Merge pull request #12 from firstdag/differentiate-empty-and-no-params
Differentiate calls with no params and calls with empty list param
2 parents bf800b0 + a544293 commit 374e165

File tree

2 files changed

+38
-12
lines changed

2 files changed

+38
-12
lines changed

include/jsonrpccxx/client.hpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ namespace jsonrpccxx {
2323
virtual ~JsonRpcClient() = default;
2424

2525
template <typename T>
26-
T CallMethod(const id_type &id, const std::string &name, const positional_parameter &params = {}) { return call_method(id, name, params).result.get<T>(); }
26+
T CallMethod(const id_type &id, const std::string &name) { return call_method(id, name, json::object()).result.get<T>(); }
27+
template <typename T>
28+
T CallMethod(const id_type &id, const std::string &name, const positional_parameter &params) { return call_method(id, name, params).result.get<T>(); }
2729
template <typename T>
2830
T CallMethodNamed(const id_type &id, const std::string &name, const named_parameter &params = {}) { return call_method(id, name, params).result.get<T>(); }
2931

@@ -48,6 +50,8 @@ namespace jsonrpccxx {
4850
}
4951
if (!params.empty() && !params.is_null()) {
5052
j["params"] = params;
53+
} else if (params.is_array()) {
54+
j["params"] = params;
5155
} else if (v == version::v1) {
5256
j["params"] = nullptr;
5357
}
@@ -83,4 +87,4 @@ namespace jsonrpccxx {
8387
connector.Send(j.dump());
8488
}
8589
};
86-
} // namespace jsonrpccxx
90+
} // namespace jsonrpccxx

test/client.cpp

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@ struct F {
1717
};
1818

1919
TEST_CASE_METHOD(F, "v2_method_noparams", TEST_MODULE) {
20-
c.SetResult(true);
21-
clientV2.CallMethod<json>("000-000-000", "some.method_1", {});
22-
c.VerifyMethodRequest(version::v2, "some.method_1", "000-000-000");
23-
CHECK(!has_key(c.request, "params"));
24-
2520
c.SetResult(true);
2621
clientV2.CallMethod<json>("000-000-000", "some.method_1");
2722
c.VerifyMethodRequest(version::v2, "some.method_1", "000-000-000");
@@ -30,14 +25,41 @@ TEST_CASE_METHOD(F, "v2_method_noparams", TEST_MODULE) {
3025

3126
TEST_CASE_METHOD(F, "v1_method_noparams", TEST_MODULE) {
3227
c.SetResult(true);
33-
clientV1.CallMethod<json>(37, "some.method_1", {});
28+
clientV1.CallMethod<json>(37, "some.method_1");
3429
c.VerifyMethodRequest(version::v1, "some.method_1", 37);
3530
CHECK(has_key_type(c.request, "params", json::value_t::null));
31+
}
3632

33+
TEST_CASE_METHOD(F, "v2_method_call_params_empty", TEST_MODULE) {
3734
c.SetResult(true);
38-
clientV1.CallMethod<json>(37, "some.method_1");
39-
c.VerifyMethodRequest(version::v1, "some.method_1", 37);
40-
CHECK(has_key_type(c.request, "params", json::value_t::null));
35+
clientV2.CallMethod<json>("1", "some.method_1", {});
36+
c.VerifyMethodRequest(version::v2, "some.method_1", "1");
37+
CHECK(c.request["params"].is_array());
38+
CHECK(c.request["params"].empty());
39+
CHECK(c.request["params"].dump() == "[]");
40+
41+
c.SetResult(true);
42+
clientV2.CallMethod<json>("1", "some.method_1", json::array());
43+
c.VerifyMethodRequest(version::v2, "some.method_1", "1");
44+
CHECK(c.request["params"].is_array());
45+
CHECK(c.request["params"].empty());
46+
CHECK(c.request["params"].dump() == "[]");
47+
}
48+
49+
TEST_CASE_METHOD(F, "v1_method_call_params_empty", TEST_MODULE) {
50+
c.SetResult(true);
51+
clientV1.CallMethod<json>("1", "some.method_1", {});
52+
c.VerifyMethodRequest(version::v1, "some.method_1", "1");
53+
CHECK(c.request["params"].is_array());
54+
CHECK(c.request["params"].empty());
55+
CHECK(c.request["params"].dump() == "[]");
56+
57+
c.SetResult(true);
58+
clientV1.CallMethod<json>("1", "some.method_1", json::array());
59+
c.VerifyMethodRequest(version::v1, "some.method_1", "1");
60+
CHECK(c.request["params"].is_array());
61+
CHECK(c.request["params"].empty());
62+
CHECK(c.request["params"].dump() == "[]");
4163
}
4264

4365
TEST_CASE_METHOD(F, "v2_method_call_params_byname", TEST_MODULE) {
@@ -256,4 +278,4 @@ TEST_CASE_METHOD(F, "v1_notification_call_params_byposition", TEST_MODULE) {
256278
CHECK(c.request["params"][2] == true);
257279
}
258280

259-
// TODO: test cases with return type mapping and param mapping for v1/v2 method and notification
281+
// TODO: test cases with return type mapping and param mapping for v1/v2 method and notification

0 commit comments

Comments
 (0)