add api_version to response (#1088)

Fix #1020
This commit is contained in:
cyan317
2024-01-09 15:53:09 +00:00
committed by GitHub
parent 61f1e0853d
commit d30e63d49a
3 changed files with 32 additions and 19 deletions

View File

@@ -214,28 +214,31 @@ private:
// if the result is forwarded - just use it as is // if the result is forwarded - just use it as is
// if forwarded request has error, for http, error should be in "result"; for ws, error should // if forwarded request has error, for http, error should be in "result"; for ws, error should
// be at top // be at top
if (isForwarded && (json.contains("result") || connection->upgraded)) { if (isForwarded && (json.contains(JS(result)) || connection->upgraded)) {
for (auto const& [k, v] : json) for (auto const& [k, v] : json)
response.insert_or_assign(k, v); response.insert_or_assign(k, v);
} else { } else {
response["result"] = json; response[JS(result)] = json;
} }
// for ws there is an additional field "status" in the response, // for ws there is an additional field "status" in the response,
// otherwise the "status" is in the "result" field // otherwise the "status" is in the "result" field
if (connection->upgraded) { if (connection->upgraded) {
auto const id = request.contains("id") ? request.at("id") : nullptr; auto const appendFieldIfExist = [&](auto const& field) {
if (request.contains(field) and not request.at(field).is_null())
response[field] = request.at(field);
};
if (not id.is_null()) appendFieldIfExist(JS(id));
response["id"] = id; appendFieldIfExist(JS(api_version));
if (!response.contains("error")) if (!response.contains(JS(error)))
response["status"] = "success"; response[JS(status)] = JS(success);
response["type"] = "response"; response[JS(type)] = JS(response);
} else { } else {
if (response.contains("result") && !response["result"].as_object().contains("error")) if (response.contains(JS(result)) && !response[JS(result)].as_object().contains(JS(error)))
response["result"].as_object()["status"] = "success"; response[JS(result)].as_object()[JS(status)] = JS(success);
} }
} }

View File

@@ -150,18 +150,23 @@ public:
auto e = rpc::makeError(error); auto e = rpc::makeError(error);
if (request_) { if (request_) {
auto const& req = request_.value(); auto const appendFieldIfExist = [&](auto const& field) {
auto const id = req.contains("id") ? req.at("id") : nullptr; if (request_->contains(field) and not request_->at(field).is_null())
if (not id.is_null()) e[field] = request_->at(field);
e["id"] = id; };
e["request"] = req; appendFieldIfExist(JS(id));
if (connection_->upgraded)
appendFieldIfExist(JS(api_version));
e[JS(request)] = request_.value();
} }
if (connection_->upgraded) { if (connection_->upgraded) {
return e; return e;
} }
return {{"result", e}}; return {{JS(result), e}};
} }
}; };

View File

@@ -129,7 +129,8 @@ TEST_F(WebRPCServerHandlerTest, WsNormalPath)
session->upgraded = true; session->upgraded = true;
static auto constexpr request = R"({ static auto constexpr request = R"({
"command": "server_info", "command": "server_info",
"id": 99 "id": 99,
"api_version": 2
})"; })";
backend->setRange(MINSEQ, MAXSEQ); backend->setRange(MINSEQ, MAXSEQ);
@@ -140,6 +141,7 @@ TEST_F(WebRPCServerHandlerTest, WsNormalPath)
"id": 99, "id": 99,
"status": "success", "status": "success",
"type": "response", "type": "response",
"api_version": 2,
"warnings": [ "warnings": [
{ {
"id": 2001, "id": 2001,
@@ -291,10 +293,12 @@ TEST_F(WebRPCServerHandlerTest, WsErrorPath)
"error_message": "ledgerIndexMalformed", "error_message": "ledgerIndexMalformed",
"status": "error", "status": "error",
"type": "response", "type": "response",
"api_version": 2,
"request": { "request": {
"command": "ledger", "command": "ledger",
"ledger_index": "xx", "ledger_index": "xx",
"id": "123" "id": "123",
"api_version": 2
}, },
"warnings": [ "warnings": [
{ {
@@ -309,7 +313,8 @@ TEST_F(WebRPCServerHandlerTest, WsErrorPath)
static auto constexpr requestJSON = R"({ static auto constexpr requestJSON = R"({
"command": "ledger", "command": "ledger",
"ledger_index": "xx", "ledger_index": "xx",
"id": "123" "id": "123",
"api_version": 2
})"; })";
EXPECT_CALL(*rpcEngine, buildResponse(testing::_)) EXPECT_CALL(*rpcEngine, buildResponse(testing::_))
.WillOnce(testing::Return(rpc::Status{rpc::RippledError::rpcINVALID_PARAMS, "ledgerIndexMalformed"})); .WillOnce(testing::Return(rpc::Status{rpc::RippledError::rpcINVALID_PARAMS, "ledgerIndexMalformed"}));