forked from loafle/openapi-generator-original
Fix not processing enums in cpp-pistache-server (#8886)
* Fix not processing enums in cpp-pistache-server Defining a reusable enum as a component schema results in an empty class. Following changes are made: - activate 'postProcessModelsEnum' in 'AbstractCppCodegen' - modify model templates for the 'cpp-pistache-server' project As 'postProcessModelsEnum' is now available in the 'AbstactCppCodegen' the 'enumVars' variables are now available in mustache templates for all cpp based code generators. As the 'AbstractCppCodegen' was touched all cpp based samples were updated. * fixes encountered on real world * PR fixes Co-authored-by: mfyuce <mfyuce@netas.com.tr>
This commit is contained in:
@@ -194,6 +194,11 @@ abstract public class AbstractCppCodegen extends DefaultCodegen implements Codeg
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toEnumValue(String value, String datatype) {
|
||||||
|
return escapeText(value);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toVarName(String name) {
|
public String toVarName(String name) {
|
||||||
if (typeMapping.keySet().contains(name) || typeMapping.values().contains(name)
|
if (typeMapping.keySet().contains(name) || typeMapping.values().contains(name)
|
||||||
|
|||||||
@@ -25,7 +25,16 @@ class {{declspec}} {{classname}}
|
|||||||
public:
|
public:
|
||||||
{{classname}}();
|
{{classname}}();
|
||||||
virtual ~{{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();
|
void validate();
|
||||||
|
|
||||||
/////////////////////////////////////////////
|
/////////////////////////////////////////////
|
||||||
@@ -40,6 +49,10 @@ public:
|
|||||||
bool {{nameInCamelCase}}IsSet() const;
|
bool {{nameInCamelCase}}IsSet() const;
|
||||||
void unset{{name}}();{{/required}}
|
void unset{{name}}();{{/required}}
|
||||||
{{/vars}}
|
{{/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 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);
|
||||||
@@ -49,6 +62,9 @@ protected:
|
|||||||
{{^required}}
|
{{^required}}
|
||||||
bool m_{{name}}IsSet;{{/required}}
|
bool m_{{name}}IsSet;{{/required}}
|
||||||
{{/vars}}
|
{{/vars}}
|
||||||
|
{{#isEnum}}
|
||||||
|
{{classname}}::e{{classname}} m_value = {{classname}}::e{{classname}}::INVALID_VALUE_OPENAPI_GENERATED;
|
||||||
|
{{/isEnum}}
|
||||||
};
|
};
|
||||||
|
|
||||||
{{#modelNamespaceDeclarations}}
|
{{#modelNamespaceDeclarations}}
|
||||||
|
|||||||
@@ -2,6 +2,8 @@
|
|||||||
{{#models}}{{#model}}
|
{{#models}}{{#model}}
|
||||||
|
|
||||||
#include "{{classname}}.h"
|
#include "{{classname}}.h"
|
||||||
|
{{#isEnum}}#include <stdexcept>
|
||||||
|
#include <sstream>{{/isEnum}}
|
||||||
|
|
||||||
{{#modelNamespaceDeclarations}}
|
{{#modelNamespaceDeclarations}}
|
||||||
namespace {{this}} {
|
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}})
|
{{#required}}j["{{baseName}}"] = o.m_{{name}};{{/required}}{{^required}}if(o.{{nameInCamelCase}}IsSet(){{#isContainer}} || !o.m_{{name}}.empty(){{/isContainer}})
|
||||||
j["{{baseName}}"] = o.m_{{name}};{{/required}}
|
j["{{baseName}}"] = o.m_{{name}};{{/required}}
|
||||||
{{/vars}}
|
{{/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)
|
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;
|
o.m_{{name}}IsSet = true;
|
||||||
} {{/required}}
|
} {{/required}}
|
||||||
{{/vars}}
|
{{/vars}}
|
||||||
|
{{#isEnum}}{{#allowableValues}}
|
||||||
|
auto s = j.get<std::string>();
|
||||||
|
{{#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}}
|
{{#vars}}{{{dataType}}}{{#isContainer}}&{{/isContainer}} {{classname}}::{{getter}}(){{^isContainer}} const{{/isContainer}}
|
||||||
@@ -64,6 +93,14 @@ void {{classname}}::unset{{name}}()
|
|||||||
}
|
}
|
||||||
{{/required}}
|
{{/required}}
|
||||||
{{/vars}}
|
{{/vars}}
|
||||||
|
{{#isEnum}}{{classname}}::e{{classname}} {{classname}}::getValue() const
|
||||||
|
{
|
||||||
|
return m_value;
|
||||||
|
}
|
||||||
|
void {{classname}}::setValue({{classname}}::e{{classname}} value)
|
||||||
|
{
|
||||||
|
m_value = value;
|
||||||
|
}{{/isEnum}}
|
||||||
|
|
||||||
{{#modelNamespaceDeclarations}}
|
{{#modelNamespaceDeclarations}}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
5.0.0-SNAPSHOT
|
5.1.0-SNAPSHOT
|
||||||
@@ -47,6 +47,7 @@ void to_json(nlohmann::json& j, const ApiResponse& o)
|
|||||||
j["type"] = o.m_Type;
|
j["type"] = o.m_Type;
|
||||||
if(o.messageIsSet())
|
if(o.messageIsSet())
|
||||||
j["message"] = o.m_Message;
|
j["message"] = o.m_Message;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void from_json(const nlohmann::json& j, ApiResponse& o)
|
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);
|
j.at("message").get_to(o.m_Message);
|
||||||
o.m_MessageIsSet = true;
|
o.m_MessageIsSet = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t ApiResponse::getCode() const
|
int32_t ApiResponse::getCode() const
|
||||||
@@ -120,6 +122,7 @@ void ApiResponse::unsetMessage()
|
|||||||
m_MessageIsSet = false;
|
m_MessageIsSet = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -43,6 +43,7 @@ void to_json(nlohmann::json& j, const Category& o)
|
|||||||
j["id"] = o.m_Id;
|
j["id"] = o.m_Id;
|
||||||
if(o.nameIsSet())
|
if(o.nameIsSet())
|
||||||
j["name"] = o.m_Name;
|
j["name"] = o.m_Name;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void from_json(const nlohmann::json& j, Category& o)
|
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);
|
j.at("name").get_to(o.m_Name);
|
||||||
o.m_NameIsSet = true;
|
o.m_NameIsSet = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int64_t Category::getId() const
|
int64_t Category::getId() const
|
||||||
@@ -94,6 +96,7 @@ void Category::unsetName()
|
|||||||
m_NameIsSet = false;
|
m_NameIsSet = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -59,6 +59,7 @@ void to_json(nlohmann::json& j, const Order& o)
|
|||||||
j["status"] = o.m_Status;
|
j["status"] = o.m_Status;
|
||||||
if(o.completeIsSet())
|
if(o.completeIsSet())
|
||||||
j["complete"] = o.m_Complete;
|
j["complete"] = o.m_Complete;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void from_json(const nlohmann::json& j, Order& o)
|
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);
|
j.at("complete").get_to(o.m_Complete);
|
||||||
o.m_CompleteIsSet = true;
|
o.m_CompleteIsSet = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int64_t Order::getId() const
|
int64_t Order::getId() const
|
||||||
@@ -198,6 +200,7 @@ void Order::unsetComplete()
|
|||||||
m_CompleteIsSet = false;
|
m_CompleteIsSet = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -52,6 +52,7 @@ void to_json(nlohmann::json& j, const Pet& o)
|
|||||||
j["tags"] = o.m_Tags;
|
j["tags"] = o.m_Tags;
|
||||||
if(o.statusIsSet())
|
if(o.statusIsSet())
|
||||||
j["status"] = o.m_Status;
|
j["status"] = o.m_Status;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void from_json(const nlohmann::json& j, Pet& o)
|
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);
|
j.at("status").get_to(o.m_Status);
|
||||||
o.m_StatusIsSet = true;
|
o.m_StatusIsSet = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int64_t Pet::getId() const
|
int64_t Pet::getId() const
|
||||||
@@ -165,6 +167,7 @@ void Pet::unsetStatus()
|
|||||||
m_StatusIsSet = false;
|
m_StatusIsSet = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -43,6 +43,7 @@ void to_json(nlohmann::json& j, const Tag& o)
|
|||||||
j["id"] = o.m_Id;
|
j["id"] = o.m_Id;
|
||||||
if(o.nameIsSet())
|
if(o.nameIsSet())
|
||||||
j["name"] = o.m_Name;
|
j["name"] = o.m_Name;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void from_json(const nlohmann::json& j, Tag& o)
|
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);
|
j.at("name").get_to(o.m_Name);
|
||||||
o.m_NameIsSet = true;
|
o.m_NameIsSet = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int64_t Tag::getId() const
|
int64_t Tag::getId() const
|
||||||
@@ -94,6 +96,7 @@ void Tag::unsetName()
|
|||||||
m_NameIsSet = false;
|
m_NameIsSet = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -67,6 +67,7 @@ void to_json(nlohmann::json& j, const User& o)
|
|||||||
j["phone"] = o.m_Phone;
|
j["phone"] = o.m_Phone;
|
||||||
if(o.userStatusIsSet())
|
if(o.userStatusIsSet())
|
||||||
j["userStatus"] = o.m_UserStatus;
|
j["userStatus"] = o.m_UserStatus;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void from_json(const nlohmann::json& j, User& o)
|
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);
|
j.at("userStatus").get_to(o.m_UserStatus);
|
||||||
o.m_UserStatusIsSet = true;
|
o.m_UserStatusIsSet = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int64_t User::getId() const
|
int64_t User::getId() const
|
||||||
@@ -250,6 +252,7 @@ void User::unsetUserStatus()
|
|||||||
m_UserStatusIsSet = false;
|
m_UserStatusIsSet = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user