Merge pull request #904 from linhaojun857/fix_902

Fix the bug of send "null" in request body causes seg-fault
This commit is contained in:
Leonid Stryzhevskyi 2024-01-11 06:38:22 +02:00 committed by GitHub
commit a5fe65becc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 57 additions and 5 deletions

View File

@ -126,7 +126,7 @@ public:
auto type = Wrapper::Class::getType(); auto type = Wrapper::Class::getType();
oatpp::parser::Caret caret(str); oatpp::parser::Caret caret(str);
auto result = read(caret, type).template cast<Wrapper>(); auto result = read(caret, type).template cast<Wrapper>();
if(result == nullptr) { if(caret.hasError()) {
throw oatpp::parser::ParsingError(caret.getErrorMessage(), caret.getErrorCode(), caret.getPosition()); throw oatpp::parser::ParsingError(caret.getErrorMessage(), caret.getErrorCode(), caret.getPosition());
} }
return result; return result;

View File

@ -341,6 +341,46 @@ void FullTest::onRun() {
OATPP_ASSERT(returnedData == data) OATPP_ASSERT(returnedData == data)
} }
{
String bodyIn = "null";
auto response = client->testBodyIsNull1(bodyIn, connection);
OATPP_ASSERT(response->getStatusCode() == 400)
auto returnedData = response->readBodyToString();
OATPP_ASSERT(returnedData)
OATPP_ASSERT(returnedData == "server=oatpp/" OATPP_VERSION "\n"
"code=400\n"
"description=Bad Request\n"
"message=Missing valid body parameter 'body'\n")
connection = client->getConnection();
}
{
String bodyIn = "\"null\"";
auto response = client->testBodyIsNull1(bodyIn, connection);
OATPP_ASSERT(response->getStatusCode() == 200)
auto returnedData = response->readBodyToString();
OATPP_ASSERT(returnedData)
OATPP_ASSERT(returnedData == "OK---null")
}
{
String bodyIn = "null";
auto response = client->testBodyIsNull2(bodyIn, connection);
OATPP_ASSERT(response->getStatusCode() == 200)
auto returnedData = response->readBodyToString();
OATPP_ASSERT(returnedData)
OATPP_ASSERT(returnedData == "OK---null")
}
{
String bodyIn = "\"null\"";
auto response = client->testBodyIsNull2(bodyIn, connection);
OATPP_ASSERT(response->getStatusCode() == 200)
auto returnedData = response->readBodyToString();
OATPP_ASSERT(returnedData)
OATPP_ASSERT(returnedData == "OK---\"null\"")
}
{ {
auto response = client->headerValueSet(" VALUE_1, VALUE_2, VALUE_3", connection); auto response = client->headerValueSet(" VALUE_1, VALUE_2, VALUE_3", connection);
OATPP_ASSERT(response->getStatusCode() == 200) OATPP_ASSERT(response->getStatusCode() == 200)

View File

@ -62,6 +62,8 @@ public:
API_CALL("GET", "enum/as-number", getHeaderEnumAsNumber, HEADER(Enum<AllowedPathParams>::AsNumber, enumValue, "enum")) API_CALL("GET", "enum/as-number", getHeaderEnumAsNumber, HEADER(Enum<AllowedPathParams>::AsNumber, enumValue, "enum"))
API_CALL("POST", "echo", echoBody, BODY_STRING(String, body)) API_CALL("POST", "echo", echoBody, BODY_STRING(String, body))
API_CALL("POST", "testBodyIsNull1", testBodyIsNull1, BODY_STRING(String, body))
API_CALL("POST", "testBodyIsNull2", testBodyIsNull2, BODY_STRING(String, body))
API_CALL("GET", "header-value-set", headerValueSet, HEADER(String, valueSet, "X-VALUE-SET")) API_CALL("GET", "header-value-set", headerValueSet, HEADER(String, valueSet, "X-VALUE-SET"))
API_CALL("GET", "default-basic-authorization", defaultBasicAuthorization, AUTHORIZATION_BASIC(String, authString)) API_CALL("GET", "default-basic-authorization", defaultBasicAuthorization, AUTHORIZATION_BASIC(String, authString))

View File

@ -62,7 +62,7 @@ public:
, available(true) , available(true)
{} {}
public: public:
static std::shared_ptr<Controller> createShared(const std::shared_ptr<ObjectMapper>& objectMapper = OATPP_GET_COMPONENT(std::shared_ptr<ObjectMapper>)){ static std::shared_ptr<Controller> createShared(const std::shared_ptr<ObjectMapper>& objectMapper = OATPP_GET_COMPONENT(std::shared_ptr<ObjectMapper>)){
return std::make_shared<Controller>(objectMapper); return std::make_shared<Controller>(objectMapper);
} }
@ -110,7 +110,7 @@ public:
dto->testValue = param; dto->testValue = param;
return createDtoResponse(Status::CODE_200, dto); return createDtoResponse(Status::CODE_200, dto);
} }
ENDPOINT("GET", "queries", getWithQueries, ENDPOINT("GET", "queries", getWithQueries,
QUERY(String, name), QUERY(Int32, age)) { QUERY(String, name), QUERY(Int32, age)) {
auto dto = TestDto::createShared(); auto dto = TestDto::createShared();
@ -134,7 +134,7 @@ public:
} }
return createDtoResponse(Status::CODE_200, dto); return createDtoResponse(Status::CODE_200, dto);
} }
ENDPOINT("GET", "headers", getWithHeaders, ENDPOINT("GET", "headers", getWithHeaders,
HEADER(String, param, "X-TEST-HEADER")) { HEADER(String, param, "X-TEST-HEADER")) {
//OATPP_LOGV(TAG, "GET headers {X-TEST-HEADER: %s}", param->c_str()) //OATPP_LOGV(TAG, "GET headers {X-TEST-HEADER: %s}", param->c_str())
@ -142,7 +142,7 @@ public:
dto->testValue = param; dto->testValue = param;
return createDtoResponse(Status::CODE_200, dto); return createDtoResponse(Status::CODE_200, dto);
} }
ENDPOINT("POST", "body", postBody, ENDPOINT("POST", "body", postBody,
BODY_STRING(String, body)) { BODY_STRING(String, body)) {
//OATPP_LOGV(TAG, "POST body %s", body->c_str()) //OATPP_LOGV(TAG, "POST body %s", body->c_str())
@ -163,6 +163,16 @@ public:
return createResponse(Status::CODE_200, body); return createResponse(Status::CODE_200, body);
} }
ENDPOINT("POST", "testBodyIsNull1", testBodyIsNull1,
BODY_DTO(String, body)) {
return createResponse(Status::CODE_200, "OK---" + body);
}
ENDPOINT("POST", "testBodyIsNull2", testBodyIsNull2,
BODY_STRING(String, body)) {
return createResponse(Status::CODE_200, "OK---" + body);
}
ENDPOINT("GET", "header-value-set", headerValueSet, ENDPOINT("GET", "header-value-set", headerValueSet,
HEADER(String, valueSet, "X-VALUE-SET")) { HEADER(String, valueSet, "X-VALUE-SET")) {