Skip to content

Commit a205625

Browse files
committed
Move normalizeParameter to private method
1 parent e6735a5 commit a205625

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

include/jsonrpccxx/dispatcher.hpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ namespace jsonrpccxx {
5151
throw JsonRpcException(-32601, "method not found: " + name);
5252
}
5353
try {
54-
return method->second(normalizeParameter(name, params));
54+
return method->second(normalize_parameter(name, params));
5555
} catch (json::type_error &e) {
5656
throw JsonRpcException(-32602, "invalid parameter: " + std::string(e.what()));
5757
} catch (JsonRpcException &e) {
@@ -65,15 +65,21 @@ namespace jsonrpccxx {
6565
throw JsonRpcException(-32601, "notification not found: " + name);
6666
}
6767
try {
68-
notification->second(normalizeParameter(name, params));
68+
notification->second(normalize_parameter(name, params));
6969
} catch (json::type_error &e) {
7070
throw JsonRpcException(-32602, "invalid parameter: " + std::string(e.what()));
7171
} catch (JsonRpcException &e) {
7272
throw process_type_error(name, e);
7373
}
7474
}
7575

76-
inline json normalizeParameter(const std::string &name, const json &params) {
76+
private:
77+
std::map<std::string, MethodHandle> methods;
78+
std::map<std::string, NotificationHandle> notifications;
79+
std::map<std::string, NamedParamMapping> mapping;
80+
81+
inline bool contains(const std::string &name) { return (methods.find(name) != methods.end() || notifications.find(name) != notifications.end()); }
82+
inline json normalize_parameter(const std::string &name, const json &params) {
7783
if (params.type() == json::value_t::array) {
7884
return params;
7985
} else if (params.type() == json::value_t::object) {
@@ -91,11 +97,5 @@ namespace jsonrpccxx {
9197
}
9298
throw JsonRpcException(-32600, "invalid request: params field must be an array, object");
9399
}
94-
95-
private:
96-
std::map<std::string, MethodHandle> methods;
97-
std::map<std::string, NotificationHandle> notifications;
98-
std::map<std::string, NamedParamMapping> mapping;
99-
inline bool contains(const std::string &name) { return (methods.find(name) != methods.end() || notifications.find(name) != notifications.end()); }
100100
};
101101
} // namespace jsonrpccxx

test/server.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ TEST_CASE_METHOD(Server2, "v2_malformed_requests", TEST_MODULE) {
2727

2828
connector.SendRawRequest("dfasdf");
2929
connector.VerifyMethodError(-32700, "parse error", nullptr);
30+
connector.SendRawRequest("true");
31+
connector.VerifyMethodError(-32600, "invalid request: expected array or object", nullptr);
3032

3133
connector.SendRequest({{"id", true}, {"method", name}, {"params", params}, {"jsonrpc", "2.0"}});
3234
connector.VerifyMethodError(-32600, "invalid request: id field must be a number, string or null", nullptr);
@@ -115,6 +117,7 @@ TEST_CASE_METHOD(Server2, "v2_invocations", TEST_MODULE) {
115117
REQUIRE(!server.Add("dirty_method2", GetHandle(&TestServer::dirty_method2, t), {"a", "b"}));
116118
REQUIRE(!server.Add("rpc.something", GetHandle(&TestServer::dirty_method2, t), {"a", "b"}));
117119
REQUIRE(!server.Add("rpc.", GetHandle(&TestServer::dirty_method2, t), {"a", "b"}));
120+
REQUIRE(!server.Add("rpc.somenotification", GetHandle(&TestServer::dirty_notification, t), {"a", "b"}));
118121
REQUIRE(server.Add("rpc", GetHandle(&TestServer::dirty_method2, t), {"a", "b"}));
119122

120123
connector.CallMethod(1, "add_function", {{"a", 3}, {"b", 4}});

0 commit comments

Comments
 (0)