Skip to content

Commit 1fbc047

Browse files
authored
fix(cpp-pistache-server): Fix enum generation for useStructModel=true mode (#13249)
1 parent 3a2b4b9 commit 1fbc047

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

modules/openapi-generator/src/main/resources/cpp-pistache-server/model-struct-header.mustache

+12
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,21 @@ namespace {{modelNamespace}}
1919
2020
struct {{classname}}
2121
{
22+
{{#isEnum}}{{#allowableValues}}
23+
enum e{{classname}} {
24+
// To have a valid default value.
25+
// Avoiding name clashes with user defined
26+
// enum values
27+
INVALID_VALUE_OPENAPI_GENERATED = 0,
28+
{{#enumVars}}
29+
{{{name}}}{{^-last}}, {{/-last}}
30+
{{/enumVars}}
31+
};{{/allowableValues}}{{/isEnum}}
32+
2233
{{#vars}}
2334
{{^required}}std::optional<{{/required}}{{{dataType}}}{{^required}}>{{/required}} {{name}};
2435
{{/vars}}
36+
{{#isEnum}}{{classname}}::e{{classname}} 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}}
2537

2638
bool operator==(const {{classname}}& other) const;
2739
bool operator!=(const {{classname}}& other) const;

modules/openapi-generator/src/main/resources/cpp-pistache-server/model-struct-source.mustache

+33-3
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ bool {{classname}}::validate(std::stringstream& msg, const std::string& pathPref
4141
const std::string _pathPrefix = pathPrefix.empty() ? "{{classname}}" : pathPrefix;
4242
4343
{{#isEnum}}{{! Special case for enum types }}
44-
if (m_value == {{classname}}::e{{classname}}::INVALID_VALUE_OPENAPI_GENERATED)
44+
if (value == {{classname}}::e{{classname}}::INVALID_VALUE_OPENAPI_GENERATED)
4545
{
4646
success = false;
4747
msg << _pathPrefix << ": has no value;";
@@ -73,7 +73,9 @@ bool {{classname}}::validate(std::stringstream& msg, const std::string& pathPref
7373

7474
bool {{classname}}::operator==(const {{classname}}& other) const
7575
{
76-
return {{#vars}}{{name}} == other.{{name}}{{^-last}} && {{/-last}}{{/vars}};
76+
return
77+
{{#isEnum}}value == other.value{{/isEnum}}
78+
{{#vars}}{{name}} == other.{{name}}{{^-last}} && {{/-last}}{{/vars}};
7779
}
7880

7981
bool {{classname}}::operator!=(const {{classname}}& other) const
@@ -87,6 +89,20 @@ void to_json(nlohmann::json& j, const {{classname}}& o)
8789
{{^required}}if (o.{{name}}.has_value()){{/required}}
8890
j["{{baseName}}"] = o.{{name}}{{^required}}.value(){{/required}};
8991
{{/vars}}
92+
{{#isEnum}}{{#allowableValues}}
93+
switch (o.value)
94+
{
95+
{{#enumVars}}
96+
{{#-first}}
97+
case {{classname}}::e{{classname}}::INVALID_VALUE_OPENAPI_GENERATED:
98+
j = "INVALID_VALUE_OPENAPI_GENERATED";
99+
break;
100+
{{/-first}}
101+
case {{classname}}::e{{classname}}::{{name}}:
102+
j = "{{value}}";
103+
break;
104+
{{/enumVars}}
105+
}{{/allowableValues}}{{/isEnum}}{{#vendorExtensions.x-is-string-enum-container}}{{#anyOf}}{{#-first}}to_json(j, o.m_value);{{/-first}}{{/anyOf}}{{/vendorExtensions.x-is-string-enum-container}}
90106
}
91107

92108
void from_json(const nlohmann::json& j, {{classname}}& o)
@@ -96,9 +112,23 @@ void from_json(const nlohmann::json& j, {{classname}}& o)
96112
{{^required}}if (j.find("{{baseName}}") != j.end()) {
97113
{{{dataType}}} temporary_{{name}};
98114
j.at("{{baseName}}").get_to(temporary_{{name}});
99-
o.{{name}} = temporary_{{name}};
115+
o.{{name}} = std::move(temporary_{{name}});
100116
}{{/required}}
101117
{{/vars}}
118+
{{#isEnum}}{{#allowableValues}}
119+
auto s = j.get<std::string>();
120+
{{#enumVars}}
121+
{{#-first}}
122+
if{{/-first}}{{^-first}}else if{{/-first}}(s == "{{value}}") {
123+
o.value = {{classname}}::e{{classname}}::{{name}};
124+
} {{#-last}} else {
125+
std::stringstream ss;
126+
ss << "Unexpected value " << s << " in json"
127+
<< " cannot be converted to enum of type"
128+
<< " {{classname}}::e{{classname}}";
129+
throw std::invalid_argument(ss.str());
130+
} {{/-last}}
131+
{{/enumVars}}{{/allowableValues}}{{/isEnum}}
102132
}
103133

104134
} // namespace {{modelNamespace}}

0 commit comments

Comments
 (0)