From f8f3a082825f63d794fa333f74c2dd250add6f0c Mon Sep 17 00:00:00 2001 From: sunn <33183834+etherealjoy@users.noreply.github.com> Date: Sat, 3 Nov 2018 14:09:31 +0100 Subject: [PATCH] [cpp-pistache]Add support for map (#1359) * Add support for map * Add support for nested maps * Simplify Array and Map Helper * Use const reference wherever possible --- .../cpp-pistache-server/api-source.mustache | 2 +- .../cpp-pistache-server/model-header.mustache | 2 +- .../cpp-pistache-server/model-source.mustache | 82 ++++++--- .../modelbase-header.mustache | 163 ++++++++---------- .../server/petstore/cpp-pistache/api/PetApi.h | 2 +- .../petstore/cpp-pistache/api/StoreApi.h | 2 +- .../petstore/cpp-pistache/api/UserApi.cpp | 4 +- .../petstore/cpp-pistache/api/UserApi.h | 2 +- .../cpp-pistache/model/ApiResponse.cpp | 2 +- .../petstore/cpp-pistache/model/ApiResponse.h | 2 +- .../petstore/cpp-pistache/model/Category.cpp | 2 +- .../petstore/cpp-pistache/model/Category.h | 2 +- .../petstore/cpp-pistache/model/ModelBase.h | 163 ++++++++---------- .../petstore/cpp-pistache/model/Order.cpp | 2 +- .../petstore/cpp-pistache/model/Order.h | 2 +- .../petstore/cpp-pistache/model/Pet.cpp | 45 +++-- .../server/petstore/cpp-pistache/model/Pet.h | 2 +- .../petstore/cpp-pistache/model/Tag.cpp | 2 +- .../server/petstore/cpp-pistache/model/Tag.h | 2 +- .../petstore/cpp-pistache/model/User.cpp | 2 +- .../server/petstore/cpp-pistache/model/User.h | 2 +- 21 files changed, 246 insertions(+), 243 deletions(-) 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 b8a4e34fad1..f1a7841c402 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 @@ -73,7 +73,7 @@ void {{classname}}::{{operationIdSnakeCase}}_handler(const Pistache::Rest::Reque nlohmann::json request_body = nlohmann::json::parse(request.body()); {{^isPrimitiveType}}{{^isContainer}} {{paramName}}.fromJson(request_body); - {{/isContainer}}{{/isPrimitiveType}}{{#isContainer}} {{paramName}} = {{#isListContainer}} {{prefix}}ModelArrayHelper{{/isListContainer}}{{#isMapContainer}} {{prefix}}ModelMapHelper{{/isMapContainer}}::fromJson<{{items.baseType}}>(request_body);{{/isContainer}} + {{/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; 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 0fb65fa26b3..10c0c8181f6 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 @@ -34,7 +34,7 @@ public: void validate() override; nlohmann::json toJson() const override; - void fromJson(nlohmann::json& json) override; + void fromJson(const nlohmann::json& json) override; ///////////////////////////////////////////// /// {{classname}} members 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 41d64dcf75d..ca57408bcb2 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 @@ -45,20 +45,34 @@ nlohmann::json {{classname}}::toJson() const if(jsonArray.size() > 0) { val["{{baseName}}"] = jsonArray; - } - {{/required}} + } {{/required}} } - {{/isListContainer}}{{^isListContainer}}{{^isPrimitiveType}}{{^required}}if(m_{{name}}IsSet) + {{/isListContainer}}{{#isMapContainer}}{ + nlohmann::json jsonObj; + for( auto const& 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}}{{/isListContainer}}{{/vars}} + {{/required}}{{/isPrimitiveType}}{{/isContainer}}{{/vars}} return val; } -void {{classname}}::fromJson(nlohmann::json& val) +void {{classname}}::fromJson(const nlohmann::json& val) { {{#vars}}{{#isPrimitiveType}}{{^isListContainer}}{{^required}}if(val.find("{{baseName}}") != val.end()) { @@ -67,33 +81,53 @@ void {{classname}}::fromJson(nlohmann::json& val) {{/required}}{{#required}}{{setter}}(val.at("{{baseName}}")); {{/required}}{{/isListContainer}}{{/isPrimitiveType}}{{#isListContainer}}{ m_{{name}}.clear(); - nlohmann::json jsonArray; {{^required}}if(val.find("{{baseName}}") != val.end()) { {{/required}} - for( 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()) + for( auto& item : val["{{baseName}}"] ) { - m_{{name}}.push_back( {{{items.datatype}}}() ); + {{#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}} } - else - { - {{{items.datatype}}} newItem; - newItem.fromJson(item); - m_{{name}}.push_back( newItem ); - } - {{/items.isDateTime}}{{/items.isString}}{{/isPrimitiveType}} - } {{^required}} } {{/required}} } - {{/isListContainer}}{{^isListContainer}}{{^isPrimitiveType}}{{^required}}if(val.find("{{baseName}}") != val.end()) + {{/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( 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()) { {{#isString}}{{setter}}(val.at("{{baseName}}"));{{/isString}}{{#isByteArray}}{{setter}}(val.at("{{baseName}}"));{{/isByteArray}}{{#isBinary}}{{setter}}(val.at("{{baseName}}")); {{/isBinary}}{{^isString}}{{#isDateTime}}{{setter}}(val.at("{{baseName}}")); @@ -107,7 +141,7 @@ void {{classname}}::fromJson(nlohmann::json& val) } {{/required}}{{#required}}{{#isString}}{{setter}}(val.at("{{baseName}}")); {{/isString}}{{^isString}}{{#isDateTime}}{{setter}}(val.at("{{baseName}}")); - {{/isDateTime}}{{/isString}}{{/required}}{{/isPrimitiveType}}{{/isListContainer}}{{/vars}} + {{/isDateTime}}{{/isString}}{{/required}}{{/isPrimitiveType}}{{/isContainer}}{{/vars}} } diff --git a/modules/openapi-generator/src/main/resources/cpp-pistache-server/modelbase-header.mustache b/modules/openapi-generator/src/main/resources/cpp-pistache-server/modelbase-header.mustache index faeff483038..8840764a57a 100644 --- a/modules/openapi-generator/src/main/resources/cpp-pistache-server/modelbase-header.mustache +++ b/modules/openapi-generator/src/main/resources/cpp-pistache-server/modelbase-header.mustache @@ -28,7 +28,7 @@ public: virtual void validate() = 0; virtual nlohmann::json toJson() const = 0; - virtual void fromJson(nlohmann::json& json) = 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 ); @@ -39,100 +39,85 @@ public: static nlohmann::json toJson({{prefix}}ModelBase const& content ); }; -class {{prefix}}ModelArrayHelper { -public: - template - static std::vector fromJson(nlohmann::json& json) { - T *ptrTest; - std::vector val; - if (dynamic_cast<{{prefix}}ModelBase*>(ptrTest) != nullptr) { - if (!json.empty()) { - for (auto &item : json.items()) { - T entry; - entry.fromJson(item.value()); - val.push_back(entry); - } - } - } - return val; - } - template - static nlohmann::json toJson(std::vector val) { - nlohmann::json json; - for(auto item : val){ - json.push_back(item.toJson()); - } - return json; - } -}; - 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(nlohmann::json& json) { - std::vector val; - nlohmann::from_json(json, val); - return val; - } - template - static nlohmann::json toJson(std::vector val) { - nlohmann::json json; - nlohmann::to_json(json, val); - return json; - } -}; - -class {{prefix}}ModelMapHelper { -public: - template - static std::map & fromJson(nlohmann::json& json) { - T *ptrTest; - std::map val; - if (dynamic_cast<{{prefix}}ModelBase*>(ptrTest) != nullptr) { - if (!json.empty()) { - for (auto &item : json.items()) { - T entry; - entry.fromJson(item.value()); - val.insert(val.end(), - std::pair(item.key(), entry)); - } - } - } - return val; - } - template - static nlohmann::json toJson(std::map val) { - nlohmann::json json; - for (auto const& item : val) { - json[item.first] = item.second.toJson(); - } - return json; - } + 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(nlohmann::json& json) { - std::map val; - if (!json.empty()) { - for (auto &item : json.items()) { - T entry = item.value(); - val.insert(val.end(), - std::pair(item.key(), entry)); - } - } - return val; - } - template - static nlohmann::json toJson(std::map val) { - nlohmann::json json; - for (auto const& item : val) { - nlohmann::json jitem = item.second; - json[item.first] = jitem; - } - return json; - } + 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}} diff --git a/samples/server/petstore/cpp-pistache/api/PetApi.h b/samples/server/petstore/cpp-pistache/api/PetApi.h index 5f4e983458e..d34ff4afa7e 100644 --- a/samples/server/petstore/cpp-pistache/api/PetApi.h +++ b/samples/server/petstore/cpp-pistache/api/PetApi.h @@ -22,9 +22,9 @@ #include #include #include - #include + #include "ApiResponse.h" #include "Pet.h" #include diff --git a/samples/server/petstore/cpp-pistache/api/StoreApi.h b/samples/server/petstore/cpp-pistache/api/StoreApi.h index e75f9374d08..53117f32a2e 100644 --- a/samples/server/petstore/cpp-pistache/api/StoreApi.h +++ b/samples/server/petstore/cpp-pistache/api/StoreApi.h @@ -22,9 +22,9 @@ #include #include #include - #include + #include "Order.h" #include #include diff --git a/samples/server/petstore/cpp-pistache/api/UserApi.cpp b/samples/server/petstore/cpp-pistache/api/UserApi.cpp index 62dc29967ee..b534b87f219 100644 --- a/samples/server/petstore/cpp-pistache/api/UserApi.cpp +++ b/samples/server/petstore/cpp-pistache/api/UserApi.cpp @@ -71,7 +71,7 @@ void UserApi::create_users_with_array_input_handler(const Pistache::Rest::Reques try { nlohmann::json request_body = nlohmann::json::parse(request.body()); - user = ModelArrayHelper::fromJson(request_body); + user = ArrayHelper::fromJson(request_body); this->create_users_with_array_input(user, response); } catch (std::runtime_error & e) { //send a 400 error @@ -87,7 +87,7 @@ void UserApi::create_users_with_list_input_handler(const Pistache::Rest::Request try { nlohmann::json request_body = nlohmann::json::parse(request.body()); - user = ModelArrayHelper::fromJson(request_body); + user = ArrayHelper::fromJson(request_body); this->create_users_with_list_input(user, response); } catch (std::runtime_error & e) { //send a 400 error diff --git a/samples/server/petstore/cpp-pistache/api/UserApi.h b/samples/server/petstore/cpp-pistache/api/UserApi.h index cd81b195ed0..8ec6bb5692f 100644 --- a/samples/server/petstore/cpp-pistache/api/UserApi.h +++ b/samples/server/petstore/cpp-pistache/api/UserApi.h @@ -22,9 +22,9 @@ #include #include #include - #include + #include "User.h" #include #include diff --git a/samples/server/petstore/cpp-pistache/model/ApiResponse.cpp b/samples/server/petstore/cpp-pistache/model/ApiResponse.cpp index b565e4b2675..d316194ce8c 100644 --- a/samples/server/petstore/cpp-pistache/model/ApiResponse.cpp +++ b/samples/server/petstore/cpp-pistache/model/ApiResponse.cpp @@ -59,7 +59,7 @@ nlohmann::json ApiResponse::toJson() const return val; } -void ApiResponse::fromJson(nlohmann::json& val) +void ApiResponse::fromJson(const nlohmann::json& val) { if(val.find("code") != val.end()) { diff --git a/samples/server/petstore/cpp-pistache/model/ApiResponse.h b/samples/server/petstore/cpp-pistache/model/ApiResponse.h index 51c96e3838c..fb8cc43693f 100644 --- a/samples/server/petstore/cpp-pistache/model/ApiResponse.h +++ b/samples/server/petstore/cpp-pistache/model/ApiResponse.h @@ -44,7 +44,7 @@ public: void validate() override; nlohmann::json toJson() const override; - void fromJson(nlohmann::json& json) override; + void fromJson(const nlohmann::json& json) override; ///////////////////////////////////////////// /// ApiResponse members diff --git a/samples/server/petstore/cpp-pistache/model/Category.cpp b/samples/server/petstore/cpp-pistache/model/Category.cpp index a5a67cba1b8..9ae18edc686 100644 --- a/samples/server/petstore/cpp-pistache/model/Category.cpp +++ b/samples/server/petstore/cpp-pistache/model/Category.cpp @@ -53,7 +53,7 @@ nlohmann::json Category::toJson() const return val; } -void Category::fromJson(nlohmann::json& val) +void Category::fromJson(const nlohmann::json& val) { if(val.find("id") != val.end()) { diff --git a/samples/server/petstore/cpp-pistache/model/Category.h b/samples/server/petstore/cpp-pistache/model/Category.h index 19b951a44e7..4ed3fafc8c8 100644 --- a/samples/server/petstore/cpp-pistache/model/Category.h +++ b/samples/server/petstore/cpp-pistache/model/Category.h @@ -44,7 +44,7 @@ public: void validate() override; nlohmann::json toJson() const override; - void fromJson(nlohmann::json& json) override; + void fromJson(const nlohmann::json& json) override; ///////////////////////////////////////////// /// Category members diff --git a/samples/server/petstore/cpp-pistache/model/ModelBase.h b/samples/server/petstore/cpp-pistache/model/ModelBase.h index 98afcd48d78..6bc0dc92124 100644 --- a/samples/server/petstore/cpp-pistache/model/ModelBase.h +++ b/samples/server/petstore/cpp-pistache/model/ModelBase.h @@ -39,7 +39,7 @@ public: virtual void validate() = 0; virtual nlohmann::json toJson() const = 0; - virtual void fromJson(nlohmann::json& json) = 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 ); @@ -50,100 +50,85 @@ public: static nlohmann::json toJson(ModelBase const& content ); }; -class ModelArrayHelper { -public: - template - static std::vector fromJson(nlohmann::json& json) { - T *ptrTest; - std::vector val; - if (dynamic_cast(ptrTest) != nullptr) { - if (!json.empty()) { - for (auto &item : json.items()) { - T entry; - entry.fromJson(item.value()); - val.push_back(entry); - } - } - } - return val; - } - template - static nlohmann::json toJson(std::vector val) { - nlohmann::json json; - for(auto item : val){ - json.push_back(item.toJson()); - } - return json; - } -}; - 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(nlohmann::json& json) { - std::vector val; - nlohmann::from_json(json, val); - return val; - } - template - static nlohmann::json toJson(std::vector val) { - nlohmann::json json; - nlohmann::to_json(json, val); - return json; - } -}; - -class ModelMapHelper { -public: - template - static std::map & fromJson(nlohmann::json& json) { - T *ptrTest; - std::map val; - if (dynamic_cast(ptrTest) != nullptr) { - if (!json.empty()) { - for (auto &item : json.items()) { - T entry; - entry.fromJson(item.value()); - val.insert(val.end(), - std::pair(item.key(), entry)); - } - } - } - return val; - } - template - static nlohmann::json toJson(std::map val) { - nlohmann::json json; - for (auto const& item : val) { - json[item.first] = item.second.toJson(); - } - return json; - } + 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(nlohmann::json& json) { - std::map val; - if (!json.empty()) { - for (auto &item : json.items()) { - T entry = item.value(); - val.insert(val.end(), - std::pair(item.key(), entry)); - } - } - return val; - } - template - static nlohmann::json toJson(std::map val) { - nlohmann::json json; - for (auto const& item : val) { - nlohmann::json jitem = item.second; - json[item.first] = jitem; - } - return json; - } + 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; + } }; } diff --git a/samples/server/petstore/cpp-pistache/model/Order.cpp b/samples/server/petstore/cpp-pistache/model/Order.cpp index 223fa7cfe99..6baadd493ac 100644 --- a/samples/server/petstore/cpp-pistache/model/Order.cpp +++ b/samples/server/petstore/cpp-pistache/model/Order.cpp @@ -77,7 +77,7 @@ nlohmann::json Order::toJson() const return val; } -void Order::fromJson(nlohmann::json& val) +void Order::fromJson(const nlohmann::json& val) { if(val.find("id") != val.end()) { diff --git a/samples/server/petstore/cpp-pistache/model/Order.h b/samples/server/petstore/cpp-pistache/model/Order.h index 548f9de0614..bc77d091059 100644 --- a/samples/server/petstore/cpp-pistache/model/Order.h +++ b/samples/server/petstore/cpp-pistache/model/Order.h @@ -44,7 +44,7 @@ public: void validate() override; nlohmann::json toJson() const override; - void fromJson(nlohmann::json& json) override; + void fromJson(const nlohmann::json& json) override; ///////////////////////////////////////////// /// Order members diff --git a/samples/server/petstore/cpp-pistache/model/Pet.cpp b/samples/server/petstore/cpp-pistache/model/Pet.cpp index ec3738e3510..834ac8ff632 100644 --- a/samples/server/petstore/cpp-pistache/model/Pet.cpp +++ b/samples/server/petstore/cpp-pistache/model/Pet.cpp @@ -59,7 +59,8 @@ nlohmann::json Pet::toJson() const jsonArray.push_back(ModelBase::toJson(item)); } val["photoUrls"] = jsonArray; - } + + } { nlohmann::json jsonArray; for( auto& item : m_Tags ) @@ -70,7 +71,7 @@ nlohmann::json Pet::toJson() const if(jsonArray.size() > 0) { val["tags"] = jsonArray; - } + } } if(m_StatusIsSet) { @@ -81,7 +82,7 @@ nlohmann::json Pet::toJson() const return val; } -void Pet::fromJson(nlohmann::json& val) +void Pet::fromJson(const nlohmann::json& val) { if(val.find("id") != val.end()) { @@ -100,33 +101,31 @@ void Pet::fromJson(nlohmann::json& val) setName(val.at("name")); { m_PhotoUrls.clear(); - nlohmann::json jsonArray; - for( auto& item : val["photoUrls"] ) - { - m_PhotoUrls.push_back(item); - - } + for( auto& item : val["photoUrls"] ) + { + m_PhotoUrls.push_back(item); + + } } { m_Tags.clear(); - nlohmann::json jsonArray; if(val.find("tags") != val.end()) { - for( auto& item : val["tags"] ) - { - - if(item.is_null()) + for( auto& item : val["tags"] ) { - m_Tags.push_back( Tag() ); + + if(item.is_null()) + { + m_Tags.push_back( Tag() ); + } + else + { + Tag newItem; + newItem.fromJson(item); + m_Tags.push_back( newItem ); + } + } - else - { - Tag newItem; - newItem.fromJson(item); - m_Tags.push_back( newItem ); - } - - } } } if(val.find("status") != val.end()) diff --git a/samples/server/petstore/cpp-pistache/model/Pet.h b/samples/server/petstore/cpp-pistache/model/Pet.h index 4117dcf468d..6f7e9a565a6 100644 --- a/samples/server/petstore/cpp-pistache/model/Pet.h +++ b/samples/server/petstore/cpp-pistache/model/Pet.h @@ -47,7 +47,7 @@ public: void validate() override; nlohmann::json toJson() const override; - void fromJson(nlohmann::json& json) override; + void fromJson(const nlohmann::json& json) override; ///////////////////////////////////////////// /// Pet members diff --git a/samples/server/petstore/cpp-pistache/model/Tag.cpp b/samples/server/petstore/cpp-pistache/model/Tag.cpp index 6fdab108957..9efe5aeb9db 100644 --- a/samples/server/petstore/cpp-pistache/model/Tag.cpp +++ b/samples/server/petstore/cpp-pistache/model/Tag.cpp @@ -53,7 +53,7 @@ nlohmann::json Tag::toJson() const return val; } -void Tag::fromJson(nlohmann::json& val) +void Tag::fromJson(const nlohmann::json& val) { if(val.find("id") != val.end()) { diff --git a/samples/server/petstore/cpp-pistache/model/Tag.h b/samples/server/petstore/cpp-pistache/model/Tag.h index 1b5d79e31c9..f46aecf78b4 100644 --- a/samples/server/petstore/cpp-pistache/model/Tag.h +++ b/samples/server/petstore/cpp-pistache/model/Tag.h @@ -44,7 +44,7 @@ public: void validate() override; nlohmann::json toJson() const override; - void fromJson(nlohmann::json& json) override; + void fromJson(const nlohmann::json& json) override; ///////////////////////////////////////////// /// Tag members diff --git a/samples/server/petstore/cpp-pistache/model/User.cpp b/samples/server/petstore/cpp-pistache/model/User.cpp index cd93eadd64e..186fc58924d 100644 --- a/samples/server/petstore/cpp-pistache/model/User.cpp +++ b/samples/server/petstore/cpp-pistache/model/User.cpp @@ -89,7 +89,7 @@ nlohmann::json User::toJson() const return val; } -void User::fromJson(nlohmann::json& val) +void User::fromJson(const nlohmann::json& val) { if(val.find("id") != val.end()) { diff --git a/samples/server/petstore/cpp-pistache/model/User.h b/samples/server/petstore/cpp-pistache/model/User.h index b9a53345866..a52a183cd46 100644 --- a/samples/server/petstore/cpp-pistache/model/User.h +++ b/samples/server/petstore/cpp-pistache/model/User.h @@ -44,7 +44,7 @@ public: void validate() override; nlohmann::json toJson() const override; - void fromJson(nlohmann::json& json) override; + void fromJson(const nlohmann::json& json) override; ///////////////////////////////////////////// /// User members