From ccd031a3dade070bb470a6a9e4c1eeff33eb1371 Mon Sep 17 00:00:00 2001 From: Mehmet YUCE Date: Sun, 20 Jun 2021 17:13:38 +0300 Subject: [PATCH] Continuing from #1317 and its PRs for pistache server string enum code generation (#9786) * Continuing from #1317 and its PRs for pistache server string enum code generation; * A class that has an `anyOf` specification, in cpp side will have no members: in stead it should have a member having the type `classname_anyOf` * Thus, Its `==` operator is not present or wrongly filled * An string enum, should have a better usage, hence the `setEnumValue` * this PR, is a brigde between `stringenumclassname_anyOf` and `stringenumclassname` * `anyOf` specification is not just about `Enums`, so a better handling is needed from mustache templates, hence new template model parameter `isStringEnumContainer` * PR fix: muttleyxd: `double semicolon` * PR fix: wing328: `I think std::string is C++ only. What about adding x-is-string-enum-container instead in the postProcessModel operation in the C++ pistache server generator?` * PR fix: wing328: `I think std::string is C++ only...` after fix get latest codes and then generate samples Co-authored-by: Mehmet Fatih --- .../languages/CppPistacheServerCodegen.java | 8 ++++ .../cpp-pistache-server/model-header.mustache | 15 +++++--- .../cpp-pistache-server/model-source.mustache | 37 ++++++++++++++----- .../cpp-pistache/model/ApiResponse.cpp | 5 +-- .../petstore/cpp-pistache/model/ApiResponse.h | 1 + .../petstore/cpp-pistache/model/Category.cpp | 4 +- .../petstore/cpp-pistache/model/Category.h | 1 + .../petstore/cpp-pistache/model/Order.cpp | 8 +--- .../petstore/cpp-pistache/model/Order.h | 1 + .../petstore/cpp-pistache/model/Pet.cpp | 12 ++---- .../server/petstore/cpp-pistache/model/Pet.h | 1 + .../petstore/cpp-pistache/model/Tag.cpp | 4 +- .../server/petstore/cpp-pistache/model/Tag.h | 1 + .../petstore/cpp-pistache/model/User.cpp | 10 +---- .../server/petstore/cpp-pistache/model/User.h | 1 + 15 files changed, 59 insertions(+), 50 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 cb21ea5a6de..5d199f8e0fb 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 @@ -218,6 +218,14 @@ public class CppPistacheServerCodegen extends AbstractCppCodegen { } } + if(!codegenModel.isEnum + && codegenModel.anyOf.size()>1 + && codegenModel.anyOf.contains("std::string") + && !codegenModel.anyOf.contains("AnyType") + && codegenModel.interfaces.size()==1 + ){ + codegenModel.vendorExtensions.put("x-is-string-enum-container",true); + } return codegenModel; } 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 656109b155e..ccd8e876af3 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 @@ -70,19 +70,22 @@ public: {{#isEnum}} {{classname}}::e{{classname}} getValue() const; void setValue({{classname}}::e{{classname}} value); - {{/isEnum}} - + {{/isEnum}}{{#vendorExtensions.x-is-string-enum-container}}{{#anyOf}}{{#-first}} + {{{this}}} getValue() const; + void setValue({{{this}}} value); + {{{this}}}::e{{{this}}} getEnumValue() const; + void setEnumValue({{{this}}}::e{{{this}}} value);{{/-first}}{{/anyOf}}{{/vendorExtensions.x-is-string-enum-container}} friend void to_json(nlohmann::json& j, const {{classname}}& o); - friend void from_json(const nlohmann::json& j, {{classname}}& o); + friend void from_json(const nlohmann::json& j, {{classname}}& o);{{#vendorExtensions.x-is-string-enum-container}}{{#anyOf}}{{#-first}} + friend void to_json(nlohmann::json& j, const {{{this}}}& o); + friend void from_json(const nlohmann::json& j, {{{this}}}& o);{{/-first}}{{/anyOf}}{{/vendorExtensions.x-is-string-enum-container}} protected: {{#vars}} {{{dataType}}} m_{{name}}; {{^required}} bool m_{{name}}IsSet;{{/required}} {{/vars}} - {{#isEnum}} - {{classname}}::e{{classname}} m_value = {{classname}}::e{{classname}}::INVALID_VALUE_OPENAPI_GENERATED; - {{/isEnum}} + {{#isEnum}}{{classname}}::e{{classname}} m_value = {{classname}}::e{{classname}}::INVALID_VALUE_OPENAPI_GENERATED;{{/isEnum}}{{#vendorExtensions.x-is-string-enum-container}}{{#anyOf}}{{#-first}}{{{this}}} m_value;{{/-first}}{{/anyOf}}{{/vendorExtensions.x-is-string-enum-container}} }; } // namespace {{modelNamespace}} 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 8ef6abf0e4c..9ce767ed01b 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 @@ -60,10 +60,11 @@ bool {{classname}}::validate(std::stringstream& msg, const std::string& pathPref const std::string currentValuePath = _pathPrefix + ".{{nameInCamelCase}}"; {{> model-validation-body }} } - {{/hasValidation}}{{/isArray}} - {{/vars}} - {{/isEnum}} - + {{/hasValidation}}{{/isArray}}{{/vars}}{{/isEnum}}{{#vendorExtensions.x-is-string-enum-container}}{{#anyOf}}{{#-first}} + if (!m_value.validate(msg)) + { + success = false; + }{{/-first}}{{/anyOf}}{{/vendorExtensions.x-is-string-enum-container}} return success; } @@ -71,10 +72,10 @@ bool {{classname}}::operator==(const {{classname}}& rhs) const { return {{#isEnum}}getValue() == rhs.getValue(){{/isEnum}} - {{^isEnum}}{{#vars}} + {{^isEnum}}{{#hasVars}}{{#vars}} {{#required}}({{getter}}() == rhs.{{getter}}()){{/required}} {{^required}}((!{{nameInCamelCase}}IsSet() && !rhs.{{nameInCamelCase}}IsSet()) || ({{nameInCamelCase}}IsSet() && rhs.{{nameInCamelCase}}IsSet() && {{getter}}() == rhs.{{getter}}())){{/required}}{{^-last}} &&{{/-last}} - {{/vars}}{{/isEnum}} + {{/vars}}{{/hasVars}}{{^hasVars}}{{#vendorExtensions.x-is-string-enum-container}}{{#anyOf}}{{#-first}}getValue() == rhs.getValue(){{/-first}}{{/anyOf}}{{^anyOf}}true{{/anyOf}}{{/vendorExtensions.x-is-string-enum-container}}{{/hasVars}}{{/isEnum}} ; } @@ -103,7 +104,7 @@ void to_json(nlohmann::json& j, const {{classname}}& o) j = "{{value}}"; break; {{/enumVars}} - }{{/allowableValues}}{{/isEnum}} + }{{/allowableValues}}{{/isEnum}}{{#vendorExtensions.x-is-string-enum-container}}{{#anyOf}}{{#-first}}to_json(j, o.m_value);{{/-first}}{{/anyOf}}{{/vendorExtensions.x-is-string-enum-container}} } void from_json(const nlohmann::json& j, {{classname}}& o) @@ -127,7 +128,7 @@ void from_json(const nlohmann::json& j, {{classname}}& o) << " {{classname}}::e{{classname}}"; throw std::invalid_argument(ss.str()); } {{/-last}} -{{/enumVars}}{{/allowableValues}}{{/isEnum}} +{{/enumVars}}{{/allowableValues}}{{/isEnum}}{{#vendorExtensions.x-is-string-enum-container}}{{#anyOf}}{{#-first}}from_json(j, o.m_value);{{/-first}}{{/anyOf}}{{/vendorExtensions.x-is-string-enum-container}} } {{#vars}}{{{dataType}}} {{classname}}::{{getter}}() const @@ -156,7 +157,25 @@ void {{classname}}::unset{{name}}() void {{classname}}::setValue({{classname}}::e{{classname}} value) { m_value = value; -}{{/isEnum}} +}{{/isEnum}}{{#vendorExtensions.x-is-string-enum-container}}{{#anyOf}}{{#-first}}{{{this}}} {{classname}}::getValue() const +{ + return m_value; +} + +void {{classname}}::setValue({{{this}}} value) +{ + m_value = value; +} + +{{{this}}}::e{{{this}}} {{classname}}::getEnumValue() const +{ + return m_value.getValue(); +} + +void {{classname}}::setEnumValue({{{this}}}::e{{{this}}} value) +{ + m_value.setValue(value); +}{{/-first}}{{/anyOf}}{{/vendorExtensions.x-is-string-enum-container}} } // namespace {{modelNamespace}} diff --git a/samples/server/petstore/cpp-pistache/model/ApiResponse.cpp b/samples/server/petstore/cpp-pistache/model/ApiResponse.cpp index 049f6c19d6d..4050e84f8d1 100644 --- a/samples/server/petstore/cpp-pistache/model/ApiResponse.cpp +++ b/samples/server/petstore/cpp-pistache/model/ApiResponse.cpp @@ -49,10 +49,7 @@ bool ApiResponse::validate(std::stringstream& msg, const std::string& pathPrefix bool success = true; const std::string _pathPrefix = pathPrefix.empty() ? "ApiResponse" : pathPrefix; - - - - + return success; } diff --git a/samples/server/petstore/cpp-pistache/model/ApiResponse.h b/samples/server/petstore/cpp-pistache/model/ApiResponse.h index c4227b21876..f9d9308ef0a 100644 --- a/samples/server/petstore/cpp-pistache/model/ApiResponse.h +++ b/samples/server/petstore/cpp-pistache/model/ApiResponse.h @@ -89,6 +89,7 @@ protected: bool m_TypeIsSet; std::string m_Message; bool m_MessageIsSet; + }; } // namespace org::openapitools::server::model diff --git a/samples/server/petstore/cpp-pistache/model/Category.cpp b/samples/server/petstore/cpp-pistache/model/Category.cpp index deb18ff9bb7..114adb15ee6 100644 --- a/samples/server/petstore/cpp-pistache/model/Category.cpp +++ b/samples/server/petstore/cpp-pistache/model/Category.cpp @@ -47,9 +47,7 @@ bool Category::validate(std::stringstream& msg, const std::string& pathPrefix) c bool success = true; const std::string _pathPrefix = pathPrefix.empty() ? "Category" : pathPrefix; - - - + return success; } diff --git a/samples/server/petstore/cpp-pistache/model/Category.h b/samples/server/petstore/cpp-pistache/model/Category.h index 71682c36c3b..eede5172427 100644 --- a/samples/server/petstore/cpp-pistache/model/Category.h +++ b/samples/server/petstore/cpp-pistache/model/Category.h @@ -80,6 +80,7 @@ protected: bool m_IdIsSet; std::string m_Name; bool m_NameIsSet; + }; } // namespace org::openapitools::server::model diff --git a/samples/server/petstore/cpp-pistache/model/Order.cpp b/samples/server/petstore/cpp-pistache/model/Order.cpp index 2d04f0ac637..33d08aad068 100644 --- a/samples/server/petstore/cpp-pistache/model/Order.cpp +++ b/samples/server/petstore/cpp-pistache/model/Order.cpp @@ -55,13 +55,7 @@ bool Order::validate(std::stringstream& msg, const std::string& pathPrefix) cons bool success = true; const std::string _pathPrefix = pathPrefix.empty() ? "Order" : pathPrefix; - - - - - - - + return success; } diff --git a/samples/server/petstore/cpp-pistache/model/Order.h b/samples/server/petstore/cpp-pistache/model/Order.h index 86a6e35f87f..f725c509990 100644 --- a/samples/server/petstore/cpp-pistache/model/Order.h +++ b/samples/server/petstore/cpp-pistache/model/Order.h @@ -116,6 +116,7 @@ protected: bool m_StatusIsSet; bool m_Complete; bool m_CompleteIsSet; + }; } // namespace org::openapitools::server::model diff --git a/samples/server/petstore/cpp-pistache/model/Pet.cpp b/samples/server/petstore/cpp-pistache/model/Pet.cpp index 9c7ba886a44..51d97b41979 100644 --- a/samples/server/petstore/cpp-pistache/model/Pet.cpp +++ b/samples/server/petstore/cpp-pistache/model/Pet.cpp @@ -50,10 +50,7 @@ bool Pet::validate(std::stringstream& msg, const std::string& pathPrefix) const bool success = true; const std::string _pathPrefix = pathPrefix.empty() ? "Pet" : pathPrefix; - - - - + /* PhotoUrls */ { const std::vector& value = m_PhotoUrls; @@ -74,8 +71,7 @@ bool Pet::validate(std::stringstream& msg, const std::string& pathPrefix) const } } - - + if (tagsIsSet()) { const std::vector& value = m_Tags; @@ -96,9 +92,7 @@ bool Pet::validate(std::stringstream& msg, const std::string& pathPrefix) const } } - - - + return success; } diff --git a/samples/server/petstore/cpp-pistache/model/Pet.h b/samples/server/petstore/cpp-pistache/model/Pet.h index af773cb3e5c..c3cee2e2b4d 100644 --- a/samples/server/petstore/cpp-pistache/model/Pet.h +++ b/samples/server/petstore/cpp-pistache/model/Pet.h @@ -115,6 +115,7 @@ protected: bool m_TagsIsSet; std::string m_Status; bool m_StatusIsSet; + }; } // namespace org::openapitools::server::model diff --git a/samples/server/petstore/cpp-pistache/model/Tag.cpp b/samples/server/petstore/cpp-pistache/model/Tag.cpp index 659565e4034..fecaccfc64c 100644 --- a/samples/server/petstore/cpp-pistache/model/Tag.cpp +++ b/samples/server/petstore/cpp-pistache/model/Tag.cpp @@ -47,9 +47,7 @@ bool Tag::validate(std::stringstream& msg, const std::string& pathPrefix) const bool success = true; const std::string _pathPrefix = pathPrefix.empty() ? "Tag" : pathPrefix; - - - + return success; } diff --git a/samples/server/petstore/cpp-pistache/model/Tag.h b/samples/server/petstore/cpp-pistache/model/Tag.h index 1b607e27fd9..822c760be7e 100644 --- a/samples/server/petstore/cpp-pistache/model/Tag.h +++ b/samples/server/petstore/cpp-pistache/model/Tag.h @@ -80,6 +80,7 @@ protected: bool m_IdIsSet; std::string m_Name; bool m_NameIsSet; + }; } // namespace org::openapitools::server::model diff --git a/samples/server/petstore/cpp-pistache/model/User.cpp b/samples/server/petstore/cpp-pistache/model/User.cpp index c997bf9734d..05f504ed30d 100644 --- a/samples/server/petstore/cpp-pistache/model/User.cpp +++ b/samples/server/petstore/cpp-pistache/model/User.cpp @@ -59,15 +59,7 @@ bool User::validate(std::stringstream& msg, const std::string& pathPrefix) const bool success = true; const std::string _pathPrefix = pathPrefix.empty() ? "User" : pathPrefix; - - - - - - - - - + return success; } diff --git a/samples/server/petstore/cpp-pistache/model/User.h b/samples/server/petstore/cpp-pistache/model/User.h index 38048fe58e4..aebc4fe5a35 100644 --- a/samples/server/petstore/cpp-pistache/model/User.h +++ b/samples/server/petstore/cpp-pistache/model/User.h @@ -134,6 +134,7 @@ protected: bool m_PhoneIsSet; int32_t m_UserStatus; bool m_UserStatusIsSet; + }; } // namespace org::openapitools::server::model