diff --git a/lib/jsonrpc/jsonrpc_server.c b/lib/jsonrpc/jsonrpc_server.c index 52cd4605f..b0a34caef 100644 --- a/lib/jsonrpc/jsonrpc_server.c +++ b/lib/jsonrpc/jsonrpc_server.c @@ -62,6 +62,7 @@ static void parse_single_request(struct spdk_jsonrpc_request *request, struct spdk_json_val *values) { struct jsonrpc_request req = {}; + const struct spdk_json_val *params = NULL; if (spdk_json_decode_object(values, jsonrpc_request_decoders, SPDK_COUNTOF(jsonrpc_request_decoders), @@ -89,13 +90,17 @@ parse_single_request(struct spdk_jsonrpc_request *request, struct spdk_json_val } if (req.params) { - if (req.params->type != SPDK_JSON_VAL_ARRAY_BEGIN && - req.params->type != SPDK_JSON_VAL_OBJECT_BEGIN) { - goto invalid; + /* null json value is as if there were no parameters */ + if (req.params->type != SPDK_JSON_VAL_NULL) { + if (req.params->type != SPDK_JSON_VAL_ARRAY_BEGIN && + req.params->type != SPDK_JSON_VAL_OBJECT_BEGIN) { + goto invalid; + } + params = req.params; } } - spdk_jsonrpc_server_handle_request(request, req.method, req.params); + spdk_jsonrpc_server_handle_request(request, req.method, params); return; invalid: diff --git a/test/rpc_client/rpc_client_test.c b/test/rpc_client/rpc_client_test.c index 228d6754d..5c056bd6e 100644 --- a/test/rpc_client/rpc_client_test.c +++ b/test/rpc_client/rpc_client_test.c @@ -141,6 +141,61 @@ out: return rc; } +static int +spdk_jsonrpc_client_check_null_params_method(struct spdk_jsonrpc_client *client) +{ + int rc; + bool res = false; + struct spdk_jsonrpc_client_response *json_resp = NULL; + struct spdk_json_write_ctx *w; + struct spdk_jsonrpc_client_request *request; + + request = spdk_jsonrpc_client_create_request(); + if (request == NULL) { + return -ENOMEM; + } + + w = spdk_jsonrpc_begin_request(request, 1, "test_null_params"); + spdk_json_write_name(w, "params"); + spdk_json_write_null(w); + spdk_jsonrpc_end_request(request, w); + spdk_jsonrpc_client_send_request(client, request); + + rc = _rpc_client_wait_for_response(client); + if (rc <= 0) { + goto out; + } + + json_resp = spdk_jsonrpc_client_get_response(client); + if (json_resp == NULL) { + SPDK_ERRLOG("spdk_jsonrpc_client_get_response() failed\n"); + rc = -1; + goto out; + + } + + /* Check for error response */ + if (json_resp->error != NULL) { + SPDK_ERRLOG("Unexpected error response\n"); + rc = -1; + goto out; + } + + assert(json_resp->result); + + if (spdk_json_decode_bool(json_resp->result, &res) != 0 || res != true) { + SPDK_ERRLOG("Response is not a boolean or it is not 'true'\n"); + rc = -EINVAL; + goto out; + } else { + rc = 0; + } + +out: + spdk_jsonrpc_client_free_response(json_resp); + return rc; +} + static void rpc_test_method_startup(struct spdk_jsonrpc_request *request, const struct spdk_json_val *params) { @@ -157,6 +212,24 @@ rpc_test_method_runtime(struct spdk_jsonrpc_request *request, const struct spdk_ } SPDK_RPC_REGISTER("test_method_runtime", rpc_test_method_runtime, SPDK_RPC_RUNTIME) +static void +rpc_test_method_null_params(struct spdk_jsonrpc_request *request, + const struct spdk_json_val *params) +{ + struct spdk_json_write_ctx *w; + + if (params != NULL) { + spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, + "rpc_test_method_null_params(): Parameters are not NULL"); + return; + } + w = spdk_jsonrpc_begin_result(request); + assert(w != NULL); + spdk_json_write_bool(w, true); + spdk_jsonrpc_end_result(request, w); +} +SPDK_RPC_REGISTER("test_null_params", rpc_test_method_null_params, SPDK_RPC_RUNTIME) + static bool g_conn_close_detected; static void @@ -325,6 +398,12 @@ rpc_client_th(void *arg) goto out; } + rc = spdk_jsonrpc_client_check_null_params_method(client); + if (rc) { + fprintf(stderr, "spdk_jsonrpc_client_null_params_method() failed: rc=%d errno=%d\n", rc, errno); + goto out; + } + rc = spdk_jsonrpc_client_hook_conn_close(client); if (rc) { fprintf(stderr, "spdk_jsonrpc_client_hook_conn_close() failed: rc=%d errno=%d\n", rc, errno);