Skip to content

Commit f37ac8d

Browse files
authored
Improvements for qt5 server (OpenAPITools#942)
Remove beta tag Supported nested containers Move repetitive code to inline function Fix docker file Fix CMakeLists.txt when building external project
1 parent 6fe6ec2 commit f37ac8d

File tree

10 files changed

+49
-67
lines changed

10 files changed

+49
-67
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public CppQt5QHttpEngineServerCodegen() {
125125

126126
// Write defaults namespace in properties so that it can be accessible in templates.
127127
// At this point command line has not been parsed so if value is given
128-
// in command line it will superseed this content
128+
// in command line it will supersede this content
129129
additionalProperties.put("cppNamespace", cppNamespace);
130130

131131
/*
@@ -180,7 +180,7 @@ public CppQt5QHttpEngineServerCodegen() {
180180
// mapped as "file" type for OAS 3.0
181181
typeMapping.put("ByteArray", "QByteArray");
182182
// UUID support - possible enhancement : use QUuid instead of QString.
183-
// beware though that Serialisation/deserialisation of QUuid does not
183+
// beware though that Serialization/deserialization of QUuid does not
184184
// come out of the box and will need to be sorted out (at least imply
185185
// modifications on multiple templates)
186186
typeMapping.put("UUID", "QString");
@@ -260,7 +260,7 @@ public String getName() {
260260
*/
261261
@Override
262262
public String getHelp() {
263-
return "Generates a Qt5 C++ Server (beta) using the QHTTPEngine HTTP Library.";
263+
return "Generates a Qt5 C++ Server using the QHTTPEngine HTTP Library.";
264264
}
265265

266266
@Override

modules/openapi-generator/src/main/resources/cpp-qt5-qhttpengine-server/CMakeLists.txt.mustache

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ set(EXTERNAL_INSTALL_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/external)
77

88
ExternalProject_Add(QHTTPENGINE
99
GIT_REPOSITORY https://github.com/etherealjoy/qhttpengine.git
10-
CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${EXTERNAL_INSTALL_LOCATION}
10+
CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${EXTERNAL_INSTALL_LOCATION}
11+
-DCMAKE_PREFIX_PATH=${CMAKE_PREFIX_PATH}
1112
)
1213

1314
include_directories(${EXTERNAL_INSTALL_LOCATION}/include)

modules/openapi-generator/src/main/resources/cpp-qt5-qhttpengine-server/Dockerfile.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ RUN apk add --update \
2323
openssl
2424

2525
WORKDIR /usr/server
26-
COPY --from=build /usr/server/build/src/cpp-qt5-server ./build/src/
26+
COPY --from=build /usr/server/build/src/cpp-qt5-qhttpengine-server ./build/src/
2727
COPY --from=build /usr/server/external/ ./external
2828
EXPOSE 8080/tcp
29-
ENTRYPOINT ["/usr/server/build/src/cpp-qt5-server"]
29+
ENTRYPOINT ["/usr/server/build/src/cpp-qt5-qhttpengine-server"]

modules/openapi-generator/src/main/resources/cpp-qt5-qhttpengine-server/apirouter.cpp.mustache

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include <QJsonDocument>
44
#include <QJsonObject>
55
#include <QVariantMap>
6-
#include <QRegularExpression>
6+
77

88
#include "{{prefix}}ApiRouter.h"
99
{{#apiInfo}}{{#apis}}{{#operations}}#include "{{classname}}Request.h"
@@ -18,7 +18,7 @@ namespace {{this}} {
1818
}
1919

2020
{{prefix}}ApiRouter::~{{prefix}}ApiRouter(){
21-
qDebug() << "~ApiRouter()";{{#apiInfo}}{{#apis}}
21+
{{#apiInfo}}{{#apis}}
2222
delete {{classname}}ApiHandler;{{/apis}}{{/apiInfo}}
2323
}
2424

@@ -62,11 +62,7 @@ bool {{prefix}}ApiRouter::handleRequestAndExtractPathParam(QHttpEngine::Socket *
6262
{
6363
auto completePath = QString("%1 %2").arg("{{httpMethod}}").arg("{{{basePathWithoutHost}}}{{{path}}}").toLower();
6464
if ( reqPath.startsWith(completePath.leftRef( completePath.indexOf(QString("/{")))) ) {
65-
QRegularExpression parExpr( R"(\{([^\/\\s]+)\})" );
66-
completePath.replace( parExpr, R"((?<\1>[^\/\s]+))" );
67-
completePath.append("[\\/]?$");
68-
QRegularExpression pathExpr( completePath );
69-
QRegularExpressionMatch match = pathExpr.match( reqPath );
65+
QRegularExpressionMatch match = getRequestMatch( completePath, reqPath );
7066
if ( match.hasMatch() ){
7167
{{#pathParams}}
7268
QString {{baseName}} = match.captured(QString("{{baseName}}").toLower());

modules/openapi-generator/src/main/resources/cpp-qt5-qhttpengine-server/apirouter.h.mustache

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <QSharedPointer>
88
#include <QList>
99
#include <QMultiMap>
10+
#include <QRegularExpression>
1011

1112
#include <qhttpengine/socket.h>
1213
#include <qhttpengine/handler.h>
@@ -76,6 +77,14 @@ private :
7677
}
7778
return QStringLiteral("");
7879
}
80+
81+
inline QRegularExpressionMatch getRequestMatch(QString serverTemplatePath, QString requestPath){
82+
QRegularExpression parExpr( R"(\{([^\/\\s]+)\})" );
83+
serverTemplatePath.replace( parExpr, R"((?<\1>[^\/\s]+))" );
84+
serverTemplatePath.append("[\\/]?$");
85+
QRegularExpression pathExpr( serverTemplatePath );
86+
return pathExpr.match( requestPath );
87+
}
7988

8089
};
8190

modules/openapi-generator/src/main/resources/cpp-qt5-qhttpengine-server/model-body.mustache

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,19 @@ void
4848
if(json["{{baseName}}"].isArray()){
4949
auto arr = json["{{baseName}}"].toArray();
5050
for (const QJsonValue & jval : arr) {
51-
{{items.baseType}} item;
52-
{{name}}.push_back(::{{cppNamespace}}::fromJsonValue(item, jval));
51+
{{^items.isContainer}}{{items.baseType}}{{/items.isContainer}}{{#items.isListContainer}}QList<{{items.items.baseType}}>{{/items.isListContainer}}{{#items.isMapContainer}}QMap<QString, {{items.items.baseType}}>{{/items.isMapContainer}} item;
52+
::{{cppNamespace}}::fromJsonValue(item, jval)
53+
{{name}}.push_back(item);
5354
}
5455
}{{/isListContainer}}{{#isMapContainer}}
5556
if(json["{{baseName}}"].isObject()){
5657
auto varmap = json["{{baseName}}"].toObject().toVariantMap();
5758
if(varmap.count() > 0){
5859
for(auto val : varmap.keys()){
59-
{{items.baseType}} item;
60+
{{^items.isContainer}}{{items.baseType}}{{/items.isContainer}}{{#items.isListContainer}}QList<{{items.items.baseType}}>{{/items.isListContainer}}{{#items.isMapContainer}}QMap<QString, {{items.items.baseType}}>{{/items.isMapContainer}} item;
6061
auto jval = QJsonValue::fromVariant(varmap.value(val));
61-
{{name}}.insert({{name}}.end(), val, ::{{cppNamespace}}::fromJsonValue(item, jval));
62+
::{{cppNamespace}}::fromJsonValue(item, jval);
63+
{{name}}.insert({{name}}.end(), val, item);
6264
}
6365
}
6466
}{{/isMapContainer}}{{/items.isContainer}}{{/isContainer}}

samples/server/petstore/cpp-qt5-qhttpengine-server/server/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ set(EXTERNAL_INSTALL_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/external)
77

88
ExternalProject_Add(QHTTPENGINE
99
GIT_REPOSITORY https://github.com/etherealjoy/qhttpengine.git
10-
CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${EXTERNAL_INSTALL_LOCATION}
10+
CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${EXTERNAL_INSTALL_LOCATION}
11+
-DCMAKE_PREFIX_PATH=${CMAKE_PREFIX_PATH}
1112
)
1213

1314
include_directories(${EXTERNAL_INSTALL_LOCATION}/include)

samples/server/petstore/cpp-qt5-qhttpengine-server/server/Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ RUN apk add --update \
2323
openssl
2424

2525
WORKDIR /usr/server
26-
COPY --from=build /usr/server/build/src/cpp-qt5-server ./build/src/
26+
COPY --from=build /usr/server/build/src/cpp-qt5-qhttpengine-server ./build/src/
2727
COPY --from=build /usr/server/external/ ./external
2828
EXPOSE 8080/tcp
29-
ENTRYPOINT ["/usr/server/build/src/cpp-qt5-server"]
29+
ENTRYPOINT ["/usr/server/build/src/cpp-qt5-qhttpengine-server"]

samples/server/petstore/cpp-qt5-qhttpengine-server/server/src/handlers/OAIApiRouter.cpp

Lines changed: 11 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#include <QJsonDocument>
1515
#include <QJsonObject>
1616
#include <QVariantMap>
17-
#include <QRegularExpression>
17+
1818

1919
#include "OAIApiRouter.h"
2020
#include "OAIPetApiRequest.h"
@@ -29,7 +29,7 @@ OAIApiRouter::OAIApiRouter() {
2929
}
3030

3131
OAIApiRouter::~OAIApiRouter(){
32-
qDebug() << "~ApiRouter()";
32+
3333
delete OAIPetApiApiHandler;
3434
delete OAIStoreApiApiHandler;
3535
delete OAIUserApiApiHandler;
@@ -117,11 +117,7 @@ bool OAIApiRouter::handleRequestAndExtractPathParam(QHttpEngine::Socket *socket)
117117
{
118118
auto completePath = QString("%1 %2").arg("DELETE").arg("/v2/pet/{petId}").toLower();
119119
if ( reqPath.startsWith(completePath.leftRef( completePath.indexOf(QString("/{")))) ) {
120-
QRegularExpression parExpr( R"(\{([^\/\\s]+)\})" );
121-
completePath.replace( parExpr, R"((?<\1>[^\/\s]+))" );
122-
completePath.append("[\\/]?$");
123-
QRegularExpression pathExpr( completePath );
124-
QRegularExpressionMatch match = pathExpr.match( reqPath );
120+
QRegularExpressionMatch match = getRequestMatch( completePath, reqPath );
125121
if ( match.hasMatch() ){
126122
QString petId = match.captured(QString("petId").toLower());
127123
auto reqObj = new OAIPetApiRequest(socket, OAIPetApiApiHandler);
@@ -133,11 +129,7 @@ bool OAIApiRouter::handleRequestAndExtractPathParam(QHttpEngine::Socket *socket)
133129
{
134130
auto completePath = QString("%1 %2").arg("GET").arg("/v2/pet/{petId}").toLower();
135131
if ( reqPath.startsWith(completePath.leftRef( completePath.indexOf(QString("/{")))) ) {
136-
QRegularExpression parExpr( R"(\{([^\/\\s]+)\})" );
137-
completePath.replace( parExpr, R"((?<\1>[^\/\s]+))" );
138-
completePath.append("[\\/]?$");
139-
QRegularExpression pathExpr( completePath );
140-
QRegularExpressionMatch match = pathExpr.match( reqPath );
132+
QRegularExpressionMatch match = getRequestMatch( completePath, reqPath );
141133
if ( match.hasMatch() ){
142134
QString petId = match.captured(QString("petId").toLower());
143135
auto reqObj = new OAIPetApiRequest(socket, OAIPetApiApiHandler);
@@ -149,11 +141,7 @@ bool OAIApiRouter::handleRequestAndExtractPathParam(QHttpEngine::Socket *socket)
149141
{
150142
auto completePath = QString("%1 %2").arg("POST").arg("/v2/pet/{petId}").toLower();
151143
if ( reqPath.startsWith(completePath.leftRef( completePath.indexOf(QString("/{")))) ) {
152-
QRegularExpression parExpr( R"(\{([^\/\\s]+)\})" );
153-
completePath.replace( parExpr, R"((?<\1>[^\/\s]+))" );
154-
completePath.append("[\\/]?$");
155-
QRegularExpression pathExpr( completePath );
156-
QRegularExpressionMatch match = pathExpr.match( reqPath );
144+
QRegularExpressionMatch match = getRequestMatch( completePath, reqPath );
157145
if ( match.hasMatch() ){
158146
QString petId = match.captured(QString("petId").toLower());
159147
auto reqObj = new OAIPetApiRequest(socket, OAIPetApiApiHandler);
@@ -165,11 +153,7 @@ bool OAIApiRouter::handleRequestAndExtractPathParam(QHttpEngine::Socket *socket)
165153
{
166154
auto completePath = QString("%1 %2").arg("POST").arg("/v2/pet/{petId}/uploadImage").toLower();
167155
if ( reqPath.startsWith(completePath.leftRef( completePath.indexOf(QString("/{")))) ) {
168-
QRegularExpression parExpr( R"(\{([^\/\\s]+)\})" );
169-
completePath.replace( parExpr, R"((?<\1>[^\/\s]+))" );
170-
completePath.append("[\\/]?$");
171-
QRegularExpression pathExpr( completePath );
172-
QRegularExpressionMatch match = pathExpr.match( reqPath );
156+
QRegularExpressionMatch match = getRequestMatch( completePath, reqPath );
173157
if ( match.hasMatch() ){
174158
QString petId = match.captured(QString("petId").toLower());
175159
auto reqObj = new OAIPetApiRequest(socket, OAIPetApiApiHandler);
@@ -181,11 +165,7 @@ bool OAIApiRouter::handleRequestAndExtractPathParam(QHttpEngine::Socket *socket)
181165
{
182166
auto completePath = QString("%1 %2").arg("DELETE").arg("/v2/store/order/{orderId}").toLower();
183167
if ( reqPath.startsWith(completePath.leftRef( completePath.indexOf(QString("/{")))) ) {
184-
QRegularExpression parExpr( R"(\{([^\/\\s]+)\})" );
185-
completePath.replace( parExpr, R"((?<\1>[^\/\s]+))" );
186-
completePath.append("[\\/]?$");
187-
QRegularExpression pathExpr( completePath );
188-
QRegularExpressionMatch match = pathExpr.match( reqPath );
168+
QRegularExpressionMatch match = getRequestMatch( completePath, reqPath );
189169
if ( match.hasMatch() ){
190170
QString orderId = match.captured(QString("orderId").toLower());
191171
auto reqObj = new OAIStoreApiRequest(socket, OAIStoreApiApiHandler);
@@ -197,11 +177,7 @@ bool OAIApiRouter::handleRequestAndExtractPathParam(QHttpEngine::Socket *socket)
197177
{
198178
auto completePath = QString("%1 %2").arg("GET").arg("/v2/store/order/{orderId}").toLower();
199179
if ( reqPath.startsWith(completePath.leftRef( completePath.indexOf(QString("/{")))) ) {
200-
QRegularExpression parExpr( R"(\{([^\/\\s]+)\})" );
201-
completePath.replace( parExpr, R"((?<\1>[^\/\s]+))" );
202-
completePath.append("[\\/]?$");
203-
QRegularExpression pathExpr( completePath );
204-
QRegularExpressionMatch match = pathExpr.match( reqPath );
180+
QRegularExpressionMatch match = getRequestMatch( completePath, reqPath );
205181
if ( match.hasMatch() ){
206182
QString orderId = match.captured(QString("orderId").toLower());
207183
auto reqObj = new OAIStoreApiRequest(socket, OAIStoreApiApiHandler);
@@ -213,11 +189,7 @@ bool OAIApiRouter::handleRequestAndExtractPathParam(QHttpEngine::Socket *socket)
213189
{
214190
auto completePath = QString("%1 %2").arg("DELETE").arg("/v2/user/{username}").toLower();
215191
if ( reqPath.startsWith(completePath.leftRef( completePath.indexOf(QString("/{")))) ) {
216-
QRegularExpression parExpr( R"(\{([^\/\\s]+)\})" );
217-
completePath.replace( parExpr, R"((?<\1>[^\/\s]+))" );
218-
completePath.append("[\\/]?$");
219-
QRegularExpression pathExpr( completePath );
220-
QRegularExpressionMatch match = pathExpr.match( reqPath );
192+
QRegularExpressionMatch match = getRequestMatch( completePath, reqPath );
221193
if ( match.hasMatch() ){
222194
QString username = match.captured(QString("username").toLower());
223195
auto reqObj = new OAIUserApiRequest(socket, OAIUserApiApiHandler);
@@ -229,11 +201,7 @@ bool OAIApiRouter::handleRequestAndExtractPathParam(QHttpEngine::Socket *socket)
229201
{
230202
auto completePath = QString("%1 %2").arg("GET").arg("/v2/user/{username}").toLower();
231203
if ( reqPath.startsWith(completePath.leftRef( completePath.indexOf(QString("/{")))) ) {
232-
QRegularExpression parExpr( R"(\{([^\/\\s]+)\})" );
233-
completePath.replace( parExpr, R"((?<\1>[^\/\s]+))" );
234-
completePath.append("[\\/]?$");
235-
QRegularExpression pathExpr( completePath );
236-
QRegularExpressionMatch match = pathExpr.match( reqPath );
204+
QRegularExpressionMatch match = getRequestMatch( completePath, reqPath );
237205
if ( match.hasMatch() ){
238206
QString username = match.captured(QString("username").toLower());
239207
auto reqObj = new OAIUserApiRequest(socket, OAIUserApiApiHandler);
@@ -245,11 +213,7 @@ bool OAIApiRouter::handleRequestAndExtractPathParam(QHttpEngine::Socket *socket)
245213
{
246214
auto completePath = QString("%1 %2").arg("PUT").arg("/v2/user/{username}").toLower();
247215
if ( reqPath.startsWith(completePath.leftRef( completePath.indexOf(QString("/{")))) ) {
248-
QRegularExpression parExpr( R"(\{([^\/\\s]+)\})" );
249-
completePath.replace( parExpr, R"((?<\1>[^\/\s]+))" );
250-
completePath.append("[\\/]?$");
251-
QRegularExpression pathExpr( completePath );
252-
QRegularExpressionMatch match = pathExpr.match( reqPath );
216+
QRegularExpressionMatch match = getRequestMatch( completePath, reqPath );
253217
if ( match.hasMatch() ){
254218
QString username = match.captured(QString("username").toLower());
255219
auto reqObj = new OAIUserApiRequest(socket, OAIUserApiApiHandler);

samples/server/petstore/cpp-qt5-qhttpengine-server/server/src/handlers/OAIApiRouter.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <QSharedPointer>
1919
#include <QList>
2020
#include <QMultiMap>
21+
#include <QRegularExpression>
2122

2223
#include <qhttpengine/socket.h>
2324
#include <qhttpengine/handler.h>
@@ -89,6 +90,14 @@ private :
8990
}
9091
return QStringLiteral("");
9192
}
93+
94+
inline QRegularExpressionMatch getRequestMatch(QString serverTemplatePath, QString requestPath){
95+
QRegularExpression parExpr( R"(\{([^\/\\s]+)\})" );
96+
serverTemplatePath.replace( parExpr, R"((?<\1>[^\/\s]+))" );
97+
serverTemplatePath.append("[\\/]?$");
98+
QRegularExpression pathExpr( serverTemplatePath );
99+
return pathExpr.match( requestPath );
100+
}
92101

93102
};
94103

0 commit comments

Comments
 (0)