Corvusoft / restbed

Corvusoft's Restbed framework brings asynchronous RESTful functionality to C++14 applications.
http://www.corvusoft.co.uk
Other
1.93k stars 377 forks source link

OpenAPI Generator does not add json.encode line to the generated GET handler #506

Closed cornhedgehog closed 3 months ago

cornhedgehog commented 2 years ago

Hi, I am using OpenAPI generator to generate Restbed C++ API server. Here is what I've noticed in the generated GET handler: for some reason resultObject never gets parsed, and the result is empty. Here is this handler from the OpenAPI Generator repository, line 483,:

void PetApiPetFindByStatusResource::handler_GET_internal(const std::shared_ptr<restbed::Session> session)
{
const auto request = session->get_request();

// Getting the query params

int status_code = 500;
std::vector<std::shared_ptr<Pet>> resultObject = std::vector<std::shared_ptr<Pet>>();
std::string result = "";

try {
    std::tie(status_code, resultObject) =
         handler_GET(status);
}
catch(const PetApiException& e) {
    std::tie(status_code, result) = handlePetApiException(e);
}
catch(const std::exception& e) {
    std::tie(status_code, result) = handleStdException(e);
}
catch(...) {
    std::tie(status_code, result) = handleUnspecifiedException();
}

if (status_code == 200) {

    const constexpr auto contentType = "application/json";
    //I want to return the result here
    returnResponse(session, 200, result.empty() ? "successful operation" : result, contentType);
    return;
}
if (status_code == 400) {

    const constexpr auto contentType = "text/plain";
    returnResponse(session, 400, result.empty() ? "Invalid status value" : result, contentType);
    return;
}
defaultSessionClose(session, status_code, result);
}

I can observe the same results for my project while using Restbed generator. I think the only way to fix it is to add some sort of result = resultObject.to_json_string() every time API is re-generated but this is not how it should work. Is there any workaround?

Could it be the wrong .json usage? The .json file I use for this code is this:

{ "openapi": "3.0.0", "info": { "version": "1.0.0", "title": "My API" }, "paths": { "/myobject": { "get": { "responses": { "200": { "content": { "application/json": { "schema": { "type": "array", "items": { "$ref": "#/components/schemas/MyObject" } } } } }, "default": { "description": "unexpected error" } } } } }, "components": { "schemas": { "MyObject": { "type": "object", "properties": { "prop1": { "type": "string" }, "prop2": { "type": "number" } } } } } }

ben-crowhurst commented 3 months ago

Please take this issue up with the generator project team.