Continuing from #1317 and its PRs for pistache server string enum code generation (#9786)

* Continuing from #1317 and its PRs for pistache server string enum code generation;
* A class that has an `anyOf` specification, in cpp side will have no members: in stead it should have a member having the type `classname_anyOf`
* Thus, Its `==` operator is not present or wrongly filled
* An string enum, should have a better usage, hence the `setEnumValue`
* this PR, is a brigde between `stringenumclassname_anyOf` and `stringenumclassname`
* `anyOf` specification is not just about `Enums`, so a better handling is needed from mustache templates, hence new template model parameter `isStringEnumContainer`

* PR fix: muttleyxd: `double semicolon`

* PR fix: wing328: `I think std::string is C++ only. What about adding x-is-string-enum-container instead in the postProcessModel operation in the C++ pistache server generator?`

* PR fix: wing328: `I think std::string is C++ only...` after fix get latest codes and then generate samples

Co-authored-by: Mehmet Fatih <mfyuce@netas.com.tr>
This commit is contained in:
Mehmet YUCE 2021-06-20 17:13:38 +03:00 committed by GitHub
parent 463d905664
commit ccd031a3da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 59 additions and 50 deletions

View File

@ -218,6 +218,14 @@ public class CppPistacheServerCodegen extends AbstractCppCodegen {
} }
} }
if(!codegenModel.isEnum
&& codegenModel.anyOf.size()>1
&& codegenModel.anyOf.contains("std::string")
&& !codegenModel.anyOf.contains("AnyType")
&& codegenModel.interfaces.size()==1
){
codegenModel.vendorExtensions.put("x-is-string-enum-container",true);
}
return codegenModel; return codegenModel;
} }

View File

