[cpp-restbed-server] make the restbed SERVER generator more extensible (#7679)

* Simple fix to allow users to override the Api class and set handlers

* fix definition order

* Regen petstore example files
This commit is contained in:
Francesco Montorsi
2020-10-27 08:35:10 +01:00
committed by GitHub
parent 3d6bd48b67
commit 4860eb65e1
21 changed files with 146 additions and 243 deletions

View File

@@ -5,7 +5,7 @@
* The version of the OpenAPI document: 1.0.0
*
*
* NOTE: This class is auto generated by OpenAPI-Generator unset.
* NOTE: This class is auto generated by OpenAPI-Generator 5.0.0-SNAPSHOT.
* https://openapi-generator.tech
* Do not edit the class manually.
*/
@@ -26,26 +26,31 @@ namespace api {
using namespace org::openapitools::server::model;
PetApi::PetApi() {
std::shared_ptr<PetApiPetResource> spPetApiPetResource = std::make_shared<PetApiPetResource>();
this->publish(spPetApiPetResource);
std::shared_ptr<PetApiPetPetIdResource> spPetApiPetPetIdResource = std::make_shared<PetApiPetPetIdResource>();
this->publish(spPetApiPetPetIdResource);
std::shared_ptr<PetApiPetFindByStatusResource> spPetApiPetFindByStatusResource = std::make_shared<PetApiPetFindByStatusResource>();
this->publish(spPetApiPetFindByStatusResource);
std::shared_ptr<PetApiPetFindByTagsResource> spPetApiPetFindByTagsResource = std::make_shared<PetApiPetFindByTagsResource>();
this->publish(spPetApiPetFindByTagsResource);
std::shared_ptr<PetApiPetPetIdUploadImageResource> spPetApiPetPetIdUploadImageResource = std::make_shared<PetApiPetPetIdUploadImageResource>();
this->publish(spPetApiPetPetIdUploadImageResource);
}
PetApi::~PetApi() {}
void PetApi::startService(int const& port) {
// A typical pattern is to derive a class from PetApi and allocate the shared pointers for restbed::Resource objects
// and manipulate them (e.g. binding GET/POST handler functions) before this startService() gets called.
// In such a case we want to use our m_spXXX variables.
// However in case these shared pointers are nullptr, then allocate the restbed::Resources now:
if (!m_spPetApiPetResource)
m_spPetApiPetResource = std::make_shared<PetApiPetResource>();
this->publish(m_spPetApiPetResource);
if (!m_spPetApiPetPetIdResource)
m_spPetApiPetPetIdResource = std::make_shared<PetApiPetPetIdResource>();
this->publish(m_spPetApiPetPetIdResource);
if (!m_spPetApiPetFindByStatusResource)
m_spPetApiPetFindByStatusResource = std::make_shared<PetApiPetFindByStatusResource>();
this->publish(m_spPetApiPetFindByStatusResource);
if (!m_spPetApiPetFindByTagsResource)
m_spPetApiPetFindByTagsResource = std::make_shared<PetApiPetFindByTagsResource>();
this->publish(m_spPetApiPetFindByTagsResource);
if (!m_spPetApiPetPetIdUploadImageResource)
m_spPetApiPetPetIdUploadImageResource = std::make_shared<PetApiPetPetIdUploadImageResource>();
this->publish(m_spPetApiPetPetIdUploadImageResource);
std::shared_ptr<restbed::Settings> settings = std::make_shared<restbed::Settings>();
settings->set_port(port);
settings->set_root("/v2");
@@ -66,9 +71,6 @@ PetApiPetResource::PetApiPetResource()
this->set_method_handler("PUT",
std::bind(&PetApiPetResource::PUT_method_handler, this,
std::placeholders::_1));
body = std::make_shared<Pet>();
}
PetApiPetResource::~PetApiPetResource()
@@ -175,16 +177,9 @@ PetApiPetPetIdResource::PetApiPetPetIdResource()
this->set_method_handler("DELETE",
std::bind(&PetApiPetPetIdResource::DELETE_method_handler, this,
std::placeholders::_1));
this->set_method_handler("GET",
std::bind(&PetApiPetPetIdResource::GET_method_handler, this,
std::placeholders::_1));
this->set_method_handler("POST",
std::bind(&PetApiPetPetIdResource::POST_method_handler, this,
std::placeholders::_1));
petId = 0L;
apiKey = "";
}
PetApiPetPetIdResource::~PetApiPetPetIdResource()
@@ -198,12 +193,6 @@ void PetApiPetPetIdResource::set_handler_DELETE(
handler_DELETE_ = std::move(handler);
}
void PetApiPetPetIdResource::set_handler_GET(
std::function<std::pair<int, std::string>(
int64_t const &
)> handler) {
handler_GET_ = std::move(handler);
}
void PetApiPetPetIdResource::set_handler_POST(
std::function<std::pair<int, std::string>(
int64_t const &, std::string const &, std::string const &
@@ -240,41 +229,6 @@ void PetApiPetPetIdResource::DELETE_method_handler(const std::shared_ptr<restbed
}
void PetApiPetPetIdResource::GET_method_handler(const std::shared_ptr<restbed::Session> session) {
const auto request = session->get_request();
// Getting the path params
const int64_t petId = request->get_path_parameter("petId", 0L);
// Change the value of this variable to the appropriate response before sending the response
int status_code = 200;
std::string result = "successful operation";
if (handler_GET_)
{
std::tie(status_code, result) = handler_GET_(
petId
);
}
if (status_code == 200) {
std::shared_ptr<Pet> response = NULL;
session->close(200, result.empty() ? "successful operation" : std::move(result), { {"Connection", "close"} });
return;
}
if (status_code == 400) {
session->close(400, result.empty() ? "Invalid ID supplied" : std::move(result), { {"Connection", "close"} });
return;
}
if (status_code == 404) {
session->close(404, result.empty() ? "Pet not found" : std::move(result), { {"Connection", "close"} });
return;
}
}
void PetApiPetPetIdResource::POST_method_handler(const std::shared_ptr<restbed::Session> session) {
const auto request = session->get_request();
@@ -309,8 +263,6 @@ PetApiPetFindByStatusResource::PetApiPetFindByStatusResource()
this->set_method_handler("GET",
std::bind(&PetApiPetFindByStatusResource::GET_method_handler, this,
std::placeholders::_1));
}
PetApiPetFindByStatusResource::~PetApiPetFindByStatusResource()
@@ -363,8 +315,6 @@ PetApiPetFindByTagsResource::PetApiPetFindByTagsResource()
this->set_method_handler("GET",
std::bind(&PetApiPetFindByTagsResource::GET_method_handler, this,
std::placeholders::_1));
}
PetApiPetFindByTagsResource::~PetApiPetFindByTagsResource()
@@ -417,11 +367,6 @@ PetApiPetPetIdUploadImageResource::PetApiPetPetIdUploadImageResource()
this->set_method_handler("POST",
std::bind(&PetApiPetPetIdUploadImageResource::POST_method_handler, this,
std::placeholders::_1));
petId = 0L;
additionalMetadata = "";
file = "";
}
PetApiPetPetIdUploadImageResource::~PetApiPetPetIdUploadImageResource()