Skip to content

Differentiate calls with no params and calls with empty list param #12

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

Merged
Merged
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
8 changes: 6 additions & 2 deletions include/jsonrpccxx/client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ namespace jsonrpccxx {
virtual ~JsonRpcClient() = default;

template <typename T>
T CallMethod(const id_type &id, const std::string &name, const positional_parameter &params = {}) { return call_method(id, name, params).result.get<T>(); }
T CallMethod(const id_type &id, const std::string &name) { return call_method(id, name, json::object()).result.get<T>(); }
template <typename T>
T CallMethod(const id_type &id, const std::string &name, const positional_parameter &params) { return call_method(id, name, params).result.get<T>(); }
template <typename T>
T CallMethodNamed(const id_type &id, const std::string &name, const named_parameter &params = {}) { return call_method(id, name, params).result.get<T>(); }

Expand All @@ -48,6 +50,8 @@ namespace jsonrpccxx {
}
if (!params.empty() && !params.is_null()) {
j["params"] = params;
} else if (params.is_array()) {
j["params"] = params;
} else if (v == version::v1) {
j["params"] = nullptr;
}
Expand Down Expand Up @@ -83,4 +87,4 @@ namespace jsonrpccxx {
connector.Send(j.dump());
}
};
} // namespace jsonrpccxx
} // namespace jsonrpccxx
42 changes: 32 additions & 10 deletions test/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ struct F {
};

TEST_CASE_METHOD(F, "v2_method_noparams", TEST_MODULE) {
c.SetResult(true);
clientV2.CallMethod<json>("000-000-000", "some.method_1", {});
c.VerifyMethodRequest(version::v2, "some.method_1", "000-000-000");
CHECK(!has_key(c.request, "params"));

c.SetResult(true);
clientV2.CallMethod<json>("000-000-000", "some.method_1");
c.VerifyMethodRequest(version::v2, "some.method_1", "000-000-000");
Expand All @@ -30,14 +25,41 @@ TEST_CASE_METHOD(F, "v2_method_noparams", TEST_MODULE) {

TEST_CASE_METHOD(F, "v1_method_noparams", TEST_MODULE) {
c.SetResult(true);
clientV1.CallMethod<json>(37, "some.method_1", {});
clientV1.CallMethod<json>(37, "some.method_1");
c.VerifyMethodRequest(version::v1, "some.method_1", 37);
CHECK(has_key_type(c.request, "params", json::value_t::null));
}

TEST_CASE_METHOD(F, "v2_method_call_params_empty", TEST_MODULE) {
c.SetResult(true);
clientV1.CallMethod<json>(37, "some.method_1");
c.VerifyMethodRequest(version::v1, "some.method_1", 37);
CHECK(has_key_type(c.request, "params", json::value_t::null));
clientV2.CallMethod<json>("1", "some.method_1", {});
c.VerifyMethodRequest(version::v2, "some.method_1", "1");
CHECK(c.request["params"].is_array());
CHECK(c.request["params"].empty());
CHECK(c.request["params"].dump() == "[]");

c.SetResult(true);
clientV2.CallMethod<json>("1", "some.method_1", json::array());
c.VerifyMethodRequest(version::v2, "some.method_1", "1");
CHECK(c.request["params"].is_array());
CHECK(c.request["params"].empty());
CHECK(c.request["params"].dump() == "[]");
}

TEST_CASE_METHOD(F, "v1_method_call_params_empty", TEST_MODULE) {
c.SetResult(true);
clientV1.CallMethod<json>("1", "some.method_1", {});
c.VerifyMethodRequest(version::v1, "some.method_1", "1");
CHECK(c.request["params"].is_array());
CHECK(c.request["params"].empty());
CHECK(c.request["params"].dump() == "[]");

c.SetResult(true);
clientV1.CallMethod<json>("1", "some.method_1", json::array());
c.VerifyMethodRequest(version::v1, "some.method_1", "1");
CHECK(c.request["params"].is_array());
CHECK(c.request["params"].empty());
CHECK(c.request["params"].dump() == "[]");
}

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

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