@@ -41,7 +41,7 @@ bool {{classname}}::validate(std::stringstream& msg, const std::string& pathPref
41
41
const std::string _pathPrefix = pathPrefix.empty() ? " {{classname}}" : pathPrefix;
42
42
43
43
{{#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)
45
45
{
46
46
success = false ;
47
47
msg << _pathPrefix << " : has no value;" ;
@@ -73,7 +73,9 @@ bool {{classname}}::validate(std::stringstream& msg, const std::string& pathPref
73
73
74
74
bool { {classname} }::operator==(const { {classname} }& other) const
75
75
{
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} };
77
79
}
78
80
79
81
bool { {classname} }::operator!=(const { {classname} }& other) const
@@ -87,6 +89,20 @@ void to_json(nlohmann::json& j, const {{classname}}& o)
87
89
{ {^required} }if (o.{ {name} }.has_value()){ {/required} }
88
90
j["{ {baseName} }"] = o.{ {name} }{ {^required} }.value(){ {/required} };
89
91
{ {/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} }
90
106
}
91
107
92
108
void from_json(const nlohmann::json& j, { {classname} }& o)
@@ -96,9 +112,23 @@ void from_json(const nlohmann::json& j, {{classname}}& o)
96
112
{ {^required} }if (j.find("{ {baseName} }") != j.end()) {
97
113
{{{dataType} }} temporary_{ {name} };
98
114
j.at("{ {baseName} }").get_to(temporary_{ {name} });
99
- o.{ {name} } = temporary_{ {name} };
115
+ o.{ {name} } = std::move( temporary_{ {name} }) ;
100
116
}{ {/required} }
101
117
{ {/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} }
102
132
}
103
133
104
134
} // namespace { {modelNamespace} }
0 commit comments