[cpp-pistache] Fix compilation of petstore for Pistache (#497)

* Fix compilation of petstore for Pistache
Add Map support

* Add support for ByteArray

* Add Support for ByteArray in cpprest

* Implement TODOs
This commit is contained in:
sunn 2018-07-20 16:04:25 +02:00 committed by William Cheng
parent 1b2d12286f
commit 97d6b71460
29 changed files with 284 additions and 68 deletions

View File

@ -102,6 +102,7 @@ public class CppPistacheServerCodegen extends AbstractCppCodegen {
typeMapping.put("binary", "std::string"); typeMapping.put("binary", "std::string");
typeMapping.put("number", "double"); typeMapping.put("number", "double");
typeMapping.put("UUID", "std::string"); typeMapping.put("UUID", "std::string");
typeMapping.put("ByteArray", "std::string");
super.importMapping = new HashMap<String, String>(); super.importMapping = new HashMap<String, String>();
importMapping.put("std::vector", "#include <vector>"); importMapping.put("std::vector", "#include <vector>");
@ -292,6 +293,9 @@ public class CppPistacheServerCodegen extends AbstractCppCodegen {
Schema inner = (Schema) p.getAdditionalProperties(); Schema inner = (Schema) p.getAdditionalProperties();
return getSchemaType(p) + "<std::string, " + getTypeDeclaration(inner) + ">"; return getSchemaType(p) + "<std::string, " + getTypeDeclaration(inner) + ">";
} }
else if (ModelUtils.isByteArraySchema(p)) {
return "std::string";
}
if (ModelUtils.isStringSchema(p) if (ModelUtils.isStringSchema(p)
|| ModelUtils.isDateSchema(p) || ModelUtils.isDateSchema(p)
|| ModelUtils.isDateTimeSchema(p) || ModelUtils.isFileSchema(p) || ModelUtils.isDateTimeSchema(p) || ModelUtils.isFileSchema(p)
@ -320,6 +324,9 @@ public class CppPistacheServerCodegen extends AbstractCppCodegen {
return "0L"; return "0L";
} }
return "0"; return "0";
}
else if (ModelUtils.isByteArraySchema(p)) {
return "";
} else if (ModelUtils.isMapSchema(p)) { } else if (ModelUtils.isMapSchema(p)) {
String inner = getSchemaType((Schema) p.getAdditionalProperties()); String inner = getSchemaType((Schema) p.getAdditionalProperties());
return "std::map<std::string, " + inner + ">()"; return "std::map<std::string, " + inner + ">()";
@ -403,4 +410,9 @@ public class CppPistacheServerCodegen extends AbstractCppCodegen {
public String escapeUnsafeCharacters(String input) { public String escapeUnsafeCharacters(String input) {
return input.replace("*/", "*_/").replace("/*", "/_*"); return input.replace("*/", "*_/").replace("/*", "/_*");
} }
@Override
public String getTypeDeclaration(String str) {
return toModelName(str);
}
} }

View File

@ -153,6 +153,7 @@ public class CppRestSdkClientCodegen extends AbstractCppCodegen {
typeMapping.put("binary", "std::string"); typeMapping.put("binary", "std::string");
typeMapping.put("number", "double"); typeMapping.put("number", "double");
typeMapping.put("UUID", "utility::string_t"); typeMapping.put("UUID", "utility::string_t");
typeMapping.put("ByteArray", "utility::string_t");
super.importMapping = new HashMap<String, String>(); super.importMapping = new HashMap<String, String>();
importMapping.put("std::vector", "#include <vector>"); importMapping.put("std::vector", "#include <vector>");

View File

@ -52,8 +52,8 @@ void {{classname}}::{{operationIdSnakeCase}}_handler(const Pistache::Rest::Reque
{{/hasPathParams}}{{#hasBodyParam}} {{/hasPathParams}}{{#hasBodyParam}}
// Getting the body param // Getting the body param
{{#bodyParam}} {{#bodyParam}}
{{^isPrimitiveType}} {{^isPrimitiveType}}{{^isContainer}}
{{baseType}} {{paramName}};{{/isPrimitiveType}} {{baseType}} {{paramName}};{{/isContainer}}{{#isListContainer}}std::vector<{{items.baseType}}> {{paramName}};{{/isListContainer}}{{#isMapContainer}}std::map<std::string, {{items.baseType}}> {{paramName}};{{/isMapContainer}}{{/isPrimitiveType}}
{{#isPrimitiveType}} {{#isPrimitiveType}}
{{dataType}} {{paramName}}; {{dataType}} {{paramName}};
{{/isPrimitiveType}} {{/isPrimitiveType}}
@ -74,9 +74,9 @@ void {{classname}}::{{operationIdSnakeCase}}_handler(const Pistache::Rest::Reque
{{#hasBodyParam}} {{#hasBodyParam}}
{{#bodyParam}} {{#bodyParam}}
nlohmann::json request_body = nlohmann::json::parse(request.body()); nlohmann::json request_body = nlohmann::json::parse(request.body());
{{^isPrimitiveType}} {{^isPrimitiveType}}{{^isContainer}}
{{paramName}}.fromJson(request_body); {{paramName}}.fromJson(request_body);
{{/isPrimitiveType}} {{/isContainer}}{{/isPrimitiveType}}{{#isContainer}} {{paramName}} = {{#isListContainer}} ModelArrayHelper{{/isListContainer}}{{#isMapContainer}} ModelMapHelper{{/isMapContainer}}::fromJson<{{items.baseType}}>(request_body);{{/isContainer}}
{{#isPrimitiveType}} {{#isPrimitiveType}}
// The conversion is done automatically by the json library // The conversion is done automatically by the json library
{{paramName}} = request_body; {{paramName}} = request_body;

View File

@ -2,7 +2,7 @@ cmake_minimum_required (VERSION 3.2)
project(server) project(server)
set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} -std=c++11) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pg -g3" )
link_directories(/usr/local/lib/) link_directories(/usr/local/lib/)

View File

@ -79,11 +79,11 @@ void {{classname}}::fromJson(nlohmann::json& val)
{{/items.isDateTime}}{{^items.isDateTime}} {{/items.isDateTime}}{{^items.isDateTime}}
if(item.is_null()) if(item.is_null())
{ {
m_{{name}}.push_back( {{{items.datatype}}}(nullptr) ); m_{{name}}.push_back( {{{items.datatype}}}() );
} }
else else
{ {
{{{items.datatype}}} newItem({{{items.defaultValue}}}); {{{items.datatype}}} newItem;
newItem.fromJson(item); newItem.fromJson(item);
m_{{name}}.push_back( newItem ); m_{{name}}.push_back( newItem );
} }
@ -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}}{{setter}}(val.at("{{baseName}}"));{{/isString}}{{#isByteArray}}{{setter}}(val.at("{{baseName}}"));
{{/isString}}{{^isString}}{{#isDateTime}}{{setter}}(val.at("{{baseName}}")); {{/isByteArray}}{{^isString}}{{#isDateTime}}{{setter}}(val.at("{{baseName}}"));
{{/isDateTime}}{{^isDateTime}}if(!val["{{baseName}}"].is_null()) {{/isDateTime}}{{^isDateTime}}{{^isByteArray}}if(!val["{{baseName}}"].is_null())
{ {
{{{dataType}}} newItem({{{defaultValue}}}); {{{dataType}}} newItem;
newItem.fromJson(val["{{baseName}}"]); newItem.fromJson(val["{{baseName}}"]);
{{setter}}( newItem ); {{setter}}( newItem );
} }
{{/isDateTime}}{{/isString}} {{/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

@ -12,6 +12,8 @@
#include "json.hpp" #include "json.hpp"
#include <ctime> #include <ctime>
#include <string> #include <string>
#include <vector>
#include <map>
{{#modelNamespaceDeclarations}} {{#modelNamespaceDeclarations}}
namespace {{this}} { namespace {{this}} {
@ -34,8 +36,103 @@ public:
static int64_t toJson( int64_t const value ); static int64_t toJson( int64_t const value );
static double toJson( double const value ); static double toJson( double const value );
static bool toJson( bool const value ); static bool toJson( bool const value );
static nlohmann::json toJson(ModelBase const& content ); static nlohmann::json toJson(ModelBase const& content );
};
class ModelArrayHelper {
public:
template<typename T>
static std::vector<T> fromJson(nlohmann::json& json) {
T *ptrTest;
std::vector<T> val;
if (dynamic_cast<ModelBase*>(ptrTest) != nullptr) {
if (!json.empty()) {
for (auto &item : json.items()) {
T entry;
entry.fromJson(item.value());
val.push_back(entry);
}
}
}
return val;
}
template<typename T>
static nlohmann::json toJson(std::vector<T> val) {
nlohmann::json json;
for(auto item : val){
json.push_back(item.toJson());
}
return json;
}
};
class ArrayHelper {
public:
template<typename T>
static std::vector<T> fromJson(nlohmann::json& json) {
std::vector<T> val;
nlohmann::from_json(json, val);
return val;
}
template<typename T>
static nlohmann::json toJson(std::vector<T> val) {
nlohmann::json json;
nlohmann::to_json(json, val);
return json;
}
};
class ModelMapHelper {
public:
template<typename T>
static std::map<std::string, T> & fromJson(nlohmann::json& json) {
T *ptrTest;
std::map<std::string, T> val;
if (dynamic_cast<ModelBase*>(ptrTest) != nullptr) {
if (!json.empty()) {
for (auto &item : json.items()) {
T entry;
entry.fromJson(item.value());
val.insert(val.end(),
std::pair<std::string, T>(item.key(), entry));
}
}
}
return val;
}
template<typename T>
static nlohmann::json toJson(std::map<std::string, T> val) {
nlohmann::json json;
for (auto const& item : val) {
json[item.first] = item.second.toJson();
}
return json;
}
};
class MapHelper {
public:
template<typename T>
static std::map<std::string, T> & fromJson(nlohmann::json& json) {
std::map<std::string, T> val;
if (!json.empty()) {
for (auto &item : json.items()) {
T entry = item.value();
val.insert(val.end(),
std::pair<std::string, T>(item.key(), entry));
}
}
return val;
}
template<typename T>
static nlohmann::json toJson(std::map<std::string, T> val) {
nlohmann::json json;
for (auto const& item : val) {
nlohmann::json jitem = item.second;
json[item.first] = jitem;
}
return json;
}
}; };
{{#modelNamespaceDeclarations}} {{#modelNamespaceDeclarations}}

View File

@ -44,7 +44,7 @@ bool ModelBase::toJson( bool const value )
return value; return value;
} }
nlohmann::json ModelBase::toJson(ModelBase content ) nlohmann::json ModelBase::toJson(ModelBase const& content )
{ {
return content.toJson(); return content.toJson();
} }

View File

@ -245,26 +245,29 @@ void {{classname}}::fromJson(web::json::value& val)
{{#isString}} {{#isString}}
{{setter}}(ModelBase::stringFromJson(val[utility::conversions::to_string_t("{{baseName}}")])); {{setter}}(ModelBase::stringFromJson(val[utility::conversions::to_string_t("{{baseName}}")]));
{{/isString}} {{/isString}}
{{#isByteArray}}{{setter}}(ModelBase::stringFromJson(val[utility::conversions::to_string_t("{{baseName}}")]));{{/isByteArray}}
{{^isString}} {{^isString}}
{{#isDateTime}} {{#isDateTime}}
{{setter}}(ModelBase::dateFromJson(val[utility::conversions::to_string_t("{{baseName}}")])); {{setter}}(ModelBase::dateFromJson(val[utility::conversions::to_string_t("{{baseName}}")]));
{{/isDateTime}} {{/isDateTime}}
{{^isDateTime}} {{^isDateTime}}{{^isByteArray}}
if(!val[utility::conversions::to_string_t("{{baseName}}")].is_null()) if(!val[utility::conversions::to_string_t("{{baseName}}")].is_null())
{ {
{{{dataType}}} newItem({{{defaultValue}}}); {{{dataType}}} newItem({{{defaultValue}}});
newItem->fromJson(val[utility::conversions::to_string_t("{{baseName}}")]); newItem->fromJson(val[utility::conversions::to_string_t("{{baseName}}")]);
{{setter}}( newItem ); {{setter}}( newItem );
} }
{{/isDateTime}} {{/isByteArray}}{{/isDateTime}}
{{/isString}} {{/isString}}
} }
{{/required}} {{/required}}
{{#required}} {{#required}}
{{#isString}} {{#isString}}
{{setter}}(ModelBase::stringFromJson(val[utility::conversions::to_string_t("{{baseName}}")])); {{setter}}(ModelBase::stringFromJson(val[utility::conversions::to_string_t("{{baseName}}")]));
{{/isString}} {{/isString}}{{#isByteArray}}
{{^isString}} {{setter}}(ModelBase::stringFromJson(val[utility::conversions::to_string_t("{{baseName}}")]));
{{/isByteArray}}
{{^isString}}{{^isByteArray}}
{{#isDateTime}} {{#isDateTime}}
{{setter}} {{setter}}
(ModelBase::dateFromJson(val[utility::conversions::to_string_t("{{baseName}}")])); (ModelBase::dateFromJson(val[utility::conversions::to_string_t("{{baseName}}")]));
@ -279,7 +282,7 @@ void {{classname}}::fromJson(web::json::value& val)
{{setter}}( new{{name}} ); {{setter}}( new{{name}} );
{{/vendorExtensions.x-codegen-file}} {{/vendorExtensions.x-codegen-file}}
{{/isDateTime}} {{/isDateTime}}
{{/isString}} {{/isByteArray}}{{/isString}}
{{/required}} {{/required}}
{{/isPrimitiveType}} {{/isPrimitiveType}}
{{/isMapContainer}} {{/isMapContainer}}
@ -353,31 +356,33 @@ void {{classname}}::toMultipart(std::shared_ptr<MultipartFormData> multipart, co
{{^required}} {{^required}}
if(m_{{name}}IsSet) if(m_{{name}}IsSet)
{ {
{{#isString}}multipart->add(ModelBase::toHttpContent(namePrefix + utility::conversions::to_string_t("{{baseName}}"), m_{{name}})); {{#isString}}multipart->add(ModelBase::toHttpContent(namePrefix + utility::conversions::to_string_t("{{baseName}}"), m_{{name}}));{{/isString}}{{#isByteArray}}multipart->add(ModelBase::toHttpContent(namePrefix + utility::conversions::to_string_t("{{baseName}}"), m_{{name}}));
{{/isString}}{{^isString}}{{#isDateTime}}multipart->add(ModelBase::toHttpContent(namePrefix + utility::conversions::to_string_t("{{baseName}}"), m_{{name}})); {{/isByteArray}}{{^isString}}{{#isDateTime}}multipart->add(ModelBase::toHttpContent(namePrefix + utility::conversions::to_string_t("{{baseName}}"), m_{{name}}));
{{/isDateTime}}{{^isDateTime}}if (m_{{name}}.get()) {{/isDateTime}}{{^isDateTime}}{{^isByteArray}}if (m_{{name}}.get())
{ {
m_{{name}}->toMultipart(multipart, utility::conversions::to_string_t("{{baseName}}.")); m_{{name}}->toMultipart(multipart, utility::conversions::to_string_t("{{baseName}}."));
} }
{{/isDateTime}}{{/isString}} {{/isByteArray}}{{/isDateTime}}{{/isString}}
} }
{{/required}} {{/required}}
{{#required}} {{#required}}
{{#isString}} {{#isString}}
multipart->add(ModelBase::toHttpContent(namePrefix + utility::conversions::to_string_t("{{baseName}}"), m_{{name}})); multipart->add(ModelBase::toHttpContent(namePrefix + utility::conversions::to_string_t("{{baseName}}"), m_{{name}}));
{{/isString}} {{/isString}}{{#isByteArray}}
multipart->add(ModelBase::toHttpContent(namePrefix + utility::conversions::to_string_t("{{baseName}}"), m_{{name}}));
{{/isByteArray}}
{{^isString}} {{^isString}}
{{#isDateTime}} {{#isDateTime}}
multipart->add(ModelBase::toHttpContent(namePrefix + utility::conversions::to_string_t("{{baseName}}"), m_{{name}})); multipart->add(ModelBase::toHttpContent(namePrefix + utility::conversions::to_string_t("{{baseName}}"), m_{{name}}));
{{/isDateTime}} {{/isDateTime}}
{{^isDateTime}} {{^isDateTime}}{{^isByteArray}}
{{#vendorExtensions.x-codegen-file}} {{#vendorExtensions.x-codegen-file}}
multipart->add(ModelBase::toHttpContent(namePrefix + utility::conversions::to_string_t("{{baseName}}"), m_{{name}})); multipart->add(ModelBase::toHttpContent(namePrefix + utility::conversions::to_string_t("{{baseName}}"), m_{{name}}));
{{/vendorExtensions.x-codegen-file}} {{/vendorExtensions.x-codegen-file}}
{{^vendorExtensions.x-codegen-file}} {{^vendorExtensions.x-codegen-file}}
m_{{name}}->toMultipart(multipart, utility::conversions::to_string_t("{{baseName}}.")); m_{{name}}->toMultipart(multipart, utility::conversions::to_string_t("{{baseName}}."));
{{/vendorExtensions.x-codegen-file}} {{/vendorExtensions.x-codegen-file}}
{{/isDateTime}} {{/isByteArray}}{{/isDateTime}}
{{/isString}} {{/isString}}
{{/required}} {{/required}}
{{/isPrimitiveType}} {{/isPrimitiveType}}
@ -508,7 +513,8 @@ void {{classname}}::fromMultiPart(std::shared_ptr<MultipartFormData> multipart,
{{#isString}} {{#isString}}
{{setter}}(ModelBase::stringFromHttpContent(multipart->getContent(utility::conversions::to_string_t("{{baseName}}")))); {{setter}}(ModelBase::stringFromHttpContent(multipart->getContent(utility::conversions::to_string_t("{{baseName}}"))));
{{/isString}} {{/isString}}
{{^isString}} {{#isByteArray}}{{setter}}(ModelBase::stringFromHttpContent(multipart->getContent(utility::conversions::to_string_t("{{baseName}}"))));{{/isByteArray}}
{{^isString}}{{^isByteArray}}
{{#isDateTime}} {{#isDateTime}}
{{setter}}(ModelBase::dateFromHttpContent(multipart->getContent(utility::conversions::to_string_t("{{baseName}}")))); {{setter}}(ModelBase::dateFromHttpContent(multipart->getContent(utility::conversions::to_string_t("{{baseName}}"))));
{{/isDateTime}} {{/isDateTime}}
@ -520,14 +526,14 @@ void {{classname}}::fromMultiPart(std::shared_ptr<MultipartFormData> multipart,
{{setter}}( newItem ); {{setter}}( newItem );
} }
{{/isDateTime}} {{/isDateTime}}
{{/isString}} {{/isByteArray}}{{/isString}}
} }
{{/required}} {{/required}}
{{#required}} {{#required}}
{{#isString}} {{#isString}}
{{setter}}(ModelBase::stringFromHttpContent(multipart->getContent(utility::conversions::to_string_t("{{baseName}}")))); {{setter}}(ModelBase::stringFromHttpContent(multipart->getContent(utility::conversions::to_string_t("{{baseName}}"))));
{{/isString}} {{/isString}}{{#isByteArray}}{{setter}}(ModelBase::stringFromHttpContent(multipart->getContent(utility::conversions::to_string_t("{{baseName}}"))));{{/isByteArray}}
{{^isString}} {{^isString}}{{^isByteArray}}
{{#isDateTime}} {{#isDateTime}}
{{setter}}(ModelBase::dateFromHttpContent(multipart->getContent(utility::conversions::to_string_t("{{baseName}}")))); {{setter}}(ModelBase::dateFromHttpContent(multipart->getContent(utility::conversions::to_string_t("{{baseName}}"))));
{{/isDateTime}} {{/isDateTime}}
@ -541,7 +547,7 @@ void {{classname}}::fromMultiPart(std::shared_ptr<MultipartFormData> multipart,
{{setter}}( new{{name}} ); {{setter}}( new{{name}} );
{{/vendorExtensions.x-codegen-file}} {{/vendorExtensions.x-codegen-file}}
{{/isDateTime}} {{/isDateTime}}
{{/isString}} {{/isByteArray}}{{/isString}}
{{/required}} {{/required}}
{{/isPrimitiveType}} {{/isPrimitiveType}}
{{/isMapContainer}} {{/isMapContainer}}

View File

@ -2,7 +2,7 @@ cmake_minimum_required (VERSION 3.2)
project(server) project(server)
set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} -std=c++11) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pg -g3" )
link_directories(/usr/local/lib/) link_directories(/usr/local/lib/)

View File

@ -59,11 +59,14 @@ void PetApi::setupRoutes() {
void PetApi::add_pet_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response) { void PetApi::add_pet_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response) {
// Getting the body param // Getting the body param
Pet pet; Pet pet;
try { try {
nlohmann::json request_body = nlohmann::json::parse(request.body()); nlohmann::json request_body = nlohmann::json::parse(request.body());
pet.fromJson(request_body); pet.fromJson(request_body);
this->add_pet(pet, response); this->add_pet(pet, response);
} catch (std::runtime_error & e) { } catch (std::runtime_error & e) {
//send a 400 error //send a 400 error
@ -132,11 +135,14 @@ void PetApi::get_pet_by_id_handler(const Pistache::Rest::Request &request, Pista
void PetApi::update_pet_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response) { void PetApi::update_pet_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response) {
// Getting the body param // Getting the body param
Pet pet; Pet pet;
try { try {
nlohmann::json request_body = nlohmann::json::parse(request.body()); nlohmann::json request_body = nlohmann::json::parse(request.body());
pet.fromJson(request_body); pet.fromJson(request_body);
this->update_pet(pet, response); this->update_pet(pet, response);
} catch (std::runtime_error & e) { } catch (std::runtime_error & e) {
//send a 400 error //send a 400 error

View File

@ -71,7 +71,7 @@ private:
/// ///
/// </remarks> /// </remarks>
/// <param name="pet">Pet object that needs to be added to the store</param> /// <param name="pet">Pet object that needs to be added to the store</param>
virtual void add_pet(const std::shared_ptr<Pet> &pet, Pistache::Http::ResponseWriter &response) = 0; virtual void add_pet(const Pet &pet, Pistache::Http::ResponseWriter &response) = 0;
/// <summary> /// <summary>
/// Deletes a pet /// Deletes a pet
@ -117,7 +117,7 @@ private:
/// ///
/// </remarks> /// </remarks>
/// <param name="pet">Pet object that needs to be added to the store</param> /// <param name="pet">Pet object that needs to be added to the store</param>
virtual void update_pet(const std::shared_ptr<Pet> &pet, Pistache::Http::ResponseWriter &response) = 0; virtual void update_pet(const Pet &pet, Pistache::Http::ResponseWriter &response) = 0;
/// <summary> /// <summary>
/// Updates a pet in the store with form data /// Updates a pet in the store with form data

View File

@ -92,11 +92,14 @@ void StoreApi::get_order_by_id_handler(const Pistache::Rest::Request &request, P
void StoreApi::place_order_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response) { void StoreApi::place_order_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response) {
// Getting the body param // Getting the body param
Order order; Order order;
try { try {
nlohmann::json request_body = nlohmann::json::parse(request.body()); nlohmann::json request_body = nlohmann::json::parse(request.body());
order.fromJson(request_body); order.fromJson(request_body);
this->place_order(order, response); this->place_order(order, response);
} catch (std::runtime_error & e) { } catch (std::runtime_error & e) {
//send a 400 error //send a 400 error

View File

@ -93,7 +93,7 @@ private:
/// ///
/// </remarks> /// </remarks>
/// <param name="order">order placed for purchasing the pet</param> /// <param name="order">order placed for purchasing the pet</param>
virtual void place_order(const std::shared_ptr<Order> &order, Pistache::Http::ResponseWriter &response) = 0; virtual void place_order(const Order &order, Pistache::Http::ResponseWriter &response) = 0;
}; };

View File

@ -59,11 +59,14 @@ void UserApi::setupRoutes() {
void UserApi::create_user_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response) { void UserApi::create_user_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response) {
// Getting the body param // Getting the body param
User user; User user;
try { try {
nlohmann::json request_body = nlohmann::json::parse(request.body()); nlohmann::json request_body = nlohmann::json::parse(request.body());
user.fromJson(request_body); user.fromJson(request_body);
this->create_user(user, response); this->create_user(user, response);
} catch (std::runtime_error & e) { } catch (std::runtime_error & e) {
//send a 400 error //send a 400 error
@ -75,11 +78,11 @@ void UserApi::create_user_handler(const Pistache::Rest::Request &request, Pistac
void UserApi::create_users_with_array_input_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response) { void UserApi::create_users_with_array_input_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response) {
// Getting the body param // Getting the body param
std::vector user; std::vector<User> user;
try { try {
nlohmann::json request_body = nlohmann::json::parse(request.body()); nlohmann::json request_body = nlohmann::json::parse(request.body());
user.fromJson(request_body); user = ModelArrayHelper::fromJson<User>(request_body);
this->create_users_with_array_input(user, response); this->create_users_with_array_input(user, response);
} catch (std::runtime_error & e) { } catch (std::runtime_error & e) {
//send a 400 error //send a 400 error
@ -91,11 +94,11 @@ void UserApi::create_users_with_array_input_handler(const Pistache::Rest::Reques
void UserApi::create_users_with_list_input_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response) { void UserApi::create_users_with_list_input_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response) {
// Getting the body param // Getting the body param
std::vector user; std::vector<User> user;
try { try {
nlohmann::json request_body = nlohmann::json::parse(request.body()); nlohmann::json request_body = nlohmann::json::parse(request.body());
user.fromJson(request_body); user = ModelArrayHelper::fromJson<User>(request_body);
this->create_users_with_list_input(user, response); this->create_users_with_list_input(user, response);
} catch (std::runtime_error & e) { } catch (std::runtime_error & e) {
//send a 400 error //send a 400 error
@ -161,11 +164,14 @@ void UserApi::update_user_handler(const Pistache::Rest::Request &request, Pistac
auto username = request.param(":username").as<std::string>(); auto username = request.param(":username").as<std::string>();
// Getting the body param // Getting the body param
User user; User user;
try { try {
nlohmann::json request_body = nlohmann::json::parse(request.body()); nlohmann::json request_body = nlohmann::json::parse(request.body());
user.fromJson(request_body); user.fromJson(request_body);
this->update_user(username, user, response); this->update_user(username, user, response);
} catch (std::runtime_error & e) { } catch (std::runtime_error & e) {
//send a 400 error //send a 400 error

View File

@ -71,7 +71,7 @@ private:
/// This can only be done by the logged in user. /// This can only be done by the logged in user.
/// </remarks> /// </remarks>
/// <param name="user">Created user object</param> /// <param name="user">Created user object</param>
virtual void create_user(const std::shared_ptr<User> &user, Pistache::Http::ResponseWriter &response) = 0; virtual void create_user(const User &user, Pistache::Http::ResponseWriter &response) = 0;
/// <summary> /// <summary>
/// Creates list of users with given input array /// Creates list of users with given input array
@ -135,7 +135,7 @@ private:
/// </remarks> /// </remarks>
/// <param name="username">name that need to be deleted</param> /// <param name="username">name that need to be deleted</param>
/// <param name="user">Updated user object</param> /// <param name="user">Updated user object</param>
virtual void update_user(const std::string &username, const std::shared_ptr<User> &user, Pistache::Http::ResponseWriter &response) = 0; virtual void update_user(const std::string &username, const User &user, Pistache::Http::ResponseWriter &response) = 0;
}; };

View File

@ -23,7 +23,7 @@ PetApiImpl::PetApiImpl(Pistache::Address addr)
: PetApi(addr) : PetApi(addr)
{ } { }
void PetApiImpl::add_pet(const std::shared_ptr<Pet> &pet, Pistache::Http::ResponseWriter &response) { void PetApiImpl::add_pet(const Pet &pet, 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::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) {
@ -38,7 +38,7 @@ void PetApiImpl::find_pets_by_tags(const Pistache::Optional<std::string> &tags,
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) {
response.send(Pistache::Http::Code::Ok, "Do some magic\n"); response.send(Pistache::Http::Code::Ok, "Do some magic\n");
} }
void PetApiImpl::update_pet(const std::shared_ptr<Pet> &pet, Pistache::Http::ResponseWriter &response) { void PetApiImpl::update_pet(const Pet &pet, 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::update_pet_with_form(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter &response){ void PetApiImpl::update_pet_with_form(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter &response){

View File

@ -45,12 +45,12 @@ public:
PetApiImpl(Pistache::Address addr); PetApiImpl(Pistache::Address addr);
~PetApiImpl() { }; ~PetApiImpl() { };
void add_pet(const std::shared_ptr<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::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::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 std::shared_ptr<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);
void upload_file(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter &response); void upload_file(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter &response);

View File

@ -32,7 +32,7 @@ void StoreApiImpl::get_inventory(Pistache::Http::ResponseWriter &response) {
void StoreApiImpl::get_order_by_id(const int64_t &orderId, Pistache::Http::ResponseWriter &response) { void StoreApiImpl::get_order_by_id(const int64_t &orderId, 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 StoreApiImpl::place_order(const std::shared_ptr<Order> &order, Pistache::Http::ResponseWriter &response) { void StoreApiImpl::place_order(const Order &order, Pistache::Http::ResponseWriter &response) {
response.send(Pistache::Http::Code::Ok, "Do some magic\n"); response.send(Pistache::Http::Code::Ok, "Do some magic\n");
} }

View File

@ -48,7 +48,7 @@ public:
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);
void get_order_by_id(const int64_t &orderId, Pistache::Http::ResponseWriter &response); void get_order_by_id(const int64_t &orderId, Pistache::Http::ResponseWriter &response);
void place_order(const std::shared_ptr<Order> &order, Pistache::Http::ResponseWriter &response); void place_order(const Order &order, Pistache::Http::ResponseWriter &response);
}; };

View File

@ -23,7 +23,7 @@ UserApiImpl::UserApiImpl(Pistache::Address addr)
: UserApi(addr) : UserApi(addr)
{ } { }
void UserApiImpl::create_user(const std::shared_ptr<User> &user, Pistache::Http::ResponseWriter &response) { void UserApiImpl::create_user(const User &user, 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 UserApiImpl::create_users_with_array_input(const std::vector<User> &user, Pistache::Http::ResponseWriter &response) { void UserApiImpl::create_users_with_array_input(const std::vector<User> &user, Pistache::Http::ResponseWriter &response) {
@ -44,7 +44,7 @@ void UserApiImpl::login_user(const Pistache::Optional<std::string> &username, co
void UserApiImpl::logout_user(Pistache::Http::ResponseWriter &response) { void UserApiImpl::logout_user(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 UserApiImpl::update_user(const std::string &username, const std::shared_ptr<User> &user, Pistache::Http::ResponseWriter &response) { void UserApiImpl::update_user(const std::string &username, const User &user, Pistache::Http::ResponseWriter &response) {
response.send(Pistache::Http::Code::Ok, "Do some magic\n"); response.send(Pistache::Http::Code::Ok, "Do some magic\n");
} }

View File

@ -45,14 +45,14 @@ public:
UserApiImpl(Pistache::Address addr); UserApiImpl(Pistache::Address addr);
~UserApiImpl() { }; ~UserApiImpl() { };
void create_user(const std::shared_ptr<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);
void create_users_with_list_input(const std::vector<User> &user, Pistache::Http::ResponseWriter &response); void create_users_with_list_input(const std::vector<User> &user, Pistache::Http::ResponseWriter &response);
void delete_user(const std::string &username, Pistache::Http::ResponseWriter &response); void delete_user(const std::string &username, Pistache::Http::ResponseWriter &response);
void get_user_by_name(const std::string &username, Pistache::Http::ResponseWriter &response); void get_user_by_name(const std::string &username, Pistache::Http::ResponseWriter &response);
void login_user(const Pistache::Optional<std::string> &username, const Pistache::Optional<std::string> &password, Pistache::Http::ResponseWriter &response); void login_user(const Pistache::Optional<std::string> &username, const Pistache::Optional<std::string> &password, Pistache::Http::ResponseWriter &response);
void logout_user(Pistache::Http::ResponseWriter &response); void logout_user(Pistache::Http::ResponseWriter &response);
void update_user(const std::string &username, const std::shared_ptr<User> &user, Pistache::Http::ResponseWriter &response); void update_user(const std::string &username, const User &user, Pistache::Http::ResponseWriter &response);
}; };

View File

@ -68,12 +68,10 @@ void ApiResponse::fromJson(nlohmann::json& val)
if(val.find("type") != val.end()) if(val.find("type") != val.end())
{ {
setType(val.at("type")); setType(val.at("type"));
} }
if(val.find("message") != val.end()) if(val.find("message") != val.end())
{ {
setMessage(val.at("message")); setMessage(val.at("message"));
} }
} }

View File

@ -62,7 +62,6 @@ void Category::fromJson(nlohmann::json& val)
if(val.find("name") != val.end()) if(val.find("name") != val.end())
{ {
setName(val.at("name")); setName(val.at("name"));
} }
} }

View File

@ -55,7 +55,7 @@ bool ModelBase::toJson( bool const value )
return value; return value;
} }
nlohmann::json ModelBase::toJson(ModelBase content ) nlohmann::json ModelBase::toJson(ModelBase const& content )
{ {
return content.toJson(); return content.toJson();
} }

View File

@ -22,6 +22,8 @@
#include "json.hpp" #include "json.hpp"
#include <ctime> #include <ctime>
#include <string> #include <string>
#include <vector>
#include <map>
namespace org { namespace org {
namespace openapitools { namespace openapitools {
@ -45,8 +47,103 @@ public:
static int64_t toJson( int64_t const value ); static int64_t toJson( int64_t const value );
static double toJson( double const value ); static double toJson( double const value );
static bool toJson( bool const value ); static bool toJson( bool const value );
static nlohmann::json toJson(ModelBase const& content ); static nlohmann::json toJson(ModelBase const& content );
};
class ModelArrayHelper {
public:
template<typename T>
static std::vector<T> fromJson(nlohmann::json& json) {
T *ptrTest;
std::vector<T> val;
if (dynamic_cast<ModelBase*>(ptrTest) != nullptr) {
if (!json.empty()) {
for (auto &item : json.items()) {
T entry;
entry.fromJson(item.value());
val.push_back(entry);
}
}
}
return val;
}
template<typename T>
static nlohmann::json toJson(std::vector<T> val) {
nlohmann::json json;
for(auto item : val){
json.push_back(item.toJson());
}
return json;
}
};
class ArrayHelper {
public:
template<typename T>
static std::vector<T> fromJson(nlohmann::json& json) {
std::vector<T> val;
nlohmann::from_json(json, val);
return val;
}
template<typename T>
static nlohmann::json toJson(std::vector<T> val) {
nlohmann::json json;
nlohmann::to_json(json, val);
return json;
}
};
class ModelMapHelper {
public:
template<typename T>
static std::map<std::string, T> & fromJson(nlohmann::json& json) {
T *ptrTest;
std::map<std::string, T> val;
if (dynamic_cast<ModelBase*>(ptrTest) != nullptr) {
if (!json.empty()) {
for (auto &item : json.items()) {
T entry;
entry.fromJson(item.value());
val.insert(val.end(),
std::pair<std::string, T>(item.key(), entry));
}
}
}
return val;
}
template<typename T>
static nlohmann::json toJson(std::map<std::string, T> val) {
nlohmann::json json;
for (auto const& item : val) {
json[item.first] = item.second.toJson();
}
return json;
}
};
class MapHelper {
public:
template<typename T>
static std::map<std::string, T> & fromJson(nlohmann::json& json) {
std::map<std::string, T> val;
if (!json.empty()) {
for (auto &item : json.items()) {
T entry = item.value();
val.insert(val.end(),
std::pair<std::string, T>(item.key(), entry));
}
}
return val;
}
template<typename T>
static nlohmann::json toJson(std::map<std::string, T> val) {
nlohmann::json json;
for (auto const& item : val) {
nlohmann::json jitem = item.second;
json[item.first] = jitem;
}
return json;
}
}; };
} }

View File

@ -99,7 +99,6 @@ void Order::fromJson(nlohmann::json& val)
if(val.find("status") != val.end()) if(val.find("status") != val.end())
{ {
setStatus(val.at("status")); setStatus(val.at("status"));
} }
if(val.find("complete") != val.end()) if(val.find("complete") != val.end())
{ {

View File

@ -91,7 +91,7 @@ void Pet::fromJson(nlohmann::json& val)
{ {
if(!val["category"].is_null()) if(!val["category"].is_null())
{ {
Category newItem(Category()); Category newItem;
newItem.fromJson(val["category"]); newItem.fromJson(val["category"]);
setCategory( newItem ); setCategory( newItem );
} }
@ -117,11 +117,11 @@ void Pet::fromJson(nlohmann::json& val)
if(item.is_null()) if(item.is_null())
{ {
m_Tags.push_back( Tag(nullptr) ); m_Tags.push_back( Tag() );
} }
else else
{ {
Tag newItem(Tag()); Tag newItem;
newItem.fromJson(item); newItem.fromJson(item);
m_Tags.push_back( newItem ); m_Tags.push_back( newItem );
} }
@ -132,7 +132,6 @@ void Pet::fromJson(nlohmann::json& val)
if(val.find("status") != val.end()) if(val.find("status") != val.end())
{ {
setStatus(val.at("status")); setStatus(val.at("status"));
} }
} }

View File

@ -62,7 +62,6 @@ void Tag::fromJson(nlohmann::json& val)
if(val.find("name") != val.end()) if(val.find("name") != val.end())
{ {
setName(val.at("name")); setName(val.at("name"));
} }
} }

View File

@ -98,32 +98,26 @@ void User::fromJson(nlohmann::json& val)
if(val.find("username") != val.end()) if(val.find("username") != val.end())
{ {
setUsername(val.at("username")); setUsername(val.at("username"));
} }
if(val.find("firstName") != val.end()) if(val.find("firstName") != val.end())
{ {
setFirstName(val.at("firstName")); setFirstName(val.at("firstName"));
} }
if(val.find("lastName") != val.end()) if(val.find("lastName") != val.end())
{ {
setLastName(val.at("lastName")); setLastName(val.at("lastName"));
} }
if(val.find("email") != val.end()) if(val.find("email") != val.end())
{ {
setEmail(val.at("email")); setEmail(val.at("email"));
} }
if(val.find("password") != val.end()) if(val.find("password") != val.end())
{ {
setPassword(val.at("password")); setPassword(val.at("password"));
} }
if(val.find("phone") != val.end()) if(val.find("phone") != val.end())
{ {
setPhone(val.at("phone")); setPhone(val.at("phone"));
} }
if(val.find("userStatus") != val.end()) if(val.find("userStatus") != val.end())
{ {