Fix ledger_data to return an empty list: (#4398)

Change `ledger_data` to return an empty list when all entries are
filtered out.

When the `type` field is specified for the `ledger_data` method, it is
possible that no objects of the specified type are found. This can even
occur if those objects exist, but not in the section that the server
checked while serving your request. Previously, the `state` field of the
response has the value `null`, instead of an empty array `[]`. By
changing this to an empty array, the response is the same data type so
that clients can handle it consistently.

For example, in Python, `for entry in state` should now work correctly.
It would raise an exception if `state` is `null` (or `None`). 

This could break client code that explicitly checks for null. However,
this fix aligns the response with the documentation, where the `state`
field is an array.

Fix #4392.
This commit is contained in:
drlongle
2023-03-30 20:59:10 +02:00
committed by GitHub
parent 5ebcaf0a6c
commit 2f3f6dcb03
2 changed files with 32 additions and 9 deletions

View File

@@ -94,6 +94,10 @@ doLedgerData(RPC::JsonContext& context)
return jvResult;
}
Json::Value& nodes = jvResult[jss::state];
if (nodes.type() == Json::nullValue)
{
nodes = Json::Value(Json::arrayValue);
}
auto e = lpLedger->sles.end();
for (auto i = lpLedger->sles.upper_bound(key); i != e; ++i)

View File

@@ -314,6 +314,34 @@ public:
auto const USD = gw["USD"];
env.fund(XRP(100000), gw);
auto makeRequest = [&env](Json::StaticString const& type) {
Json::Value jvParams;
jvParams[jss::ledger_index] = "current";
jvParams[jss::type] = type;
return env.rpc(
"json",
"ledger_data",
boost::lexical_cast<std::string>(jvParams))[jss::result];
};
// Assert that state is an empty array.
for (auto const& type :
{jss::amendments,
jss::check,
jss::directory,
jss::fee,
jss::offer,
jss::signer_list,
jss::state,
jss::ticket,
jss::escrow,
jss::payment_channel,
jss::deposit_preauth})
{
auto const jrr = makeRequest(type);
BEAST_EXPECT(checkArraySize(jrr[jss::state], 0));
}
int const num_accounts = 10;
for (auto i = 0; i < num_accounts; i++)
@@ -372,15 +400,6 @@ public:
env.close();
// Now fetch each type
auto makeRequest = [&env](Json::StaticString t) {
Json::Value jvParams;
jvParams[jss::ledger_index] = "current";
jvParams[jss::type] = t;
return env.rpc(
"json",
"ledger_data",
boost::lexical_cast<std::string>(jvParams))[jss::result];
};
{ // jvParams[jss::type] = "account";
auto const jrr = makeRequest(jss::account);