Skip to content

Commit

Permalink
Improvements for qt5 server (OpenAPITools#942)
Browse files Browse the repository at this point in the history
Remove beta tag
Supported nested containers
Move repetitive code to inline function
Fix docker file
Fix CMakeLists.txt when building external project
  • Loading branch information
etherealjoy authored and jaumard committed Sep 21, 2018
1 parent 5fc1638 commit 32f4cb5
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public CppQt5QHttpEngineServerCodegen() {

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

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

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ set(EXTERNAL_INSTALL_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/external)

ExternalProject_Add(QHTTPENGINE
GIT_REPOSITORY https://github.com/etherealjoy/qhttpengine.git
CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${EXTERNAL_INSTALL_LOCATION}
CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${EXTERNAL_INSTALL_LOCATION}
-DCMAKE_PREFIX_PATH=${CMAKE_PREFIX_PATH}
)

include_directories(${EXTERNAL_INSTALL_LOCATION}/include)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ RUN apk add --update \
openssl

WORKDIR /usr/server
COPY --from=build /usr/server/build/src/cpp-qt5-server ./build/src/
COPY --from=build /usr/server/build/src/cpp-qt5-qhttpengine-server ./build/src/
COPY --from=build /usr/server/external/ ./external
EXPOSE 8080/tcp
ENTRYPOINT ["/usr/server/build/src/cpp-qt5-server"]
ENTRYPOINT ["/usr/server/build/src/cpp-qt5-qhttpengine-server"]
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <QJsonDocument>
#include <QJsonObject>
#include <QVariantMap>
#include <QRegularExpression>


#include "{{prefix}}ApiRouter.h"
{{#apiInfo}}{{#apis}}{{#operations}}#include "{{classname}}Request.h"
Expand All @@ -18,7 +18,7 @@ namespace {{this}} {
}

{{prefix}}ApiRouter::~{{prefix}}ApiRouter(){
qDebug() << "~ApiRouter()";{{#apiInfo}}{{#apis}}
{{#apiInfo}}{{#apis}}
delete {{classname}}ApiHandler;{{/apis}}{{/apiInfo}}
}

Expand Down Expand Up @@ -62,11 +62,7 @@ bool {{prefix}}ApiRouter::handleRequestAndExtractPathParam(QHttpEngine::Socket *
{
auto completePath = QString("%1 %2").arg("{{httpMethod}}").arg("{{{basePathWithoutHost}}}{{{path}}}").toLower();
if ( reqPath.startsWith(completePath.leftRef( completePath.indexOf(QString("/{")))) ) {
QRegularExpression parExpr( R"(\{([^\/\\s]+)\})" );
completePath.replace( parExpr, R"((?<\1>[^\/\s]+))" );
completePath.append("[\\/]?$");
QRegularExpression pathExpr( completePath );
QRegularExpressionMatch match = pathExpr.match( reqPath );
QRegularExpressionMatch match = getRequestMatch( completePath, reqPath );
if ( match.hasMatch() ){
{{#pathParams}}
QString {{baseName}} = match.captured(QString("{{baseName}}").toLower());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <QSharedPointer>
#include <QList>
#include <QMultiMap>
#include <QRegularExpression>

#include <qhttpengine/socket.h>
#include <qhttpengine/handler.h>
Expand Down Expand Up @@ -76,6 +77,14 @@ private :
}
return QStringLiteral("");
}

inline QRegularExpressionMatch getRequestMatch(QString serverTemplatePath, QString requestPath){
QRegularExpression parExpr( R"(\{([^\/\\s]+)\})" );
serverTemplatePath.replace( parExpr, R"((?<\1>[^\/\s]+))" );
serverTemplatePath.append("[\\/]?$");
QRegularExpression pathExpr( serverTemplatePath );
return pathExpr.match( requestPath );
}

};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,19 @@ void
if(json["{{baseName}}"].isArray()){
auto arr = json["{{baseName}}"].toArray();
for (const QJsonValue & jval : arr) {
{{items.baseType}} item;
{{name}}.push_back(::{{cppNamespace}}::fromJsonValue(item, jval));
{{^items.isContainer}}{{items.baseType}}{{/items.isContainer}}{{#items.isListContainer}}QList<{{items.items.baseType}}>{{/items.isListContainer}}{{#items.isMapContainer}}QMap<QString, {{items.items.baseType}}>{{/items.isMapContainer}} item;
::{{cppNamespace}}::fromJsonValue(item, jval)
{{name}}.push_back(item);
}
}{{/isListContainer}}{{#isMapContainer}}
if(json["{{baseName}}"].isObject()){
auto varmap = json["{{baseName}}"].toObject().toVariantMap();
if(varmap.count() > 0){
for(auto val : varmap.keys()){
{{items.baseType}} item;
{{^items.isContainer}}{{items.baseType}}{{/items.isContainer}}{{#items.isListContainer}}QList<{{items.items.baseType}}>{{/items.isListContainer}}{{#items.isMapContainer}}QMap<QString, {{items.items.baseType}}>{{/items.isMapContainer}} item;
auto jval = QJsonValue::fromVariant(varmap.value(val));
{{name}}.insert({{name}}.end(), val, ::{{cppNamespace}}::fromJsonValue(item, jval));
::{{cppNamespace}}::fromJsonValue(item, jval);
{{name}}.insert({{name}}.end(), val, item);
}
}
}{{/isMapContainer}}{{/items.isContainer}}{{/isContainer}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ set(EXTERNAL_INSTALL_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/external)

ExternalProject_Add(QHTTPENGINE
GIT_REPOSITORY https://github.com/etherealjoy/qhttpengine.git
CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${EXTERNAL_INSTALL_LOCATION}
CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${EXTERNAL_INSTALL_LOCATION}
-DCMAKE_PREFIX_PATH=${CMAKE_PREFIX_PATH}
)

include_directories(${EXTERNAL_INSTALL_LOCATION}/include)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ RUN apk add --update \
openssl

WORKDIR /usr/server
COPY --from=build /usr/server/build/src/cpp-qt5-server ./build/src/
COPY --from=build /usr/server/build/src/cpp-qt5-qhttpengine-server ./build/src/
COPY --from=build /usr/server/external/ ./external
EXPOSE 8080/tcp
ENTRYPOINT ["/usr/server/build/src/cpp-qt5-server"]
ENTRYPOINT ["/usr/server/build/src/cpp-qt5-qhttpengine-server"]
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include <QJsonDocument>
#include <QJsonObject>
#include <QVariantMap>
#include <QRegularExpression>


#include "OAIApiRouter.h"
#include "OAIPetApiRequest.h"
Expand All @@ -29,7 +29,7 @@ OAIApiRouter::OAIApiRouter() {
}

OAIApiRouter::~OAIApiRouter(){
qDebug() << "~ApiRouter()";

delete OAIPetApiApiHandler;
delete OAIStoreApiApiHandler;
delete OAIUserApiApiHandler;
Expand Down Expand Up @@ -117,11 +117,7 @@ bool OAIApiRouter::handleRequestAndExtractPathParam(QHttpEngine::Socket *socket)
{
auto completePath = QString("%1 %2").arg("DELETE").arg("/v2/pet/{petId}").toLower();
if ( reqPath.startsWith(completePath.leftRef( completePath.indexOf(QString("/{")))) ) {
QRegularExpression parExpr( R"(\{([^\/\\s]+)\})" );
completePath.replace( parExpr, R"((?<\1>[^\/\s]+))" );
completePath.append("[\\/]?$");
QRegularExpression pathExpr( completePath );
QRegularExpressionMatch match = pathExpr.match( reqPath );
QRegularExpressionMatch match = getRequestMatch( completePath, reqPath );
if ( match.hasMatch() ){
QString petId = match.captured(QString("petId").toLower());
auto reqObj = new OAIPetApiRequest(socket, OAIPetApiApiHandler);
Expand All @@ -133,11 +129,7 @@ bool OAIApiRouter::handleRequestAndExtractPathParam(QHttpEngine::Socket *socket)
{
auto completePath = QString("%1 %2").arg("GET").arg("/v2/pet/{petId}").toLower();
if ( reqPath.startsWith(completePath.leftRef( completePath.indexOf(QString("/{")))) ) {
QRegularExpression parExpr( R"(\{([^\/\\s]+)\})" );
completePath.replace( parExpr, R"((?<\1>[^\/\s]+))" );
completePath.append("[\\/]?$");
QRegularExpression pathExpr( completePath );
QRegularExpressionMatch match = pathExpr.match( reqPath );
QRegularExpressionMatch match = getRequestMatch( completePath, reqPath );
if ( match.hasMatch() ){
QString petId = match.captured(QString("petId").toLower());
auto reqObj = new OAIPetApiRequest(socket, OAIPetApiApiHandler);
Expand All @@ -149,11 +141,7 @@ bool OAIApiRouter::handleRequestAndExtractPathParam(QHttpEngine::Socket *socket)
{
auto completePath = QString("%1 %2").arg("POST").arg("/v2/pet/{petId}").toLower();
if ( reqPath.startsWith(completePath.leftRef( completePath.indexOf(QString("/{")))) ) {
QRegularExpression parExpr( R"(\{([^\/\\s]+)\})" );
completePath.replace( parExpr, R"((?<\1>[^\/\s]+))" );
completePath.append("[\\/]?$");
QRegularExpression pathExpr( completePath );
QRegularExpressionMatch match = pathExpr.match( reqPath );
QRegularExpressionMatch match = getRequestMatch( completePath, reqPath );
if ( match.hasMatch() ){
QString petId = match.captured(QString("petId").toLower());
auto reqObj = new OAIPetApiRequest(socket, OAIPetApiApiHandler);
Expand All @@ -165,11 +153,7 @@ bool OAIApiRouter::handleRequestAndExtractPathParam(QHttpEngine::Socket *socket)
{
auto completePath = QString("%1 %2").arg("POST").arg("/v2/pet/{petId}/uploadImage").toLower();
if ( reqPath.startsWith(completePath.leftRef( completePath.indexOf(QString("/{")))) ) {
QRegularExpression parExpr( R"(\{([^\/\\s]+)\})" );
completePath.replace( parExpr, R"((?<\1>[^\/\s]+))" );
completePath.append("[\\/]?$");
QRegularExpression pathExpr( completePath );
QRegularExpressionMatch match = pathExpr.match( reqPath );
QRegularExpressionMatch match = getRequestMatch( completePath, reqPath );
if ( match.hasMatch() ){
QString petId = match.captured(QString("petId").toLower());
auto reqObj = new OAIPetApiRequest(socket, OAIPetApiApiHandler);
Expand All @@ -181,11 +165,7 @@ bool OAIApiRouter::handleRequestAndExtractPathParam(QHttpEngine::Socket *socket)
{
auto completePath = QString("%1 %2").arg("DELETE").arg("/v2/store/order/{orderId}").toLower();
if ( reqPath.startsWith(completePath.leftRef( completePath.indexOf(QString("/{")))) ) {
QRegularExpression parExpr( R"(\{([^\/\\s]+)\})" );
completePath.replace( parExpr, R"((?<\1>[^\/\s]+))" );
completePath.append("[\\/]?$");
QRegularExpression pathExpr( completePath );
QRegularExpressionMatch match = pathExpr.match( reqPath );
QRegularExpressionMatch match = getRequestMatch( completePath, reqPath );
if ( match.hasMatch() ){
QString orderId = match.captured(QString("orderId").toLower());
auto reqObj = new OAIStoreApiRequest(socket, OAIStoreApiApiHandler);
Expand All @@ -197,11 +177,7 @@ bool OAIApiRouter::handleRequestAndExtractPathParam(QHttpEngine::Socket *socket)
{
auto completePath = QString("%1 %2").arg("GET").arg("/v2/store/order/{orderId}").toLower();
if ( reqPath.startsWith(completePath.leftRef( completePath.indexOf(QString("/{")))) ) {
QRegularExpression parExpr( R"(\{([^\/\\s]+)\})" );
completePath.replace( parExpr, R"((?<\1>[^\/\s]+))" );
completePath.append("[\\/]?$");
QRegularExpression pathExpr( completePath );
QRegularExpressionMatch match = pathExpr.match( reqPath );
QRegularExpressionMatch match = getRequestMatch( completePath, reqPath );
if ( match.hasMatch() ){
QString orderId = match.captured(QString("orderId").toLower());
auto reqObj = new OAIStoreApiRequest(socket, OAIStoreApiApiHandler);
Expand All @@ -213,11 +189,7 @@ bool OAIApiRouter::handleRequestAndExtractPathParam(QHttpEngine::Socket *socket)
{
auto completePath = QString("%1 %2").arg("DELETE").arg("/v2/user/{username}").toLower();
if ( reqPath.startsWith(completePath.leftRef( completePath.indexOf(QString("/{")))) ) {
QRegularExpression parExpr( R"(\{([^\/\\s]+)\})" );
completePath.replace( parExpr, R"((?<\1>[^\/\s]+))" );
completePath.append("[\\/]?$");
QRegularExpression pathExpr( completePath );
QRegularExpressionMatch match = pathExpr.match( reqPath );
QRegularExpressionMatch match = getRequestMatch( completePath, reqPath );
if ( match.hasMatch() ){
QString username = match.captured(QString("username").toLower());
auto reqObj = new OAIUserApiRequest(socket, OAIUserApiApiHandler);
Expand All @@ -229,11 +201,7 @@ bool OAIApiRouter::handleRequestAndExtractPathParam(QHttpEngine::Socket *socket)
{
auto completePath = QString("%1 %2").arg("GET").arg("/v2/user/{username}").toLower();
if ( reqPath.startsWith(completePath.leftRef( completePath.indexOf(QString("/{")))) ) {
QRegularExpression parExpr( R"(\{([^\/\\s]+)\})" );
completePath.replace( parExpr, R"((?<\1>[^\/\s]+))" );
completePath.append("[\\/]?$");
QRegularExpression pathExpr( completePath );
QRegularExpressionMatch match = pathExpr.match( reqPath );
QRegularExpressionMatch match = getRequestMatch( completePath, reqPath );
if ( match.hasMatch() ){
QString username = match.captured(QString("username").toLower());
auto reqObj = new OAIUserApiRequest(socket, OAIUserApiApiHandler);
Expand All @@ -245,11 +213,7 @@ bool OAIApiRouter::handleRequestAndExtractPathParam(QHttpEngine::Socket *socket)
{
auto completePath = QString("%1 %2").arg("PUT").arg("/v2/user/{username}").toLower();
if ( reqPath.startsWith(completePath.leftRef( completePath.indexOf(QString("/{")))) ) {
QRegularExpression parExpr( R"(\{([^\/\\s]+)\})" );
completePath.replace( parExpr, R"((?<\1>[^\/\s]+))" );
completePath.append("[\\/]?$");
QRegularExpression pathExpr( completePath );
QRegularExpressionMatch match = pathExpr.match( reqPath );
QRegularExpressionMatch match = getRequestMatch( completePath, reqPath );
if ( match.hasMatch() ){
QString username = match.captured(QString("username").toLower());
auto reqObj = new OAIUserApiRequest(socket, OAIUserApiApiHandler);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <QSharedPointer>
#include <QList>
#include <QMultiMap>
#include <QRegularExpression>

#include <qhttpengine/socket.h>
#include <qhttpengine/handler.h>
Expand Down Expand Up @@ -89,6 +90,14 @@ private :
}
return QStringLiteral("");
}

inline QRegularExpressionMatch getRequestMatch(QString serverTemplatePath, QString requestPath){
QRegularExpression parExpr( R"(\{([^\/\\s]+)\})" );
serverTemplatePath.replace( parExpr, R"((?<\1>[^\/\s]+))" );
serverTemplatePath.append("[\\/]?$");
QRegularExpression pathExpr( serverTemplatePath );
return pathExpr.match( requestPath );
}

};

Expand Down

0 comments on commit 32f4cb5

Please sign in to comment.