Skip to content

Commit

Permalink
Fix not processing enums in cpp-pistache-server (#8886)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
mfyuce and jenkinsci-etuzel authored Mar 9, 2021
1 parent 065c524 commit 86e418d
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

/////////////////////////////////////////////
Expand All @@ -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);
Expand All @@ -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}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
{{#models}}{{#model}}

#include "{{classname}}.h"
{{#isEnum}}#include <stdexcept>
#include <sstream>{{/isEnum}}

{{#modelNamespaceDeclarations}}
namespace {{this}} {
Expand Down Expand Up @@ -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)
Expand All @@ -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}}
Expand All @@ -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}}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5.0.0-SNAPSHOT
5.1.0-SNAPSHOT
3 changes: 3 additions & 0 deletions samples/server/petstore/cpp-pistache/model/ApiResponse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -120,6 +122,7 @@ void ApiResponse::unsetMessage()
m_MessageIsSet = false;
}


}
}
}
Expand Down
3 changes: 3 additions & 0 deletions samples/server/petstore/cpp-pistache/model/Category.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -94,6 +96,7 @@ void Category::unsetName()
m_NameIsSet = false;
}


}
}
}
Expand Down
3 changes: 3 additions & 0 deletions samples/server/petstore/cpp-pistache/model/Order.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -198,6 +200,7 @@ void Order::unsetComplete()
m_CompleteIsSet = false;
}


}
}
}
Expand Down
3 changes: 3 additions & 0 deletions samples/server/petstore/cpp-pistache/model/Pet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -165,6 +167,7 @@ void Pet::unsetStatus()
m_StatusIsSet = false;
}


}
}
}
Expand Down
3 changes: 3 additions & 0 deletions samples/server/petstore/cpp-pistache/model/Tag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -94,6 +96,7 @@ void Tag::unsetName()
m_NameIsSet = false;
}


}
}
}
Expand Down
3 changes: 3 additions & 0 deletions samples/server/petstore/cpp-pistache/model/User.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -250,6 +252,7 @@ void User::unsetUserStatus()
m_UserStatusIsSet = false;
}


}
}
}
Expand Down

0 comments on commit 86e418d

Please sign in to comment.