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

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

Open
cornhedgehog opened this issue Feb 10, 2022 · 0 comments

Comments

@cornhedgehog
Copy link

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" } } } } } }

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

No branches or pull requests

1 participant