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..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 { @@ -236,7 +250,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..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}} @@ -62,7 +64,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(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/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..f08981eff5a6 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/cpp-pistache-server/helpers-source.mustache @@ -0,0 +1,86 @@ +{{>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){ + bool result = true; + inStr == "true"?value = true: inStr == "false"?value = false: result = false; + return result; +} + +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/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}}")); 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..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) @@ -94,7 +96,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(fromStringValue(statusQuery.get(), value)){ + status = Pistache::Some(value); + } + } try { this->find_pets_by_status(status, response); @@ -108,7 +117,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(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.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/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..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) @@ -136,8 +138,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(fromStringValue(usernameQuery.get(), value)){ + username = Pistache::Some(value); + } + } + auto passwordQuery = request.query().get("password"); + Pistache::Optional password; + if(!passwordQuery.isEmpty()){ + std::string value; + if(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/Helpers.cpp b/samples/server/petstore/cpp-pistache/model/Helpers.cpp new file mode 100644 index 000000000000..8c0e859b6421 --- /dev/null +++ b/samples/server/petstore/cpp-pistache/model/Helpers.cpp @@ -0,0 +1,98 @@ +/** +* 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){ + bool result = true; + inStr == "true"?value = true: inStr == "false"?value = false: result = false; + return result; +} + +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