[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,29 +26,34 @@ namespace api {
using namespace org::openapitools::server::model;
UserApi::UserApi() {
std::shared_ptr<UserApiUserResource> spUserApiUserResource = std::make_shared<UserApiUserResource>();
this->publish(spUserApiUserResource);
std::shared_ptr<UserApiUserCreateWithArrayResource> spUserApiUserCreateWithArrayResource = std::make_shared<UserApiUserCreateWithArrayResource>();
this->publish(spUserApiUserCreateWithArrayResource);
std::shared_ptr<UserApiUserCreateWithListResource> spUserApiUserCreateWithListResource = std::make_shared<UserApiUserCreateWithListResource>();
this->publish(spUserApiUserCreateWithListResource);
std::shared_ptr<UserApiUserUsernameResource> spUserApiUserUsernameResource = std::make_shared<UserApiUserUsernameResource>();
this->publish(spUserApiUserUsernameResource);
std::shared_ptr<UserApiUserLoginResource> spUserApiUserLoginResource = std::make_shared<UserApiUserLoginResource>();
this->publish(spUserApiUserLoginResource);
std::shared_ptr<UserApiUserLogoutResource> spUserApiUserLogoutResource = std::make_shared<UserApiUserLogoutResource>();
this->publish(spUserApiUserLogoutResource);
}
UserApi::~UserApi() {}
void UserApi::startService(int const& port) {
// A typical pattern is to derive a class from UserApi 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_spUserApiUserResource)
m_spUserApiUserResource = std::make_shared<UserApiUserResource>();
this->publish(m_spUserApiUserResource);
if (!m_spUserApiUserCreateWithArrayResource)
m_spUserApiUserCreateWithArrayResource = std::make_shared<UserApiUserCreateWithArrayResource>();
this->publish(m_spUserApiUserCreateWithArrayResource);
if (!m_spUserApiUserCreateWithListResource)
m_spUserApiUserCreateWithListResource = std::make_shared<UserApiUserCreateWithListResource>();
this->publish(m_spUserApiUserCreateWithListResource);
if (!m_spUserApiUserUsernameResource)
m_spUserApiUserUsernameResource = std::make_shared<UserApiUserUsernameResource>();
this->publish(m_spUserApiUserUsernameResource);
if (!m_spUserApiUserLoginResource)
m_spUserApiUserLoginResource = std::make_shared<UserApiUserLoginResource>();
this->publish(m_spUserApiUserLoginResource);
if (!m_spUserApiUserLogoutResource)
m_spUserApiUserLogoutResource = std::make_shared<UserApiUserLogoutResource>();
this->publish(m_spUserApiUserLogoutResource);
std::shared_ptr<restbed::Settings> settings = std::make_shared<restbed::Settings>();
settings->set_port(port);
settings->set_root("/v2");
@@ -66,9 +71,6 @@ UserApiUserResource::UserApiUserResource()
this->set_method_handler("POST",
std::bind(&UserApiUserResource::POST_method_handler, this,
std::placeholders::_1));
body = std::make_shared<User>();
}
UserApiUserResource::~UserApiUserResource()
@@ -128,8 +130,6 @@ UserApiUserCreateWithArrayResource::UserApiUserCreateWithArrayResource()
this->set_method_handler("POST",
std::bind(&UserApiUserCreateWithArrayResource::POST_method_handler, this,
std::placeholders::_1));
}
UserApiUserCreateWithArrayResource::~UserApiUserCreateWithArrayResource()
@@ -189,8 +189,6 @@ UserApiUserCreateWithListResource::UserApiUserCreateWithListResource()
this->set_method_handler("POST",
std::bind(&UserApiUserCreateWithListResource::POST_method_handler, this,
std::placeholders::_1));
}
UserApiUserCreateWithListResource::~UserApiUserCreateWithListResource()
@@ -250,15 +248,9 @@ UserApiUserUsernameResource::UserApiUserUsernameResource()
this->set_method_handler("DELETE",
std::bind(&UserApiUserUsernameResource::DELETE_method_handler, this,
std::placeholders::_1));
this->set_method_handler("GET",
std::bind(&UserApiUserUsernameResource::GET_method_handler, this,
std::placeholders::_1));
this->set_method_handler("PUT",
std::bind(&UserApiUserUsernameResource::PUT_method_handler, this,
std::placeholders::_1));
username = "";
}
UserApiUserUsernameResource::~UserApiUserUsernameResource()
@@ -272,12 +264,6 @@ void UserApiUserUsernameResource::set_handler_DELETE(
handler_DELETE_ = std::move(handler);
}
void UserApiUserUsernameResource::set_handler_GET(
std::function<std::pair<int, std::string>(
std::string const &
)> handler) {
handler_GET_ = std::move(handler);
}
void UserApiUserUsernameResource::set_handler_PUT(
std::function<std::pair<int, std::string>(
std::string const &, std::shared_ptr<User> const &
@@ -316,41 +302,6 @@ void UserApiUserUsernameResource::DELETE_method_handler(const std::shared_ptr<re
}
void UserApiUserUsernameResource::GET_method_handler(const std::shared_ptr<restbed::Session> session) {
const auto request = session->get_request();
// Getting the path params
const std::string username = request->get_path_parameter("username", "");
// 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_(
username
);
}
if (status_code == 200) {
std::shared_ptr<User> 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 username supplied" : std::move(result), { {"Connection", "close"} });
return;
}
if (status_code == 404) {
session->close(404, result.empty() ? "User not found" : std::move(result), { {"Connection", "close"} });
return;
}
}
void UserApiUserUsernameResource::PUT_method_handler(const std::shared_ptr<restbed::Session> session) {
const auto request = session->get_request();
@@ -398,10 +349,6 @@ UserApiUserLoginResource::UserApiUserLoginResource()
this->set_method_handler("GET",
std::bind(&UserApiUserLoginResource::GET_method_handler, this,
std::placeholders::_1));
username = "";
password = "";
}
UserApiUserLoginResource::~UserApiUserLoginResource()
@@ -460,8 +407,6 @@ UserApiUserLogoutResource::UserApiUserLogoutResource()
this->set_method_handler("GET",
std::bind(&UserApiUserLogoutResource::GET_method_handler, this,
std::placeholders::_1));
}
UserApiUserLogoutResource::~UserApiUserLogoutResource()