@ -70,19 +70,22 @@ public:
{{#isEnum}} {{#isEnum}}
{{classname}}::e{{classname}} getValue() const; {{classname}}::e{{classname}} getValue() const;
void setValue({{classname}}::e{{classname}} value); void setValue({{classname}}::e{{classname}} value);
{{/isEnum}} {{/isEnum}}{{#vendorExtensions.x-is-string-enum-container}}{{#anyOf}}{{#-first}}
{{{this}}} getValue() const;
void setValue({{{this}}} value);
{{{this}}}::e{{{this}}} getEnumValue() const;
void setEnumValue({{{this}}}::e{{{this}}} value);{{/-first}}{{/anyOf}}{{/vendorExtensions.x-is-string-enum-container}}
friend void to_json(nlohmann::json& j, const {{classname}}& o); friend void to_json(nlohmann::json& j, const {{classname}}& o);
friend void from_json(const nlohmann::json& j, {{classname}}& o); friend void from_json(const nlohmann::json& j, {{classname}}& o);{{#vendorExtensions.x-is-string-enum-container}}{{#anyOf}}{{#-first}}
friend void to_json(nlohmann::json& j, const {{{this}}}& o);
friend void from_json(const nlohmann::json& j, {{{this}}}& o);{{/-first}}{{/anyOf}}{{/vendorExtensions.x-is-string-enum-container}}
protected: protected:
{{#vars}} {{#vars}}
{{{dataType}}} m_{{name}}; {{{dataType}}} m_{{name}};
{{^required}} {{^required}}
bool m_{{name}}IsSet;{{/required}} bool m_{{name}}IsSet;{{/required}}
{{/vars}} {{/vars}}
{{#isEnum}} {{#isEnum}}{{classname}}::e{{classname}} m_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}}
{{classname}}::e{{classname}} m_value = {{classname}}::e{{classname}}::INVALID_VALUE_OPENAPI_GENERATED;
{{/isEnum}}
}; };
} // namespace {{modelNamespace}} } // namespace {{modelNamespace}}

View File

@ -60,10 +60,11 @@ bool {{classname}}::validate(std::stringstream& msg, const std::string& pathPref
const std::string currentValuePath = _pathPrefix + ".{{nameInCamelCase}}"; const std::string currentValuePath = _pathPrefix + ".{{nameInCamelCase}}";
{{> model-validation-body }} {{> model-validation-body }}
} }
{{/hasValidation}}{{/isArray}} {{/hasValidation}}{{/isArray}}{{/vars}}{{/isEnum}}{{#vendorExtensions.x-is-string-enum-container}}{{#anyOf}}{{#-first}}
{{/vars}} if (!m_value.validate(msg))
{{/isEnum}} {
success = false;
}{{/-first}}{{/anyOf}}{{/vendorExtensions.x-is-string-enum-container}}
return success; return success;
} }
@ -71,10 +72,10 @@ bool {{classname}}::operator==(const {{classname}}& rhs) const
{ {
return return
{{#isEnum}}getValue() == rhs.getValue(){{/isEnum}} {{#isEnum}}getValue() == rhs.getValue(){{/isEnum}}
{{^isEnum}}{{#vars}} {{^isEnum}}{{#hasVars}}{{#vars}}
{{#required}}({{getter}}() == rhs.{{getter}}()){{/required}} {{#required}}({{getter}}() == rhs.{{getter}}()){{/required}}
{{^required}}((!{{nameInCamelCase}}IsSet() && !rhs.{{nameInCamelCase}}IsSet()) || ({{nameInCamelCase}}IsSet() && rhs.{{nameInCamelCase}}IsSet() && {{getter}}() == rhs.{{getter}}())){{/required}}{{^-last}} &&{{/-last}} {{^required}}((!{{nameInCamelCase}}IsSet() && !rhs.{{nameInCamelCase}}IsSet()) || ({{nameInCamelCase}}IsSet() && rhs.{{nameInCamelCase}}IsSet() && {{getter}}() == rhs.{{getter}}())){{/required}}{{^-last}} &&{{/-last}}
{{/vars}}{{/isEnum}} {{/vars}}{{/hasVars}}{{^hasVars}}{{#vendorExtensions.x-is-string-enum-container}}{{#anyOf}}{{#-first}}getValue() == rhs.getValue(){{/-first}}{{/anyOf}}{{^anyOf}}true{{/anyOf}}{{/vendorExtensions.x-is-string-enum-container}}{{/hasVars}}{{/isEnum}}
; ;
} }
@ -103,7 +104,7 @@ void to_json(nlohmann::json& j, const {{classname}}& o)
j = "{{value}}"; j = "{{value}}";
break; break;
{{/enumVars}} {{/enumVars}}
}{{/allowableValues}}{{/isEnum}} }{{/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)
@ -127,7 +128,7 @@ void from_json(const nlohmann::json& j, {{classname}}& o)
<< " {{classname}}::e{{classname}}"; << " {{classname}}::e{{classname}}";
throw std::invalid_argument(ss.str()); throw std::invalid_argument(ss.str());
} {{/-last}} } {{/-last}}
{{/enumVars}}{{/allowableValues}}{{/isEnum}} {{/enumVars}}{{/allowableValues}}{{/isEnum}}{{#vendorExtensions.x-is-string-enum-container}}{{#anyOf}}{{#-first}}from_json(j, o.m_value);{{/-first}}{{/anyOf}}{{/vendorExtensions.x-is-string-enum-container}}
} }
{{#vars}}{{{dataType}}} {{classname}}::{{getter}}() const {{#vars}}{{{dataType}}} {{classname}}::{{getter}}() const
@ -156,7 +157,25 @@ void {{classname}}::unset{{name}}()
void {{classname}}::setValue({{classname}}::e{{classname}} value) void {{classname}}::setValue({{classname}}::e{{classname}} value)
{ {
m_value = value; m_value = value;
}{{/isEnum}} }{{/isEnum}}{{#vendorExtensions.x-is-string-enum-container}}{{#anyOf}}{{#-first}}{{{this}}} {{classname}}::getValue() const
{
return m_value;
}
void {{classname}}::setValue({{{this}}} value)
{
m_value = value;
}
{{{this}}}::e{{{this}}} {{classname}}::getEnumValue() const
{
return m_value.getValue();
}
void {{classname}}::setEnumValue({{{this}}}::e{{{this}}} value)
{
m_value.setValue(value);
}{{/-first}}{{/anyOf}}{{/vendorExtensions.x-is-string-enum-container}}
} // namespace {{modelNamespace}} } // namespace {{modelNamespace}}

View File

@ -50,9 +50,6 @@ bool ApiResponse::validate(std::stringstream& msg, const std::string& pathPrefix
const std::string _pathPrefix = pathPrefix.empty() ? "ApiResponse" : pathPrefix; const std::string _pathPrefix = pathPrefix.empty() ? "ApiResponse" : pathPrefix;
return success; return success;
} }

View File

@ -89,6 +89,7 @@ protected:
bool m_TypeIsSet; bool m_TypeIsSet;
std::string m_Message; std::string m_Message;
bool m_MessageIsSet; bool m_MessageIsSet;
}; };
} // namespace org::openapitools::server::model } // namespace org::openapitools::server::model

View File

@ -48,8 +48,6 @@ bool Category::validate(std::stringstream& msg, const std::string& pathPrefix) c
const std::string _pathPrefix = pathPrefix.empty() ? "Category" : pathPrefix; const std::string _pathPrefix = pathPrefix.empty() ? "Category" : pathPrefix;
return success; return success;
} }

View File

@ -80,6 +80,7 @@ protected:
bool m_IdIsSet; bool m_IdIsSet;
std::string m_Name; std::string m_Name;
bool m_NameIsSet; bool m_NameIsSet;
}; };
} // namespace org::openapitools::server::model } // namespace org::openapitools::server::model

View File

@ -56,12 +56,6 @@ bool Order::validate(std::stringstream& msg, const std::string& pathPrefix) cons
const std::string _pathPrefix = pathPrefix.empty() ? "Order" : pathPrefix; const std::string _pathPrefix = pathPrefix.empty() ? "Order" : pathPrefix;
return success; return success;
} }

View File

@ -116,6 +116,7 @@ protected:
bool m_StatusIsSet; bool m_StatusIsSet;
bool m_Complete; bool m_Complete;
bool m_CompleteIsSet; bool m_CompleteIsSet;
}; };
} // namespace org::openapitools::server::model } // namespace org::openapitools::server::model

