diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractCppCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractCppCodegen.java index 8a2c3c5eb7af..c4dc7b5fd92d 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractCppCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractCppCodegen.java @@ -194,6 +194,11 @@ public String toModelName(String type) { } } + @Override + public String toEnumValue(String value, String datatype) { + return escapeText(value); + } + @Override public String toVarName(String name) { if (typeMapping.keySet().contains(name) || typeMapping.values().contains(name) 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 b999e5997b4e..c16fdbb09467 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 @@ -25,7 +25,16 @@ class {{declspec}} {{classname}} public: {{classname}}(); virtual ~{{classname}}(); - +{{#isEnum}}{{#allowableValues}} + enum class e{{classname}} { + // To have a valid default value. + // Avoiding nameclashes with user defined + // enum values + INVALID_VALUE_OPENAPI_GENERATED = 0, + {{#enumVars}} + {{{name}}}{{^-last}}, {{/-last}} + {{/enumVars}} + };{{/allowableValues}}{{/isEnum}} void validate(); ///////////////////////////////////////////// @@ -40,6 +49,10 @@ public: bool {{nameInCamelCase}}IsSet() const; void unset{{name}}();{{/required}} {{/vars}} + {{#isEnum}} + {{classname}}::e{{classname}} getValue() const; + void setValue({{classname}}::e{{classname}} value); + {{/isEnum}} friend void to_json(nlohmann::json& j, const {{classname}}& o); friend void from_json(const nlohmann::json& j, {{classname}}& o); @@ -49,6 +62,9 @@ protected: {{^required}} bool m_{{name}}IsSet;{{/required}} {{/vars}} + {{#isEnum}} + {{classname}}::e{{classname}} m_value = {{classname}}::e{{classname}}::INVALID_VALUE_OPENAPI_GENERATED; + {{/isEnum}} }; {{#modelNamespaceDeclarations}} 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 03b8d574df46..d603dc2186e0 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 @@ -2,6 +2,8 @@ {{#models}}{{#model}} #include "{{classname}}.h" +{{#isEnum}}#include +#include {{/isEnum}} {{#modelNamespaceDeclarations}} namespace {{this}} { @@ -32,6 +34,20 @@ void to_json(nlohmann::json& j, const {{classname}}& o) {{#required}}j["{{baseName}}"] = o.m_{{name}};{{/required}}{{^required}}if(o.{{nameInCamelCase}}IsSet(){{#isContainer}} || !o.m_{{name}}.empty(){{/isContainer}}) j["{{baseName}}"] = o.m_{{name}};{{/required}} {{/vars}} + {{#isEnum}}{{#allowableValues}} + switch (o.getValue()) + { + {{#enumVars}} + {{#-first}} + case {{classname}}::e{{classname}}::INVALID_VALUE_OPENAPI_GENERATED: + j = "INVALID_VALUE_OPENAPI_GENERATED"; + break; + {{/-first}} + case {{classname}}::e{{classname}}::{{name}}: + j = "{{value}}"; + break; + {{/enumVars}} + }{{/allowableValues}}{{/isEnum}} } void from_json(const nlohmann::json& j, {{classname}}& o) @@ -43,6 +59,19 @@ void from_json(const nlohmann::json& j, {{classname}}& o) o.m_{{name}}IsSet = true; } {{/required}} {{/vars}} + {{#isEnum}}{{#allowableValues}} + auto s = j.get(); + {{#enumVars}} + {{#-first}}if{{/-first}}{{^-first}}else if{{/-first}} (s == "{{value}}") { + o.setValue({{classname}}::e{{classname}}::{{name}}); + } {{#-last}} else { + std::stringstream ss; + ss << "Unexpected value " << s << " in json" + << " cannot be converted to enum of type" + << " {{classname}}::e{{classname}}"; + throw std::invalid_argument(ss.str()); + } {{/-last}} +{{/enumVars}}{{/allowableValues}}{{/isEnum}} } {{#vars}}{{{dataType}}}{{#isContainer}}&{{/isContainer}} {{classname}}::{{getter}}(){{^isContainer}} const{{/isContainer}} @@ -64,6 +93,14 @@ void {{classname}}::unset{{name}}() } {{/required}} {{/vars}} +{{#isEnum}}{{classname}}::e{{classname}} {{classname}}::getValue() const +{ + return m_value; +} +void {{classname}}::setValue({{classname}}::e{{classname}} value) +{ + m_value = value; +}{{/isEnum}} {{#modelNamespaceDeclarations}} } diff --git a/samples/server/petstore/cpp-pistache/.openapi-generator/VERSION b/samples/server/petstore/cpp-pistache/.openapi-generator/VERSION index d99e7162d01f..c30f0ec2be7f 100644 --- a/samples/server/petstore/cpp-pistache/.openapi-generator/VERSION +++ b/samples/server/petstore/cpp-pistache/.openapi-generator/VERSION @@ -1 +1 @@ -5.0.0-SNAPSHOT \ No newline at end of file +5.1.0-SNAPSHOT \ No newline at end of file diff --git a/samples/server/petstore/cpp-pistache/model/ApiResponse.cpp b/samples/server/petstore/cpp-pistache/model/ApiResponse.cpp index 77cd466e145f..4b9ca7419208 100644 --- a/samples/server/petstore/cpp-pistache/model/ApiResponse.cpp +++ b/samples/server/petstore/cpp-pistache/model/ApiResponse.cpp @@ -47,6 +47,7 @@ void to_json(nlohmann::json& j, const ApiResponse& o) j["type"] = o.m_Type; if(o.messageIsSet()) j["message"] = o.m_Message; + } void from_json(const nlohmann::json& j, ApiResponse& o) @@ -66,6 +67,7 @@ void from_json(const nlohmann::json& j, ApiResponse& o) j.at("message").get_to(o.m_Message); o.m_MessageIsSet = true; } + } int32_t ApiResponse::getCode() const @@ -120,6 +122,7 @@ void ApiResponse::unsetMessage() m_MessageIsSet = false; } + } } } diff --git a/samples/server/petstore/cpp-pistache/model/Category.cpp b/samples/server/petstore/cpp-pistache/model/Category.cpp index 43ff1d490004..3b08114f61de 100644 --- a/samples/server/petstore/cpp-pistache/model/Category.cpp +++ b/samples/server/petstore/cpp-pistache/model/Category.cpp @@ -43,6 +43,7 @@ void to_json(nlohmann::json& j, const Category& o) j["id"] = o.m_Id; if(o.nameIsSet()) j["name"] = o.m_Name; + } void from_json(const nlohmann::json& j, Category& o) @@ -57,6 +58,7 @@ void from_json(const nlohmann::json& j, Category& o) j.at("name").get_to(o.m_Name); o.m_NameIsSet = true; } + } int64_t Category::getId() const @@ -94,6 +96,7 @@ void Category::unsetName() m_NameIsSet = false; } + } } } diff --git a/samples/server/petstore/cpp-pistache/model/Order.cpp b/samples/server/petstore/cpp-pistache/model/Order.cpp index 82efbce6cb22..986cf6b24fa9 100644 --- a/samples/server/petstore/cpp-pistache/model/Order.cpp +++ b/samples/server/petstore/cpp-pistache/model/Order.cpp @@ -59,6 +59,7 @@ void to_json(nlohmann::json& j, const Order& o) j["status"] = o.m_Status; if(o.completeIsSet()) j["complete"] = o.m_Complete; + } void from_json(const nlohmann::json& j, Order& o) @@ -93,6 +94,7 @@ void from_json(const nlohmann::json& j, Order& o) j.at("complete").get_to(o.m_Complete); o.m_CompleteIsSet = true; } + } int64_t Order::getId() const @@ -198,6 +200,7 @@ void Order::unsetComplete() m_CompleteIsSet = false; } + } } } diff --git a/samples/server/petstore/cpp-pistache/model/Pet.cpp b/samples/server/petstore/cpp-pistache/model/Pet.cpp index 0ff016b545f0..434b1339cd06 100644 --- a/samples/server/petstore/cpp-pistache/model/Pet.cpp +++ b/samples/server/petstore/cpp-pistache/model/Pet.cpp @@ -52,6 +52,7 @@ void to_json(nlohmann::json& j, const Pet& o) j["tags"] = o.m_Tags; if(o.statusIsSet()) j["status"] = o.m_Status; + } void from_json(const nlohmann::json& j, Pet& o) @@ -78,6 +79,7 @@ void from_json(const nlohmann::json& j, Pet& o) j.at("status").get_to(o.m_Status); o.m_StatusIsSet = true; } + } int64_t Pet::getId() const @@ -165,6 +167,7 @@ void Pet::unsetStatus() m_StatusIsSet = false; } + } } } diff --git a/samples/server/petstore/cpp-pistache/model/Tag.cpp b/samples/server/petstore/cpp-pistache/model/Tag.cpp index 7f56fb2bcca9..cba759bad1d0 100644 --- a/samples/server/petstore/cpp-pistache/model/Tag.cpp +++ b/samples/server/petstore/cpp-pistache/model/Tag.cpp @@ -43,6 +43,7 @@ void to_json(nlohmann::json& j, const Tag& o) j["id"] = o.m_Id; if(o.nameIsSet()) j["name"] = o.m_Name; + } void from_json(const nlohmann::json& j, Tag& o) @@ -57,6 +58,7 @@ void from_json(const nlohmann::json& j, Tag& o) j.at("name").get_to(o.m_Name); o.m_NameIsSet = true; } + } int64_t Tag::getId() const @@ -94,6 +96,7 @@ void Tag::unsetName() m_NameIsSet = false; } + } } } diff --git a/samples/server/petstore/cpp-pistache/model/User.cpp b/samples/server/petstore/cpp-pistache/model/User.cpp index 20ae06352f25..2db9316b75d9 100644 --- a/samples/server/petstore/cpp-pistache/model/User.cpp +++ b/samples/server/petstore/cpp-pistache/model/User.cpp @@ -67,6 +67,7 @@ void to_json(nlohmann::json& j, const User& o) j["phone"] = o.m_Phone; if(o.userStatusIsSet()) j["userStatus"] = o.m_UserStatus; + } void from_json(const nlohmann::json& j, User& o) @@ -111,6 +112,7 @@ void from_json(const nlohmann::json& j, User& o) j.at("userStatus").get_to(o.m_UserStatus); o.m_UserStatusIsSet = true; } + } int64_t User::getId() const @@ -250,6 +252,7 @@ void User::unsetUserStatus() m_UserStatusIsSet = false; } + } } }