Skip to content

Commit 23ab517

Browse files
stkrworkwing328
authored andcommitted
[C++][Pistache] Fix optional error and wrong function signatures (#264)
* Added missing includes for optional * Removed shared pointer from pistache generator * Changed Net namespace to Pistache Namespace * Clean up code and removed unnecessary lines in mustache files * Removed remaining shared pointer syntax * Code review fixes + updated samples * Added const to all model setter functions, and reference to all params in setters that are not primitives * Refactored modelbase * Removed const * Updated samples
1 parent 825e4e9 commit 23ab517

35 files changed

Lines changed: 163 additions & 163 deletions
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3.0.0-SNAPSHOT
1+
3.0.1-SNAPSHOT

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CppPistacheServerCodegen.java

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -219,14 +219,14 @@ public Map<String, Object> postProcessOperations(Map<String, Object> objs) {
219219

220220
//TODO: This changes the info about the real type but it is needed to parse the header params
221221
if (param.isHeaderParam) {
222-
param.dataType = "Optional<Net::Http::Header::Raw>";
223-
param.baseType = "Optional<Net::Http::Header::Raw>";
222+
param.dataType = "Pistache::Optional<Pistache::Http::Header::Raw>";
223+
param.baseType = "Pistache::Optional<Pistache::Http::Header::Raw>";
224224
} else if (param.isQueryParam) {
225225
if (param.isPrimitiveType) {
226-
param.dataType = "Optional<" + param.dataType + ">";
226+
param.dataType = "Pistache::Optional<" + param.dataType + ">";
227227
} else {
228-
param.dataType = "Optional<" + param.baseType + ">";
229-
param.baseType = "Optional<" + param.baseType + ">";
228+
param.dataType = "Pistache::Optional<" + param.baseType + ">";
229+
param.baseType = "Pistache::Optional<" + param.baseType + ">";
230230
}
231231
}
232232
}
@@ -299,7 +299,7 @@ public String getTypeDeclaration(Schema p) {
299299
return toModelName(openAPIType);
300300
}
301301

302-
return "std::shared_ptr<" + openAPIType + ">";
302+
return openAPIType;
303303
}
304304

305305
@Override
@@ -326,30 +326,14 @@ public String toDefaultValue(Schema p) {
326326
} else if (ModelUtils.isArraySchema(p)) {
327327
ArraySchema ap = (ArraySchema) p;
328328
String inner = getSchemaType(ap.getItems());
329-
if (!languageSpecificPrimitives.contains(inner)) {
330-
inner = "std::shared_ptr<" + inner + ">";
331-
}
332329
return "std::vector<" + inner + ">()";
333330
} else if (!StringUtils.isEmpty(p.get$ref())) { // model
334-
return "new " + toModelName(ModelUtils.getSimpleRef(p.get$ref())) + "()";
331+
return toModelName(ModelUtils.getSimpleRef(p.get$ref())) + "()";
335332
} else if (ModelUtils.isStringSchema(p)) {
336333
return "\"\"";
337334
}
338335

339-
return "nullptr";
340-
}
341-
342-
@Override
343-
public void postProcessParameter(CodegenParameter parameter) {
344-
super.postProcessParameter(parameter);
345-
346-
boolean isPrimitiveType = parameter.isPrimitiveType == Boolean.TRUE;
347-
boolean isListContainer = parameter.isListContainer == Boolean.TRUE;
348-
boolean isString = parameter.isString == Boolean.TRUE;
349-
350-
if (!isPrimitiveType && !isListContainer && !isString && !parameter.dataType.startsWith("std::shared_ptr")) {
351-
parameter.dataType = "std::shared_ptr<" + parameter.dataType + ">";
352-
}
336+
return "";
353337
}
354338

