c++jsonoat++

Oat++ framework. Seg fault from getting value of the json parsed to DTO object field. Assertion for ALL fileds at once


MAIN POINT of my question in the end of the text

Okay, I ran into a common problem: if I send JSON without some key:value pair to my oat++ server, parse it with BODY_DTO in my ENDPOINT and then try to get value of non-exist filed, I will get segmentation fault and my server will immediately crush

My controller endpoint:

ENDPOINT("POST", "/servers/{serverId}", getServerById,
                    PATH(String, serverId, "serverId"), BODY_DTO(Object<MyDTO>, body)) {
            try{
                std::cout << "body->noInJSONField : " << body->noInJSONField <<std::endl;
            } catch(std::exception& exc) {
                std::cout << exc.what() <<std::endl;
            }
}

My DTO:

class MyDTO : public oatpp::DTO {

  DTO_INIT(MyDTO, DTO  )

  DTO_FIELD(Int16, inJSONField, "inJSONField");     // 
  DTO_FIELD(Int16, noInJSONField, "noInJSONField");
  DTO_FIELD(Object<MyANOTHER_DTO>, smallDerivedJSONobjInMainJsonObj, "emmUhhOkay");
}

Extremely important in my problem that I know about similar question on stackOverflow:

Bug with sending incorrect json doc

And the same on GitHub:

https://github.com/oatpp/oatpp/issues/340#issuecomment-727563144

So, my question: is there any way to assert (Maybe some OATPP_ASSERT_ALL_FIELDS(%DTO_OBJ%)) all fields of my objects (And all fields of included objects in my DTO like third field)?

Why am I asking? Because check all fileds like

OATPP_ASSERT(body);
OATPP_ASSERT(body->inJSONField);
OATPP_ASSERT(body->noInJSONField);
OATPP_ASSERT(body->smallDerivedJSONobjInMainJsonObj);
OATPP_ASSERT(body->smallDerivedJSONobjInMainJsonObj->field1);
OATPP_ASSERT(body->smallDerivedJSONobjInMainJsonObj->field2);
OATPP_ASSERT(body->smallDerivedJSONobjInMainJsonObj->field3_obj->wow_another_field);

feels exactly as it looks - utterly cumbersome


Solution

  • try this, in the DTO definition just force the required field(s), like this:

    DTO_FIELD_INFO(noInJSONField) { 
        info->required = true; }
    

    OAT++ will catch the missing field(s) and will answer with an explicit error.