forked from loafle/openapi-generator-original
fix(cpp-pistache-server): Fix enum generation for useStructModel=true mode (#13249)
This commit is contained in:
parent
3a2b4b9bdd
commit
1fbc047478
@ -19,9 +19,21 @@ namespace {{modelNamespace}}
|
|||||||
|
|
||||||
struct {{classname}}
|
struct {{classname}}
|
||||||
{
|
{
|
||||||
|
{{#isEnum}}{{#allowableValues}}
|
||||||
|
enum e{{classname}} {
|
||||||
|
// To have a valid default value.
|
||||||
|
// Avoiding name clashes with user defined
|
||||||
|
// enum values
|
||||||
|
INVALID_VALUE_OPENAPI_GENERATED = 0,
|
||||||
|
{{#enumVars}}
|
||||||
|
{{{name}}}{{^-last}}, {{/-last}}
|
||||||
|
{{/enumVars}}
|
||||||
|
};{{/allowableValues}}{{/isEnum}}
|
||||||
|
|
||||||
{{#vars}}
|
{{#vars}}
|
||||||
{{^required}}std::optional<{{/required}}{{{dataType}}}{{^required}}>{{/required}} {{name}};
|
{{^required}}std::optional<{{/required}}{{{dataType}}}{{^required}}>{{/required}} {{name}};
|
||||||
{{/vars}}
|
{{/vars}}
|
||||||
|
{{#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}}
|
||||||
|
|
||||||
bool operator==(const {{classname}}& other) const;
|
bool operator==(const {{classname}}& other) const;
|
||||||
bool operator!=(const {{classname}}& other) const;
|
bool operator!=(const {{classname}}& other) const;
|
||||||
|
@ -41,7 +41,7 @@ bool {{classname}}::validate(std::stringstream& msg, const std::string& pathPref
|
|||||||
const std::string _pathPrefix = pathPrefix.empty() ? "{{classname}}" : pathPrefix;
|
const std::string _pathPrefix = pathPrefix.empty() ? "{{classname}}" : pathPrefix;
|
||||||
|
|
||||||
{{#isEnum}}{{! Special case for enum types }}
|
{{#isEnum}}{{! Special case for enum types }}
|
||||||
if (m_value == {{classname}}::e{{classname}}::INVALID_VALUE_OPENAPI_GENERATED)
|
if (value == {{classname}}::e{{classname}}::INVALID_VALUE_OPENAPI_GENERATED)
|
||||||
{
|
{
|
||||||
success = false;
|
success = false;
|
||||||
msg << _pathPrefix << ": has no value;";
|
msg << _pathPrefix << ": has no value;";
|
||||||
@ -73,7 +73,9 @@ bool {{classname}}::validate(std::stringstream& msg, const std::string& pathPref
|
|||||||
|
|
||||||
bool {{classname}}::operator==(const {{classname}}& other) const
|
bool {{classname}}::operator==(const {{classname}}& other) const
|
||||||
{
|
{
|
||||||
return {{#vars}}{{name}} == other.{{name}}{{^-last}} && {{/-last}}{{/vars}};
|
return
|
||||||
|
{{#isEnum}}value == other.value{{/isEnum}}
|
||||||
|
{{#vars}}{{name}} == other.{{name}}{{^-last}} && {{/-last}}{{/vars}};
|
||||||
}
|
}
|
||||||
|
|
||||||
bool {{classname}}::operator!=(const {{classname}}& other) const
|
bool {{classname}}::operator!=(const {{classname}}& other) const
|
||||||
@ -87,6 +89,20 @@ void to_json(nlohmann::json& j, const {{classname}}& o)
|
|||||||
{{^required}}if (o.{{name}}.has_value()){{/required}}
|
{{^required}}if (o.{{name}}.has_value()){{/required}}
|
||||||
j["{{baseName}}"] = o.{{name}}{{^required}}.value(){{/required}};
|
j["{{baseName}}"] = o.{{name}}{{^required}}.value(){{/required}};
|
||||||
{{/vars}}
|
{{/vars}}
|
||||||
|
{{#isEnum}}{{#allowableValues}}
|
||||||
|
switch (o.value)
|
||||||
|
{
|
||||||
|
{{#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}}{{#vendorExtensions.x-is-string-enum-container}}{{#anyOf}}{{#-first}}to_json(j, o.m_value);{{/-first}}{{/anyOf}}{{/vendorExtensions.x-is-string-enum-container}}
|
||||||
}
|
}
|
||||||
|
|
||||||
void from_json(const nlohmann::json& j, {{classname}}& o)
|
void from_json(const nlohmann::json& j, {{classname}}& o)
|
||||||
@ -96,9 +112,23 @@ void from_json(const nlohmann::json& j, {{classname}}& o)
|
|||||||
{{^required}}if (j.find("{{baseName}}") != j.end()) {
|
{{^required}}if (j.find("{{baseName}}") != j.end()) {
|
||||||
{{{dataType}}} temporary_{{name}};
|
{{{dataType}}} temporary_{{name}};
|
||||||
j.at("{{baseName}}").get_to(temporary_{{name}});
|
j.at("{{baseName}}").get_to(temporary_{{name}});
|
||||||
o.{{name}} = temporary_{{name}};
|
o.{{name}} = std::move(temporary_{{name}});
|
||||||
}{{/required}}
|
}{{/required}}
|
||||||
{{/vars}}
|
{{/vars}}
|
||||||
|
{{#isEnum}}{{#allowableValues}}
|
||||||
|
auto s = j.get<std::string>();
|
||||||
|
{{#enumVars}}
|
||||||
|
{{#-first}}
|
||||||
|
if{{/-first}}{{^-first}}else if{{/-first}}(s == "{{value}}") {
|
||||||
|
o.value = {{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}}
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace {{modelNamespace}}
|
} // namespace {{modelNamespace}}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user