From 1fbc047478e7f0abf7d97395d881cb511dc6adb2 Mon Sep 17 00:00:00 2001 From: ivkesh Date: Wed, 24 Aug 2022 18:32:52 +0300 Subject: [PATCH] fix(cpp-pistache-server): Fix enum generation for useStructModel=true mode (#13249) --- .../model-struct-header.mustache | 12 +++++++ .../model-struct-source.mustache | 36 +++++++++++++++++-- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/cpp-pistache-server/model-struct-header.mustache b/modules/openapi-generator/src/main/resources/cpp-pistache-server/model-struct-header.mustache index 8c2df227eab..b03b9c43151 100644 --- a/modules/openapi-generator/src/main/resources/cpp-pistache-server/model-struct-header.mustache +++ b/modules/openapi-generator/src/main/resources/cpp-pistache-server/model-struct-header.mustache @@ -19,9 +19,21 @@ namespace {{modelNamespace}} 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}} {{^required}}std::optional<{{/required}}{{{dataType}}}{{^required}}>{{/required}} {{name}}; {{/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; diff --git a/modules/openapi-generator/src/main/resources/cpp-pistache-server/model-struct-source.mustache b/modules/openapi-generator/src/main/resources/cpp-pistache-server/model-struct-source.mustache index 7b1db917e28..9ced9452aec 100644 --- a/modules/openapi-generator/src/main/resources/cpp-pistache-server/model-struct-source.mustache +++ b/modules/openapi-generator/src/main/resources/cpp-pistache-server/model-struct-source.mustache @@ -41,7 +41,7 @@ bool {{classname}}::validate(std::stringstream& msg, const std::string& pathPref const std::string _pathPrefix = pathPrefix.empty() ? "{{classname}}" : pathPrefix; {{#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; 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 { - 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 @@ -87,6 +89,20 @@ void to_json(nlohmann::json& j, const {{classname}}& o) {{^required}}if (o.{{name}}.has_value()){{/required}} j["{{baseName}}"] = o.{{name}}{{^required}}.value(){{/required}}; {{/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) @@ -96,9 +112,23 @@ void from_json(const nlohmann::json& j, {{classname}}& o) {{^required}}if (j.find("{{baseName}}") != j.end()) { {{{dataType}}} temporary_{{name}}; j.at("{{baseName}}").get_to(temporary_{{name}}); - o.{{name}} = temporary_{{name}}; + o.{{name}} = std::move(temporary_{{name}}); }{{/required}} {{/vars}} + {{#isEnum}}{{#allowableValues}} + auto s = j.get(); + {{#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}}