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:
Mehmet YUCE
2021-03-09 10:17:25 +03:00
committed by GitHub
parent 065c524894
commit 86e418d57b
10 changed files with 78 additions and 2 deletions

View File

@@ -194,6 +194,11 @@ abstract public class AbstractCppCodegen extends DefaultCodegen implements Codeg
}
}
@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)

View File

@@ -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}}

View File

@@ -2,6 +2,8 @@
{{#models}}{{#model}}
#include "{{classname}}.h"
{{#isEnum}}#include <stdexcept>
#include <sstream>{{/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<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}}
@@ -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}}
}

View File

@@ -1 +1 @@
5.0.0-SNAPSHOT
5.1.0-SNAPSHOT

View File

@@ -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;
}
}
}
}

View File

@@ -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;
}
}
}
}

View File

@@ -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;
}
}
}
}

View File

@@ -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;
}
}
}
}

View File

@@ -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;
}
}
}
}

View File

@@ -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;
}
}
}
}