Skip to content

Commit 9116411

Browse files
authored
[cpp-rest] Fixes segfault for nullable strings (#22405)
1 parent 1c447a1 commit 9116411

File tree

24 files changed

+137
-48
lines changed

24 files changed

+137
-48
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <variant>
1717
{{/-first}}
1818
{{/oneOf}}
19+
#include <boost/optional.hpp>
1920
{{^parent}}
2021
{{{defaultInclude}}}
2122
#include "{{packageName}}/ModelBase.h"
@@ -264,13 +265,23 @@ public:
264265
protected:
265266
{{#vars}}
266267
{{^isInherited}}
268+
{{#isNullable}}
269+
{{^isEnum}}
270+
boost::optional<{{{dataType}}}> m_{{name}};
271+
{{/isEnum}}
272+
{{#isEnum}}
273+
boost::optional<{{^isMap}}{{{datatypeWithEnum}}}{{/isMap}}{{#isMap}}{{{dataType}}}{{/isMap}}> m_{{name}};
274+
{{/isEnum}}
275+
{{/isNullable}}
276+
{{^isNullable}}
267277
{{^isEnum}}
268278
{{{dataType}}} m_{{name}};
269279
{{/isEnum}}
270280
{{#isEnum}}
271281
{{^isMap}}{{{datatypeWithEnum}}}{{/isMap}}{{#isMap}}{{{dataType}}}{{/isMap}} m_{{name}};
272282
{{/isEnum}}
273283
bool m_{{name}}IsSet;
284+
{{/isNullable}}
274285
{{/isInherited}}
275286

276287
{{/vars}}

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

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ void {{classname}}::setValue({{classname}}::e{{classname}} const value)
187187
{
188188
{{#vars}}
189189
{{^isInherited}}
190+
{{^isNullable}}
190191
{{^isContainer}}
191192
{{^isEnum}}
192193
{{#isPrimitiveType}}
@@ -203,6 +204,7 @@ void {{classname}}::setValue({{classname}}::e{{classname}} const value)
203204
{{/isEnum}}
204205
{{/isContainer}}
205206
m_{{name}}IsSet = false;
207+
{{/isNullable}}
206208
{{/isInherited}}
207209
{{/vars}}
208210
}
@@ -226,8 +228,25 @@ web::json::value {{classname}}::toJson() const
226228
{{/parent}}
227229
{{#vars}}
228230
{{^isInherited}}
231+
{{#isNullable}}
232+
if(m_{{name}}.has_value())
233+
{
234+
{{#isEnum}}{{#isContainer}}{{#isArray}}
235+
{{{dataType}}} refVal = from{{{enumName}}}(m_{{name}}.get());
236+
{{/isArray}}{{#isMap}}
237+
val[utility::conversions::to_string_t(_XPLATSTR("{{baseName}}"))] = ModelBase::toJson(m_{{name}}.get());
238+
{{/isMap}}{{/isContainer}}{{^isContainer}}
239+
utility::string_t refVal = from{{{datatypeWithEnum}}}(m_{{name}}.get());
240+
{{/isContainer}}{{^isMap}}val[utility::conversions::to_string_t(_XPLATSTR("{{baseName}}"))] = ModelBase::toJson(refVal);
241+
{{/isMap}}{{/isEnum}}
242+
{{^isEnum}}
243+
val[utility::conversions::to_string_t(_XPLATSTR("{{baseName}}"))] = ModelBase::toJson(m_{{name}}.get());
244+
{{/isEnum}}
245+
}
246+
{{/isNullable}}
247+
{{^isNullable}}
229248
if(m_{{name}}IsSet)
230-
{
249+
{
231250
{{#isEnum}}{{#isContainer}}{{#isArray}}
232251
{{{dataType}}} refVal = from{{{enumName}}}(m_{{name}});
233252
{{/isArray}}{{#isMap}}
@@ -240,6 +259,7 @@ web::json::value {{classname}}::toJson() const
240259
val[utility::conversions::to_string_t(_XPLATSTR("{{baseName}}"))] = ModelBase::toJson(m_{{name}});
241260
{{/isEnum}}
242261
}
262+
{{/isNullable}}
243263
{{/isInherited}}
244264
{{/vars}}
245265

@@ -286,6 +306,27 @@ void {{classname}}::toMultipart(std::shared_ptr<MultipartFormData> multipart, co
286306
namePrefix += utility::conversions::to_string_t(_XPLATSTR("."));
287307
}
288308
{{#vars}}
309+
{{#isNullable}}
310+
if(m_{{name}}.has_value())
311+
{
312+
{{^isEnum}}
313+
multipart->add(ModelBase::toHttpContent(namePrefix + utility::conversions::to_string_t(_XPLATSTR("{{baseName}}")), m_{{name}}.get()));
314+
{{/isEnum}}
315+
{{#isEnum}}
316+
{{#isContainer}}
317+
{{#isArray}}
318+
multipart->add(ModelBase::toHttpContent(namePrefix + utility::conversions::to_string_t(_XPLATSTR("{{baseName}}")), from{{{enumName}}}(m_{{name}}.get())));
319+
{{/isArray}}{{#isMap}}
320+
multipart->add(ModelBase::toHttpContent(namePrefix + utility::conversions::to_string_t(_XPLATSTR("{{baseName}}")), m_{{name}}.get()));
321+
{{/isMap}}
322+
{{/isContainer}}
323+
{{^isContainer}}
324+
multipart->add(ModelBase::toHttpContent(namePrefix + utility::conversions::to_string_t(_XPLATSTR("{{baseName}}")), from{{{datatypeWithEnum}}}(m_{{name}}.get())));
325+
{{/isContainer}}
326+
{{/isEnum}}
327+
}
328+
{{/isNullable}}
329+
{{^isNullable}}
289330
if(m_{{name}}IsSet)
290331
{
291332
{{^isEnum}}
@@ -304,6 +345,7 @@ void {{classname}}::toMultipart(std::shared_ptr<MultipartFormData> multipart, co
304345
{{/isContainer}}
305346
{{/isEnum}}
306347
}
348+
{{/isNullable}}
307349
{{/vars}}
308350
}
309351

@@ -405,22 +447,37 @@ const {{dataType}} {{classname}}::from{{{datatypeWithEnum}}}(const {{{datatypeWi
405447
{{^isEnum}}
406448
{{{dataType}}} {{classname}}::{{getter}}() const
407449
{
450+
{{#isNullable}}
451+
return m_{{name}}.get();
452+
{{/isNullable}}
453+
{{^isNullable}}
408454
return m_{{name}};
455+
{{/isNullable}}
409456
}
410457
{{/isEnum}}
411458
{{/isContainer}}
412459
{{^isContainer}}
413460
{{^isEnum}}
414461
{{{dataType}}} {{classname}}::{{getter}}() const
415462
{
463+
{{#isNullable}}
464+
return m_{{name}}.get();
465+
{{/isNullable}}
466+
{{^isNullable}}
416467
return m_{{name}};
468+
{{/isNullable}}
417469
}
418470
{{/isEnum}}
419471
{{/isContainer}}
420472
{{#isEnum}}
421473
{{^isMap}}{{#isArray}}{{{baseType}}}<{{/isArray}}{{{classname}}}::{{{enumName}}}{{#isArray}}>{{/isArray}}{{/isMap}}{{#isMap}}{{{dataType}}}{{/isMap}} {{classname}}::{{getter}}() const
422474
{
475+
{{#isNullable}}
476+
return m_{{name}}.get();
477+
{{/isNullable}}
478+
{{^isNullable}}
423479
return m_{{name}};
480+
{{/isNullable}}
424481
}
425482
{{/isEnum}}
426483

@@ -433,17 +490,29 @@ void {{classname}}::{{setter}}(const {{^isMap}}{{{datatypeWithEnum}}}{{/isMap}}{
433490
{{/isEnum}}
434491
{
435492
m_{{name}} = value;
493+
{{^isNullable}}
436494
m_{{name}}IsSet = true;
495+
{{/isNullable}}
437496
}
438497

439498
bool {{classname}}::{{nameInCamelCase}}IsSet() const
440499
{
500+
{{#isNullable}}
501+
return m_{{name}}.has_value();
502+
{{/isNullable}}
503+
{{^isNullable}}
441504
return m_{{name}}IsSet;
505+
{{/isNullable}}
442506
}
443507

444508
void {{classname}}::unset{{name}}()
445509
{
510+
{{#isNullable}}
511+
m_{{name}}.reset();
512+
{{/isNullable}}
513+
{{^isNullable}}
446514
m_{{name}}IsSet = false;
515+
{{/isNullable}}
447516
}
448517
{{/isInherited}}{{/vars}}
449518
{{/isEnum}}

samples/client/petstore/cpp-restsdk/client/include/CppRestPetstoreClient/model/ApiResponse.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#ifndef ORG_OPENAPITOOLS_CLIENT_MODEL_ApiResponse_H_
1919
#define ORG_OPENAPITOOLS_CLIENT_MODEL_ApiResponse_H_
2020

21+
#include <boost/optional.hpp>
2122

2223
#include "CppRestPetstoreClient/ModelBase.h"
2324

samples/client/petstore/cpp-restsdk/client/include/CppRestPetstoreClient/model/Category.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#ifndef ORG_OPENAPITOOLS_CLIENT_MODEL_Category_H_
1919
#define ORG_OPENAPITOOLS_CLIENT_MODEL_Category_H_
2020

21+
#include <boost/optional.hpp>
2122

2223
#include "CppRestPetstoreClient/ModelBase.h"
2324

samples/client/petstore/cpp-restsdk/client/include/CppRestPetstoreClient/model/Color.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#ifndef ORG_OPENAPITOOLS_CLIENT_MODEL_Color_H_
1919
#define ORG_OPENAPITOOLS_CLIENT_MODEL_Color_H_
2020

21+
#include <boost/optional.hpp>
2122

2223
#include "CppRestPetstoreClient/ModelBase.h"
2324

samples/client/petstore/cpp-restsdk/client/include/CppRestPetstoreClient/model/CreateUserOrPet_request.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#include <stdexcept>
2222
#include <variant>
23+
#include <boost/optional.hpp>
2324

2425
#include "CppRestPetstoreClient/ModelBase.h"
2526

samples/client/petstore/cpp-restsdk/client/include/CppRestPetstoreClient/model/Order.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#define ORG_OPENAPITOOLS_CLIENT_MODEL_Order_H_
2020

2121
#include <stdexcept>
22+
#include <boost/optional.hpp>
2223

2324
#include "CppRestPetstoreClient/ModelBase.h"
2425

samples/client/petstore/cpp-restsdk/client/include/CppRestPetstoreClient/model/Page.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#ifndef ORG_OPENAPITOOLS_CLIENT_MODEL_Page_H_
1919
#define ORG_OPENAPITOOLS_CLIENT_MODEL_Page_H_
2020

21+
#include <boost/optional.hpp>
2122

2223
#include "CppRestPetstoreClient/ModelBase.h"
2324

samples/client/petstore/cpp-restsdk/client/include/CppRestPetstoreClient/model/Pet.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#define ORG_OPENAPITOOLS_CLIENT_MODEL_Pet_H_
2020

2121
#include <stdexcept>
22+
#include <boost/optional.hpp>
2223

2324
#include "CppRestPetstoreClient/ModelBase.h"
2425

samples/client/petstore/cpp-restsdk/client/include/CppRestPetstoreClient/model/SchemaWithSet.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#ifndef ORG_OPENAPITOOLS_CLIENT_MODEL_SchemaWithSet_H_
1919
#define ORG_OPENAPITOOLS_CLIENT_MODEL_SchemaWithSet_H_
2020

21+
#include <boost/optional.hpp>
2122

2223
#include "CppRestPetstoreClient/ModelBase.h"
2324

0 commit comments

Comments
 (0)