Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BODY_DTO fails to correctly parse "false" for Boolean #901

Open
OneDevTwoDev opened this issue Dec 13, 2023 · 1 comment · May be fixed by #903
Open

BODY_DTO fails to correctly parse "false" for Boolean #901

OneDevTwoDev opened this issue Dec 13, 2023 · 1 comment · May be fixed by #903

Comments

@OneDevTwoDev
Copy link

OneDevTwoDev commented Dec 13, 2023

Observing some strange behavior when trying to parse a body dto for an endpoint which contains a single boolean value. Value is correctly parsed when equal to true, but fails when equal to false. Wondering if this is related to 673.

Issue is reproducible with a controller w/ the following structure:

ENDPOINT("PUT", "/set_flag", setFlag, BODY_DTO(Boolean, aBool))
{
  std::cout << "param = " << *aBool << std::endl;
  return createResponse(Status::CODE_200, "OK");
}

When sending a request to this endpoint w/ a request body of "false", a 400 error is generated. When sending a request w/ a request body of "true", the boolean is correctly parsed.

I'm suspicious of the following section:

if(!OATPP_MACRO_FIRSTARG PARAM_LIST) { \
throw oatpp::web::protocol::http::HttpError(Status::CODE_400, "Missing valid body parameter '" OATPP_MACRO_FIRSTARG_STR PARAM_LIST "'"); \
}

Suspect that check should compare to nullptr similar to fix for 673.

@OneDevTwoDev
Copy link
Author

When modifying the body parameter check mentioned above to the following:

if(OATPP_MACRO_FIRSTARG PARAM_LIST != nullptr) {
  throw oatpp::web::protocol::http::HttpError(Status::CODE_400, "Missing valid body parameter '" OATPP_MACRO_FIRSTARG_STR PARAM_LIST "'");
}

I noticed I also needed to declare one of the operator!= definitions as const in the base DTOWrapper class:

template<typename T,
typename enabled = typename std::enable_if<std::is_same<T, std::nullptr_t>::value, void>::type
>
inline bool operator == (T){
return this->m_ptr.get() == nullptr;
}
template<typename T,
typename enabled = typename std::enable_if<std::is_same<T, std::nullptr_t>::value, void>::type
>
inline bool operator != (T){
return this->m_ptr.get() != nullptr;
}

I suspect this is because the variable specified in the BODY_DTO macro has a const modifier:

const auto& OATPP_MACRO_FIRSTARG PARAM_LIST = \
__request->readBodyToDto<TYPE>(getDefaultObjectMapper().get()); \

Couldn't think of a reason why making this method const would break anything and suspect it may need to be part of a fix for this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant