OpenAPITools / OpenAPITools/openapi-generator
[BUG] [C++] [Restbed] Restbed C++ OpenAPI Generator does not convert a vector of custom objects into a json string in the generated code
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Description
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 examples in the OpenAPI Generator repository:
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 but it would never happen since we are not using resultObject anywhere
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?
openapi-generator version
I am using version 5.4.0 which is the stable latest version.
OpenAPI declaration file content or url
Here is my .json spec file from my project that seems to be correct:
{
"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"
}
}
}
}
}
}
Generation Details, Steps to Reproduce
I am using npx CLI tool to generate API using this command
npx @openapitools/openapi-generator-cli generate -i apispec.json -g cpp-restbed-server -o ../OpenAPI/generatedapi
The issue can be reproduced with help of Petstore example using Restbed generator, both for .json or .yaml spec.
npx @openapitools/openapi-generator-cli generate -i petstore.yaml -g cpp-restbed-server -o /tmp/restbed-test
In other words it seems to be easily reproduced. Please let me know if I can provide you with any more details.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the generated samples/server/petstore/cpp-restbed/api/PetApi.cpp handler shown in the issue, then reproduce it with the petstore.yaml generation command. Trace how the vector result is handled after handler_GET; done means a successful array response contains the generated JSON rather than an empty or fallback message.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100