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 34deb0b0fca7..ef72bae11fd3 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 @@ -81,8 +81,6 @@ public CppPistacheServerCodegen() { 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("main-api-server.mustache", "", modelNamePrefix + "main-api-server.cpp")); @@ -124,8 +122,6 @@ public void processOpts() { 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("main-api-server.mustache", "", modelNamePrefix + "main-api-server.cpp")); diff --git a/modules/openapi-generator/src/main/resources/cpp-pistache-server/README.mustache b/modules/openapi-generator/src/main/resources/cpp-pistache-server/README.mustache index e6e1a0b2883d..c568d8316d49 100644 --- a/modules/openapi-generator/src/main/resources/cpp-pistache-server/README.mustache +++ b/modules/openapi-generator/src/main/resources/cpp-pistache-server/README.mustache @@ -41,7 +41,7 @@ cd build ## Libraries required - [pistache](http://pistache.io/quickstart) - [JSON for Modern C++](https://github.com/nlohmann/json/#integration): Please download the `json.hpp` file and -put it under the model folder +put it under the model/nlohmann folder ## Namespaces {{{apiPackage}}} 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 f1a7841c4020..c529fbea5799 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 @@ -70,14 +70,7 @@ void {{classname}}::{{operationIdSnakeCase}}_handler(const Pistache::Rest::Reque try { {{#hasBodyParam}} {{#bodyParam}} - nlohmann::json request_body = nlohmann::json::parse(request.body()); - {{^isPrimitiveType}}{{^isContainer}} - {{paramName}}.fromJson(request_body); - {{/isContainer}}{{/isPrimitiveType}}{{#isContainer}} {{paramName}} = {{#isListContainer}} {{prefix}}ArrayHelper{{/isListContainer}}{{#isMapContainer}} {{prefix}}MapHelper{{/isMapContainer}}::fromJson<{{items.baseType}}>(request_body);{{/isContainer}} - {{#isPrimitiveType}} - // The conversion is done automatically by the json library - {{paramName}} = request_body; - {{/isPrimitiveType}} + nlohmann::json::parse(request.body()).get_to({{paramName}}); {{/bodyParam}} {{/hasBodyParam}} this->{{operationIdSnakeCase}}({{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}{{#hasParams}}, {{/hasParams}}response); 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 115fac401ca0..a822ae28ae59 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 @@ -20,7 +20,6 @@ ExternalProject_Add(NLOHMANN ) include_directories(${EXTERNAL_INSTALL_LOCATION}/include) -include_directories(${EXTERNAL_INSTALL_LOCATION}/include/nlohmann) link_directories(${EXTERNAL_INSTALL_LOCATION}/lib) {{/addExternalLibs}} diff --git a/modules/openapi-generator/src/main/resources/cpp-pistache-server/model-header.mustache b/modules/openapi-generator/src/main/resources/cpp-pistache-server/model-header.mustache index b297ab131dbc..a05667766a9f 100644 --- a/modules/openapi-generator/src/main/resources/cpp-pistache-server/model-header.mustache +++ b/modules/openapi-generator/src/main/resources/cpp-pistache-server/model-header.mustache @@ -9,10 +9,9 @@ #define {{classname}}_H_ {{{defaultInclude}}} -#include "{{prefix}}ModelBase.h" - {{#imports}}{{{this}}} {{/imports}} +#include {{#modelNamespaceDeclarations}} namespace {{this}} { @@ -22,19 +21,12 @@ namespace {{this}} { /// {{description}} /// class {{declspec}} {{classname}} - : public {{prefix}}ModelBase { public: {{classname}}(); virtual ~{{classname}}(); - ///////////////////////////////////////////// - /// {{prefix}}ModelBase overrides - - void validate() override; - - nlohmann::json toJson() const override; - void fromJson(const nlohmann::json& json) override; + void validate(); ///////////////////////////////////////////// /// {{classname}} members @@ -51,6 +43,8 @@ public: {{/required}} {{/vars}} + friend void to_json(nlohmann::json& j, const {{classname}}& o); + friend void from_json(const nlohmann::json& j, {{classname}}& o); protected: {{#vars}} {{{dataType}}} m_{{name}}; 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 5ad0ef24006e..e6cf11ea5273 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 @@ -25,126 +25,26 @@ void {{classname}}::validate() // TODO: implement validation } -nlohmann::json {{classname}}::toJson() const +void to_json(nlohmann::json& j, const {{classname}}& o) { - nlohmann::json val = nlohmann::json::object(); - - {{#vars}}{{#isPrimitiveType}}{{^isListContainer}}{{^required}}if(m_{{name}}IsSet) - { - val["{{baseName}}"] = m_{{name}}; - } - {{/required}}{{#required}}val["{{baseName}}"] = m_{{name}}; - {{/required}}{{/isListContainer}}{{/isPrimitiveType}}{{#isListContainer}}{ - nlohmann::json jsonArray; - for( const auto& item : m_{{name}} ) - { - jsonArray.push_back({{prefix}}ModelBase::toJson(item)); - } - {{#required}}val["{{baseName}}"] = jsonArray; - {{/required}}{{^required}} - if(jsonArray.size() > 0) - { - val["{{baseName}}"] = jsonArray; - } {{/required}} - } - {{/isListContainer}}{{#isMapContainer}}{ - nlohmann::json jsonObj; - for( const auto& item : m_{{name}} ) - { {{^items.isContainer}} - jsonObj[item.first] = {{prefix}}ModelBase::toJson(item.second);{{/items.isContainer}} {{#items.isListContainer}} - jsonObj[item.first] = {{prefix}}ArrayHelper::toJson<{{{items.items.datatype}}}>(item.second); - {{/items.isListContainer}} {{#items.isMapContainer}} - jsonObj[item.first] = {{prefix}}MapHelper::toJson<{{{items.items.datatype}}}>(item.second); {{/items.isMapContainer}} - } - {{#required}}val["{{baseName}}"] = jsonObj; {{/required}}{{^required}} - if(jsonObj.size() > 0) - { - val["{{baseName}}"] = jsonObj; - } {{/required}} - } - {{/isMapContainer}}{{^isContainer}}{{^isPrimitiveType}}{{^required}}if(m_{{name}}IsSet) - { - val["{{baseName}}"] = {{prefix}}ModelBase::toJson(m_{{name}}); - } - {{/required}}{{#required}}val["{{baseName}}"] = {{prefix}}ModelBase::toJson(m_{{name}}); - {{/required}}{{/isPrimitiveType}}{{/isContainer}}{{/vars}} - - return val; + j = nlohmann::json(); + {{#vars}} + {{#required}}j["{{baseName}}"] = o.m_{{name}};{{/required}}{{^required}}if(o.{{nameInCamelCase}}IsSet()) + j["{{baseName}}"] = o.m_{{name}};{{/required}} + {{/vars}} } -void {{classname}}::fromJson(const nlohmann::json& val) +void from_json(const nlohmann::json& j, {{classname}}& o) { - {{#vars}}{{#isPrimitiveType}}{{^isListContainer}}{{^required}}if(val.find("{{baseName}}") != val.end()) - { - {{setter}}(val.at("{{baseName}}")); - } - {{/required}}{{#required}}{{setter}}(val.at("{{baseName}}")); - {{/required}}{{/isListContainer}}{{/isPrimitiveType}}{{#isListContainer}}{ - m_{{name}}.clear(); - {{^required}}if(val.find("{{baseName}}") != val.end()) - { - {{/required}} - for( const auto& item : val["{{baseName}}"] ) - { - {{#isPrimitiveType}}m_{{name}}.push_back(item); - {{/isPrimitiveType}}{{^isPrimitiveType}}{{#items.isString}}m_{{name}}.push_back(item); - {{/items.isString}}{{^items.isString}}{{#items.isDateTime}}m_{{name}}.push_back(item); - {{/items.isDateTime}}{{^items.isDateTime}} - if(item.is_null()) - { - m_{{name}}.push_back( {{{items.datatype}}}() ); - } - else - { - {{{items.datatype}}} newItem; - newItem.fromJson(item); - m_{{name}}.push_back( newItem ); - } - {{/items.isDateTime}}{{/items.isString}}{{/isPrimitiveType}} - } - {{^required}} - } - {{/required}} - } - {{/isListContainer}}{{#isMapContainer}}{ - m_{{name}}.clear(); - {{^required}}if(val.find("{{baseName}}") != val.end()) - { - {{/required}} - if(val["{{baseName}}"].is_object()) { {{^items.isContainer}} - m_{{name}} = {{prefix}}MapHelper::fromJson<{{{items.datatype}}}>(val["{{baseName}}"]); - {{/items.isContainer}} {{#items.isContainer}} - for( const auto& item : val["{{baseName}}"].items() ) - { {{#items.isMapContainer}} - {{{items.datatype}}} newItem = {{prefix}}MapHelper::fromJson<{{{items.items.datatype}}}>(item.value()); - {{/items.isMapContainer}}{{#items.isListContainer}} - {{{items.datatype}}} newItem = {{prefix}}ArrayHelper::fromJson<{{{items.items.datatype}}}>(item.value()); - {{/items.isListContainer}} - m_{{name}}.insert(m_{{name}}.end(), std::pair< std::string, {{{items.datatype}}} >(item.key(), newItem)); - } {{/items.isContainer}} - } - {{^required}} - } - {{/required}} - } - {{/isMapContainer}}{{^isContainer}}{{^isPrimitiveType}}{{^required}}if(val.find("{{baseName}}") != val.end()) + {{#vars}} + {{#required}}j.at("{{baseName}}").get_to(o.m_{{name}});{{/required}}{{^required}}if(!j.at("{{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 ); - } - {{/isBinary}}{{/isByteArray}}{{/isDateTime}}{{/isString}} - } - {{/required}}{{#required}}{{#isString}}{{setter}}(val.at("{{baseName}}")); - {{/isString}}{{^isString}}{{#isDateTime}}{{setter}}(val.at("{{baseName}}")); - {{/isDateTime}}{{/isString}}{{/required}}{{/isPrimitiveType}}{{/isContainer}}{{/vars}} + j.at("{{baseName}}").get_to(o.m_{{name}}); + o.m_{{name}}IsSet = true; + } {{/required}} + {{/vars}} } - {{#vars}}{{#isContainer}}{{{dataType}}}& {{classname}}::{{getter}}() { return m_{{name}}; diff --git a/modules/openapi-generator/src/main/resources/cpp-pistache-server/modelbase-header.mustache b/modules/openapi-generator/src/main/resources/cpp-pistache-server/modelbase-header.mustache deleted file mode 100644 index 8840764a57a2..000000000000 --- a/modules/openapi-generator/src/main/resources/cpp-pistache-server/modelbase-header.mustache +++ /dev/null @@ -1,127 +0,0 @@ -{{>licenseInfo}} -/* - * {{prefix}}ModelBase.h - * - * This is the base class for all model classes - */ - -#ifndef {{prefix}}ModelBase_H_ -#define {{prefix}}ModelBase_H_ - -{{{defaultInclude}}} -#include "json.hpp" -#include -#include -#include -#include - -{{#modelNamespaceDeclarations}} -namespace {{this}} { -{{/modelNamespaceDeclarations}} - -class {{declspec}} {{prefix}}ModelBase -{ -public: - {{prefix}}ModelBase(); - virtual ~{{prefix}}ModelBase(); - - virtual void validate() = 0; - - virtual nlohmann::json toJson() const = 0; - virtual void fromJson(const nlohmann::json& json) = 0; - - static std::string toJson( std::string const& value ); - static std::string toJson( std::time_t const& value ); - static int32_t toJson( int32_t const value ); - static int64_t toJson( int64_t const value ); - static double toJson( double const value ); - static bool toJson( bool const value ); - static nlohmann::json toJson({{prefix}}ModelBase const& content ); -}; - -class {{prefix}}ArrayHelper { -private: - template::value>::value> - static void itemFromJson(T& item, const nlohmann::json& json){ - item = json; - } - static void itemFromJson(ModelBase& item, const nlohmann::json& json){ - item.fromJson(json); - } - template::value>::value> - static nlohmann::json itemtoJson(const T& item){ - return item; - } - static nlohmann::json itemtoJson(const ModelBase& item){ - return item.toJson(); - } -public: - template - static std::vector fromJson(const nlohmann::json& json) { - std::vector val; - if (!json.empty()) { - for (const auto& item : json.items()) { - T entry; - itemFromJson(entry, item.value()); - val.push_back(entry); - } - } - return val; - } - template - static nlohmann::json toJson(const std::vector& val) { - nlohmann::json json; - for(const auto& item : val){ - json.push_back(itemtoJson(item)); - } - return json; - } -}; - -class {{prefix}}MapHelper { -private: - template::value>::value> - static void itemFromJson(T &item, const nlohmann::json& json){ - item = json; - } - static void itemFromJson(ModelBase &item, const nlohmann::json& json){ - item.fromJson(json); - } - template::value>::value> - static nlohmann::json itemtoJson(const T& item){ - return item; - } - static nlohmann::json itemtoJson(const ModelBase& item){ - return item.toJson(); - } - -public: - template - static std::map fromJson(const nlohmann::json& json) { - std::map val; - if (!json.empty()) { - for (const auto& item : json.items()) { - T entry; - itemfromJson(entry, item.value()); - val.insert(val.end(), - std::pair(item.key(), entry)); - } - } - return val; - } - template - static nlohmann::json toJson(const std::map& val) { - nlohmann::json json; - for (const auto& item : val) { - nlohmann::json jitem = itemtoJson(item.second); - json[item.first] = jitem; - } - return json; - } -}; - -{{#modelNamespaceDeclarations}} -} -{{/modelNamespaceDeclarations}} - -#endif /* {{prefix}}ModelBase_H_ */ diff --git a/modules/openapi-generator/src/main/resources/cpp-pistache-server/modelbase-source.mustache b/modules/openapi-generator/src/main/resources/cpp-pistache-server/modelbase-source.mustache deleted file mode 100644 index f097c3a4a199..000000000000 --- a/modules/openapi-generator/src/main/resources/cpp-pistache-server/modelbase-source.mustache +++ /dev/null @@ -1,54 +0,0 @@ -{{>licenseInfo}} -#include "{{prefix}}ModelBase.h" - -{{#modelNamespaceDeclarations}} -namespace {{this}} { -{{/modelNamespaceDeclarations}} - -{{prefix}}ModelBase::{{prefix}}ModelBase() -{ -} -{{prefix}}ModelBase::~{{prefix}}ModelBase() -{ -} - -std::string {{prefix}}ModelBase::toJson( std::string const& value ) -{ - return value; -} - -std::string {{prefix}}ModelBase::toJson( std::time_t const& value ) -{ - char buf[sizeof "2011-10-08T07:07:09Z"]; - strftime(buf, sizeof buf, "%FT%TZ", gmtime(&value)); - return buf; -} - -int32_t {{prefix}}ModelBase::toJson( int32_t const value ) -{ - return value; -} - -int64_t {{prefix}}ModelBase::toJson( int64_t const value ) -{ - return value; -} - -double {{prefix}}ModelBase::toJson( double const value ) -{ - return value; -} - -bool {{prefix}}ModelBase::toJson( bool const value ) -{ - return value; -} - -nlohmann::json {{prefix}}ModelBase::toJson({{prefix}}ModelBase const& content ) -{ - return content.toJson(); -} - -{{#modelNamespaceDeclarations}} -} -{{/modelNamespaceDeclarations}} diff --git a/samples/server/petstore/cpp-pistache/.openapi-generator/VERSION b/samples/server/petstore/cpp-pistache/.openapi-generator/VERSION index 0f58aa041419..afa636560641 100644 --- a/samples/server/petstore/cpp-pistache/.openapi-generator/VERSION +++ b/samples/server/petstore/cpp-pistache/.openapi-generator/VERSION @@ -1 +1 @@ -3.1.2-SNAPSHOT \ No newline at end of file +4.0.0-SNAPSHOT \ No newline at end of file diff --git a/samples/server/petstore/cpp-pistache/CMakeLists.txt b/samples/server/petstore/cpp-pistache/CMakeLists.txt index 278caa8ab3d2..0cd21eda2986 100644 --- a/samples/server/petstore/cpp-pistache/CMakeLists.txt +++ b/samples/server/petstore/cpp-pistache/CMakeLists.txt @@ -19,7 +19,6 @@ ExternalProject_Add(NLOHMANN ) include_directories(${EXTERNAL_INSTALL_LOCATION}/include) -include_directories(${EXTERNAL_INSTALL_LOCATION}/include/nlohmann) link_directories(${EXTERNAL_INSTALL_LOCATION}/lib) include_directories(model) diff --git a/samples/server/petstore/cpp-pistache/README.md b/samples/server/petstore/cpp-pistache/README.md index c8587ab3d706..7d4392fde7e9 100644 --- a/samples/server/petstore/cpp-pistache/README.md +++ b/samples/server/petstore/cpp-pistache/README.md @@ -41,7 +41,7 @@ cd build ## Libraries required - [pistache](http://pistache.io/quickstart) - [JSON for Modern C++](https://github.com/nlohmann/json/#integration): Please download the `json.hpp` file and -put it under the model folder +put it under the model/nlohmann folder ## Namespaces org.openapitools.server.api diff --git a/samples/server/petstore/cpp-pistache/api/PetApi.cpp b/samples/server/petstore/cpp-pistache/api/PetApi.cpp index c62755f78de7..5ffab955c55c 100644 --- a/samples/server/petstore/cpp-pistache/api/PetApi.cpp +++ b/samples/server/petstore/cpp-pistache/api/PetApi.cpp @@ -49,14 +49,11 @@ void PetApi::add_pet_handler(const Pistache::Rest::Request &request, Pistache::H // Getting the body param - Pet pet; + Pet body; try { - nlohmann::json request_body = nlohmann::json::parse(request.body()); - - pet.fromJson(request_body); - - this->add_pet(pet, response); + nlohmann::json::parse(request.body()).get_to(body); + this->add_pet(body, response); } catch (std::runtime_error & e) { //send a 400 error response.send(Pistache::Http::Code::Bad_Request, e.what()); @@ -139,14 +136,11 @@ void PetApi::update_pet_handler(const Pistache::Rest::Request &request, Pistache // Getting the body param - Pet pet; + Pet body; try { - nlohmann::json request_body = nlohmann::json::parse(request.body()); - - pet.fromJson(request_body); - - this->update_pet(pet, response); + nlohmann::json::parse(request.body()).get_to(body); + this->update_pet(body, response); } catch (std::runtime_error & e) { //send a 400 error response.send(Pistache::Http::Code::Bad_Request, e.what()); diff --git a/samples/server/petstore/cpp-pistache/api/PetApi.h b/samples/server/petstore/cpp-pistache/api/PetApi.h index d34ff4afa7ef..c84ca384279e 100644 --- a/samples/server/petstore/cpp-pistache/api/PetApi.h +++ b/samples/server/petstore/cpp-pistache/api/PetApi.h @@ -65,8 +65,8 @@ class PetApi { /// /// /// - /// Pet object that needs to be added to the store - virtual void add_pet(const Pet &pet, Pistache::Http::ResponseWriter &response) = 0; + /// Pet object that needs to be added to the store + virtual void add_pet(const Pet &body, Pistache::Http::ResponseWriter &response) = 0; /// /// Deletes a pet @@ -111,8 +111,8 @@ class PetApi { /// /// /// - /// Pet object that needs to be added to the store - virtual void update_pet(const Pet &pet, Pistache::Http::ResponseWriter &response) = 0; + /// Pet object that needs to be added to the store + virtual void update_pet(const Pet &body, Pistache::Http::ResponseWriter &response) = 0; /// /// Updates a pet in the store with form data diff --git a/samples/server/petstore/cpp-pistache/api/StoreApi.cpp b/samples/server/petstore/cpp-pistache/api/StoreApi.cpp index 58b9b28986d8..98e4d387a190 100644 --- a/samples/server/petstore/cpp-pistache/api/StoreApi.cpp +++ b/samples/server/petstore/cpp-pistache/api/StoreApi.cpp @@ -82,14 +82,11 @@ void StoreApi::place_order_handler(const Pistache::Rest::Request &request, Pista // Getting the body param - Order order; + Order body; try { - nlohmann::json request_body = nlohmann::json::parse(request.body()); - - order.fromJson(request_body); - - this->place_order(order, response); + nlohmann::json::parse(request.body()).get_to(body); + this->place_order(body, response); } catch (std::runtime_error & e) { //send a 400 error response.send(Pistache::Http::Code::Bad_Request, e.what()); diff --git a/samples/server/petstore/cpp-pistache/api/StoreApi.h b/samples/server/petstore/cpp-pistache/api/StoreApi.h index 53117f32a2e4..cde0755f1383 100644 --- a/samples/server/petstore/cpp-pistache/api/StoreApi.h +++ b/samples/server/petstore/cpp-pistache/api/StoreApi.h @@ -87,8 +87,8 @@ class StoreApi { /// /// /// - /// order placed for purchasing the pet - virtual void place_order(const Order &order, Pistache::Http::ResponseWriter &response) = 0; + /// order placed for purchasing the pet + virtual void place_order(const Order &body, Pistache::Http::ResponseWriter &response) = 0; }; diff --git a/samples/server/petstore/cpp-pistache/api/UserApi.cpp b/samples/server/petstore/cpp-pistache/api/UserApi.cpp index b534b87f2190..f539a61c7e96 100644 --- a/samples/server/petstore/cpp-pistache/api/UserApi.cpp +++ b/samples/server/petstore/cpp-pistache/api/UserApi.cpp @@ -49,14 +49,11 @@ void UserApi::create_user_handler(const Pistache::Rest::Request &request, Pistac // Getting the body param - User user; + User body; try { - nlohmann::json request_body = nlohmann::json::parse(request.body()); - - user.fromJson(request_body); - - this->create_user(user, response); + nlohmann::json::parse(request.body()).get_to(body); + this->create_user(body, response); } catch (std::runtime_error & e) { //send a 400 error response.send(Pistache::Http::Code::Bad_Request, e.what()); @@ -67,12 +64,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) { // Getting the body param - std::vector user; + std::vector body; try { - nlohmann::json request_body = nlohmann::json::parse(request.body()); - user = ArrayHelper::fromJson(request_body); - this->create_users_with_array_input(user, response); + nlohmann::json::parse(request.body()).get_to(body); + this->create_users_with_array_input(body, response); } catch (std::runtime_error & e) { //send a 400 error response.send(Pistache::Http::Code::Bad_Request, e.what()); @@ -83,12 +79,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) { // Getting the body param - std::vector user; + std::vector body; try { - nlohmann::json request_body = nlohmann::json::parse(request.body()); - user = ArrayHelper::fromJson(request_body); - this->create_users_with_list_input(user, response); + nlohmann::json::parse(request.body()).get_to(body); + this->create_users_with_list_input(body, response); } catch (std::runtime_error & e) { //send a 400 error response.send(Pistache::Http::Code::Bad_Request, e.what()); @@ -168,14 +163,11 @@ void UserApi::update_user_handler(const Pistache::Rest::Request &request, Pistac // Getting the body param - User user; + User body; try { - nlohmann::json request_body = nlohmann::json::parse(request.body()); - - user.fromJson(request_body); - - this->update_user(username, user, response); + nlohmann::json::parse(request.body()).get_to(body); + this->update_user(username, body, response); } catch (std::runtime_error & e) { //send a 400 error response.send(Pistache::Http::Code::Bad_Request, e.what()); diff --git a/samples/server/petstore/cpp-pistache/api/UserApi.h b/samples/server/petstore/cpp-pistache/api/UserApi.h index 8ec6bb5692f7..7083c7643070 100644 --- a/samples/server/petstore/cpp-pistache/api/UserApi.h +++ b/samples/server/petstore/cpp-pistache/api/UserApi.h @@ -65,8 +65,8 @@ class UserApi { /// /// This can only be done by the logged in user. /// - /// Created user object - virtual void create_user(const User &user, Pistache::Http::ResponseWriter &response) = 0; + /// Created user object + virtual void create_user(const User &body, Pistache::Http::ResponseWriter &response) = 0; /// /// Creates list of users with given input array @@ -74,8 +74,8 @@ class UserApi { /// /// /// - /// List of user object - virtual void create_users_with_array_input(const std::vector &user, Pistache::Http::ResponseWriter &response) = 0; + /// List of user object + virtual void create_users_with_array_input(const std::vector &body, Pistache::Http::ResponseWriter &response) = 0; /// /// Creates list of users with given input array @@ -83,8 +83,8 @@ class UserApi { /// /// /// - /// List of user object - virtual void create_users_with_list_input(const std::vector &user, Pistache::Http::ResponseWriter &response) = 0; + /// List of user object + virtual void create_users_with_list_input(const std::vector &body, Pistache::Http::ResponseWriter &response) = 0; /// /// Delete user @@ -129,8 +129,8 @@ class UserApi { /// This can only be done by the logged in user. /// /// name that need to be deleted - /// Updated user object - virtual void update_user(const std::string &username, const User &user, Pistache::Http::ResponseWriter &response) = 0; + /// Updated user object + virtual void update_user(const std::string &username, const User &body, Pistache::Http::ResponseWriter &response) = 0; }; diff --git a/samples/server/petstore/cpp-pistache/impl/PetApiImpl.cpp b/samples/server/petstore/cpp-pistache/impl/PetApiImpl.cpp index 6e8801d1a30b..b35b40899b01 100644 --- a/samples/server/petstore/cpp-pistache/impl/PetApiImpl.cpp +++ b/samples/server/petstore/cpp-pistache/impl/PetApiImpl.cpp @@ -23,7 +23,7 @@ PetApiImpl::PetApiImpl(std::shared_ptr rtr) : PetApi(rtr) { } -void PetApiImpl::add_pet(const Pet &pet, Pistache::Http::ResponseWriter &response) { +void PetApiImpl::add_pet(const Pet &body, Pistache::Http::ResponseWriter &response) { response.send(Pistache::Http::Code::Ok, "Do some magic\n"); } void PetApiImpl::delete_pet(const int64_t &petId, const Pistache::Optional &apiKey, Pistache::Http::ResponseWriter &response) { @@ -38,7 +38,7 @@ void PetApiImpl::find_pets_by_tags(const Pistache::Optional); ~PetApiImpl() {} - void add_pet(const Pet &pet, Pistache::Http::ResponseWriter &response); + void add_pet(const Pet &body, 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 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 &body, 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); diff --git a/samples/server/petstore/cpp-pistache/impl/StoreApiImpl.cpp b/samples/server/petstore/cpp-pistache/impl/StoreApiImpl.cpp index f5367c72b7f9..63030737b61e 100644 --- a/samples/server/petstore/cpp-pistache/impl/StoreApiImpl.cpp +++ b/samples/server/petstore/cpp-pistache/impl/StoreApiImpl.cpp @@ -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) { response.send(Pistache::Http::Code::Ok, "Do some magic\n"); } -void StoreApiImpl::place_order(const Order &order, Pistache::Http::ResponseWriter &response) { +void StoreApiImpl::place_order(const Order &body, Pistache::Http::ResponseWriter &response) { response.send(Pistache::Http::Code::Ok, "Do some magic\n"); } diff --git a/samples/server/petstore/cpp-pistache/impl/StoreApiImpl.h b/samples/server/petstore/cpp-pistache/impl/StoreApiImpl.h index d28acea1f670..71689ae18b1d 100644 --- a/samples/server/petstore/cpp-pistache/impl/StoreApiImpl.h +++ b/samples/server/petstore/cpp-pistache/impl/StoreApiImpl.h @@ -48,7 +48,7 @@ class StoreApiImpl : public org::openapitools::server::api::StoreApi { void delete_order(const std::string &orderId, 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 place_order(const Order &order, Pistache::Http::ResponseWriter &response); + void place_order(const Order &body, Pistache::Http::ResponseWriter &response); }; diff --git a/samples/server/petstore/cpp-pistache/impl/UserApiImpl.cpp b/samples/server/petstore/cpp-pistache/impl/UserApiImpl.cpp index a810c266fdf0..2e7b06d956d8 100644 --- a/samples/server/petstore/cpp-pistache/impl/UserApiImpl.cpp +++ b/samples/server/petstore/cpp-pistache/impl/UserApiImpl.cpp @@ -23,13 +23,13 @@ UserApiImpl::UserApiImpl(std::shared_ptr rtr) : UserApi(rtr) { } -void UserApiImpl::create_user(const User &user, Pistache::Http::ResponseWriter &response) { +void UserApiImpl::create_user(const User &body, Pistache::Http::ResponseWriter &response) { response.send(Pistache::Http::Code::Ok, "Do some magic\n"); } -void UserApiImpl::create_users_with_array_input(const std::vector &user, Pistache::Http::ResponseWriter &response) { +void UserApiImpl::create_users_with_array_input(const std::vector &body, Pistache::Http::ResponseWriter &response) { response.send(Pistache::Http::Code::Ok, "Do some magic\n"); } -void UserApiImpl::create_users_with_list_input(const std::vector &user, Pistache::Http::ResponseWriter &response) { +void UserApiImpl::create_users_with_list_input(const std::vector &body, Pistache::Http::ResponseWriter &response) { response.send(Pistache::Http::Code::Ok, "Do some magic\n"); } void UserApiImpl::delete_user(const std::string &username, Pistache::Http::ResponseWriter &response) { @@ -44,7 +44,7 @@ void UserApiImpl::login_user(const Pistache::Optional &username, co void UserApiImpl::logout_user(Pistache::Http::ResponseWriter &response) { response.send(Pistache::Http::Code::Ok, "Do some magic\n"); } -void UserApiImpl::update_user(const std::string &username, const User &user, Pistache::Http::ResponseWriter &response) { +void UserApiImpl::update_user(const std::string &username, const User &body, Pistache::Http::ResponseWriter &response) { response.send(Pistache::Http::Code::Ok, "Do some magic\n"); } diff --git a/samples/server/petstore/cpp-pistache/impl/UserApiImpl.h b/samples/server/petstore/cpp-pistache/impl/UserApiImpl.h index 771f3eaeef24..9bde4fc572a2 100644 --- a/samples/server/petstore/cpp-pistache/impl/UserApiImpl.h +++ b/samples/server/petstore/cpp-pistache/impl/UserApiImpl.h @@ -45,14 +45,14 @@ class UserApiImpl : public org::openapitools::server::api::UserApi { UserApiImpl(std::shared_ptr); ~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); - void create_users_with_list_input(const std::vector &user, Pistache::Http::ResponseWriter &response); + void create_user(const User &body, Pistache::Http::ResponseWriter &response); + void create_users_with_array_input(const std::vector &body, Pistache::Http::ResponseWriter &response); + void create_users_with_list_input(const std::vector &body, 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 login_user(const Pistache::Optional &username, const Pistache::Optional &password, Pistache::Http::ResponseWriter &response); void logout_user(Pistache::Http::ResponseWriter &response); - void update_user(const std::string &username, const User &user, Pistache::Http::ResponseWriter &response); + void update_user(const std::string &username, const User &body, Pistache::Http::ResponseWriter &response); }; diff --git a/samples/server/petstore/cpp-pistache/model/ApiResponse.cpp b/samples/server/petstore/cpp-pistache/model/ApiResponse.cpp index d316194ce8cb..abcbbf3db084 100644 --- a/samples/server/petstore/cpp-pistache/model/ApiResponse.cpp +++ b/samples/server/petstore/cpp-pistache/model/ApiResponse.cpp @@ -38,45 +38,36 @@ void ApiResponse::validate() // TODO: implement validation } -nlohmann::json ApiResponse::toJson() const +void to_json(nlohmann::json& j, const ApiResponse& o) { - nlohmann::json val = nlohmann::json::object(); - - if(m_CodeIsSet) - { - val["code"] = m_Code; - } - if(m_TypeIsSet) - { - val["type"] = ModelBase::toJson(m_Type); - } - if(m_MessageIsSet) - { - val["message"] = ModelBase::toJson(m_Message); - } - - - return val; + j = nlohmann::json(); + if(o.codeIsSet()) + j["code"] = o.m_Code; + if(o.typeIsSet()) + j["type"] = o.m_Type; + if(o.messageIsSet()) + j["message"] = o.m_Message; } -void ApiResponse::fromJson(const nlohmann::json& val) +void from_json(const nlohmann::json& j, ApiResponse& o) { - if(val.find("code") != val.end()) + if(!j.at("code").is_null()) { - setCode(val.at("code")); - } - if(val.find("type") != val.end()) + j.at("code").get_to(o.m_Code); + o.m_CodeIsSet = true; + } + if(!j.at("type").is_null()) { - setType(val.at("type")); - } - if(val.find("message") != val.end()) + j.at("type").get_to(o.m_Type); + o.m_TypeIsSet = true; + } + if(!j.at("message").is_null()) { - setMessage(val.at("message")); - } - + j.at("message").get_to(o.m_Message); + o.m_MessageIsSet = true; + } } - int32_t ApiResponse::getCode() const { return m_Code; diff --git a/samples/server/petstore/cpp-pistache/model/ApiResponse.h b/samples/server/petstore/cpp-pistache/model/ApiResponse.h index fb8cc43693fa..3291251d8cb5 100644 --- a/samples/server/petstore/cpp-pistache/model/ApiResponse.h +++ b/samples/server/petstore/cpp-pistache/model/ApiResponse.h @@ -19,9 +19,8 @@ #define ApiResponse_H_ -#include "ModelBase.h" - #include +#include namespace org { namespace openapitools { @@ -32,19 +31,12 @@ namespace model { /// Describes the result of uploading an image resource /// class ApiResponse - : public ModelBase { public: ApiResponse(); virtual ~ApiResponse(); - ///////////////////////////////////////////// - /// ModelBase overrides - - void validate() override; - - nlohmann::json toJson() const override; - void fromJson(const nlohmann::json& json) override; + void validate(); ///////////////////////////////////////////// /// ApiResponse members @@ -71,6 +63,8 @@ class ApiResponse bool messageIsSet() const; void unsetMessage(); + friend void to_json(nlohmann::json& j, const ApiResponse& o); + friend void from_json(const nlohmann::json& j, ApiResponse& o); protected: int32_t m_Code; bool m_CodeIsSet; diff --git a/samples/server/petstore/cpp-pistache/model/Category.cpp b/samples/server/petstore/cpp-pistache/model/Category.cpp index 9ae18edc686d..856540a37542 100644 --- a/samples/server/petstore/cpp-pistache/model/Category.cpp +++ b/samples/server/petstore/cpp-pistache/model/Category.cpp @@ -36,37 +36,29 @@ void Category::validate() // TODO: implement validation } -nlohmann::json Category::toJson() const +void to_json(nlohmann::json& j, const Category& o) { - nlohmann::json val = nlohmann::json::object(); - - if(m_IdIsSet) - { - val["id"] = m_Id; - } - if(m_NameIsSet) - { - val["name"] = ModelBase::toJson(m_Name); - } - - - return val; + j = nlohmann::json(); + if(o.idIsSet()) + j["id"] = o.m_Id; + if(o.nameIsSet()) + j["name"] = o.m_Name; } -void Category::fromJson(const nlohmann::json& val) +void from_json(const nlohmann::json& j, Category& o) { - if(val.find("id") != val.end()) + if(!j.at("id").is_null()) { - setId(val.at("id")); - } - if(val.find("name") != val.end()) + j.at("id").get_to(o.m_Id); + o.m_IdIsSet = true; + } + if(!j.at("name").is_null()) { - setName(val.at("name")); - } - + j.at("name").get_to(o.m_Name); + o.m_NameIsSet = true; + } } - int64_t Category::getId() const { return m_Id; diff --git a/samples/server/petstore/cpp-pistache/model/Category.h b/samples/server/petstore/cpp-pistache/model/Category.h index 4ed3fafc8c8c..4018607b4350 100644 --- a/samples/server/petstore/cpp-pistache/model/Category.h +++ b/samples/server/petstore/cpp-pistache/model/Category.h @@ -19,9 +19,8 @@ #define Category_H_ -#include "ModelBase.h" - #include +#include namespace org { namespace openapitools { @@ -32,19 +31,12 @@ namespace model { /// A category for a pet /// class Category - : public ModelBase { public: Category(); virtual ~Category(); - ///////////////////////////////////////////// - /// ModelBase overrides - - void validate() override; - - nlohmann::json toJson() const override; - void fromJson(const nlohmann::json& json) override; + void validate(); ///////////////////////////////////////////// /// Category members @@ -64,6 +56,8 @@ class Category bool nameIsSet() const; void unsetName(); + friend void to_json(nlohmann::json& j, const Category& o); + friend void from_json(const nlohmann::json& j, Category& o); protected: int64_t m_Id; bool m_IdIsSet; diff --git a/samples/server/petstore/cpp-pistache/model/ModelBase.cpp b/samples/server/petstore/cpp-pistache/model/ModelBase.cpp deleted file mode 100644 index 19910b9d6012..000000000000 --- a/samples/server/petstore/cpp-pistache/model/ModelBase.cpp +++ /dev/null @@ -1,66 +0,0 @@ -/** -* 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 "ModelBase.h" - -namespace org { -namespace openapitools { -namespace server { -namespace model { - -ModelBase::ModelBase() -{ -} -ModelBase::~ModelBase() -{ -} - -std::string ModelBase::toJson( std::string const& value ) -{ - return value; -} - -std::string ModelBase::toJson( std::time_t const& value ) -{ - char buf[sizeof "2011-10-08T07:07:09Z"]; - strftime(buf, sizeof buf, "%FT%TZ", gmtime(&value)); - return buf; -} - -int32_t ModelBase::toJson( int32_t const value ) -{ - return value; -} - -int64_t ModelBase::toJson( int64_t const value ) -{ - return value; -} - -double ModelBase::toJson( double const value ) -{ - return value; -} - -bool ModelBase::toJson( bool const value ) -{ - return value; -} - -nlohmann::json ModelBase::toJson(ModelBase const& content ) -{ - return content.toJson(); -} - -} -} -} -} diff --git a/samples/server/petstore/cpp-pistache/model/ModelBase.h b/samples/server/petstore/cpp-pistache/model/ModelBase.h deleted file mode 100644 index 6bc0dc921243..000000000000 --- a/samples/server/petstore/cpp-pistache/model/ModelBase.h +++ /dev/null @@ -1,139 +0,0 @@ -/** -* 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. -*/ -/* - * ModelBase.h - * - * This is the base class for all model classes - */ - -#ifndef ModelBase_H_ -#define ModelBase_H_ - - -#include "json.hpp" -#include -#include -#include -#include - -namespace org { -namespace openapitools { -namespace server { -namespace model { - -class ModelBase -{ -public: - ModelBase(); - virtual ~ModelBase(); - - virtual void validate() = 0; - - virtual nlohmann::json toJson() const = 0; - virtual void fromJson(const nlohmann::json& json) = 0; - - static std::string toJson( std::string const& value ); - static std::string toJson( std::time_t const& value ); - static int32_t toJson( int32_t const value ); - static int64_t toJson( int64_t const value ); - static double toJson( double const value ); - static bool toJson( bool const value ); - static nlohmann::json toJson(ModelBase const& content ); -}; - -class ArrayHelper { -private: - template::value>::value> - static void itemFromJson(T& item, const nlohmann::json& json){ - item = json; - } - static void itemFromJson(ModelBase& item, const nlohmann::json& json){ - item.fromJson(json); - } - template::value>::value> - static nlohmann::json itemtoJson(const T& item){ - return item; - } - static nlohmann::json itemtoJson(const ModelBase& item){ - return item.toJson(); - } -public: - template - static std::vector fromJson(const nlohmann::json& json) { - std::vector val; - if (!json.empty()) { - for (const auto& item : json.items()) { - T entry; - itemFromJson(entry, item.value()); - val.push_back(entry); - } - } - return val; - } - template - static nlohmann::json toJson(const std::vector& val) { - nlohmann::json json; - for(const auto& item : val){ - json.push_back(itemtoJson(item)); - } - return json; - } -}; - -class MapHelper { -private: - template::value>::value> - static void itemFromJson(T &item, const nlohmann::json& json){ - item = json; - } - static void itemFromJson(ModelBase &item, const nlohmann::json& json){ - item.fromJson(json); - } - template::value>::value> - static nlohmann::json itemtoJson(const T& item){ - return item; - } - static nlohmann::json itemtoJson(const ModelBase& item){ - return item.toJson(); - } - -public: - template - static std::map fromJson(const nlohmann::json& json) { - std::map val; - if (!json.empty()) { - for (const auto& item : json.items()) { - T entry; - itemfromJson(entry, item.value()); - val.insert(val.end(), - std::pair(item.key(), entry)); - } - } - return val; - } - template - static nlohmann::json toJson(const std::map& val) { - nlohmann::json json; - for (const auto& item : val) { - nlohmann::json jitem = itemtoJson(item.second); - json[item.first] = jitem; - } - return json; - } -}; - -} -} -} -} - -#endif /* ModelBase_H_ */ diff --git a/samples/server/petstore/cpp-pistache/model/Order.cpp b/samples/server/petstore/cpp-pistache/model/Order.cpp index 6baadd493acc..0d6c43da14f5 100644 --- a/samples/server/petstore/cpp-pistache/model/Order.cpp +++ b/samples/server/petstore/cpp-pistache/model/Order.cpp @@ -44,70 +44,57 @@ void Order::validate() // TODO: implement validation } -nlohmann::json Order::toJson() const -{ - nlohmann::json val = nlohmann::json::object(); - - if(m_IdIsSet) - { - val["id"] = m_Id; - } - if(m_PetIdIsSet) - { - val["petId"] = m_PetId; - } - if(m_QuantityIsSet) - { - val["quantity"] = m_Quantity; - } - if(m_ShipDateIsSet) - { - val["shipDate"] = ModelBase::toJson(m_ShipDate); - } - if(m_StatusIsSet) - { - val["status"] = ModelBase::toJson(m_Status); - } - if(m_CompleteIsSet) - { - val["complete"] = m_Complete; - } - - - return val; +void to_json(nlohmann::json& j, const Order& o) +{ + j = nlohmann::json(); + if(o.idIsSet()) + j["id"] = o.m_Id; + if(o.petIdIsSet()) + j["petId"] = o.m_PetId; + if(o.quantityIsSet()) + j["quantity"] = o.m_Quantity; + if(o.shipDateIsSet()) + j["shipDate"] = o.m_ShipDate; + if(o.statusIsSet()) + j["status"] = o.m_Status; + if(o.completeIsSet()) + j["complete"] = o.m_Complete; } -void Order::fromJson(const nlohmann::json& val) +void from_json(const nlohmann::json& j, Order& o) { - if(val.find("id") != val.end()) + if(!j.at("id").is_null()) { - setId(val.at("id")); - } - if(val.find("petId") != val.end()) + j.at("id").get_to(o.m_Id); + o.m_IdIsSet = true; + } + if(!j.at("petId").is_null()) { - setPetId(val.at("petId")); - } - if(val.find("quantity") != val.end()) + j.at("petId").get_to(o.m_PetId); + o.m_PetIdIsSet = true; + } + if(!j.at("quantity").is_null()) { - setQuantity(val.at("quantity")); - } - if(val.find("shipDate") != val.end()) + j.at("quantity").get_to(o.m_Quantity); + o.m_QuantityIsSet = true; + } + if(!j.at("shipDate").is_null()) { - setShipDate(val.at("shipDate")); - - } - if(val.find("status") != val.end()) + j.at("shipDate").get_to(o.m_ShipDate); + o.m_ShipDateIsSet = true; + } + if(!j.at("status").is_null()) { - setStatus(val.at("status")); - } - if(val.find("complete") != val.end()) + j.at("status").get_to(o.m_Status); + o.m_StatusIsSet = true; + } + if(!j.at("complete").is_null()) { - setComplete(val.at("complete")); - } - + j.at("complete").get_to(o.m_Complete); + o.m_CompleteIsSet = true; + } } - int64_t Order::getId() const { return m_Id; diff --git a/samples/server/petstore/cpp-pistache/model/Order.h b/samples/server/petstore/cpp-pistache/model/Order.h index bc77d0910597..b4d4598b749a 100644 --- a/samples/server/petstore/cpp-pistache/model/Order.h +++ b/samples/server/petstore/cpp-pistache/model/Order.h @@ -19,9 +19,8 @@ #define Order_H_ -#include "ModelBase.h" - #include +#include namespace org { namespace openapitools { @@ -32,19 +31,12 @@ namespace model { /// An order for a pets from the pet store /// class Order - : public ModelBase { public: Order(); virtual ~Order(); - ///////////////////////////////////////////// - /// ModelBase overrides - - void validate() override; - - nlohmann::json toJson() const override; - void fromJson(const nlohmann::json& json) override; + void validate(); ///////////////////////////////////////////// /// Order members @@ -92,6 +84,8 @@ class Order bool completeIsSet() const; void unsetComplete(); + friend void to_json(nlohmann::json& j, const Order& o); + friend void from_json(const nlohmann::json& j, Order& o); protected: int64_t m_Id; bool m_IdIsSet; diff --git a/samples/server/petstore/cpp-pistache/model/Pet.cpp b/samples/server/petstore/cpp-pistache/model/Pet.cpp index 834ac8ff6326..cecbac69840d 100644 --- a/samples/server/petstore/cpp-pistache/model/Pet.cpp +++ b/samples/server/petstore/cpp-pistache/model/Pet.cpp @@ -39,103 +39,47 @@ void Pet::validate() // TODO: implement validation } -nlohmann::json Pet::toJson() const -{ - nlohmann::json val = nlohmann::json::object(); - - if(m_IdIsSet) - { - val["id"] = m_Id; - } - if(m_CategoryIsSet) - { - val["category"] = ModelBase::toJson(m_Category); - } - val["name"] = ModelBase::toJson(m_Name); - { - nlohmann::json jsonArray; - for( auto& item : m_PhotoUrls ) - { - jsonArray.push_back(ModelBase::toJson(item)); - } - val["photoUrls"] = jsonArray; - - } - { - nlohmann::json jsonArray; - for( auto& item : m_Tags ) - { - jsonArray.push_back(ModelBase::toJson(item)); - } - - if(jsonArray.size() > 0) - { - val["tags"] = jsonArray; - } - } - if(m_StatusIsSet) - { - val["status"] = ModelBase::toJson(m_Status); - } - - - return val; +void to_json(nlohmann::json& j, const Pet& o) +{ + j = nlohmann::json(); + if(o.idIsSet()) + j["id"] = o.m_Id; + if(o.categoryIsSet()) + j["category"] = o.m_Category; + j["name"] = o.m_Name; + j["photoUrls"] = o.m_PhotoUrls; + if(o.tagsIsSet()) + j["tags"] = o.m_Tags; + if(o.statusIsSet()) + j["status"] = o.m_Status; } -void Pet::fromJson(const nlohmann::json& val) +void from_json(const nlohmann::json& j, Pet& o) { - if(val.find("id") != val.end()) + if(!j.at("id").is_null()) { - setId(val.at("id")); - } - if(val.find("category") != val.end()) + j.at("id").get_to(o.m_Id); + o.m_IdIsSet = true; + } + if(!j.at("category").is_null()) { - if(!val["category"].is_null()) - { - Category newItem; - newItem.fromJson(val["category"]); - setCategory( newItem ); - } - - } - setName(val.at("name")); + j.at("category").get_to(o.m_Category); + o.m_CategoryIsSet = true; + } + j.at("name").get_to(o.m_Name); + j.at("photoUrls").get_to(o.m_PhotoUrls); + if(!j.at("tags").is_null()) { - m_PhotoUrls.clear(); - for( auto& item : val["photoUrls"] ) - { - m_PhotoUrls.push_back(item); - - } - } + j.at("tags").get_to(o.m_Tags); + o.m_TagsIsSet = true; + } + if(!j.at("status").is_null()) { - m_Tags.clear(); - if(val.find("tags") != val.end()) - { - for( auto& item : val["tags"] ) - { - - if(item.is_null()) - { - m_Tags.push_back( Tag() ); - } - else - { - Tag newItem; - newItem.fromJson(item); - m_Tags.push_back( newItem ); - } - - } - } - } - if(val.find("status") != val.end()) - { - setStatus(val.at("status")); - } - + j.at("status").get_to(o.m_Status); + o.m_StatusIsSet = true; + } } - int64_t Pet::getId() const { return m_Id; diff --git a/samples/server/petstore/cpp-pistache/model/Pet.h b/samples/server/petstore/cpp-pistache/model/Pet.h index 6f7e9a565a63..22eeb9570f48 100644 --- a/samples/server/petstore/cpp-pistache/model/Pet.h +++ b/samples/server/petstore/cpp-pistache/model/Pet.h @@ -19,12 +19,11 @@ #define Pet_H_ -#include "ModelBase.h" - #include "Tag.h" #include #include "Category.h" #include +#include namespace org { namespace openapitools { @@ -35,19 +34,12 @@ namespace model { /// A pet for sale in the pet store /// class Pet - : public ModelBase { public: Pet(); virtual ~Pet(); - ///////////////////////////////////////////// - /// ModelBase overrides - - void validate() override; - - nlohmann::json toJson() const override; - void fromJson(const nlohmann::json& json) override; + void validate(); ///////////////////////////////////////////// /// Pet members @@ -89,6 +81,8 @@ class Pet bool statusIsSet() const; void unsetStatus(); + friend void to_json(nlohmann::json& j, const Pet& o); + friend void from_json(const nlohmann::json& j, Pet& o); protected: int64_t m_Id; bool m_IdIsSet; diff --git a/samples/server/petstore/cpp-pistache/model/Tag.cpp b/samples/server/petstore/cpp-pistache/model/Tag.cpp index 9efe5aeb9dbe..19a4e27ed2ed 100644 --- a/samples/server/petstore/cpp-pistache/model/Tag.cpp +++ b/samples/server/petstore/cpp-pistache/model/Tag.cpp @@ -36,37 +36,29 @@ void Tag::validate() // TODO: implement validation } -nlohmann::json Tag::toJson() const +void to_json(nlohmann::json& j, const Tag& o) { - nlohmann::json val = nlohmann::json::object(); - - if(m_IdIsSet) - { - val["id"] = m_Id; - } - if(m_NameIsSet) - { - val["name"] = ModelBase::toJson(m_Name); - } - - - return val; + j = nlohmann::json(); + if(o.idIsSet()) + j["id"] = o.m_Id; + if(o.nameIsSet()) + j["name"] = o.m_Name; } -void Tag::fromJson(const nlohmann::json& val) +void from_json(const nlohmann::json& j, Tag& o) { - if(val.find("id") != val.end()) + if(!j.at("id").is_null()) { - setId(val.at("id")); - } - if(val.find("name") != val.end()) + j.at("id").get_to(o.m_Id); + o.m_IdIsSet = true; + } + if(!j.at("name").is_null()) { - setName(val.at("name")); - } - + j.at("name").get_to(o.m_Name); + o.m_NameIsSet = true; + } } - int64_t Tag::getId() const { return m_Id; diff --git a/samples/server/petstore/cpp-pistache/model/Tag.h b/samples/server/petstore/cpp-pistache/model/Tag.h index f46aecf78b40..bb5dff9f922b 100644 --- a/samples/server/petstore/cpp-pistache/model/Tag.h +++ b/samples/server/petstore/cpp-pistache/model/Tag.h @@ -19,9 +19,8 @@ #define Tag_H_ -#include "ModelBase.h" - #include +#include namespace org { namespace openapitools { @@ -32,19 +31,12 @@ namespace model { /// A tag for a pet /// class Tag - : public ModelBase { public: Tag(); virtual ~Tag(); - ///////////////////////////////////////////// - /// ModelBase overrides - - void validate() override; - - nlohmann::json toJson() const override; - void fromJson(const nlohmann::json& json) override; + void validate(); ///////////////////////////////////////////// /// Tag members @@ -64,6 +56,8 @@ class Tag bool nameIsSet() const; void unsetName(); + friend void to_json(nlohmann::json& j, const Tag& o); + friend void from_json(const nlohmann::json& j, Tag& o); protected: int64_t m_Id; bool m_IdIsSet; diff --git a/samples/server/petstore/cpp-pistache/model/User.cpp b/samples/server/petstore/cpp-pistache/model/User.cpp index 186fc58924d2..6bc726b0563d 100644 --- a/samples/server/petstore/cpp-pistache/model/User.cpp +++ b/samples/server/petstore/cpp-pistache/model/User.cpp @@ -48,85 +48,71 @@ void User::validate() // TODO: implement validation } -nlohmann::json User::toJson() const -{ - nlohmann::json val = nlohmann::json::object(); - - if(m_IdIsSet) - { - val["id"] = m_Id; - } - if(m_UsernameIsSet) - { - val["username"] = ModelBase::toJson(m_Username); - } - if(m_FirstNameIsSet) - { - val["firstName"] = ModelBase::toJson(m_FirstName); - } - if(m_LastNameIsSet) - { - val["lastName"] = ModelBase::toJson(m_LastName); - } - if(m_EmailIsSet) - { - val["email"] = ModelBase::toJson(m_Email); - } - if(m_PasswordIsSet) - { - val["password"] = ModelBase::toJson(m_Password); - } - if(m_PhoneIsSet) - { - val["phone"] = ModelBase::toJson(m_Phone); - } - if(m_UserStatusIsSet) - { - val["userStatus"] = m_UserStatus; - } - - - return val; +void to_json(nlohmann::json& j, const User& o) +{ + j = nlohmann::json(); + if(o.idIsSet()) + j["id"] = o.m_Id; + if(o.usernameIsSet()) + j["username"] = o.m_Username; + if(o.firstNameIsSet()) + j["firstName"] = o.m_FirstName; + if(o.lastNameIsSet()) + j["lastName"] = o.m_LastName; + if(o.emailIsSet()) + j["email"] = o.m_Email; + if(o.passwordIsSet()) + j["password"] = o.m_Password; + if(o.phoneIsSet()) + j["phone"] = o.m_Phone; + if(o.userStatusIsSet()) + j["userStatus"] = o.m_UserStatus; } -void User::fromJson(const nlohmann::json& val) +void from_json(const nlohmann::json& j, User& o) { - if(val.find("id") != val.end()) + if(!j.at("id").is_null()) { - setId(val.at("id")); - } - if(val.find("username") != val.end()) + j.at("id").get_to(o.m_Id); + o.m_IdIsSet = true; + } + if(!j.at("username").is_null()) { - setUsername(val.at("username")); - } - if(val.find("firstName") != val.end()) + j.at("username").get_to(o.m_Username); + o.m_UsernameIsSet = true; + } + if(!j.at("firstName").is_null()) { - setFirstName(val.at("firstName")); - } - if(val.find("lastName") != val.end()) + j.at("firstName").get_to(o.m_FirstName); + o.m_FirstNameIsSet = true; + } + if(!j.at("lastName").is_null()) { - setLastName(val.at("lastName")); - } - if(val.find("email") != val.end()) + j.at("lastName").get_to(o.m_LastName); + o.m_LastNameIsSet = true; + } + if(!j.at("email").is_null()) { - setEmail(val.at("email")); - } - if(val.find("password") != val.end()) + j.at("email").get_to(o.m_Email); + o.m_EmailIsSet = true; + } + if(!j.at("password").is_null()) { - setPassword(val.at("password")); - } - if(val.find("phone") != val.end()) + j.at("password").get_to(o.m_Password); + o.m_PasswordIsSet = true; + } + if(!j.at("phone").is_null()) { - setPhone(val.at("phone")); - } - if(val.find("userStatus") != val.end()) + j.at("phone").get_to(o.m_Phone); + o.m_PhoneIsSet = true; + } + if(!j.at("userStatus").is_null()) { - setUserStatus(val.at("userStatus")); - } - + j.at("userStatus").get_to(o.m_UserStatus); + o.m_UserStatusIsSet = true; + } } - int64_t User::getId() const { return m_Id; diff --git a/samples/server/petstore/cpp-pistache/model/User.h b/samples/server/petstore/cpp-pistache/model/User.h index a52a183cd46d..4b8cd2b19626 100644 --- a/samples/server/petstore/cpp-pistache/model/User.h +++ b/samples/server/petstore/cpp-pistache/model/User.h @@ -19,9 +19,8 @@ #define User_H_ -#include "ModelBase.h" - #include +#include namespace org { namespace openapitools { @@ -32,19 +31,12 @@ namespace model { /// A User who is purchasing from the pet store /// class User - : public ModelBase { public: User(); virtual ~User(); - ///////////////////////////////////////////// - /// ModelBase overrides - - void validate() override; - - nlohmann::json toJson() const override; - void fromJson(const nlohmann::json& json) override; + void validate(); ///////////////////////////////////////////// /// User members @@ -106,6 +98,8 @@ class User bool userStatusIsSet() const; void unsetUserStatus(); + friend void to_json(nlohmann::json& j, const User& o); + friend void from_json(const nlohmann::json& j, User& o); protected: int64_t m_Id; bool m_IdIsSet;