Skip to content

Commit 0023f3b

Browse files
authored
Improve templates for C++ Restbed (#10543)
* Improve templates for C++ Restbed The templates now generate classes which have virtual functions that can be overridden to implement handlers. * Fix missing HTTP methods in generated restbed C++ code There was a wrong handling of "x-codegen-other-methods". Change-Id: If6526d2672434beb5ebb0871d84cb80d84c34c38
1 parent 80c3a0e commit 0023f3b

File tree

24 files changed

+3351
-1293
lines changed

24 files changed

+3351
-1293
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,13 +315,14 @@ public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> o
315315
if (!foundInNewList) {
316316
if (op1.path.equals(op.path)) {
317317
foundInNewList = true;
318-
List<CodegenOperation> currentOtherMethodList = (List<CodegenOperation>) op1.vendorExtensions.get("x-codegen-otherMethods");
318+
final String X_CODEGEN_OTHER_METHODS = "x-codegen-other-methods";
319+
List<CodegenOperation> currentOtherMethodList = (List<CodegenOperation>) op1.vendorExtensions.get(X_CODEGEN_OTHER_METHODS);
319320
if (currentOtherMethodList == null) {
320321
currentOtherMethodList = new ArrayList<CodegenOperation>();
321322
}
322323
op.operationIdCamelCase = op1.operationIdCamelCase;
323324
currentOtherMethodList.add(op);
324-
op1.vendorExtensions.put("x-codegen-other-methods", currentOtherMethodList);
325+
op1.vendorExtensions.put(X_CODEGEN_OTHER_METHODS, currentOtherMethodList);
325326
}
326327
}
327328
}

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

Lines changed: 135 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@
1111
{{{defaultInclude}}}
1212
#include <memory>
1313
#include <utility>
14+
#include <exception>
1415

1516
#include <corvusoft/restbed/session.hpp>
1617
#include <corvusoft/restbed/resource.hpp>
18+
#include <corvusoft/restbed/request.hpp>
1719
#include <corvusoft/restbed/service.hpp>
20+
#include <corvusoft/restbed/settings.hpp>
1821

