From e7b61b38583020d1d336598a03756f9bac4818db Mon Sep 17 00:00:00 2001 From: "Mateusz Szychowski (Muttley)" Date: Sat, 23 Apr 2022 22:14:53 +0200 Subject: [PATCH] [C++][Pistache] Add support for validating arrays, use full namespace type with custom types fixes #11246 --- .../codegen/languages/CppPistacheServerCodegen.java | 3 ++- .../cpp-pistache-server/api-source.mustache | 6 ++++-- samples/server/petstore/cpp-pistache/api/UserApi.cpp | 6 ++++-- samples/server/petstore/cpp-pistache/api/UserApi.h | 4 ++-- .../petstore/cpp-pistache/impl/UserApiImpl.cpp | 4 ++-- .../server/petstore/cpp-pistache/impl/UserApiImpl.h | 4 ++-- samples/server/petstore/cpp-pistache/model/Pet.cpp | 12 ++++++------ samples/server/petstore/cpp-pistache/model/Pet.h | 12 ++++++------ 8 files changed, 28 insertions(+), 23 deletions(-) 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 8a49343ca12e..e6ce6acc3e27 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 @@ -378,7 +378,8 @@ public String getTypeDeclaration(Schema p) { return toModelName(openAPIType); } - return openAPIType; + String namespace = (String)additionalProperties.get("modelNamespace"); + return namespace + "::" + openAPIType; } @Override 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 6bf2d10eb017..555fe1078377 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 @@ -93,8 +93,10 @@ void {{classname}}::{{operationIdSnakeCase}}_handler(const Pistache::Rest::Reque {{#hasBodyParam}} {{#bodyParam}} {{^isPrimitiveType}} - nlohmann::json::parse(request.body()).get_to({{paramName}}); - {{paramName}}.validate(); + nlohmann::json::parse(request.body()).get_to({{paramName}});{{#isArray}} + for (const auto& validationParam : {{paramName}}) + validationParam.validate();{{/isArray}}{{^isArray}} + {{paramName}}.validate();{{/isArray}} {{/isPrimitiveType}} {{#isPrimitiveType}} {{paramName}} = request.body(); diff --git a/samples/server/petstore/cpp-pistache/api/UserApi.cpp b/samples/server/petstore/cpp-pistache/api/UserApi.cpp index 43873a3d3fe2..7655107bc557 100644 --- a/samples/server/petstore/cpp-pistache/api/UserApi.cpp +++ b/samples/server/petstore/cpp-pistache/api/UserApi.cpp @@ -106,7 +106,8 @@ void UserApi::create_users_with_array_input_handler(const Pistache::Rest::Reques try { nlohmann::json::parse(request.body()).get_to(body); - body.validate(); + for (const auto& validationParam : body) + validationParam.validate(); } catch (std::exception &e) { const std::pair errorInfo = this->handleParsingException(e); response.send(errorInfo.first, errorInfo.second); @@ -138,7 +139,8 @@ void UserApi::create_users_with_list_input_handler(const Pistache::Rest::Request try { nlohmann::json::parse(request.body()).get_to(body); - body.validate(); + for (const auto& validationParam : body) + validationParam.validate(); } catch (std::exception &e) { const std::pair errorInfo = this->handleParsingException(e); response.send(errorInfo.first, errorInfo.second); diff --git a/samples/server/petstore/cpp-pistache/api/UserApi.h b/samples/server/petstore/cpp-pistache/api/UserApi.h index 0c2fc319f577..eaf425b69322 100644 --- a/samples/server/petstore/cpp-pistache/api/UserApi.h +++ b/samples/server/petstore/cpp-pistache/api/UserApi.h @@ -85,7 +85,7 @@ class UserApi { /// /// /// List of user object - virtual void create_users_with_array_input(const std::vector &body, Pistache::Http::ResponseWriter &response) = 0; + virtual void create_users_with_array_input(const std::vector &body, Pistache::Http::ResponseWriter &response) = 0; /// /// Creates list of users with given input array /// @@ -93,7 +93,7 @@ class UserApi { /// /// /// List of user object - virtual void create_users_with_list_input(const std::vector &body, Pistache::Http::ResponseWriter &response) = 0; + virtual void create_users_with_list_input(const std::vector &body, Pistache::Http::ResponseWriter &response) = 0; /// /// Delete user /// diff --git a/samples/server/petstore/cpp-pistache/impl/UserApiImpl.cpp b/samples/server/petstore/cpp-pistache/impl/UserApiImpl.cpp index 252895e8d368..54510e9970f9 100644 --- a/samples/server/petstore/cpp-pistache/impl/UserApiImpl.cpp +++ b/samples/server/petstore/cpp-pistache/impl/UserApiImpl.cpp @@ -27,10 +27,10 @@ UserApiImpl::UserApiImpl(const std::shared_ptr& rtr) 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 &body, 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 &body, 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) { diff --git a/samples/server/petstore/cpp-pistache/impl/UserApiImpl.h b/samples/server/petstore/cpp-pistache/impl/UserApiImpl.h index 920d94846ed3..0eedd6d0a0de 100644 --- a/samples/server/petstore/cpp-pistache/impl/UserApiImpl.h +++ b/samples/server/petstore/cpp-pistache/impl/UserApiImpl.h @@ -44,8 +44,8 @@ class UserApiImpl : public org::openapitools::server::api::UserApi { ~UserApiImpl() override = default; 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 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 std::optional &username, const std::optional &password, Pistache::Http::ResponseWriter &response); diff --git a/samples/server/petstore/cpp-pistache/model/Pet.cpp b/samples/server/petstore/cpp-pistache/model/Pet.cpp index 51d97b419795..1c3dbfb557a6 100644 --- a/samples/server/petstore/cpp-pistache/model/Pet.cpp +++ b/samples/server/petstore/cpp-pistache/model/Pet.cpp @@ -74,14 +74,14 @@ bool Pet::validate(std::stringstream& msg, const std::string& pathPrefix) const if (tagsIsSet()) { - const std::vector& value = m_Tags; + const std::vector& value = m_Tags; const std::string currentValuePath = _pathPrefix + ".tags"; { // Recursive validation of array elements const std::string oldValuePath = currentValuePath; int i = 0; - for (const Tag& value : value) + for (const org::openapitools::server::model::Tag& value : value) { const std::string currentValuePath = oldValuePath + "[" + std::to_string(i) + "]"; @@ -187,11 +187,11 @@ void Pet::unsetId() { m_IdIsSet = false; } -Category Pet::getCategory() const +org::openapitools::server::model::Category Pet::getCategory() const { return m_Category; } -void Pet::setCategory(Category const& value) +void Pet::setCategory(org::openapitools::server::model::Category const& value) { m_Category = value; m_CategoryIsSet = true; @@ -220,11 +220,11 @@ void Pet::setPhotoUrls(std::vector const& value) { m_PhotoUrls = value; } -std::vector Pet::getTags() const +std::vector Pet::getTags() const { return m_Tags; } -void Pet::setTags(std::vector const& value) +void Pet::setTags(std::vector const& value) { m_Tags = value; m_TagsIsSet = true; diff --git a/samples/server/petstore/cpp-pistache/model/Pet.h b/samples/server/petstore/cpp-pistache/model/Pet.h index c3cee2e2b4de..ee4bbffbfae3 100644 --- a/samples/server/petstore/cpp-pistache/model/Pet.h +++ b/samples/server/petstore/cpp-pistache/model/Pet.h @@ -71,8 +71,8 @@ class Pet /// /// /// - Category getCategory() const; - void setCategory(Category const& value); + org::openapitools::server::model::Category getCategory() const; + void setCategory(org::openapitools::server::model::Category const& value); bool categoryIsSet() const; void unsetCategory(); /// @@ -88,8 +88,8 @@ class Pet /// /// /// - std::vector getTags() const; - void setTags(std::vector const& value); + std::vector getTags() const; + void setTags(std::vector const& value); bool tagsIsSet() const; void unsetTags(); /// @@ -105,13 +105,13 @@ class Pet protected: int64_t m_Id; bool m_IdIsSet; - Category m_Category; + org::openapitools::server::model::Category m_Category; bool m_CategoryIsSet; std::string m_Name; std::vector m_PhotoUrls; - std::vector m_Tags; + std::vector m_Tags; bool m_TagsIsSet; std::string m_Status; bool m_StatusIsSet;