Skip to content

Commit 737724d

Browse files
committed
fix: support boolean value for service info parameters
1 parent 71cb66a commit 737724d

File tree

3 files changed

+63
-9
lines changed

3 files changed

+63
-9
lines changed

src/http/controller.hpp

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,14 @@
2626
#include <vector>
2727
#include <iostream>
2828

29-
#include <boost/lexical_cast.hpp>
30-
3129
#include "oatpp/web/server/api/ApiController.hpp"
3230
#include "oatpp/parser/json/mapping/ObjectMapper.hpp"
3331
#include "oatpp/core/macro/codegen.hpp"
3432
#include "oatpp/core/macro/component.hpp"
3533

3634
#include "apidata.h"
3735
#include "oatppjsonapi.h"
36+
#include "utils/utils.hpp"
3837
#include "dto/info.hpp"
3938
#include "dto/service_predict.hpp"
4039
#include "dto/service_create.hpp"
@@ -77,8 +76,16 @@ class DedeController : public oatpp::web::server::api::ApiController
7776

7877
oatpp::String qs_status = queryParams.get("status");
7978
bool status = false;
80-
if (qs_status)
81-
status = boost::lexical_cast<bool>(*qs_status);
79+
try
80+
{
81+
if (qs_status)
82+
status = dd::dd_utils::parse_bool(*qs_status);
83+
}
84+
catch (boost::bad_lexical_cast &)
85+
{
86+
return _oja->response_bad_request_400(
87+
"status must be a boolean value");
88+
}
8289

8390
auto hit = _oja->_mlservices.begin();
8491
while (hit != _oja->_mlservices.end())
@@ -101,13 +108,29 @@ class DedeController : public oatpp::web::server::api::ApiController
101108
{
102109
oatpp::String qs_status = queryParams.get("status");
103110
bool status = true;
104-
if (qs_status)
105-
status = boost::lexical_cast<bool>(*qs_status);
111+
try
112+
{
113+
if (qs_status)
114+
status = dd::dd_utils::parse_bool(*qs_status);
115+
}
116+
catch (boost::bad_lexical_cast &)
117+
{
118+
return _oja->response_bad_request_400(
119+
"status must be a boolean value");
120+
}
106121

107122
oatpp::String qs_labels = queryParams.get("labels");
108123
bool labels = false;
109-
if (qs_labels)
110-
labels = boost::lexical_cast<bool>(*qs_labels);
124+
try
125+
{
126+
if (qs_labels)
127+
labels = dd::dd_utils::parse_bool(*qs_labels);
128+
}
129+
catch (boost::bad_lexical_cast &)
130+
{
131+
return _oja->response_bad_request_400(
132+
"labels must be a boolean value");
133+
}
111134

112135
auto janswer = _oja->service_status(service_name, status, labels);
113136
return _oja->jdoc_to_response(janswer);

src/utils/utils.hpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
#include <vector>
2727
#include <algorithm>
2828

29+
#include <boost/lexical_cast.hpp>
30+
2931
namespace dd
3032
{
3133
namespace dd_utils
@@ -102,6 +104,25 @@ namespace dd
102104
return count == 1;
103105
}
104106

107+
/** boost::lexical_cast<bool> but accept "true" and "false" */
108+
inline bool parse_bool(const std::string &str)
109+
{
110+
try
111+
{
112+
return boost::lexical_cast<bool>(str);
113+
}
114+
catch (boost::bad_lexical_cast &)
115+
{
116+
if (str == "true")
117+
return true;
118+
else if (str == "false")
119+
return false;
120+
else
121+
throw;
122+
}
123+
return false;
124+
}
125+
105126
#ifdef WIN32
106127
inline int my_hardware_concurrency()
107128
{

tests/ut-oatpp.cc

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ void test_services(std::shared_ptr<DedeApiTestClient> client)
7979
ASSERT_EQ(201, d["status"]["code"].GetInt());
8080

8181
// service info
82-
response = client->get_service_with_labels(serv.c_str(), "1");
82+
response = client->get_service_with_labels(serv.c_str(), "true");
8383
message = response->readBodyToString();
8484
ASSERT_TRUE(message != nullptr);
8585
std::cout << "jstr=" << *message << std::endl;
@@ -102,6 +102,16 @@ void test_services(std::shared_ptr<DedeApiTestClient> client)
102102
ASSERT_TRUE(d["body"].HasMember("labels"));
103103
ASSERT_EQ(d["body"]["labels"].Size(), 0);
104104

105+
// test other boolean values
106+
response = client->get_service_with_labels(serv.c_str(), "1");
107+
ASSERT_EQ(response->getStatusCode(), 200);
108+
response = client->get_service_with_labels(serv.c_str(), "false");
109+
ASSERT_EQ(response->getStatusCode(), 200);
110+
response = client->get_service_with_labels(serv.c_str(), "0");
111+
ASSERT_EQ(response->getStatusCode(), 200);
112+
response = client->get_service_with_labels(serv.c_str(), "flase");
113+
ASSERT_EQ(response->getStatusCode(), 400);
114+
105115
// info call
106116
response = client->get_info();
107117
message = response->readBodyToString();

0 commit comments

Comments
 (0)