Skip to content

Commit d80f3a6

Browse files
whoanetherealjoy
authored andcommitted
[cpp rest-sdk] Constness (OpenAPITools#1295)
* Improve method signatures to use const when the value won't change * Update PetStore * Change setters for non-primitive types to receive const reference parameters * Update PetStore
1 parent 1074674 commit d80f3a6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+270
-208
lines changed

modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/model-header.mustache

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public:
3636
void validate() override;
3737

3838
web::json::value toJson() const override;
39-
void fromJson(web::json::value& json) override;
39+
void fromJson(const web::json::value& json) override;
4040

4141
void toMultipart(std::shared_ptr<MultipartFormData> multipart, const utility::string_t& namePrefix) const override;
4242
void fromMultiPart(std::shared_ptr<MultipartFormData> multipart, const utility::string_t& namePrefix) override;
@@ -54,7 +54,14 @@ public:
5454
{{/isNotContainer}}{{^required}}bool {{nameInCamelCase}}IsSet() const;
5555
void unset{{name}}();
5656
{{/required}}
57+
58+
{{#isPrimitiveType}}
5759
void {{setter}}({{{dataType}}} value);
60+
{{/isPrimitiveType}}
61+
{{^isPrimitiveType}}
62+
void {{setter}}(const {{{dataType}}}& value);
63+
{{/isPrimitiveType}}
64+
5865
{{/isInherited}}
5966
{{/vars}}
6067

modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/model-source.mustache

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ web::json::value {{classname}}::toJson() const
126126
return val;
127127
}
128128

129-
void {{classname}}::fromJson(web::json::value& val)
129+
void {{classname}}::fromJson(const web::json::value& val)
130130
{
131131
{{#parent}}
132132
this->{{{parent}}}::fromJson(val);
@@ -140,15 +140,15 @@ void {{classname}}::fromJson(web::json::value& val)
140140
{{^required}}
141141
if(val.has_field(utility::conversions::to_string_t("{{baseName}}")))
142142
{
143-
web::json::value& fieldValue = val[utility::conversions::to_string_t("{{baseName}}")];
143+
const web::json::value& fieldValue = val.at(utility::conversions::to_string_t("{{baseName}}"));
144144
if(!fieldValue.is_null())
145145
{
146146
{{setter}}(ModelBase::{{baseType}}FromJson(fieldValue));
147147
}
148148
}
149149
{{/required}}
150150
{{#required}}
151-
{{setter}}(ModelBase::{{baseType}}FromJson(val[utility::conversions::to_string_t("{{baseName}}")]));
151+
{{setter}}(ModelBase::{{baseType}}FromJson(val.at(utility::conversions::to_string_t("{{baseName}}"))));
152152
{{/required}}
153153
{{/isMapContainer}}
154154
{{/isListContainer}}
@@ -161,7 +161,7 @@ void {{classname}}::fromJson(web::json::value& val)
161161
if(val.has_field(utility::conversions::to_string_t("{{baseName}}")))
162162
{
163163
{{/required}}
164-
for( auto& item : val[utility::conversions::to_string_t("{{baseName}}")].as_array() )
164+
for( auto& item : val.at(utility::conversions::to_string_t("{{baseName}}")).as_array() )
165165
{
166166
{{#items.isPrimitiveType}}
167167
m_{{name}}.push_back(ModelBase::{{items.baseType}}FromJson(item));
@@ -202,21 +202,21 @@ void {{classname}}::fromJson(web::json::value& val)
202202
if(val.has_field(utility::conversions::to_string_t("{{baseName}}")))
203203
{
204204
{{/required}}
205-
for( auto& item : val[utility::conversions::to_string_t("{{baseName}}")].as_array() )
205+
for( const auto& item : val.at(utility::conversions::to_string_t("{{baseName}}")).as_array() )
206206
{
207207
if(item.has_field(utility::conversions::to_string_t("key")))
208208
{
209-
utility::string_t key = ModelBase::stringFromJson(item[utility::conversions::to_string_t("key")]);
209+
utility::string_t key = ModelBase::stringFromJson(item.at(utility::conversions::to_string_t("key")));
210210
{{#items.isPrimitiveType}}
211-
m_{{name}}.insert(std::pair<utility::string_t,{{{items.datatype}}}>( key, ModelBase::{{items.baseType}}FromJson(item[utility::conversions::to_string_t("value")])));
211+
m_{{name}}.insert(std::pair<utility::string_t,{{{items.datatype}}}>( key, ModelBase::{{items.baseType}}FromJson(item.at(utility::conversions::to_string_t("value")))));
212212
{{/items.isPrimitiveType}}
213213
{{^items.isPrimitiveType}}
214214
{{#items.isString}}
215-
m_{{name}}.insert(std::pair<utility::string_t,{{{items.datatype}}}>( key, ModelBase::stringFromJson(item[utility::conversions::to_string_t("value")])));
215+
m_{{name}}.insert(std::pair<utility::string_t,{{{items.datatype}}}>( key, ModelBase::stringFromJson(item.at(utility::conversions::to_string_t("value")))));
216216
{{/items.isString}}
217217
{{^items.isString}}
218218
{{#items.isDateTime}}
219-
m_{{name}}.insert(std::pair<utility::string_t,{{{items.datatype}}}>( key, ModelBase::dateFromJson(item[utility::conversions::to_string_t("value")])));
219+
m_{{name}}.insert(std::pair<utility::string_t,{{{items.datatype}}}>( key, ModelBase::dateFromJson(item.at(utility::conversions::to_string_t("value")))));
220220
{{/items.isDateTime}}
221221
{{^items.isDateTime}}
222222
if(item.is_null())
@@ -226,7 +226,7 @@ void {{classname}}::fromJson(web::json::value& val)
226226
else
227227
{
228228
{{{items.datatype}}} newItem({{{items.defaultValue}}});
229-
newItem->fromJson(item[utility::conversions::to_string_t("value")]);
229+
newItem->fromJson(item.at(utility::conversions::to_string_t("value")));
230230
m_{{name}}.insert(std::pair<utility::string_t,{{{items.datatype}}}>( key, newItem ));
231231
}
232232
{{/items.isDateTime}}
@@ -245,7 +245,7 @@ void {{classname}}::fromJson(web::json::value& val)
245245
{{^required}}
246246
if(val.has_field(utility::conversions::to_string_t("{{baseName}}")))
247247
{
248-
web::json::value& fieldValue = val[utility::conversions::to_string_t("{{baseName}}")];
248+
const web::json::value& fieldValue = val.at(utility::conversions::to_string_t("{{baseName}}"));
249249
if(!fieldValue.is_null())
250250
{
251251
{{#isString}}
@@ -271,7 +271,7 @@ void {{classname}}::fromJson(web::json::value& val)
271271
{{/required}}
272272
{{#required}}
273273
{{#isString}}
274-
{{setter}}(ModelBase::stringFromJson(val[utility::conversions::to_string_t("{{baseName}}")]));
274+
{{setter}}(ModelBase::stringFromJson(val.at(utility::conversions::to_string_t("{{baseName}}"))));
275275
{{/isString}}
276276
{{#isByteArray}}
277277
{{setter}}(ModelBase::stringFromJson(val[utility::conversions::to_string_t("{{baseName}}")]));
@@ -280,15 +280,15 @@ void {{classname}}::fromJson(web::json::value& val)
280280
{{^isByteArray}}
281281
{{#isDateTime}}
282282
{{setter}}
283-
(ModelBase::dateFromJson(val[utility::conversions::to_string_t("{{baseName}}")]));
283+
(ModelBase::dateFromJson(val.at(utility::conversions::to_string_t("{{baseName}}"))));
284284
{{/isDateTime}}
285285
{{^isDateTime}}
286286
{{#vendorExtensions.x-codegen-file}}
287-
{{setter}}(ModelBase::fileFromJson(val[utility::conversions::to_string_t("{{baseName}}")]));
287+
{{setter}}(ModelBase::fileFromJson(val.at(utility::conversions::to_string_t("{{baseName}}"))));
288288
{{/vendorExtensions.x-codegen-file}}
289289
{{^vendorExtensions.x-codegen-file}}
290290
{{{dataType}}} new{{name}}({{{defaultValue}}});
291-
new{{name}}->fromJson(val[utility::conversions::to_string_t("{{baseName}}")]);
291+
new{{name}}->fromJson(val.at(utility::conversions::to_string_t("{{baseName}}")));
292292
{{setter}}( new{{name}} );
293293
{{/vendorExtensions.x-codegen-file}}
294294
{{/isDateTime}}
@@ -596,26 +596,25 @@ void {{classname}}::fromMultiPart(std::shared_ptr<MultipartFormData> multipart,
596596
{
597597
return m_{{name}};
598598
}
599-
600-
void {{classname}}::{{setter}}({{{dataType}}} value)
601-
{
602-
m_{{name}} = value;
603-
{{^required}}m_{{name}}IsSet = true;{{/required}}
604-
}
605599
{{/isNotContainer}}
606600
{{#isNotContainer}}
607601
{{{dataType}}} {{classname}}::{{getter}}() const
608602
{
609603
return m_{{name}};
610604
}
605+
{{/isNotContainer}}
611606

612-
607+
{{#isPrimitiveType}}
613608
void {{classname}}::{{setter}}({{{dataType}}} value)
609+
{{/isPrimitiveType}}
610+
{{^isPrimitiveType}}
611+
void {{classname}}::{{setter}}(const {{{dataType}}}& value)
612+
{{/isPrimitiveType}}
614613
{
615614
m_{{name}} = value;
616615
{{^required}}m_{{name}}IsSet = true;{{/required}}
617616
}
618-
{{/isNotContainer}}
617+
619618
{{^required}}
620619
bool {{classname}}::{{nameInCamelCase}}IsSet() const
621620
{

modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/modelbase-header.mustache

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public:
3030
virtual void validate() = 0;
3131
3232
virtual web::json::value toJson() const = 0;
33-
virtual void fromJson(web::json::value& json) = 0;
33+
virtual void fromJson(const web::json::value& json) = 0;
3434
3535
virtual void toMultipart(std::shared_ptr<MultipartFormData> multipart, const utility::string_t& namePrefix) const = 0;
3636
virtual void fromMultiPart(std::shared_ptr<MultipartFormData> multipart, const utility::string_t& namePrefix) = 0;
@@ -46,14 +46,14 @@ public:
4646
template<class T>
4747
static web::json::value toJson(const std::vector<T>& value);
4848
49-
static int64_t int64_tFromJson(web::json::value& val);
50-
static int32_t int32_tFromJson(web::json::value& val);
51-
static float floatFromJson(web::json::value& val);
52-
static utility::string_t stringFromJson(web::json::value& val);
53-
static utility::datetime dateFromJson(web::json::value& val);
54-
static double doubleFromJson(web::json::value& val);
55-
static bool boolFromJson(web::json::value& val);
56-
static std::shared_ptr<HttpContent> fileFromJson(web::json::value& val);
49+
static int64_t int64_tFromJson(const web::json::value& val);
50+
static int32_t int32_tFromJson(const web::json::value& val);
51+
static float floatFromJson(const web::json::value& val);
52+
static utility::string_t stringFromJson(const web::json::value& val);
53+
static utility::datetime dateFromJson(const web::json::value& val);
54+
static double doubleFromJson(const web::json::value& val);
55+
static bool boolFromJson(const web::json::value& val);
56+
static std::shared_ptr<HttpContent> fileFromJson(const web::json::value& val);
5757
5858
static std::shared_ptr<HttpContent> toHttpContent( const utility::string_t& name, const utility::string_t& value, const utility::string_t& contentType = utility::conversions::to_string_t(""));
5959
static std::shared_ptr<HttpContent> toHttpContent( const utility::string_t& name, const utility::datetime& value, const utility::string_t& contentType = utility::conversions::to_string_t(""));

modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/modelbase-source.mustache

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,25 +46,25 @@ web::json::value ModelBase::toJson( std::shared_ptr<HttpContent> content )
4646
return value;
4747
}
4848

49-
std::shared_ptr<HttpContent> ModelBase::fileFromJson(web::json::value& val)
49+
std::shared_ptr<HttpContent> ModelBase::fileFromJson(const web::json::value& val)
5050
{
5151
std::shared_ptr<HttpContent> content(new HttpContent);
5252
5353
if(val.has_field(utility::conversions::to_string_t("ContentDisposition")))
5454
{
55-
content->setContentDisposition( ModelBase::stringFromJson(val[utility::conversions::to_string_t("ContentDisposition")]) );
55+
content->setContentDisposition( ModelBase::stringFromJson(val.at(utility::conversions::to_string_t("ContentDisposition"))) );
5656
}
5757
if(val.has_field(utility::conversions::to_string_t("ContentType")))
5858
{
59-
content->setContentType( ModelBase::stringFromJson(val[utility::conversions::to_string_t("ContentType")]) );
59+
content->setContentType( ModelBase::stringFromJson(val.at(utility::conversions::to_string_t("ContentType"))) );
6060
}
6161
if(val.has_field(utility::conversions::to_string_t("FileName")))
6262
{
63-
content->setFileName( ModelBase::stringFromJson(val[utility::conversions::to_string_t("FileName")]) );
63+
content->setFileName( ModelBase::stringFromJson(val.at(utility::conversions::to_string_t("FileName"))) );
6464
}
6565
if(val.has_field(utility::conversions::to_string_t("InputStream")))
6666
{
67-
content->setData( ModelBase::fromBase64( ModelBase::stringFromJson(val[utility::conversions::to_string_t("InputStream")]) ) );
67+
content->setData( ModelBase::fromBase64( ModelBase::stringFromJson(val.at(utility::conversions::to_string_t("InputStream")))) );
6868
}
6969

7070
return content;
@@ -263,32 +263,32 @@ std::shared_ptr<std::istream> ModelBase::fromBase64( const utility::string_t& en
263263
return result;
264264
}
265265

266-
int64_t ModelBase::int64_tFromJson(web::json::value& val)
266+
int64_t ModelBase::int64_tFromJson(const web::json::value& val)
267267
{
268268
return val.as_number().to_int64();
269269
}
270-
int32_t ModelBase::int32_tFromJson(web::json::value& val)
270+
int32_t ModelBase::int32_tFromJson(const web::json::value& val)
271271
{
272272
return val.as_integer();
273273
}
274-
float ModelBase::floatFromJson(web::json::value& val)
274+
float ModelBase::floatFromJson(const web::json::value& val)
275275
{
276276
return static_cast<float>(val.as_double());
277277
}
278-
utility::string_t ModelBase::stringFromJson(web::json::value& val)
278+
utility::string_t ModelBase::stringFromJson(const web::json::value& val)
279279
{
280280
return val.is_string() ? val.as_string() : utility::conversions::to_string_t("");
281281
}
282282

283-
utility::datetime ModelBase::dateFromJson(web::json::value& val)
283+
utility::datetime ModelBase::dateFromJson(const web::json::value& val)
284284
{
285285
return utility::datetime::from_string(val.as_string(), utility::datetime::ISO_8601);
286286
}
287-
bool ModelBase::boolFromJson(web::json::value& val)
287+
bool ModelBase::boolFromJson(const web::json::value& val)
288288
{
289289
return val.as_bool();
290290
}
291-
double ModelBase::doubleFromJson(web::json::value& val)
291+
double ModelBase::doubleFromJson(const web::json::value& val)
292292
{
293293
return val.as_double();
294294
}

modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/object-header.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public:
2929
void validate() override;
3030
3131
web::json::value toJson() const override;
32-
void fromJson(web::json::value& json) override;
32+
void fromJson(const web::json::value& json) override;
3333
3434
void toMultipart(std::shared_ptr<MultipartFormData> multipart, const utility::string_t& namePrefix) const override;
3535
void fromMultiPart(std::shared_ptr<MultipartFormData> multipart, const utility::string_t& namePrefix) override;

modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/object-source.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ web::json::value Object::toJson() const
2424
return m_object;
2525
}
2626

27-
void Object::fromJson(web::json::value& val)
27+
void Object::fromJson(const web::json::value& val)
2828
{
2929
if (val.is_object())
3030
{
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3.3.1-SNAPSHOT
1+
3.3.0-SNAPSHOT

samples/client/petstore/cpp-restsdk/ApiClient.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*
55
* OpenAPI spec version: 1.0.0
66
*
7-
* NOTE: This class is auto generated by OpenAPI-Generator 3.3.1-SNAPSHOT.
7+
* NOTE: This class is auto generated by OpenAPI-Generator 3.3.0-SNAPSHOT.
88
* https://openapi-generator.tech
99
* Do not edit the class manually.
1010
*/

samples/client/petstore/cpp-restsdk/ApiClient.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*
55
* OpenAPI spec version: 1.0.0
66
*
7-
* NOTE: This class is auto generated by OpenAPI-Generator 3.3.1-SNAPSHOT.
7+
* NOTE: This class is auto generated by OpenAPI-Generator 3.3.0-SNAPSHOT.
88
* https://openapi-generator.tech
99
* Do not edit the class manually.
1010
*/

samples/client/petstore/cpp-restsdk/ApiConfiguration.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*
55
* OpenAPI spec version: 1.0.0
66
*
7-
* NOTE: This class is auto generated by OpenAPI-Generator 3.3.1-SNAPSHOT.
7+
* NOTE: This class is auto generated by OpenAPI-Generator 3.3.0-SNAPSHOT.
88
* https://openapi-generator.tech
99
* Do not edit the class manually.
1010
*/

0 commit comments

Comments
 (0)