355339
/**

modules/openapi-generator/src/main/resources/cpp-pistache-server/api-header.mustache

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#include <pistache/router.h>
1515
#include <pistache/http_headers.h>
1616

17+
#include <pistache/optional.h>
18+
1719
{{#imports}}{{{import}}}
1820
{{/imports}}
1921

@@ -41,7 +43,7 @@ private:
4143
{{/operation}}
4244
void {{classnameSnakeLowerCase}}_default_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response);
4345

44-
std::shared_ptr<Pistache::Http::Endpoint> httpEndpoint;
46+
Pistache::Http::Endpoint httpEndpoint;
4547
Pistache::Rest::Router router;
4648

4749
{{#operation}}

modules/openapi-generator/src/main/resources/cpp-pistache-server/api-impl-header.mustache

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
#include <{{classname}}.h>
1919

20+
#include <pistache/optional.h>
21+
2022
{{#imports}}{{{import}}}
2123
{{/imports}}
2224

modules/openapi-generator/src/main/resources/cpp-pistache-server/api-source.mustache

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,24 @@ namespace {{this}} {
1010
using namespace {{modelNamespace}};
1111

1212
{{classname}}::{{classname}}(Pistache::Address addr)
13-
: httpEndpoint(std::make_shared<Pistache::Http::Endpoint>(addr))
13+
: httpEndpoint(addr)
1414
{ };
1515

1616
void {{classname}}::init(size_t thr = 2) {
1717
auto opts = Pistache::Http::Endpoint::options()
1818
.threads(thr)
1919
.flags(Pistache::Tcp::Options::InstallSignalHandler);
20-
httpEndpoint->init(opts);
20+
httpEndpoint.init(opts);
2121
setupRoutes();
2222
}
2323

2424
void {{classname}}::start() {
25-
httpEndpoint->setHandler(router.handler());
26-
httpEndpoint->serve();
25+
httpEndpoint.setHandler(router.handler());
26+
httpEndpoint.serve();
2727
}
2828

2929
void {{classname}}::shutdown() {
30-
httpEndpoint->shutdown();
30+
httpEndpoint.shutdown();
3131
}
3232

3333
void {{classname}}::setupRoutes() {

modules/openapi-generator/src/main/resources/cpp-pistache-server/model-header.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public:
4545
/// </summary>
4646
{{^isNotContainer}}{{{dataType}}}& {{getter}}();
4747
{{/isNotContainer}}{{#isNotContainer}}{{{dataType}}} {{getter}}() const;
48-
void {{setter}}({{{dataType}}} value);
48+
void {{setter}}({{{dataType}}} const{{^isPrimitiveType}}&{{/isPrimitiveType}} value);
4949
{{/isNotContainer}}{{^required}}bool {{nameInCamelCase}}IsSet() const;
5050
void unset{{name}}();
5151
{{/required}}

modules/openapi-generator/src/main/resources/cpp-pistache-server/model-source.mustache

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ void {{classname}}::fromJson(nlohmann::json& val)
8484
else
8585
{
8686
{{{items.datatype}}} newItem({{{items.defaultValue}}});
87-
newItem->fromJson(item);
87+
newItem.fromJson(item);
8888
m_{{name}}.push_back( newItem );
8989
}
9090
{{/items.isDateTime}}{{/items.isString}}{{/isPrimitiveType}}
@@ -100,7 +100,7 @@ void {{classname}}::fromJson(nlohmann::json& val)
100100
{{/isDateTime}}{{^isDateTime}}if(!val["{{baseName}}"].is_null())
101101
{
102102
{{{dataType}}} newItem({{{defaultValue}}});
103-
newItem->fromJson(val["{{baseName}}"]);
103+
newItem.fromJson(val["{{baseName}}"]);
104104
{{setter}}( newItem );
105105
}
106106
{{/isDateTime}}{{/isString}}
@@ -119,7 +119,7 @@ void {{classname}}::fromJson(nlohmann::json& val)
119119
{
120120
return m_{{name}};
121121
}
122-
void {{classname}}::{{setter}}({{{dataType}}} value)
122+
void {{classname}}::{{setter}}({{{dataType}}} const{{^isPrimitiveType}}&{{/isPrimitiveType}} value)
123123
{
124124
m_{{name}} = value;
125125
{{^required}}m_{{name}}IsSet = true;{{/required}}

modules/openapi-generator/src/main/resources/cpp-pistache-server/modelbase-header.mustache

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ public:
2828
virtual nlohmann::json toJson() const = 0;
2929
virtual void fromJson(nlohmann::json& json) = 0;
3030
31-
static std::string toJson( const std::string& value );
32-
static std::string toJson( const std::time_t& value );
33-
static int32_t toJson( int32_t value );
34-
static int64_t toJson( int64_t value );
35-
static double toJson( double value );
36-
static bool toJson( bool value );
37-
static nlohmann::json toJson( std::shared_ptr<ModelBase> content );
31+
static std::string toJson( std::string const& value );
32+
static std::string toJson( std::time_t const& value );
33+
static int32_t toJson( int32_t const value );
34+
static int64_t toJson( int64_t const value );
35+
static double toJson( double const value );
36+
static bool toJson( bool const value );
37+
static nlohmann::json toJson(ModelBase const& content );
3838
3939
};
4040

modules/openapi-generator/src/main/resources/cpp-pistache-server/modelbase-source.mustache

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,41 +12,41 @@ ModelBase::~ModelBase()
1212
{
1313
}
1414

15-
std::string ModelBase::toJson( const std::string& value )
15+
std::string ModelBase::toJson( std::string const& value )
1616
{
1717
return value;
1818
}
1919

20-
std::string ModelBase::toJson( const std::time_t& value )
20+
std::string ModelBase::toJson( std::time_t const& value )
2121
{
2222
char buf[sizeof "2011-10-08T07:07:09Z"];
2323
strftime(buf, sizeof buf, "%FT%TZ", gmtime(&value));
2424
return buf;
2525
}
2626

27-
int32_t ModelBase::toJson( int32_t value )
27+
int32_t ModelBase::toJson( int32_t const value )
2828
{
2929
return value;
3030
}
3131

32-
int64_t ModelBase::toJson( int64_t value )
32+
int64_t ModelBase::toJson( int64_t const value )
3333
{
3434
return value;
3535
}
3636

37-
double ModelBase::toJson( double value )
37+
double ModelBase::toJson( double const value )
3838
{
3939
return value;
4040
}
4141

42-
bool ModelBase::toJson( bool value )
42+
bool ModelBase::toJson( bool const value )
4343
{
4444
return value;
4545
}
4646

47-
nlohmann::json ModelBase::toJson( std::shared_ptr<ModelBase> content )
47+
nlohmann::json ModelBase::toJson(ModelBase content )
4848
{
49-
return content.get() ? content->toJson() : nlohmann::json();
49+
return content.toJson();
5050
}
5151

5252
{{#modelNamespaceDeclarations}}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3.0.0-SNAPSHOT
1+
3.0.2-SNAPSHOT

0 commit comments

Comments
 (0)