Browse Source

jsonrpc: help, even for a single item, should be in an array.

This is what we do for every other can-be-single JSON API.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
fix-test_pay_direct-flake
Rusty Russell 6 years ago
parent
commit
da355284de
  1. 1
      CHANGELOG.md
  2. 38
      lightningd/jsonrpc.c
  3. 8
      tests/test_misc.py

1
CHANGELOG.md

@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- The `short_channel_id` separator has been changed to be `x` to match the specification.
- JSON API: `listpeers` now includes `funding_allocation_msat`, which returns a map of the amounts initially funded to the channel by each peer, indexed by channel id.
- `option_data_loss_protect` is now enabled by default.
- JSON API: `help` with a `command` argument gives a JSON array, like other commands.
### Deprecated

38
lightningd/jsonrpc.c

@ -329,6 +329,17 @@ static void json_add_help_command(struct command *cmd,
}
static const struct json_command *find_command(struct json_command **commands,
const char *buffer,
const jsmntok_t *cmdtok)
{
for (size_t i = 0; i < tal_count(commands); i++) {
if (json_tok_streq(buffer, cmdtok, commands[i]->name))
return commands[i];
}
return NULL;
}
static struct command_result *json_help(struct command *cmd,
const char *buffer,
const jsmntok_t *obj UNNEEDED,
@ -337,6 +348,7 @@ static struct command_result *json_help(struct command *cmd,
struct json_stream *response;
const jsmntok_t *cmdtok;
struct json_command **commands = cmd->ld->jsonrpc->commands;
const struct json_command *one_cmd;
if (!param(cmd, buffer, params,
p_opt("command", param_tok, &cmdtok),
@ -344,29 +356,25 @@ static struct command_result *json_help(struct command *cmd,
return command_param_failed();
if (cmdtok) {
for (size_t i = 0; i < tal_count(commands); i++) {
if (json_tok_streq(buffer, cmdtok, commands[i]->name)) {
response = json_stream_success(cmd);
json_add_help_command(cmd, response, commands[i]);
goto done;
}
}
return command_fail(cmd, JSONRPC2_METHOD_NOT_FOUND,
"Unknown command '%.*s'",
cmdtok->end - cmdtok->start,
buffer + cmdtok->start);
}
one_cmd = find_command(commands, buffer, cmdtok);
if (!one_cmd)
return command_fail(cmd, JSONRPC2_METHOD_NOT_FOUND,
"Unknown command '%.*s'",
cmdtok->end - cmdtok->start,
buffer + cmdtok->start);
} else
one_cmd = NULL;
response = json_stream_success(cmd);
json_object_start(response, NULL);
json_array_start(response, "help");
for (size_t i=0; i<tal_count(commands); i++) {
json_add_help_command(cmd, response, commands[i]);
for (size_t i = 0; i < tal_count(commands); i++) {
if (!one_cmd || one_cmd == commands[i])
json_add_help_command(cmd, response, commands[i]);
}
json_array_end(response);
json_object_end(response);
done:
return command_success(cmd, response);
}

8
tests/test_misc.py

@ -731,7 +731,7 @@ def test_cli(node_factory):
'-J',
'help', 'command=help']).decode('utf-8')
j, _ = json.JSONDecoder().raw_decode(out)
assert 'help [command]' in j['verbose']
assert 'help [command]' in j['help'][0]['verbose']
# Test keyword input (forced)
out = subprocess.check_output(['cli/lightning-cli',
@ -740,7 +740,7 @@ def test_cli(node_factory):
'-J', '-k',
'help', 'command=help']).decode('utf-8')
j, _ = json.JSONDecoder().raw_decode(out)
assert 'help [command]' in j['verbose']
assert 'help [command]' in j['help'][0]['verbose']
# Test ordered input (autodetect)
out = subprocess.check_output(['cli/lightning-cli',
@ -749,7 +749,7 @@ def test_cli(node_factory):
'-J',
'help', 'help']).decode('utf-8')
j, _ = json.JSONDecoder().raw_decode(out)
assert 'help [command]' in j['verbose']
assert 'help [command]' in j['help'][0]['verbose']
# Test ordered input (forced)
out = subprocess.check_output(['cli/lightning-cli',
@ -758,7 +758,7 @@ def test_cli(node_factory):
'-J', '-o',
'help', 'help']).decode('utf-8')
j, _ = json.JSONDecoder().raw_decode(out)
assert 'help [command]' in j['verbose']
assert 'help [command]' in j['help'][0]['verbose']
# Test missing parameters.
try:

Loading…
Cancel
Save