[c++ pistache server] Support basic query handling (#943)

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
This commit is contained in:
sunn 2018-09-12 09:53:01 +02:00 committed by GitHub
parent 3cfcf77c88
commit 3d4c3c545b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
21 changed files with 413 additions and 26 deletions

View File

@ -47,8 +47,10 @@ public class CppPistacheServerCodegen extends AbstractCppCodegen {
protected boolean isAddExternalLibs = true; protected boolean isAddExternalLibs = true;
public static final String OPTIONAL_EXTERNAL_LIB = "addExternalLibs"; 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 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 final String PREFIX = "";
protected String helpersPackage = "";
@Override @Override
public CodegenType getTag() { public CodegenType getTag() {
return CodegenType.SERVER; return CodegenType.SERVER;
@ -70,6 +72,7 @@ public class CppPistacheServerCodegen extends AbstractCppCodegen {
modelNamePrefix = PREFIX; modelNamePrefix = PREFIX;
} }
helpersPackage = "org.openapitools.server.helpers";
apiPackage = "org.openapitools.server.api"; apiPackage = "org.openapitools.server.api";
modelPackage = "org.openapitools.server.model"; modelPackage = "org.openapitools.server.model";
@ -86,11 +89,14 @@ public class CppPistacheServerCodegen extends AbstractCppCodegen {
cliOptions.clear(); cliOptions.clear();
addSwitch(OPTIONAL_EXTERNAL_LIB, OPTIONAL_EXTERNAL_LIB_DESC, this.isAddExternalLibs); addSwitch(OPTIONAL_EXTERNAL_LIB, OPTIONAL_EXTERNAL_LIB_DESC, this.isAddExternalLibs);
addOption(HELPERS_PACKAGE_NAME, HELPERS_PACKAGE_NAME_DESC, this.helpersPackage);
reservedWords = new HashSet<>(); reservedWords = new HashSet<>();
supportingFiles.add(new SupportingFile("modelbase-header.mustache", "model", modelNamePrefix + "ModelBase.h")); 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("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("cmake.mustache", "", "CMakeLists.txt"));
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md")); supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
@ -123,18 +129,26 @@ public class CppPistacheServerCodegen extends AbstractCppCodegen {
@Override @Override
public void processOpts() { public void processOpts() {
super.processOpts(); super.processOpts();
if (additionalProperties.containsKey(HELPERS_PACKAGE_NAME)) {
helpersPackage = (String) additionalProperties.get(HELPERS_PACKAGE_NAME);
}
if (additionalProperties.containsKey("modelNamePrefix")) { if (additionalProperties.containsKey("modelNamePrefix")) {
additionalProperties().put("prefix", modelNamePrefix); additionalProperties().put("prefix", modelNamePrefix);
supportingFiles.clear(); supportingFiles.clear();
supportingFiles.add(new SupportingFile("modelbase-header.mustache", "model", modelNamePrefix + "ModelBase.h")); 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("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("cmake.mustache", "", "CMakeLists.txt"));
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md")); supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
} }
additionalProperties.put("modelNamespaceDeclarations", modelPackage.split("\\.")); additionalProperties.put("modelNamespaceDeclarations", modelPackage.split("\\."));
additionalProperties.put("modelNamespace", modelPackage.replaceAll("\\.", "::")); additionalProperties.put("modelNamespace", modelPackage.replaceAll("\\.", "::"));
additionalProperties.put("apiNamespaceDeclarations", apiPackage.split("\\.")); 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)) { if (additionalProperties.containsKey(OPTIONAL_EXTERNAL_LIB)) {
setAddExternalLibs(convertPropertyToBooleanAndWriteBack(OPTIONAL_EXTERNAL_LIB)); setAddExternalLibs(convertPropertyToBooleanAndWriteBack(OPTIONAL_EXTERNAL_LIB));
} else { } else {
@ -236,7 +250,7 @@ public class CppPistacheServerCodegen extends AbstractCppCodegen {
if (param.isPrimitiveType) { if (param.isPrimitiveType) {
param.dataType = "Pistache::Optional<" + param.dataType + ">"; param.dataType = "Pistache::Optional<" + param.dataType + ">";
} else { } else {
param.dataType = "Pistache::Optional<" + param.baseType + ">"; param.dataType = "Pistache::Optional<" + param.dataType + ">";
param.baseType = "Pistache::Optional<" + param.baseType + ">"; param.baseType = "Pistache::Optional<" + param.baseType + ">";
} }
} }

View File

@ -29,7 +29,7 @@ using namespace {{modelNamespace}};{{/hasModelImport}}
class {{declspec}} {{classname}} { class {{declspec}} {{classname}} {
public: public:
{{classname}}(Pistache::Address addr); {{classname}}(Pistache::Address addr);
virtual ~{{classname}}() {}; virtual ~{{classname}}() {}
void init(size_t thr); void init(size_t thr);
void start(); void start();
void shutdown(); void shutdown();

View File

@ -32,7 +32,7 @@ using namespace {{modelNamespace}};{{/hasModelImport}}
class {{classname}}Impl : public {{apiNamespace}}::{{classname}} { class {{classname}}Impl : public {{apiNamespace}}::{{classname}} {
public: public:
{{classname}}Impl(Pistache::Address addr); {{classname}}Impl(Pistache::Address addr);
~{{classname}}Impl() { }; ~{{classname}}Impl() {}
{{#operation}} {{#operation}}
{{#vendorExtensions.x-codegen-pistache-isParsingSupported}} {{#vendorExtensions.x-codegen-pistache-isParsingSupported}}

View File

@ -2,11 +2,13 @@
{{#operations}} {{#operations}}
#include "{{classname}}.h" #include "{{classname}}.h"
#include "{{prefix}}Helpers.h"
{{#apiNamespaceDeclarations}} {{#apiNamespaceDeclarations}}
namespace {{this}} { namespace {{this}} {
{{/apiNamespaceDeclarations}} {{/apiNamespaceDeclarations}}
using namespace {{helpersNamespace}};
{{#hasModelImport}} {{#hasModelImport}}
using namespace {{modelNamespace}};{{/hasModelImport}} using namespace {{modelNamespace}};{{/hasModelImport}}
@ -62,7 +64,14 @@ void {{classname}}::{{operationIdSnakeCase}}_handler(const Pistache::Rest::Reque
{{/hasBodyParam}}{{#hasQueryParams}} {{/hasBodyParam}}{{#hasQueryParams}}
// Getting the query params // Getting the query params
{{#queryParams}} {{#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}} {{/queryParams}}
{{/hasQueryParams}}{{#hasHeaderParams}} {{/hasQueryParams}}{{#hasHeaderParams}}
// Getting the header params // Getting the header params

View File

@ -56,6 +56,9 @@ set(<%classnameSnakeUpperCase%>_SERVER_SOURCES
<%#operations%> <%#operations%>
add_executable(<%classnameSnakeLowerCase%>_server add_executable(<%classnameSnakeLowerCase%>_server
${<%classnameSnakeUpperCase%>_SERVER_SOURCES}) ${<%classnameSnakeUpperCase%>_SERVER_SOURCES})
<%#addExternalLibs%>
add_dependencies(<%classnameSnakeLowerCase%>_server PISTACHE NLOHMANN)
<%/addExternalLibs%>
<%/operations%> <%/operations%>
<%/apiInfo.apis%> <%/apiInfo.apis%>

View File

@ -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 <ctime>
#include <string>
#include <sstream>
#include <vector>
#include <map>
{{#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<typename T>
bool fromStringValue(const std::vector<std::string> &inStr, std::vector<T> &value){
try{
for(auto & item : inStr){
T itemValue;
if(fromStringValue(item, itemValue)){
value.push_back(itemValue);
}
}
}
catch(...){
return false;
}
return value.size() > 0;
}
template<typename T>
bool fromStringValue(const std::string &inStr, std::vector<T> &value, char separator = ','){
std::vector<std::string> 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_

View File

@ -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}}

View File

@ -95,15 +95,15 @@ void {{classname}}::fromJson(nlohmann::json& val)
} }
{{/isListContainer}}{{^isListContainer}}{{^isPrimitiveType}}{{^required}}if(val.find("{{baseName}}") != val.end()) {{/isListContainer}}{{^isListContainer}}{{^isPrimitiveType}}{{^required}}if(val.find("{{baseName}}") != val.end())
{ {
{{#isString}}{{setter}}(val.at("{{baseName}}"));{{/isString}}{{#isByteArray}}{{setter}}(val.at("{{baseName}}")); {{#isString}}{{setter}}(val.at("{{baseName}}"));{{/isString}}{{#isByteArray}}{{setter}}(val.at("{{baseName}}"));{{/isByteArray}}{{#isBinary}}{{setter}}(val.at("{{baseName}}"));
{{/isByteArray}}{{^isString}}{{#isDateTime}}{{setter}}(val.at("{{baseName}}")); {{/isBinary}}{{^isString}}{{#isDateTime}}{{setter}}(val.at("{{baseName}}"));
{{/isDateTime}}{{^isDateTime}}{{^isByteArray}}if(!val["{{baseName}}"].is_null()) {{/isDateTime}}{{^isDateTime}}{{^isByteArray}}{{^isBinary}}if(!val["{{baseName}}"].is_null())
{ {
{{{dataType}}} newItem; {{{dataType}}} newItem;
newItem.fromJson(val["{{baseName}}"]); newItem.fromJson(val["{{baseName}}"]);
{{setter}}( newItem ); {{setter}}( newItem );
} }
{{/isByteArray}}{{/isDateTime}}{{/isString}} {{/isBinary}}{{/isByteArray}}{{/isDateTime}}{{/isString}}
} }
{{/required}}{{#required}}{{#isString}}{{setter}}(val.at("{{baseName}}")); {{/required}}{{#required}}{{#isString}}{{setter}}(val.at("{{baseName}}"));
{{/isString}}{{^isString}}{{#isDateTime}}{{setter}}(val.at("{{baseName}}")); {{/isString}}{{^isString}}{{#isDateTime}}{{setter}}(val.at("{{baseName}}"));

View File

@ -63,10 +63,13 @@ UserApiMainServer.cpp
add_executable(pet_api_server add_executable(pet_api_server
${PET_API_SERVER_SOURCES}) ${PET_API_SERVER_SOURCES})
add_dependencies(pet_api_server PISTACHE NLOHMANN)
add_executable(store_api_server add_executable(store_api_server
${STORE_API_SERVER_SOURCES}) ${STORE_API_SERVER_SOURCES})
add_dependencies(store_api_server PISTACHE NLOHMANN)
add_executable(user_api_server add_executable(user_api_server
${USER_API_SERVER_SOURCES}) ${USER_API_SERVER_SOURCES})
add_dependencies(user_api_server PISTACHE NLOHMANN)
target_link_libraries(pet_api_server pistache pthread) target_link_libraries(pet_api_server pistache pthread)
target_link_libraries(store_api_server pistache pthread) target_link_libraries(store_api_server pistache pthread)

View File

@ -11,12 +11,14 @@
*/ */
#include "PetApi.h" #include "PetApi.h"
#include "Helpers.h"
namespace org { namespace org {
namespace openapitools { namespace openapitools {
namespace server { namespace server {
namespace api { namespace api {
using namespace org::openapitools::server::helpers;
using namespace org::openapitools::server::model; using namespace org::openapitools::server::model;
PetApi::PetApi(Pistache::Address addr) 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) { void PetApi::find_pets_by_status_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response) {
// Getting the query params // Getting the query params
auto status = request.query().get("status"); auto statusQuery = request.query().get("status");
Pistache::Optional<std::vector<std::string>> status;
if(!statusQuery.isEmpty()){
std::vector<std::string> value;
if(fromStringValue(statusQuery.get(), value)){
status = Pistache::Some(value);
}
}
try { try {
this->find_pets_by_status(status, response); 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) { void PetApi::find_pets_by_tags_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response) {
// Getting the query params // Getting the query params
auto tags = request.query().get("tags"); auto tagsQuery = request.query().get("tags");
Pistache::Optional<std::vector<std::string>> tags;
if(!tagsQuery.isEmpty()){
std::vector<std::string> value;
if(fromStringValue(tagsQuery.get(), value)){
tags = Pistache::Some(value);
}
}
try { try {
this->find_pets_by_tags(tags, response); this->find_pets_by_tags(tags, response);

View File

@ -40,7 +40,7 @@ using namespace org::openapitools::server::model;
class PetApi { class PetApi {
public: public:
PetApi(Pistache::Address addr); PetApi(Pistache::Address addr);
virtual ~PetApi() {}; virtual ~PetApi() {}
void init(size_t thr); void init(size_t thr);
void start(); void start();
void shutdown(); void shutdown();
@ -90,7 +90,7 @@ private:
/// Multiple status values can be provided with comma separated strings /// Multiple status values can be provided with comma separated strings
/// </remarks> /// </remarks>
/// <param name="status">Status values that need to be considered for filter</param> /// <param name="status">Status values that need to be considered for filter</param>
virtual void find_pets_by_status(const Pistache::Optional<std::string> &status, Pistache::Http::ResponseWriter &response) = 0; virtual void find_pets_by_status(const Pistache::Optional<std::vector<std::string>> &status, Pistache::Http::ResponseWriter &response) = 0;
/// <summary> /// <summary>
/// Finds Pets by tags /// Finds Pets by tags
@ -99,7 +99,7 @@ private:
/// Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing. /// Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.
/// </remarks> /// </remarks>
/// <param name="tags">Tags to filter by</param> /// <param name="tags">Tags to filter by</param>
virtual void find_pets_by_tags(const Pistache::Optional<std::string> &tags, Pistache::Http::ResponseWriter &response) = 0; virtual void find_pets_by_tags(const Pistache::Optional<std::vector<std::string>> &tags, Pistache::Http::ResponseWriter &response) = 0;
/// <summary> /// <summary>
/// Find pet by ID /// Find pet by ID

View File

@ -11,12 +11,14 @@
*/ */
#include "StoreApi.h" #include "StoreApi.h"
#include "Helpers.h"
namespace org { namespace org {
namespace openapitools { namespace openapitools {
namespace server { namespace server {
namespace api { namespace api {
using namespace org::openapitools::server::helpers;
using namespace org::openapitools::server::model; using namespace org::openapitools::server::model;
StoreApi::StoreApi(Pistache::Address addr) StoreApi::StoreApi(Pistache::Address addr)

View File

@ -40,7 +40,7 @@ using namespace org::openapitools::server::model;
class StoreApi { class StoreApi {
public: public:
StoreApi(Pistache::Address addr); StoreApi(Pistache::Address addr);
virtual ~StoreApi() {}; virtual ~StoreApi() {}
void init(size_t thr); void init(size_t thr);
void start(); void start();
void shutdown(); void shutdown();

View File

@ -11,12 +11,14 @@
*/ */
#include "UserApi.h" #include "UserApi.h"
#include "Helpers.h"
namespace org { namespace org {
namespace openapitools { namespace openapitools {
namespace server { namespace server {
namespace api { namespace api {
using namespace org::openapitools::server::helpers;
using namespace org::openapitools::server::model; using namespace org::openapitools::server::model;
UserApi::UserApi(Pistache::Address addr) 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) { void UserApi::login_user_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response) {
// Getting the query params // Getting the query params
auto username = request.query().get("username"); auto usernameQuery = request.query().get("username");
auto password = request.query().get("password"); Pistache::Optional<std::string> username;
if(!usernameQuery.isEmpty()){
std::string value;
if(fromStringValue(usernameQuery.get(), value)){
username = Pistache::Some(value);
}
}
auto passwordQuery = request.query().get("password");
Pistache::Optional<std::string> password;
if(!passwordQuery.isEmpty()){
std::string value;
if(fromStringValue(passwordQuery.get(), value)){
password = Pistache::Some(value);
}
}
try { try {
this->login_user(username, password, response); this->login_user(username, password, response);

View File

@ -40,7 +40,7 @@ using namespace org::openapitools::server::model;
class UserApi { class UserApi {
public: public:
UserApi(Pistache::Address addr); UserApi(Pistache::Address addr);
virtual ~UserApi() {}; virtual ~UserApi() {}
void init(size_t thr); void init(size_t thr);
void start(); void start();
void shutdown(); void shutdown();

View File

@ -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<Pistache::Http::Header::Raw> &apiKey, Pistache::Http::ResponseWriter &response) { void PetApiImpl::delete_pet(const int64_t &petId, const Pistache::Optional<Pistache::Http::Header::Raw> &apiKey, Pistache::Http::ResponseWriter &response) {
response.send(Pistache::Http::Code::Ok, "Do some magic\n"); response.send(Pistache::Http::Code::Ok, "Do some magic\n");
} }
void PetApiImpl::find_pets_by_status(const Pistache::Optional<std::string> &status, Pistache::Http::ResponseWriter &response) { void PetApiImpl::find_pets_by_status(const Pistache::Optional<std::vector<std::string>> &status, Pistache::Http::ResponseWriter &response) {
response.send(Pistache::Http::Code::Ok, "Do some magic\n"); response.send(Pistache::Http::Code::Ok, "Do some magic\n");
} }
void PetApiImpl::find_pets_by_tags(const Pistache::Optional<std::string> &tags, Pistache::Http::ResponseWriter &response) { void PetApiImpl::find_pets_by_tags(const Pistache::Optional<std::vector<std::string>> &tags, Pistache::Http::ResponseWriter &response) {
response.send(Pistache::Http::Code::Ok, "Do some magic\n"); response.send(Pistache::Http::Code::Ok, "Do some magic\n");
} }
void PetApiImpl::get_pet_by_id(const int64_t &petId, Pistache::Http::ResponseWriter &response) { void PetApiImpl::get_pet_by_id(const int64_t &petId, Pistache::Http::ResponseWriter &response) {

View File

@ -43,12 +43,12 @@ using namespace org::openapitools::server::model;
class PetApiImpl : public org::openapitools::server::api::PetApi { class PetApiImpl : public org::openapitools::server::api::PetApi {
public: public:
PetApiImpl(Pistache::Address addr); PetApiImpl(Pistache::Address addr);
~PetApiImpl() { }; ~PetApiImpl() {}
void add_pet(const Pet &pet, Pistache::Http::ResponseWriter &response); void add_pet(const Pet &pet, Pistache::Http::ResponseWriter &response);
void delete_pet(const int64_t &petId, const Pistache::Optional<Pistache::Http::Header::Raw> &apiKey, Pistache::Http::ResponseWriter &response); void delete_pet(const int64_t &petId, const Pistache::Optional<Pistache::Http::Header::Raw> &apiKey, Pistache::Http::ResponseWriter &response);
void find_pets_by_status(const Pistache::Optional<std::string> &status, Pistache::Http::ResponseWriter &response); void find_pets_by_status(const Pistache::Optional<std::vector<std::string>> &status, Pistache::Http::ResponseWriter &response);
void find_pets_by_tags(const Pistache::Optional<std::string> &tags, Pistache::Http::ResponseWriter &response); void find_pets_by_tags(const Pistache::Optional<std::vector<std::string>> &tags, Pistache::Http::ResponseWriter &response);
void get_pet_by_id(const int64_t &petId, 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(const Pet &pet, Pistache::Http::ResponseWriter &response);
void update_pet_with_form(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter &response); void update_pet_with_form(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter &response);

View File

@ -43,7 +43,7 @@ using namespace org::openapitools::server::model;
class StoreApiImpl : public org::openapitools::server::api::StoreApi { class StoreApiImpl : public org::openapitools::server::api::StoreApi {
public: public:
StoreApiImpl(Pistache::Address addr); StoreApiImpl(Pistache::Address addr);
~StoreApiImpl() { }; ~StoreApiImpl() {}
void delete_order(const std::string &orderId, Pistache::Http::ResponseWriter &response); void delete_order(const std::string &orderId, Pistache::Http::ResponseWriter &response);
void get_inventory(Pistache::Http::ResponseWriter &response); void get_inventory(Pistache::Http::ResponseWriter &response);

View File

@ -43,7 +43,7 @@ using namespace org::openapitools::server::model;
class UserApiImpl : public org::openapitools::server::api::UserApi { class UserApiImpl : public org::openapitools::server::api::UserApi {
public: public:
UserApiImpl(Pistache::Address addr); UserApiImpl(Pistache::Address addr);
~UserApiImpl() { }; ~UserApiImpl() {}
void create_user(const User &user, Pistache::Http::ResponseWriter &response); void create_user(const User &user, Pistache::Http::ResponseWriter &response);
void create_users_with_array_input(const std::vector<User> &user, Pistache::Http::ResponseWriter &response); void create_users_with_array_input(const std::vector<User> &user, Pistache::Http::ResponseWriter &response);

View File

@ -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;
}
}
}
}
}

View File

@ -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 <ctime>
#include <string>
#include <sstream>
#include <vector>
#include <map>
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<typename T>
bool fromStringValue(const std::vector<std::string> &inStr, std::vector<T> &value){
try{
for(auto & item : inStr){
T itemValue;
if(fromStringValue(item, itemValue)){
value.push_back(itemValue);
}
}
}
catch(...){
return false;
}
return value.size() > 0;
}
template<typename T>
bool fromStringValue(const std::string &inStr, std::vector<T> &value, char separator = ','){
std::vector<std::string> 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_