View File

@ -52,9 +52,6 @@ bool Pet::validate(std::stringstream& msg, const std::string& pathPrefix) const
/* PhotoUrls */ { /* PhotoUrls */ {
const std::vector<std::string>& value = m_PhotoUrls; const std::vector<std::string>& value = m_PhotoUrls;
const std::string currentValuePath = _pathPrefix + ".photoUrls"; const std::string currentValuePath = _pathPrefix + ".photoUrls";
@ -75,7 +72,6 @@ bool Pet::validate(std::stringstream& msg, const std::string& pathPrefix) const
} }
if (tagsIsSet()) if (tagsIsSet())
{ {
const std::vector<Tag>& value = m_Tags; const std::vector<Tag>& value = m_Tags;
@ -97,8 +93,6 @@ bool Pet::validate(std::stringstream& msg, const std::string& pathPrefix) const
} }
return success; return success;
} }

View File

@ -115,6 +115,7 @@ protected:
bool m_TagsIsSet; bool m_TagsIsSet;
std::string m_Status; std::string m_Status;
bool m_StatusIsSet; bool m_StatusIsSet;
}; };
} // namespace org::openapitools::server::model } // namespace org::openapitools::server::model

View File

@ -48,8 +48,6 @@ bool Tag::validate(std::stringstream& msg, const std::string& pathPrefix) const
const std::string _pathPrefix = pathPrefix.empty() ? "Tag" : pathPrefix; const std::string _pathPrefix = pathPrefix.empty() ? "Tag" : pathPrefix;
return success; return success;
} }

View File

@ -80,6 +80,7 @@ protected:
bool m_IdIsSet; bool m_IdIsSet;
std::string m_Name; std::string m_Name;
bool m_NameIsSet; bool m_NameIsSet;
}; };
} // namespace org::openapitools::server::model } // namespace org::openapitools::server::model

View File

@ -60,14 +60,6 @@ bool User::validate(std::stringstream& msg, const std::string& pathPrefix) const
const std::string _pathPrefix = pathPrefix.empty() ? "User" : pathPrefix; const std::string _pathPrefix = pathPrefix.empty() ? "User" : pathPrefix;
return success; return success;
} }

View File

@ -134,6 +134,7 @@ protected:
bool m_PhoneIsSet; bool m_PhoneIsSet;
int32_t m_UserStatus; int32_t m_UserStatus;
bool m_UserStatusIsSet; bool m_UserStatusIsSet;
}; };
} // namespace org::openapitools::server::model } // namespace org::openapitools::server::model