From e63dac965a7f6f8753d05918b104f0a11cfb4b18 Mon Sep 17 00:00:00 2001 From: etherealjoy Date: Thu, 30 Aug 2018 23:39:46 +0200 Subject: [PATCH 1/7] Support basic query handling Add helpers for primitive de-serialization Remove warnings due to unneeded commas Deserialize basic types in queries Add dependencies chain for external libraries Fixes wrong parameter passed to API --- .../languages/CppPistacheServerCodegen.java | 2 +- .../cpp-pistache-server/api-header.mustache | 2 +- .../api-impl-header.mustache | 2 +- .../cpp-pistache-server/api-source.mustache | 9 ++- .../cpp-pistache-server/cmake.mustache | 3 + .../modelbase-header.mustache | 40 ++++++++++ .../modelbase-source.mustache | 75 +++++++++++++++++++ .../petstore/cpp-pistache/CMakeLists.txt | 3 + .../petstore/cpp-pistache/api/PetApi.cpp | 18 ++++- .../server/petstore/cpp-pistache/api/PetApi.h | 6 +- .../petstore/cpp-pistache/api/StoreApi.h | 2 +- .../petstore/cpp-pistache/api/UserApi.cpp | 18 ++++- .../petstore/cpp-pistache/api/UserApi.h | 2 +- .../petstore/cpp-pistache/impl/PetApiImpl.cpp | 4 +- .../petstore/cpp-pistache/impl/PetApiImpl.h | 6 +- .../petstore/cpp-pistache/impl/StoreApiImpl.h | 2 +- .../petstore/cpp-pistache/impl/UserApiImpl.h | 2 +- .../petstore/cpp-pistache/model/ModelBase.cpp | 75 +++++++++++++++++++ .../petstore/cpp-pistache/model/ModelBase.h | 40 ++++++++++ 19 files changed, 291 insertions(+), 20 deletions(-) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CppPistacheServerCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CppPistacheServerCodegen.java index 7f7a4e33b8b3..08077b7decf1 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CppPistacheServerCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CppPistacheServerCodegen.java @@ -236,7 +236,7 @@ public Map postProcessOperationsWithModels(Map o if (param.isPrimitiveType) { param.dataType = "Pistache::Optional<" + param.dataType + ">"; } else { - param.dataType = "Pistache::Optional<" + param.baseType + ">"; + param.dataType = "Pistache::Optional<" + param.dataType + ">"; param.baseType = "Pistache::Optional<" + param.baseType + ">"; } } diff --git a/modules/openapi-generator/src/main/resources/cpp-pistache-server/api-header.mustache b/modules/openapi-generator/src/main/resources/cpp-pistache-server/api-header.mustache index 9db55ca4bac7..7ecc6c48a385 100644 --- a/modules/openapi-generator/src/main/resources/cpp-pistache-server/api-header.mustache +++ b/modules/openapi-generator/src/main/resources/cpp-pistache-server/api-header.mustache @@ -29,7 +29,7 @@ using namespace {{modelNamespace}};{{/hasModelImport}} class {{declspec}} {{classname}} { public: {{classname}}(Pistache::Address addr); - virtual ~{{classname}}() {}; + virtual ~{{classname}}() {} void init(size_t thr); void start(); void shutdown(); diff --git a/modules/openapi-generator/src/main/resources/cpp-pistache-server/api-impl-header.mustache b/modules/openapi-generator/src/main/resources/cpp-pistache-server/api-impl-header.mustache index 49c274d8fd78..357765cf57f8 100644 --- a/modules/openapi-generator/src/main/resources/cpp-pistache-server/api-impl-header.mustache +++ b/modules/openapi-generator/src/main/resources/cpp-pistache-server/api-impl-header.mustache @@ -32,7 +32,7 @@ using namespace {{modelNamespace}};{{/hasModelImport}} class {{classname}}Impl : public {{apiNamespace}}::{{classname}} { public: {{classname}}Impl(Pistache::Address addr); - ~{{classname}}Impl() { }; + ~{{classname}}Impl() {} {{#operation}} {{#vendorExtensions.x-codegen-pistache-isParsingSupported}} diff --git a/modules/openapi-generator/src/main/resources/cpp-pistache-server/api-source.mustache b/modules/openapi-generator/src/main/resources/cpp-pistache-server/api-source.mustache index c40c67eaa741..0ec60a145f99 100644 --- a/modules/openapi-generator/src/main/resources/cpp-pistache-server/api-source.mustache +++ b/modules/openapi-generator/src/main/resources/cpp-pistache-server/api-source.mustache @@ -62,7 +62,14 @@ void {{classname}}::{{operationIdSnakeCase}}_handler(const Pistache::Rest::Reque {{/hasBodyParam}}{{#hasQueryParams}} // Getting the query params {{#queryParams}} - auto {{paramName}} = request.query().get("{{baseName}}"); + auto {{paramName}}Query = request.query().get("{{baseName}}"); + Pistache::Optional<{{^isContainer}}{{dataType}}{{/isContainer}}{{#isListContainer}}std::vector<{{items.baseType}}>{{/isListContainer}}> {{paramName}}; + if(!{{paramName}}Query.isEmpty()){ + {{^isContainer}}{{dataType}}{{/isContainer}}{{#isListContainer}}std::vector<{{items.baseType}}>{{/isListContainer}} value; + if(ModelBase::fromStringValue({{paramName}}Query.get(), value)){ + {{paramName}} = Pistache::Some(value); + } + } {{/queryParams}} {{/hasQueryParams}}{{#hasHeaderParams}} // Getting the header params diff --git a/modules/openapi-generator/src/main/resources/cpp-pistache-server/cmake.mustache b/modules/openapi-generator/src/main/resources/cpp-pistache-server/cmake.mustache index bec552992418..11c7a5c3c525 100644 --- a/modules/openapi-generator/src/main/resources/cpp-pistache-server/cmake.mustache +++ b/modules/openapi-generator/src/main/resources/cpp-pistache-server/cmake.mustache @@ -56,6 +56,9 @@ set(<%classnameSnakeUpperCase%>_SERVER_SOURCES <%#operations%> add_executable(<%classnameSnakeLowerCase%>_server ${<%classnameSnakeUpperCase%>_SERVER_SOURCES}) +<%#addExternalLibs%> +add_dependencies(<%classnameSnakeLowerCase%>_server PISTACHE NLOHMANN) +<%/addExternalLibs%> <%/operations%> <%/apiInfo.apis%> diff --git a/modules/openapi-generator/src/main/resources/cpp-pistache-server/modelbase-header.mustache b/modules/openapi-generator/src/main/resources/cpp-pistache-server/modelbase-header.mustache index faeff4830387..710aa77ce524 100644 --- a/modules/openapi-generator/src/main/resources/cpp-pistache-server/modelbase-header.mustache +++ b/modules/openapi-generator/src/main/resources/cpp-pistache-server/modelbase-header.mustache @@ -12,6 +12,7 @@ #include "json.hpp" #include #include +#include #include #include @@ -37,6 +38,45 @@ public: static double toJson( double const value ); static bool toJson( bool const value ); static nlohmann::json toJson({{prefix}}ModelBase const& content ); + + static std::string toStringValue(const std::string &value); + static std::string toStringValue(const int32_t &value); + static std::string toStringValue(const int64_t &value); + static std::string toStringValue(const bool &value); + static std::string toStringValue(const float &value); + static std::string toStringValue(const double &value); + + static bool fromStringValue(const std::string &inStr, std::string &value); + static bool fromStringValue(const std::string &inStr, int32_t &value); + static bool fromStringValue(const std::string &inStr, int64_t &value); + static bool fromStringValue(const std::string &inStr, bool &value); + static bool fromStringValue(const std::string &inStr, float &value); + static bool fromStringValue(const std::string &inStr, double &value); + template + static bool fromStringValue(const std::vector &inStr, std::vector &value){ + try{ + for(auto & item : inStr){ + T itemValue; + if(fromStringValue(item, itemValue)){ + value.push_back(itemValue); + } + } + } + catch(...){ + return false; + } + return value.size() > 0; + } + template + static bool fromStringValue(const std::string &inStr, std::vector &value, char separator = ','){ + std::vector inStrings; + std::istringstream f(inStr); + std::string s; + while (std::getline(f, s, separator)) { + inStrings.push_back(s); + } + return fromStringValue(inStrings, value); + } }; class {{prefix}}ModelArrayHelper { diff --git a/modules/openapi-generator/src/main/resources/cpp-pistache-server/modelbase-source.mustache b/modules/openapi-generator/src/main/resources/cpp-pistache-server/modelbase-source.mustache index f097c3a4a199..7f6b5a093136 100644 --- a/modules/openapi-generator/src/main/resources/cpp-pistache-server/modelbase-source.mustache +++ b/modules/openapi-generator/src/main/resources/cpp-pistache-server/modelbase-source.mustache @@ -49,6 +49,81 @@ nlohmann::json {{prefix}}ModelBase::toJson({{prefix}}ModelBase const& content ) return content.toJson(); } +std::string ModelBase::toStringValue(const std::string &value){ + return std::string(value); +} + +std::string ModelBase::toStringValue(const int32_t &value){ + return std::to_string(value); +} + +std::string ModelBase::toStringValue(const int64_t &value){ + return std::to_string(value); +} + +std::string ModelBase::toStringValue(const bool &value){ + return value?std::string("true"):std::string("false"); +} + +std::string ModelBase::toStringValue(const float &value){ + return std::to_string(value); +} + +std::string ModelBase::toStringValue(const double &value){ + return std::to_string(value); +} + +bool ModelBase::fromStringValue(const std::string &inStr, std::string &value){ + value = std::string(inStr); + return true; +} + +bool ModelBase::fromStringValue(const std::string &inStr, int32_t &value){ + try { + value = std::stoi( inStr ); + } + catch (const std::invalid_argument) { + return false; + } + return true; +} + +bool ModelBase::fromStringValue(const std::string &inStr, int64_t &value){ + try { + value = std::stol( inStr ); + } + catch (const std::invalid_argument) { + return false; + } + return true; +} + +bool ModelBase::fromStringValue(const std::string &inStr, bool &value){ + inStr == "true"?value = true: value = false; + return true; +} + +bool ModelBase::fromStringValue(const std::string &inStr, float &value){ + try { + value = std::stof( inStr ); + } + catch (const std::invalid_argument) { + return false; + } + return true; +} + +bool ModelBase::fromStringValue(const std::string &inStr, double &value){ + try { + value = std::stod( inStr ); + } + catch (const std::invalid_argument) { + return false; + } + return true; +} + + {{#modelNamespaceDeclarations}} } {{/modelNamespaceDeclarations}} diff --git a/samples/server/petstore/cpp-pistache/CMakeLists.txt b/samples/server/petstore/cpp-pistache/CMakeLists.txt index 0a0cf93a9f3e..f355231cf2d6 100644 --- a/samples/server/petstore/cpp-pistache/CMakeLists.txt +++ b/samples/server/petstore/cpp-pistache/CMakeLists.txt @@ -63,10 +63,13 @@ UserApiMainServer.cpp add_executable(pet_api_server ${PET_API_SERVER_SOURCES}) +add_dependencies(pet_api_server PISTACHE NLOHMANN) add_executable(store_api_server ${STORE_API_SERVER_SOURCES}) +add_dependencies(store_api_server PISTACHE NLOHMANN) add_executable(user_api_server ${USER_API_SERVER_SOURCES}) +add_dependencies(user_api_server PISTACHE NLOHMANN) target_link_libraries(pet_api_server pistache pthread) target_link_libraries(store_api_server pistache pthread) diff --git a/samples/server/petstore/cpp-pistache/api/PetApi.cpp b/samples/server/petstore/cpp-pistache/api/PetApi.cpp index 5aa08b532fc0..c5830df4cf97 100644 --- a/samples/server/petstore/cpp-pistache/api/PetApi.cpp +++ b/samples/server/petstore/cpp-pistache/api/PetApi.cpp @@ -94,7 +94,14 @@ void PetApi::delete_pet_handler(const Pistache::Rest::Request &request, Pistache void PetApi::find_pets_by_status_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response) { // Getting the query params - auto status = request.query().get("status"); + auto statusQuery = request.query().get("status"); + Pistache::Optional> status; + if(!statusQuery.isEmpty()){ + std::vector value; + if(ModelBase::fromStringValue(statusQuery.get(), value)){ + status = Pistache::Some(value); + } + } try { this->find_pets_by_status(status, response); @@ -108,7 +115,14 @@ void PetApi::find_pets_by_status_handler(const Pistache::Rest::Request &request, void PetApi::find_pets_by_tags_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response) { // Getting the query params - auto tags = request.query().get("tags"); + auto tagsQuery = request.query().get("tags"); + Pistache::Optional> tags; + if(!tagsQuery.isEmpty()){ + std::vector value; + if(ModelBase::fromStringValue(tagsQuery.get(), value)){ + tags = Pistache::Some(value); + } + } try { this->find_pets_by_tags(tags, response); diff --git a/samples/server/petstore/cpp-pistache/api/PetApi.h b/samples/server/petstore/cpp-pistache/api/PetApi.h index 1e688417ee9c..db76fa2654e9 100644 --- a/samples/server/petstore/cpp-pistache/api/PetApi.h +++ b/samples/server/petstore/cpp-pistache/api/PetApi.h @@ -40,7 +40,7 @@ using namespace org::openapitools::server::model; class PetApi { public: PetApi(Pistache::Address addr); - virtual ~PetApi() {}; + virtual ~PetApi() {} void init(size_t thr); void start(); void shutdown(); @@ -90,7 +90,7 @@ class PetApi { /// Multiple status values can be provided with comma separated strings /// /// Status values that need to be considered for filter - virtual void find_pets_by_status(const Pistache::Optional &status, Pistache::Http::ResponseWriter &response) = 0; + virtual void find_pets_by_status(const Pistache::Optional> &status, Pistache::Http::ResponseWriter &response) = 0; /// /// Finds Pets by tags @@ -99,7 +99,7 @@ class PetApi { /// Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing. /// /// Tags to filter by - virtual void find_pets_by_tags(const Pistache::Optional &tags, Pistache::Http::ResponseWriter &response) = 0; + virtual void find_pets_by_tags(const Pistache::Optional> &tags, Pistache::Http::ResponseWriter &response) = 0; /// /// Find pet by ID diff --git a/samples/server/petstore/cpp-pistache/api/StoreApi.h b/samples/server/petstore/cpp-pistache/api/StoreApi.h index e4491a771b46..b5625112db56 100644 --- a/samples/server/petstore/cpp-pistache/api/StoreApi.h +++ b/samples/server/petstore/cpp-pistache/api/StoreApi.h @@ -40,7 +40,7 @@ using namespace org::openapitools::server::model; class StoreApi { public: StoreApi(Pistache::Address addr); - virtual ~StoreApi() {}; + virtual ~StoreApi() {} void init(size_t thr); void start(); void shutdown(); diff --git a/samples/server/petstore/cpp-pistache/api/UserApi.cpp b/samples/server/petstore/cpp-pistache/api/UserApi.cpp index 45720407e08a..5632cacf777a 100644 --- a/samples/server/petstore/cpp-pistache/api/UserApi.cpp +++ b/samples/server/petstore/cpp-pistache/api/UserApi.cpp @@ -136,8 +136,22 @@ void UserApi::get_user_by_name_handler(const Pistache::Rest::Request &request, P void UserApi::login_user_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response) { // Getting the query params - auto username = request.query().get("username"); - auto password = request.query().get("password"); + auto usernameQuery = request.query().get("username"); + Pistache::Optional username; + if(!usernameQuery.isEmpty()){ + std::string value; + if(ModelBase::fromStringValue(usernameQuery.get(), value)){ + username = Pistache::Some(value); + } + } + auto passwordQuery = request.query().get("password"); + Pistache::Optional password; + if(!passwordQuery.isEmpty()){ + std::string value; + if(ModelBase::fromStringValue(passwordQuery.get(), value)){ + password = Pistache::Some(value); + } + } try { this->login_user(username, password, response); diff --git a/samples/server/petstore/cpp-pistache/api/UserApi.h b/samples/server/petstore/cpp-pistache/api/UserApi.h index 5cb7fb0a35a0..aebd1a97ac7d 100644 --- a/samples/server/petstore/cpp-pistache/api/UserApi.h +++ b/samples/server/petstore/cpp-pistache/api/UserApi.h @@ -40,7 +40,7 @@ using namespace org::openapitools::server::model; class UserApi { public: UserApi(Pistache::Address addr); - virtual ~UserApi() {}; + virtual ~UserApi() {} void init(size_t thr); void start(); void shutdown(); diff --git a/samples/server/petstore/cpp-pistache/impl/PetApiImpl.cpp b/samples/server/petstore/cpp-pistache/impl/PetApiImpl.cpp index a180ed772c55..111441543c1c 100644 --- a/samples/server/petstore/cpp-pistache/impl/PetApiImpl.cpp +++ b/samples/server/petstore/cpp-pistache/impl/PetApiImpl.cpp @@ -29,10 +29,10 @@ void PetApiImpl::add_pet(const Pet &pet, Pistache::Http::ResponseWriter &respons void PetApiImpl::delete_pet(const int64_t &petId, const Pistache::Optional &apiKey, Pistache::Http::ResponseWriter &response) { response.send(Pistache::Http::Code::Ok, "Do some magic\n"); } -void PetApiImpl::find_pets_by_status(const Pistache::Optional &status, Pistache::Http::ResponseWriter &response) { +void PetApiImpl::find_pets_by_status(const Pistache::Optional> &status, Pistache::Http::ResponseWriter &response) { response.send(Pistache::Http::Code::Ok, "Do some magic\n"); } -void PetApiImpl::find_pets_by_tags(const Pistache::Optional &tags, Pistache::Http::ResponseWriter &response) { +void PetApiImpl::find_pets_by_tags(const Pistache::Optional> &tags, Pistache::Http::ResponseWriter &response) { response.send(Pistache::Http::Code::Ok, "Do some magic\n"); } void PetApiImpl::get_pet_by_id(const int64_t &petId, Pistache::Http::ResponseWriter &response) { diff --git a/samples/server/petstore/cpp-pistache/impl/PetApiImpl.h b/samples/server/petstore/cpp-pistache/impl/PetApiImpl.h index d903d0b61326..63f72a254aec 100644 --- a/samples/server/petstore/cpp-pistache/impl/PetApiImpl.h +++ b/samples/server/petstore/cpp-pistache/impl/PetApiImpl.h @@ -43,12 +43,12 @@ using namespace org::openapitools::server::model; class PetApiImpl : public org::openapitools::server::api::PetApi { public: PetApiImpl(Pistache::Address addr); - ~PetApiImpl() { }; + ~PetApiImpl() {} void add_pet(const Pet &pet, Pistache::Http::ResponseWriter &response); void delete_pet(const int64_t &petId, const Pistache::Optional &apiKey, Pistache::Http::ResponseWriter &response); - void find_pets_by_status(const Pistache::Optional &status, Pistache::Http::ResponseWriter &response); - void find_pets_by_tags(const Pistache::Optional &tags, Pistache::Http::ResponseWriter &response); + void find_pets_by_status(const Pistache::Optional> &status, Pistache::Http::ResponseWriter &response); + void find_pets_by_tags(const Pistache::Optional> &tags, Pistache::Http::ResponseWriter &response); void get_pet_by_id(const int64_t &petId, Pistache::Http::ResponseWriter &response); void update_pet(const Pet &pet, Pistache::Http::ResponseWriter &response); void update_pet_with_form(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter &response); diff --git a/samples/server/petstore/cpp-pistache/impl/StoreApiImpl.h b/samples/server/petstore/cpp-pistache/impl/StoreApiImpl.h index 8c18fff00271..8109a2c1392a 100644 --- a/samples/server/petstore/cpp-pistache/impl/StoreApiImpl.h +++ b/samples/server/petstore/cpp-pistache/impl/StoreApiImpl.h @@ -43,7 +43,7 @@ using namespace org::openapitools::server::model; class StoreApiImpl : public org::openapitools::server::api::StoreApi { public: StoreApiImpl(Pistache::Address addr); - ~StoreApiImpl() { }; + ~StoreApiImpl() {} void delete_order(const std::string &orderId, Pistache::Http::ResponseWriter &response); void get_inventory(Pistache::Http::ResponseWriter &response); diff --git a/samples/server/petstore/cpp-pistache/impl/UserApiImpl.h b/samples/server/petstore/cpp-pistache/impl/UserApiImpl.h index 8da0e1277af6..98cb593e694d 100644 --- a/samples/server/petstore/cpp-pistache/impl/UserApiImpl.h +++ b/samples/server/petstore/cpp-pistache/impl/UserApiImpl.h @@ -43,7 +43,7 @@ using namespace org::openapitools::server::model; class UserApiImpl : public org::openapitools::server::api::UserApi { public: UserApiImpl(Pistache::Address addr); - ~UserApiImpl() { }; + ~UserApiImpl() {} void create_user(const User &user, Pistache::Http::ResponseWriter &response); void create_users_with_array_input(const std::vector &user, Pistache::Http::ResponseWriter &response); diff --git a/samples/server/petstore/cpp-pistache/model/ModelBase.cpp b/samples/server/petstore/cpp-pistache/model/ModelBase.cpp index 19910b9d6012..3fa739a9f60b 100644 --- a/samples/server/petstore/cpp-pistache/model/ModelBase.cpp +++ b/samples/server/petstore/cpp-pistache/model/ModelBase.cpp @@ -60,6 +60,81 @@ nlohmann::json ModelBase::toJson(ModelBase const& content ) return content.toJson(); } +std::string ModelBase::toStringValue(const std::string &value){ + return std::string(value); +} + +std::string ModelBase::toStringValue(const int32_t &value){ + return std::to_string(value); +} + +std::string ModelBase::toStringValue(const int64_t &value){ + return std::to_string(value); +} + +std::string ModelBase::toStringValue(const bool &value){ + return value?std::string("true"):std::string("false"); +} + +std::string ModelBase::toStringValue(const float &value){ + return std::to_string(value); +} + +std::string ModelBase::toStringValue(const double &value){ + return std::to_string(value); +} + +bool ModelBase::fromStringValue(const std::string &inStr, std::string &value){ + value = std::string(inStr); + return true; +} + +bool ModelBase::fromStringValue(const std::string &inStr, int32_t &value){ + try { + value = std::stoi( inStr ); + } + catch (const std::invalid_argument) { + return false; + } + return true; +} + +bool ModelBase::fromStringValue(const std::string &inStr, int64_t &value){ + try { + value = std::stol( inStr ); + } + catch (const std::invalid_argument) { + return false; + } + return true; +} + +bool ModelBase::fromStringValue(const std::string &inStr, bool &value){ + inStr == "true"?value = true: value = false; + return true; +} + +bool ModelBase::fromStringValue(const std::string &inStr, float &value){ + try { + value = std::stof( inStr ); + } + catch (const std::invalid_argument) { + return false; + } + return true; +} + +bool ModelBase::fromStringValue(const std::string &inStr, double &value){ + try { + value = std::stod( inStr ); + } + catch (const std::invalid_argument) { + return false; + } + return true; +} + + } } } diff --git a/samples/server/petstore/cpp-pistache/model/ModelBase.h b/samples/server/petstore/cpp-pistache/model/ModelBase.h index 98afcd48d78e..ad3d0f817e9d 100644 --- a/samples/server/petstore/cpp-pistache/model/ModelBase.h +++ b/samples/server/petstore/cpp-pistache/model/ModelBase.h @@ -22,6 +22,7 @@ #include "json.hpp" #include #include +#include #include #include @@ -48,6 +49,45 @@ class ModelBase static double toJson( double const value ); static bool toJson( bool const value ); static nlohmann::json toJson(ModelBase const& content ); + + static std::string toStringValue(const std::string &value); + static std::string toStringValue(const int32_t &value); + static std::string toStringValue(const int64_t &value); + static std::string toStringValue(const bool &value); + static std::string toStringValue(const float &value); + static std::string toStringValue(const double &value); + + static bool fromStringValue(const std::string &inStr, std::string &value); + static bool fromStringValue(const std::string &inStr, int32_t &value); + static bool fromStringValue(const std::string &inStr, int64_t &value); + static bool fromStringValue(const std::string &inStr, bool &value); + static bool fromStringValue(const std::string &inStr, float &value); + static bool fromStringValue(const std::string &inStr, double &value); + template + static bool fromStringValue(const std::vector &inStr, std::vector &value){ + try{ + for(auto & item : inStr){ + T itemValue; + if(fromStringValue(item, itemValue)){ + value.push_back(itemValue); + } + } + } + catch(...){ + return false; + } + return value.size() > 0; + } + template + static bool fromStringValue(const std::string &inStr, std::vector &value, char separator = ','){ + std::vector inStrings; + std::istringstream f(inStr); + std::string s; + while (std::getline(f, s, separator)) { + inStrings.push_back(s); + } + return fromStringValue(inStrings, value); + } }; class ModelArrayHelper { From 597d9b20d70a0c62f552f43558867097bdc59627 Mon Sep 17 00:00:00 2001 From: etherealjoy Date: Thu, 30 Aug 2018 23:44:10 +0200 Subject: [PATCH 2/7] Align whitespace --- .../resources/cpp-pistache-server/modelbase-header.mustache | 2 +- samples/server/petstore/cpp-pistache/model/ModelBase.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/cpp-pistache-server/modelbase-header.mustache b/modules/openapi-generator/src/main/resources/cpp-pistache-server/modelbase-header.mustache index 710aa77ce524..74327ec0afdf 100644 --- a/modules/openapi-generator/src/main/resources/cpp-pistache-server/modelbase-header.mustache +++ b/modules/openapi-generator/src/main/resources/cpp-pistache-server/modelbase-header.mustache @@ -53,7 +53,7 @@ public: static bool fromStringValue(const std::string &inStr, float &value); static bool fromStringValue(const std::string &inStr, double &value); template - static bool fromStringValue(const std::vector &inStr, std::vector &value){ + static bool fromStringValue(const std::vector &inStr, std::vector &value){ try{ for(auto & item : inStr){ T itemValue; diff --git a/samples/server/petstore/cpp-pistache/model/ModelBase.h b/samples/server/petstore/cpp-pistache/model/ModelBase.h index ad3d0f817e9d..56b89c2c3ae1 100644 --- a/samples/server/petstore/cpp-pistache/model/ModelBase.h +++ b/samples/server/petstore/cpp-pistache/model/ModelBase.h @@ -64,7 +64,7 @@ class ModelBase static bool fromStringValue(const std::string &inStr, float &value); static bool fromStringValue(const std::string &inStr, double &value); template - static bool fromStringValue(const std::vector &inStr, std::vector &value){ + static bool fromStringValue(const std::vector &inStr, std::vector &value){ try{ for(auto & item : inStr){ T itemValue; From 0535b38287e7ca3fbd0b946c87b3e0d99e9bf46c Mon Sep 17 00:00:00 2001 From: etherealjoy Date: Thu, 30 Aug 2018 23:47:49 +0200 Subject: [PATCH 3/7] Align whitespace :| --- .../resources/cpp-pistache-server/modelbase-header.mustache | 6 +++--- samples/server/petstore/cpp-pistache/model/ModelBase.h | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/cpp-pistache-server/modelbase-header.mustache b/modules/openapi-generator/src/main/resources/cpp-pistache-server/modelbase-header.mustache index 74327ec0afdf..a6c321232e1e 100644 --- a/modules/openapi-generator/src/main/resources/cpp-pistache-server/modelbase-header.mustache +++ b/modules/openapi-generator/src/main/resources/cpp-pistache-server/modelbase-header.mustache @@ -46,14 +46,14 @@ public: static std::string toStringValue(const float &value); static std::string toStringValue(const double &value); - static bool fromStringValue(const std::string &inStr, std::string &value); + static bool fromStringValue(const std::string &inStr, std::string &value); static bool fromStringValue(const std::string &inStr, int32_t &value); static bool fromStringValue(const std::string &inStr, int64_t &value); static bool fromStringValue(const std::string &inStr, bool &value); static bool fromStringValue(const std::string &inStr, float &value); static bool fromStringValue(const std::string &inStr, double &value); - template - static bool fromStringValue(const std::vector &inStr, std::vector &value){ + template + static bool fromStringValue(const std::vector &inStr, std::vector &value){ try{ for(auto & item : inStr){ T itemValue; diff --git a/samples/server/petstore/cpp-pistache/model/ModelBase.h b/samples/server/petstore/cpp-pistache/model/ModelBase.h index 56b89c2c3ae1..f4e7663b7079 100644 --- a/samples/server/petstore/cpp-pistache/model/ModelBase.h +++ b/samples/server/petstore/cpp-pistache/model/ModelBase.h @@ -57,14 +57,14 @@ class ModelBase static std::string toStringValue(const float &value); static std::string toStringValue(const double &value); - static bool fromStringValue(const std::string &inStr, std::string &value); + static bool fromStringValue(const std::string &inStr, std::string &value); static bool fromStringValue(const std::string &inStr, int32_t &value); static bool fromStringValue(const std::string &inStr, int64_t &value); static bool fromStringValue(const std::string &inStr, bool &value); static bool fromStringValue(const std::string &inStr, float &value); static bool fromStringValue(const std::string &inStr, double &value); - template - static bool fromStringValue(const std::vector &inStr, std::vector &value){ + template + static bool fromStringValue(const std::vector &inStr, std::vector &value){ try{ for(auto & item : inStr){ T itemValue; From 42c2395a86997e0a07acbd7068c76dd6c34ff2cb Mon Sep 17 00:00:00 2001 From: etherealjoy Date: Thu, 30 Aug 2018 23:49:12 +0200 Subject: [PATCH 4/7] Align whitespace again :( --- .../resources/cpp-pistache-server/modelbase-header.mustache | 2 +- samples/server/petstore/cpp-pistache/model/ModelBase.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/cpp-pistache-server/modelbase-header.mustache b/modules/openapi-generator/src/main/resources/cpp-pistache-server/modelbase-header.mustache index a6c321232e1e..74cadb6ad86a 100644 --- a/modules/openapi-generator/src/main/resources/cpp-pistache-server/modelbase-header.mustache +++ b/modules/openapi-generator/src/main/resources/cpp-pistache-server/modelbase-header.mustache @@ -39,7 +39,7 @@ public: static bool toJson( bool const value ); static nlohmann::json toJson({{prefix}}ModelBase const& content ); - static std::string toStringValue(const std::string &value); + static std::string toStringValue(const std::string &value); static std::string toStringValue(const int32_t &value); static std::string toStringValue(const int64_t &value); static std::string toStringValue(const bool &value); diff --git a/samples/server/petstore/cpp-pistache/model/ModelBase.h b/samples/server/petstore/cpp-pistache/model/ModelBase.h index f4e7663b7079..aae852f3869a 100644 --- a/samples/server/petstore/cpp-pistache/model/ModelBase.h +++ b/samples/server/petstore/cpp-pistache/model/ModelBase.h @@ -50,7 +50,7 @@ class ModelBase static bool toJson( bool const value ); static nlohmann::json toJson(ModelBase const& content ); - static std::string toStringValue(const std::string &value); + static std::string toStringValue(const std::string &value); static std::string toStringValue(const int32_t &value); static std::string toStringValue(const int64_t &value); static std::string toStringValue(const bool &value); From 46af0d9359ec9e87d95bdb0f6af86e33221de721 Mon Sep 17 00:00:00 2001 From: etherealjoy Date: Fri, 31 Aug 2018 01:00:51 +0200 Subject: [PATCH 5/7] Add helpers in a separate file to handle primitive serialization --- .../languages/CppPistacheServerCodegen.java | 18 +++- .../cpp-pistache-server/api-source.mustache | 4 +- .../helpers-header.mustache | 64 ++++++++++++ .../helpers-source.mustache | 85 ++++++++++++++++ .../modelbase-header.mustache | 39 -------- .../modelbase-source.mustache | 75 -------------- .../petstore/cpp-pistache/api/PetApi.cpp | 6 +- .../petstore/cpp-pistache/api/StoreApi.cpp | 2 + .../petstore/cpp-pistache/api/UserApi.cpp | 6 +- .../petstore/cpp-pistache/model/Helpers.cpp | 97 +++++++++++++++++++ .../petstore/cpp-pistache/model/Helpers.h | 76 +++++++++++++++ .../petstore/cpp-pistache/model/ModelBase.cpp | 75 -------------- .../petstore/cpp-pistache/model/ModelBase.h | 39 -------- 13 files changed, 351 insertions(+), 235 deletions(-) create mode 100644 modules/openapi-generator/src/main/resources/cpp-pistache-server/helpers-header.mustache create mode 100644 modules/openapi-generator/src/main/resources/cpp-pistache-server/helpers-source.mustache create mode 100644 samples/server/petstore/cpp-pistache/model/Helpers.cpp create mode 100644 samples/server/petstore/cpp-pistache/model/Helpers.h diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CppPistacheServerCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CppPistacheServerCodegen.java index 08077b7decf1..ec069d483af4 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CppPistacheServerCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CppPistacheServerCodegen.java @@ -47,8 +47,10 @@ public class CppPistacheServerCodegen extends AbstractCppCodegen { protected boolean isAddExternalLibs = true; public static final String OPTIONAL_EXTERNAL_LIB = "addExternalLibs"; public static final String OPTIONAL_EXTERNAL_LIB_DESC = "Add the Possibility to fetch and compile external Libraries needed by this Framework."; + public static final String HELPERS_PACKAGE_NAME = "helpersPackage"; + public static final String HELPERS_PACKAGE_NAME_DESC = "Specify the package name to be used for the helpers (e.g. org.openapitools.server.helpers)."; protected final String PREFIX = ""; - + protected String helpersPackage = ""; @Override public CodegenType getTag() { return CodegenType.SERVER; @@ -70,6 +72,7 @@ public CppPistacheServerCodegen() { modelNamePrefix = PREFIX; } + helpersPackage = "org.openapitools.server.helpers"; apiPackage = "org.openapitools.server.api"; modelPackage = "org.openapitools.server.model"; @@ -86,11 +89,14 @@ public CppPistacheServerCodegen() { cliOptions.clear(); addSwitch(OPTIONAL_EXTERNAL_LIB, OPTIONAL_EXTERNAL_LIB_DESC, this.isAddExternalLibs); + addOption(HELPERS_PACKAGE_NAME, HELPERS_PACKAGE_NAME_DESC, this.helpersPackage); reservedWords = new HashSet<>(); supportingFiles.add(new SupportingFile("modelbase-header.mustache", "model", modelNamePrefix + "ModelBase.h")); supportingFiles.add(new SupportingFile("modelbase-source.mustache", "model", modelNamePrefix + "ModelBase.cpp")); + supportingFiles.add(new SupportingFile("helpers-header.mustache", "model", modelNamePrefix + "Helpers.h")); + supportingFiles.add(new SupportingFile("helpers-source.mustache", "model", modelNamePrefix + "Helpers.cpp")); supportingFiles.add(new SupportingFile("cmake.mustache", "", "CMakeLists.txt")); supportingFiles.add(new SupportingFile("README.mustache", "", "README.md")); @@ -123,18 +129,26 @@ public CppPistacheServerCodegen() { @Override public void processOpts() { super.processOpts(); + if (additionalProperties.containsKey(HELPERS_PACKAGE_NAME)) { + helpersPackage = (String) additionalProperties.get(HELPERS_PACKAGE_NAME); + } if (additionalProperties.containsKey("modelNamePrefix")) { additionalProperties().put("prefix", modelNamePrefix); supportingFiles.clear(); supportingFiles.add(new SupportingFile("modelbase-header.mustache", "model", modelNamePrefix + "ModelBase.h")); supportingFiles.add(new SupportingFile("modelbase-source.mustache", "model", modelNamePrefix + "ModelBase.cpp")); + supportingFiles.add(new SupportingFile("helpers-header.mustache", "model", modelNamePrefix + "Helpers.h")); + supportingFiles.add(new SupportingFile("helpers-source.mustache", "model", modelNamePrefix + "Helpers.cpp")); supportingFiles.add(new SupportingFile("cmake.mustache", "", "CMakeLists.txt")); supportingFiles.add(new SupportingFile("README.mustache", "", "README.md")); } additionalProperties.put("modelNamespaceDeclarations", modelPackage.split("\\.")); additionalProperties.put("modelNamespace", modelPackage.replaceAll("\\.", "::")); additionalProperties.put("apiNamespaceDeclarations", apiPackage.split("\\.")); - additionalProperties.put("apiNamespace", apiPackage.replaceAll("\\.", "::")); + additionalProperties.put("apiNamespace", apiPackage.replaceAll("\\.", "::")); + additionalProperties.put("helpersNamespaceDeclarations", helpersPackage.split("\\.")); + additionalProperties.put("helpersNamespace", helpersPackage.replaceAll("\\.", "::")); + if (additionalProperties.containsKey(OPTIONAL_EXTERNAL_LIB)) { setAddExternalLibs(convertPropertyToBooleanAndWriteBack(OPTIONAL_EXTERNAL_LIB)); } else { diff --git a/modules/openapi-generator/src/main/resources/cpp-pistache-server/api-source.mustache b/modules/openapi-generator/src/main/resources/cpp-pistache-server/api-source.mustache index 0ec60a145f99..58104a36223b 100644 --- a/modules/openapi-generator/src/main/resources/cpp-pistache-server/api-source.mustache +++ b/modules/openapi-generator/src/main/resources/cpp-pistache-server/api-source.mustache @@ -2,11 +2,13 @@ {{#operations}} #include "{{classname}}.h" +#include "{{prefix}}Helpers.h" {{#apiNamespaceDeclarations}} namespace {{this}} { {{/apiNamespaceDeclarations}} +using namespace {{helpersNamespace}}; {{#hasModelImport}} using namespace {{modelNamespace}};{{/hasModelImport}} @@ -66,7 +68,7 @@ void {{classname}}::{{operationIdSnakeCase}}_handler(const Pistache::Rest::Reque Pistache::Optional<{{^isContainer}}{{dataType}}{{/isContainer}}{{#isListContainer}}std::vector<{{items.baseType}}>{{/isListContainer}}> {{paramName}}; if(!{{paramName}}Query.isEmpty()){ {{^isContainer}}{{dataType}}{{/isContainer}}{{#isListContainer}}std::vector<{{items.baseType}}>{{/isListContainer}} value; - if(ModelBase::fromStringValue({{paramName}}Query.get(), value)){ + if(fromStringValue({{paramName}}Query.get(), value)){ {{paramName}} = Pistache::Some(value); } } diff --git a/modules/openapi-generator/src/main/resources/cpp-pistache-server/helpers-header.mustache b/modules/openapi-generator/src/main/resources/cpp-pistache-server/helpers-header.mustache new file mode 100644 index 000000000000..c39cca04fe10 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/cpp-pistache-server/helpers-header.mustache @@ -0,0 +1,64 @@ +{{>licenseInfo}} +/* + * {{prefix}}Helpers.h + * + * This is the helper class for models and primitives + */ + +#ifndef {{prefix}}Helpers_H_ +#define {{prefix}}Helpers_H_ + +#include +#include +#include +#include +#include + +{{#helpersNamespaceDeclarations}} +namespace {{this}} { +{{/helpersNamespaceDeclarations}} + + std::string toStringValue(const std::string &value); + std::string toStringValue(const int32_t &value); + std::string toStringValue(const int64_t &value); + std::string toStringValue(const bool &value); + std::string toStringValue(const float &value); + std::string toStringValue(const double &value); + + bool fromStringValue(const std::string &inStr, std::string &value); + bool fromStringValue(const std::string &inStr, int32_t &value); + bool fromStringValue(const std::string &inStr, int64_t &value); + bool fromStringValue(const std::string &inStr, bool &value); + bool fromStringValue(const std::string &inStr, float &value); + bool fromStringValue(const std::string &inStr, double &value); + template + bool fromStringValue(const std::vector &inStr, std::vector &value){ + try{ + for(auto & item : inStr){ + T itemValue; + if(fromStringValue(item, itemValue)){ + value.push_back(itemValue); + } + } + } + catch(...){ + return false; + } + return value.size() > 0; + } + template + bool fromStringValue(const std::string &inStr, std::vector &value, char separator = ','){ + std::vector inStrings; + std::istringstream f(inStr); + std::string s; + while (std::getline(f, s, separator)) { + inStrings.push_back(s); + } + return fromStringValue(inStrings, value); + } + +{{#helpersNamespaceDeclarations}} +} +{{/helpersNamespaceDeclarations}} + +#endif // {{prefix}}Helpers_H_ \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/cpp-pistache-server/helpers-source.mustache b/modules/openapi-generator/src/main/resources/cpp-pistache-server/helpers-source.mustache new file mode 100644 index 000000000000..e2e49bc9fc68 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/cpp-pistache-server/helpers-source.mustache @@ -0,0 +1,85 @@ +{{>licenseInfo}} +#include "{{prefix}}Helpers.h" + +{{#helpersNamespaceDeclarations}} +namespace {{this}} { +{{/helpersNamespaceDeclarations}} + + +std::string toStringValue(const std::string &value){ + return std::string(value); +} + +std::string toStringValue(const int32_t &value){ + return std::to_string(value); +} + +std::string toStringValue(const int64_t &value){ + return std::to_string(value); +} + +std::string toStringValue(const bool &value){ + return value?std::string("true"):std::string("false"); +} + +std::string toStringValue(const float &value){ + return std::to_string(value); +} + +std::string toStringValue(const double &value){ + return std::to_string(value); +} + +bool fromStringValue(const std::string &inStr, std::string &value){ + value = std::string(inStr); + return true; +} + +bool fromStringValue(const std::string &inStr, int32_t &value){ + try { + value = std::stoi( inStr ); + } + catch (const std::invalid_argument) { + return false; + } + return true; +} + +bool fromStringValue(const std::string &inStr, int64_t &value){ + try { + value = std::stol( inStr ); + } + catch (const std::invalid_argument) { + return false; + } + return true; +} + +bool fromStringValue(const std::string &inStr, bool &value){ + inStr == "true"?value = true: value = false; + return true; +} + +bool fromStringValue(const std::string &inStr, float &value){ + try { + value = std::stof( inStr ); + } + catch (const std::invalid_argument) { + return false; + } + return true; +} + +bool fromStringValue(const std::string &inStr, double &value){ + try { + value = std::stod( inStr ); + } + catch (const std::invalid_argument) { + return false; + } + return true; +} + +{{#helpersNamespaceDeclarations}} +} +{{/helpersNamespaceDeclarations}} \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/cpp-pistache-server/modelbase-header.mustache b/modules/openapi-generator/src/main/resources/cpp-pistache-server/modelbase-header.mustache index 74cadb6ad86a..4e8fbce2ed23 100644 --- a/modules/openapi-generator/src/main/resources/cpp-pistache-server/modelbase-header.mustache +++ b/modules/openapi-generator/src/main/resources/cpp-pistache-server/modelbase-header.mustache @@ -38,45 +38,6 @@ public: static double toJson( double const value ); static bool toJson( bool const value ); static nlohmann::json toJson({{prefix}}ModelBase const& content ); - - static std::string toStringValue(const std::string &value); - static std::string toStringValue(const int32_t &value); - static std::string toStringValue(const int64_t &value); - static std::string toStringValue(const bool &value); - static std::string toStringValue(const float &value); - static std::string toStringValue(const double &value); - - static bool fromStringValue(const std::string &inStr, std::string &value); - static bool fromStringValue(const std::string &inStr, int32_t &value); - static bool fromStringValue(const std::string &inStr, int64_t &value); - static bool fromStringValue(const std::string &inStr, bool &value); - static bool fromStringValue(const std::string &inStr, float &value); - static bool fromStringValue(const std::string &inStr, double &value); - template - static bool fromStringValue(const std::vector &inStr, std::vector &value){ - try{ - for(auto & item : inStr){ - T itemValue; - if(fromStringValue(item, itemValue)){ - value.push_back(itemValue); - } - } - } - catch(...){ - return false; - } - return value.size() > 0; - } - template - static bool fromStringValue(const std::string &inStr, std::vector &value, char separator = ','){ - std::vector inStrings; - std::istringstream f(inStr); - std::string s; - while (std::getline(f, s, separator)) { - inStrings.push_back(s); - } - return fromStringValue(inStrings, value); - } }; class {{prefix}}ModelArrayHelper { diff --git a/modules/openapi-generator/src/main/resources/cpp-pistache-server/modelbase-source.mustache b/modules/openapi-generator/src/main/resources/cpp-pistache-server/modelbase-source.mustache index 7f6b5a093136..f097c3a4a199 100644 --- a/modules/openapi-generator/src/main/resources/cpp-pistache-server/modelbase-source.mustache +++ b/modules/openapi-generator/src/main/resources/cpp-pistache-server/modelbase-source.mustache @@ -49,81 +49,6 @@ nlohmann::json {{prefix}}ModelBase::toJson({{prefix}}ModelBase const& content ) return content.toJson(); } -std::string ModelBase::toStringValue(const std::string &value){ - return std::string(value); -} - -std::string ModelBase::toStringValue(const int32_t &value){ - return std::to_string(value); -} - -std::string ModelBase::toStringValue(const int64_t &value){ - return std::to_string(value); -} - -std::string ModelBase::toStringValue(const bool &value){ - return value?std::string("true"):std::string("false"); -} - -std::string ModelBase::toStringValue(const float &value){ - return std::to_string(value); -} - -std::string ModelBase::toStringValue(const double &value){ - return std::to_string(value); -} - -bool ModelBase::fromStringValue(const std::string &inStr, std::string &value){ - value = std::string(inStr); - return true; -} - -bool ModelBase::fromStringValue(const std::string &inStr, int32_t &value){ - try { - value = std::stoi( inStr ); - } - catch (const std::invalid_argument) { - return false; - } - return true; -} - -bool ModelBase::fromStringValue(const std::string &inStr, int64_t &value){ - try { - value = std::stol( inStr ); - } - catch (const std::invalid_argument) { - return false; - } - return true; -} - -bool ModelBase::fromStringValue(const std::string &inStr, bool &value){ - inStr == "true"?value = true: value = false; - return true; -} - -bool ModelBase::fromStringValue(const std::string &inStr, float &value){ - try { - value = std::stof( inStr ); - } - catch (const std::invalid_argument) { - return false; - } - return true; -} - -bool ModelBase::fromStringValue(const std::string &inStr, double &value){ - try { - value = std::stod( inStr ); - } - catch (const std::invalid_argument) { - return false; - } - return true; -} - - {{#modelNamespaceDeclarations}} } {{/modelNamespaceDeclarations}} diff --git a/samples/server/petstore/cpp-pistache/api/PetApi.cpp b/samples/server/petstore/cpp-pistache/api/PetApi.cpp index c5830df4cf97..df7640483892 100644 --- a/samples/server/petstore/cpp-pistache/api/PetApi.cpp +++ b/samples/server/petstore/cpp-pistache/api/PetApi.cpp @@ -11,12 +11,14 @@ */ #include "PetApi.h" +#include "Helpers.h" namespace org { namespace openapitools { namespace server { namespace api { +using namespace org::openapitools::server::helpers; using namespace org::openapitools::server::model; PetApi::PetApi(Pistache::Address addr) @@ -98,7 +100,7 @@ void PetApi::find_pets_by_status_handler(const Pistache::Rest::Request &request, Pistache::Optional> status; if(!statusQuery.isEmpty()){ std::vector value; - if(ModelBase::fromStringValue(statusQuery.get(), value)){ + if(fromStringValue(statusQuery.get(), value)){ status = Pistache::Some(value); } } @@ -119,7 +121,7 @@ void PetApi::find_pets_by_tags_handler(const Pistache::Rest::Request &request, P Pistache::Optional> tags; if(!tagsQuery.isEmpty()){ std::vector value; - if(ModelBase::fromStringValue(tagsQuery.get(), value)){ + if(fromStringValue(tagsQuery.get(), value)){ tags = Pistache::Some(value); } } diff --git a/samples/server/petstore/cpp-pistache/api/StoreApi.cpp b/samples/server/petstore/cpp-pistache/api/StoreApi.cpp index fb17690e38f8..8097ca8f2f15 100644 --- a/samples/server/petstore/cpp-pistache/api/StoreApi.cpp +++ b/samples/server/petstore/cpp-pistache/api/StoreApi.cpp @@ -11,12 +11,14 @@ */ #include "StoreApi.h" +#include "Helpers.h" namespace org { namespace openapitools { namespace server { namespace api { +using namespace org::openapitools::server::helpers; using namespace org::openapitools::server::model; StoreApi::StoreApi(Pistache::Address addr) diff --git a/samples/server/petstore/cpp-pistache/api/UserApi.cpp b/samples/server/petstore/cpp-pistache/api/UserApi.cpp index 5632cacf777a..13b70d9215a2 100644 --- a/samples/server/petstore/cpp-pistache/api/UserApi.cpp +++ b/samples/server/petstore/cpp-pistache/api/UserApi.cpp @@ -11,12 +11,14 @@ */ #include "UserApi.h" +#include "Helpers.h" namespace org { namespace openapitools { namespace server { namespace api { +using namespace org::openapitools::server::helpers; using namespace org::openapitools::server::model; UserApi::UserApi(Pistache::Address addr) @@ -140,7 +142,7 @@ void UserApi::login_user_handler(const Pistache::Rest::Request &request, Pistach Pistache::Optional username; if(!usernameQuery.isEmpty()){ std::string value; - if(ModelBase::fromStringValue(usernameQuery.get(), value)){ + if(fromStringValue(usernameQuery.get(), value)){ username = Pistache::Some(value); } } @@ -148,7 +150,7 @@ void UserApi::login_user_handler(const Pistache::Rest::Request &request, Pistach Pistache::Optional password; if(!passwordQuery.isEmpty()){ std::string value; - if(ModelBase::fromStringValue(passwordQuery.get(), value)){ + if(fromStringValue(passwordQuery.get(), value)){ password = Pistache::Some(value); } } diff --git a/samples/server/petstore/cpp-pistache/model/Helpers.cpp b/samples/server/petstore/cpp-pistache/model/Helpers.cpp new file mode 100644 index 000000000000..1dc0bd7fac13 --- /dev/null +++ b/samples/server/petstore/cpp-pistache/model/Helpers.cpp @@ -0,0 +1,97 @@ +/** +* OpenAPI Petstore +* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. +* +* OpenAPI spec version: 1.0.0 +* +* +* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). +* https://openapi-generator.tech +* Do not edit the class manually. +*/ +#include "Helpers.h" + +namespace org { +namespace openapitools { +namespace server { +namespace helpers { + + +std::string toStringValue(const std::string &value){ + return std::string(value); +} + +std::string toStringValue(const int32_t &value){ + return std::to_string(value); +} + +std::string toStringValue(const int64_t &value){ + return std::to_string(value); +} + +std::string toStringValue(const bool &value){ + return value?std::string("true"):std::string("false"); +} + +std::string toStringValue(const float &value){ + return std::to_string(value); +} + +std::string toStringValue(const double &value){ + return std::to_string(value); +} + +bool fromStringValue(const std::string &inStr, std::string &value){ + value = std::string(inStr); + return true; +} + +bool fromStringValue(const std::string &inStr, int32_t &value){ + try { + value = std::stoi( inStr ); + } + catch (const std::invalid_argument) { + return false; + } + return true; +} + +bool fromStringValue(const std::string &inStr, int64_t &value){ + try { + value = std::stol( inStr ); + } + catch (const std::invalid_argument) { + return false; + } + return true; +} + +bool fromStringValue(const std::string &inStr, bool &value){ + inStr == "true"?value = true: value = false; + return true; +} + +bool fromStringValue(const std::string &inStr, float &value){ + try { + value = std::stof( inStr ); + } + catch (const std::invalid_argument) { + return false; + } + return true; +} + +bool fromStringValue(const std::string &inStr, double &value){ + try { + value = std::stod( inStr ); + } + catch (const std::invalid_argument) { + return false; + } + return true; +} + +} +} +} +} diff --git a/samples/server/petstore/cpp-pistache/model/Helpers.h b/samples/server/petstore/cpp-pistache/model/Helpers.h new file mode 100644 index 000000000000..8c62ecda5a0a --- /dev/null +++ b/samples/server/petstore/cpp-pistache/model/Helpers.h @@ -0,0 +1,76 @@ +/** +* OpenAPI Petstore +* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. +* +* OpenAPI spec version: 1.0.0 +* +* +* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). +* https://openapi-generator.tech +* Do not edit the class manually. +*/ +/* + * Helpers.h + * + * This is the helper class for models and primitives + */ + +#ifndef Helpers_H_ +#define Helpers_H_ + +#include +#include +#include +#include +#include + +namespace org { +namespace openapitools { +namespace server { +namespace helpers { + + std::string toStringValue(const std::string &value); + std::string toStringValue(const int32_t &value); + std::string toStringValue(const int64_t &value); + std::string toStringValue(const bool &value); + std::string toStringValue(const float &value); + std::string toStringValue(const double &value); + + bool fromStringValue(const std::string &inStr, std::string &value); + bool fromStringValue(const std::string &inStr, int32_t &value); + bool fromStringValue(const std::string &inStr, int64_t &value); + bool fromStringValue(const std::string &inStr, bool &value); + bool fromStringValue(const std::string &inStr, float &value); + bool fromStringValue(const std::string &inStr, double &value); + template + bool fromStringValue(const std::vector &inStr, std::vector &value){ + try{ + for(auto & item : inStr){ + T itemValue; + if(fromStringValue(item, itemValue)){ + value.push_back(itemValue); + } + } + } + catch(...){ + return false; + } + return value.size() > 0; + } + template + bool fromStringValue(const std::string &inStr, std::vector &value, char separator = ','){ + std::vector inStrings; + std::istringstream f(inStr); + std::string s; + while (std::getline(f, s, separator)) { + inStrings.push_back(s); + } + return fromStringValue(inStrings, value); + } + +} +} +} +} + +#endif // Helpers_H_ \ No newline at end of file diff --git a/samples/server/petstore/cpp-pistache/model/ModelBase.cpp b/samples/server/petstore/cpp-pistache/model/ModelBase.cpp index 3fa739a9f60b..19910b9d6012 100644 --- a/samples/server/petstore/cpp-pistache/model/ModelBase.cpp +++ b/samples/server/petstore/cpp-pistache/model/ModelBase.cpp @@ -60,81 +60,6 @@ nlohmann::json ModelBase::toJson(ModelBase const& content ) return content.toJson(); } -std::string ModelBase::toStringValue(const std::string &value){ - return std::string(value); -} - -std::string ModelBase::toStringValue(const int32_t &value){ - return std::to_string(value); -} - -std::string ModelBase::toStringValue(const int64_t &value){ - return std::to_string(value); -} - -std::string ModelBase::toStringValue(const bool &value){ - return value?std::string("true"):std::string("false"); -} - -std::string ModelBase::toStringValue(const float &value){ - return std::to_string(value); -} - -std::string ModelBase::toStringValue(const double &value){ - return std::to_string(value); -} - -bool ModelBase::fromStringValue(const std::string &inStr, std::string &value){ - value = std::string(inStr); - return true; -} - -bool ModelBase::fromStringValue(const std::string &inStr, int32_t &value){ - try { - value = std::stoi( inStr ); - } - catch (const std::invalid_argument) { - return false; - } - return true; -} - -bool ModelBase::fromStringValue(const std::string &inStr, int64_t &value){ - try { - value = std::stol( inStr ); - } - catch (const std::invalid_argument) { - return false; - } - return true; -} - -bool ModelBase::fromStringValue(const std::string &inStr, bool &value){ - inStr == "true"?value = true: value = false; - return true; -} - -bool ModelBase::fromStringValue(const std::string &inStr, float &value){ - try { - value = std::stof( inStr ); - } - catch (const std::invalid_argument) { - return false; - } - return true; -} - -bool ModelBase::fromStringValue(const std::string &inStr, double &value){ - try { - value = std::stod( inStr ); - } - catch (const std::invalid_argument) { - return false; - } - return true; -} - - } } } diff --git a/samples/server/petstore/cpp-pistache/model/ModelBase.h b/samples/server/petstore/cpp-pistache/model/ModelBase.h index aae852f3869a..a79d4ade12d9 100644 --- a/samples/server/petstore/cpp-pistache/model/ModelBase.h +++ b/samples/server/petstore/cpp-pistache/model/ModelBase.h @@ -49,45 +49,6 @@ class ModelBase static double toJson( double const value ); static bool toJson( bool const value ); static nlohmann::json toJson(ModelBase const& content ); - - static std::string toStringValue(const std::string &value); - static std::string toStringValue(const int32_t &value); - static std::string toStringValue(const int64_t &value); - static std::string toStringValue(const bool &value); - static std::string toStringValue(const float &value); - static std::string toStringValue(const double &value); - - static bool fromStringValue(const std::string &inStr, std::string &value); - static bool fromStringValue(const std::string &inStr, int32_t &value); - static bool fromStringValue(const std::string &inStr, int64_t &value); - static bool fromStringValue(const std::string &inStr, bool &value); - static bool fromStringValue(const std::string &inStr, float &value); - static bool fromStringValue(const std::string &inStr, double &value); - template - static bool fromStringValue(const std::vector &inStr, std::vector &value){ - try{ - for(auto & item : inStr){ - T itemValue; - if(fromStringValue(item, itemValue)){ - value.push_back(itemValue); - } - } - } - catch(...){ - return false; - } - return value.size() > 0; - } - template - static bool fromStringValue(const std::string &inStr, std::vector &value, char separator = ','){ - std::vector inStrings; - std::istringstream f(inStr); - std::string s; - while (std::getline(f, s, separator)) { - inStrings.push_back(s); - } - return fromStringValue(inStrings, value); - } }; class ModelArrayHelper { From 57d0a7dc7e3137ae90087f08ece0e8a558d9ea7c Mon Sep 17 00:00:00 2001 From: etherealjoy Date: Fri, 31 Aug 2018 02:46:06 +0200 Subject: [PATCH 6/7] Small fix to handle binary strings --- .../resources/cpp-pistache-server/model-source.mustache | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/cpp-pistache-server/model-source.mustache b/modules/openapi-generator/src/main/resources/cpp-pistache-server/model-source.mustache index 9c60bbbaae08..41d64dcf75dd 100644 --- a/modules/openapi-generator/src/main/resources/cpp-pistache-server/model-source.mustache +++ b/modules/openapi-generator/src/main/resources/cpp-pistache-server/model-source.mustache @@ -95,15 +95,15 @@ void {{classname}}::fromJson(nlohmann::json& val) } {{/isListContainer}}{{^isListContainer}}{{^isPrimitiveType}}{{^required}}if(val.find("{{baseName}}") != val.end()) { - {{#isString}}{{setter}}(val.at("{{baseName}}"));{{/isString}}{{#isByteArray}}{{setter}}(val.at("{{baseName}}")); - {{/isByteArray}}{{^isString}}{{#isDateTime}}{{setter}}(val.at("{{baseName}}")); - {{/isDateTime}}{{^isDateTime}}{{^isByteArray}}if(!val["{{baseName}}"].is_null()) + {{#isString}}{{setter}}(val.at("{{baseName}}"));{{/isString}}{{#isByteArray}}{{setter}}(val.at("{{baseName}}"));{{/isByteArray}}{{#isBinary}}{{setter}}(val.at("{{baseName}}")); + {{/isBinary}}{{^isString}}{{#isDateTime}}{{setter}}(val.at("{{baseName}}")); + {{/isDateTime}}{{^isDateTime}}{{^isByteArray}}{{^isBinary}}if(!val["{{baseName}}"].is_null()) { {{{dataType}}} newItem; newItem.fromJson(val["{{baseName}}"]); {{setter}}( newItem ); } - {{/isByteArray}}{{/isDateTime}}{{/isString}} + {{/isBinary}}{{/isByteArray}}{{/isDateTime}}{{/isString}} } {{/required}}{{#required}}{{#isString}}{{setter}}(val.at("{{baseName}}")); {{/isString}}{{^isString}}{{#isDateTime}}{{setter}}(val.at("{{baseName}}")); From 3d30d93053f0d6c559098f840c6ea2e35a677ad0 Mon Sep 17 00:00:00 2001 From: etherealjoy Date: Fri, 31 Aug 2018 09:57:01 +0200 Subject: [PATCH 7/7] cleanup some includes --- .../resources/cpp-pistache-server/helpers-source.mustache | 5 +++-- .../resources/cpp-pistache-server/modelbase-header.mustache | 1 - samples/server/petstore/cpp-pistache/model/Helpers.cpp | 5 +++-- samples/server/petstore/cpp-pistache/model/ModelBase.h | 1 - 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/cpp-pistache-server/helpers-source.mustache b/modules/openapi-generator/src/main/resources/cpp-pistache-server/helpers-source.mustache index e2e49bc9fc68..f08981eff5a6 100644 --- a/modules/openapi-generator/src/main/resources/cpp-pistache-server/helpers-source.mustache +++ b/modules/openapi-generator/src/main/resources/cpp-pistache-server/helpers-source.mustache @@ -56,8 +56,9 @@ bool fromStringValue(const std::string &inStr, int64_t &value){ } bool fromStringValue(const std::string &inStr, bool &value){ - inStr == "true"?value = true: value = false; - return true; + bool result = true; + inStr == "true"?value = true: inStr == "false"?value = false: result = false; + return result; } bool fromStringValue(const std::string &inStr, float &value){ diff --git a/modules/openapi-generator/src/main/resources/cpp-pistache-server/modelbase-header.mustache b/modules/openapi-generator/src/main/resources/cpp-pistache-server/modelbase-header.mustache index 4e8fbce2ed23..faeff4830387 100644 --- a/modules/openapi-generator/src/main/resources/cpp-pistache-server/modelbase-header.mustache +++ b/modules/openapi-generator/src/main/resources/cpp-pistache-server/modelbase-header.mustache @@ -12,7 +12,6 @@ #include "json.hpp" #include #include -#include #include #include diff --git a/samples/server/petstore/cpp-pistache/model/Helpers.cpp b/samples/server/petstore/cpp-pistache/model/Helpers.cpp index 1dc0bd7fac13..8c0e859b6421 100644 --- a/samples/server/petstore/cpp-pistache/model/Helpers.cpp +++ b/samples/server/petstore/cpp-pistache/model/Helpers.cpp @@ -67,8 +67,9 @@ bool fromStringValue(const std::string &inStr, int64_t &value){ } bool fromStringValue(const std::string &inStr, bool &value){ - inStr == "true"?value = true: value = false; - return true; + bool result = true; + inStr == "true"?value = true: inStr == "false"?value = false: result = false; + return result; } bool fromStringValue(const std::string &inStr, float &value){ diff --git a/samples/server/petstore/cpp-pistache/model/ModelBase.h b/samples/server/petstore/cpp-pistache/model/ModelBase.h index a79d4ade12d9..98afcd48d78e 100644 --- a/samples/server/petstore/cpp-pistache/model/ModelBase.h +++ b/samples/server/petstore/cpp-pistache/model/ModelBase.h @@ -22,7 +22,6 @@ #include "json.hpp" #include #include -#include #include #include