1922
{{#imports}}{{{import}}}
2023
{{/imports}}
@@ -25,6 +28,22 @@ namespace {{this}} {
2528

2629
using namespace {{modelNamespace}};
2730

31+
///
32+
/// Exception to flag problems in the handlers
33+
///
34+
class {{declspec}} {{classname}}Exception: public std::exception
35+
{
36+
public:
37+
{{classname}}Exception(int status_code, std::string what);
38+
39+
int getStatus() const;
40+
const char* what() const noexcept override;
41+
42+
private:
43+
int m_status;
44+
std::string m_what;
45+
};
46+
2847
{{#operation}}
2948
/// <summary>
3049
/// {{summary}}
@@ -35,60 +54,143 @@ using namespace {{modelNamespace}};
3554
class {{declspec}} {{classname}}{{vendorExtensions.x-codegen-resource-name}}Resource: public restbed::Resource
3655
{
3756
public:
38-
{{classname}}{{vendorExtensions.x-codegen-resource-name}}Resource();
57+
{{classname}}{{vendorExtensions.x-codegen-resource-name}}Resource(const std::string& context = "{{contextPath}}");
3958
virtual ~{{classname}}{{vendorExtensions.x-codegen-resource-name}}Resource();
40-
void {{httpMethod}}_method_handler(const std::shared_ptr<restbed::Session> session);
59+
60+
protected:
61+
//////////////////////////////////////////////////////////
62+
// Override these to implement the server functionality //
63+
//////////////////////////////////////////////////////////
64+
65+
virtual {{#returnType}}std::pair<int, {{{.}}}>{{/returnType}}{{^returnType}}int{{/returnType}} handler_{{httpMethod}}(
66+
{{#allParams}}{{{dataType}}} const & {{{paramName}}}{{^-last}}, {{/-last}}{{/allParams}});
67+
68+
{{#vendorExtensions.x-codegen-other-methods}}
69+
virtual {{#returnType}}std::pair<int, {{{.}}}>{{/returnType}}{{^returnType}}int{{/returnType}} handler_{{httpMethod}}(
70+
{{#allParams}}{{{dataType}}} const & {{{paramName}}}{{^-last}}, {{/-last}}{{/allParams}});
71+
{{/vendorExtensions.x-codegen-other-methods}}
72+
73+
protected:
74+
//////////////////////////////////////
75+
// Override these for customization //
76+
//////////////////////////////////////
77+
78+
virtual std::string extractBodyContent(const std::shared_ptr<restbed::Session>& session);
79+
80+
{{#hasPathParams}}
81+
{{#pathParams}}
82+
{{#isPrimitiveType}}
83+
virtual {{{dataType}}} getPathParam_{{{paramName}}}(const std::shared_ptr<const restbed::Request>& request)
84+
{
85+
return request->get_path_parameter("{{{paramName}}}", {{{defaultValue}}});
86+
}
87+
88+
{{/isPrimitiveType}}
89+
{{/pathParams}}
90+
{{/hasPathParams}}
91+
{{#hasQueryParams}}
92+
{{#queryParams}}
93+
{{#isPrimitiveType}}
94+
virtual {{{dataType}}} getQueryParam_{{{paramName}}}(const std::shared_ptr<const restbed::Request>& request)
95+
{
96+
return request->get_query_parameter("{{{paramName}}}", {{{defaultValue}}});
97+
}
98+
99+
{{/isPrimitiveType}}
100+
{{/queryParams}}
101+
{{/hasQueryParams}}
102+
{{#hasHeaderParams}}
103+
{{#headerParams}}
104+
{{#isPrimitiveType}}
105+
virtual {{{dataType}}} getHeader_{{{baseName}}}(const std::shared_ptr<const restbed::Request>& request)
106+
{
107+
return request->get_header("{{baseName}}", {{{defaultValue}}});
108+
}
109+
110+
{{/isPrimitiveType}}
111+
{{/headerParams}}
112+
{{/hasHeaderParams}}
113+
41114
{{#vendorExtensions.x-codegen-other-methods}}
42-
void {{httpMethod}}_method_handler(const std::shared_ptr<restbed::Session> session);
115+
{{#hasPathParams}}
116+
{{#pathParams}}
117+
{{#isPrimitiveType}}
118+
virtual {{{dataType}}} getPathParam_{{{paramName}}}_x_extension(const std::shared_ptr<const restbed::Request>& request)
119+
{
120+
return request->get_path_parameter("{{{paramName}}}", {{{defaultValue}}});
121+
}
122+
{{/isPrimitiveType}}
123+
{{/pathParams}}
124+
{{/hasPathParams}}
125+
{{#hasQueryParams}}
126+
{{#queryParams}}
127+
{{#isPrimitiveType}}
128+
virtual {{{dataType}}} getQueryParam_{{{paramName}}}_x_extension(const std::shared_ptr<const restbed::Request>& request)
129+
{
130+
return request->get_query_parameter("{{{paramName}}}", {{{defaultValue}}});
131+
}
132+
{{/isPrimitiveType}}
133+
{{/queryParams}}
134+
{{/hasQueryParams}}
135+
{{#hasHeaderParams}}
136+
{{#headerParams}}
137+
{{#isPrimitiveType}}
138+
virtual {{{dataType}}} getHeader_{{{baseName}}}_x_extension(const std::shared_ptr<const restbed::Request>& request)
139+
{
140+
return request->get_header("{{baseName}}", {{{defaultValue}}});
141+
}
142+
{{/isPrimitiveType}}
143+
{{/headerParams}}
144+
{{/hasHeaderParams}}
43145
{{/vendorExtensions.x-codegen-other-methods}}
44146

45-
void set_handler_{{httpMethod}}(
46-
std::function<std::pair<int, std::string>(
47-
{{#allParams}}{{{dataType}}} const &{{^-last}}, {{/-last}}{{/allParams}}
48-
)> handler
49-
);
147+
virtual std::pair<int, std::string> handle{{classname}}Exception(const {{classname}}Exception& e);
148+
virtual std::pair<int, std::string> handleStdException(const std::exception& e);
149+
virtual std::pair<int, std::string> handleUnspecifiedException();
150+
151+
virtual void setResponseHeader(const std::shared_ptr<restbed::Session>& session,
152+
const std::string& header);
50153

51-
{{#vendorExtensions.x-codegen-other-methods}}
52-
void set_handler_{{httpMethod}}(
53-
std::function<std::pair<int, std::string>(
54-
{{#allParams}}{{{dataType}}} const &{{^-last}}, {{/-last}}{{/allParams}}
55-
)> handler
56-
);
57-
{{/vendorExtensions.x-codegen-other-methods}}
154+
155+
virtual void returnResponse(const std::shared_ptr<restbed::Session>& session,
156+
const int status, const std::string& result, const std::string& contentType);
157+
virtual void defaultSessionClose(const std::shared_ptr<restbed::Session>& session,
158+
const int status, const std::string& result);
58159

59160
private:
60-
std::function<std::pair<int, std::string>(
61-
{{#allParams}}{{{dataType}}} const &{{^-last}}, {{/-last}}{{/allParams}}
62-
)> handler_{{httpMethod}}_;
63-
64-
{{#vendorExtensions.x-codegen-other-methods}}
65-
std::function<std::pair<int, std::string>(
66-
{{#allParams}}{{{dataType}}} const &{{^-last}}, {{/-last}}{{/allParams}}
67-
)> handler_{{httpMethod}}_;
68-
{{/vendorExtensions.x-codegen-other-methods}}
69-
70-
{{#allParams}}
71-
{{{dataType}}} {{paramName}}{};
72-
{{/allParams}}
161+
void handler_{{httpMethod}}_internal(const std::shared_ptr<restbed::Session> session);
162+
{{#vendorExtensions.x-codegen-other-methods}}
163+
void handler_{{httpMethod}}_internal(const std::shared_ptr<restbed::Session> session);
164+
{{/vendorExtensions.x-codegen-other-methods}}
73165
};
74166

167+
75168
{{/operation}}
76169

77170
//
78171
// The restbed service to actually implement the REST server
79172
//
80-
class {{declspec}} {{classname}}: public restbed::Service
173+
class {{declspec}} {{classname}}
81174
{
82175
public:
83-
{{classname}}();
84-
~{{classname}}();
85-
void startService(int const& port);
86-
void stopService();
176+
explicit {{classname}}(std::shared_ptr<restbed::Service> const& restbedService);
177+
virtual ~{{classname}}();
178+
179+
{{#operation}}
180+
virtual void set{{classname}}{{vendorExtensions.x-codegen-resource-name}}Resource(std::shared_ptr<{{classname}}{{vendorExtensions.x-codegen-resource-name}}Resource> sp{{classname}}{{vendorExtensions.x-codegen-resource-name}}Resource);
181+
{{/operation}}
182+
183+
virtual void publishDefaultResources();
184+
185+
virtual std::shared_ptr<restbed::Service> service();
87186

88187
protected:
89188
{{#operation}}
90189
std::shared_ptr<{{classname}}{{vendorExtensions.x-codegen-resource-name}}Resource> m_sp{{classname}}{{vendorExtensions.x-codegen-resource-name}}Resource;
91190
{{/operation}}
191+
192+
private:
193+
std::shared_ptr<restbed::Service> m_service;
92194
};
93195

94196

0 commit comments

Comments
 (0)