From d5aa2859265b4103b5e3b95925f03f295586d730 Mon Sep 17 00:00:00 2001 From: wing328 Date: Sat, 30 Jan 2016 15:31:08 +0800 Subject: [PATCH] fix file support in form parameter (qt5cpp) --- .../main/resources/qt5cpp/api-body.mustache | 23 ++- .../client/petstore/qt5cpp/client/SWGPet.h | 4 +- .../petstore/qt5cpp/client/SWGPetApi.cpp | 143 +++++++++++++++--- .../client/petstore/qt5cpp/client/SWGPetApi.h | 6 + .../petstore/qt5cpp/client/SWGUserApi.cpp | 4 +- 5 files changed, 144 insertions(+), 36 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/qt5cpp/api-body.mustache b/modules/swagger-codegen/src/main/resources/qt5cpp/api-body.mustache index b634110418..fc69cfa440 100644 --- a/modules/swagger-codegen/src/main/resources/qt5cpp/api-body.mustache +++ b/modules/swagger-codegen/src/main/resources/qt5cpp/api-body.mustache @@ -29,7 +29,7 @@ void {{#queryParams}} {{^collectionFormat}} - if(fullPath.indexOf("?") > 0) + if (fullPath.indexOf("?") > 0) fullPath.append("&"); else fullPath.append("?"); @@ -40,10 +40,10 @@ void {{#collectionFormat}} - if({{{paramName}}}->size() > 0) { - if(QString("{{collectionFormat}}").indexOf("multi") == 0) { + if ({{{paramName}}}->size() > 0) { + if (QString("{{collectionFormat}}").indexOf("multi") == 0) { foreach({{{baseType}}} t, *{{paramName}}) { - if(fullPath.indexOf("?") > 0) + if (fullPath.indexOf("?") > 0) fullPath.append("&"); else fullPath.append("?"); @@ -51,28 +51,28 @@ void } } else if (QString("{{collectionFormat}}").indexOf("ssv") == 0) { - if(fullPath.indexOf("?") > 0) + if (fullPath.indexOf("?") > 0) fullPath.append("&"); else fullPath.append("?"); fullPath.append("{{paramName}}="); qint32 count = 0; foreach({{{baseType}}} t, *{{paramName}}) { - if(count > 0) { + if (count > 0) { fullPath.append(" "); } fullPath.append(stringValue(t)); } } else if (QString("{{collectionFormat}}").indexOf("tsv") == 0) { - if(fullPath.indexOf("?") > 0) + if (fullPath.indexOf("?") > 0) fullPath.append("&"); else fullPath.append("?"); fullPath.append("{{paramName}}="); qint32 count = 0; foreach({{{baseType}}} t, *{{paramName}}) { - if(count > 0) { + if (count > 0) { fullPath.append("\t"); } fullPath.append(stringValue(t)); @@ -86,11 +86,10 @@ void HttpRequestWorker *worker = new HttpRequestWorker(); HttpRequestInput input(fullPath, "{{httpMethod}}"); - {{#formParams}}{{^isFile}} - if({{paramName}} != NULL) { - input.add_var("{{paramName}}", *{{paramName}}); + {{#formParams}}if ({{paramName}} != NULL) { + {{^isFile}}input.add_var("{{paramName}}", *{{paramName}});{{/isFile}}{{#isFile}}input.add_file("{{paramName}}", *{{paramName}}.local_filename, *{{paramName}}.request_filename, *{{paramName}}.mime_type);{{/isFile}} } - {{/isFile}}{{/formParams}} + {{/formParams}} {{#bodyParams}} {{#isContainer}} diff --git a/samples/client/petstore/qt5cpp/client/SWGPet.h b/samples/client/petstore/qt5cpp/client/SWGPet.h index 3f9320f150..7b96ffc03f 100644 --- a/samples/client/petstore/qt5cpp/client/SWGPet.h +++ b/samples/client/petstore/qt5cpp/client/SWGPet.h @@ -10,10 +10,10 @@ #include -#include #include "SWGCategory.h" -#include #include "SWGTag.h" +#include +#include #include "SWGObject.h" diff --git a/samples/client/petstore/qt5cpp/client/SWGPetApi.cpp b/samples/client/petstore/qt5cpp/client/SWGPetApi.cpp index 2171b8ed50..a50c172d5b 100644 --- a/samples/client/petstore/qt5cpp/client/SWGPetApi.cpp +++ b/samples/client/petstore/qt5cpp/client/SWGPetApi.cpp @@ -123,10 +123,10 @@ SWGPetApi::findPetsByStatus(QList* status) { - if(status->size() > 0) { - if(QString("multi").indexOf("multi") == 0) { + if (status->size() > 0) { + if (QString("multi").indexOf("multi") == 0) { foreach(QString* t, *status) { - if(fullPath.indexOf("?") > 0) + if (fullPath.indexOf("?") > 0) fullPath.append("&"); else fullPath.append("?"); @@ -134,28 +134,28 @@ SWGPetApi::findPetsByStatus(QList* status) { } } else if (QString("multi").indexOf("ssv") == 0) { - if(fullPath.indexOf("?") > 0) + if (fullPath.indexOf("?") > 0) fullPath.append("&"); else fullPath.append("?"); fullPath.append("status="); qint32 count = 0; foreach(QString* t, *status) { - if(count > 0) { + if (count > 0) { fullPath.append(" "); } fullPath.append(stringValue(t)); } } else if (QString("multi").indexOf("tsv") == 0) { - if(fullPath.indexOf("?") > 0) + if (fullPath.indexOf("?") > 0) fullPath.append("&"); else fullPath.append("?"); fullPath.append("status="); qint32 count = 0; foreach(QString* t, *status) { - if(count > 0) { + if (count > 0) { fullPath.append("\t"); } fullPath.append(stringValue(t)); @@ -228,10 +228,10 @@ SWGPetApi::findPetsByTags(QList* tags) { - if(tags->size() > 0) { - if(QString("multi").indexOf("multi") == 0) { + if (tags->size() > 0) { + if (QString("multi").indexOf("multi") == 0) { foreach(QString* t, *tags) { - if(fullPath.indexOf("?") > 0) + if (fullPath.indexOf("?") > 0) fullPath.append("&"); else fullPath.append("?"); @@ -239,28 +239,28 @@ SWGPetApi::findPetsByTags(QList* tags) { } } else if (QString("multi").indexOf("ssv") == 0) { - if(fullPath.indexOf("?") > 0) + if (fullPath.indexOf("?") > 0) fullPath.append("&"); else fullPath.append("?"); fullPath.append("tags="); qint32 count = 0; foreach(QString* t, *tags) { - if(count > 0) { + if (count > 0) { fullPath.append(" "); } fullPath.append(stringValue(t)); } } else if (QString("multi").indexOf("tsv") == 0) { - if(fullPath.indexOf("?") > 0) + if (fullPath.indexOf("?") > 0) fullPath.append("&"); else fullPath.append("?"); fullPath.append("tags="); qint32 count = 0; foreach(QString* t, *tags) { - if(count > 0) { + if (count > 0) { fullPath.append("\t"); } fullPath.append(stringValue(t)); @@ -391,12 +391,10 @@ SWGPetApi::updatePetWithForm(QString* petId, QString* name, QString* status) { HttpRequestWorker *worker = new HttpRequestWorker(); HttpRequestInput input(fullPath, "POST"); - - if(name != NULL) { + if (name != NULL) { input.add_var("name", *name); } - - if(status != NULL) { + if (status != NULL) { input.add_var("status", *status); } @@ -493,10 +491,12 @@ SWGPetApi::uploadFile(qint64 petId, QString* additionalMetadata, SWGHttpRequestI HttpRequestWorker *worker = new HttpRequestWorker(); HttpRequestInput input(fullPath, "POST"); - - if(additionalMetadata != NULL) { + if (additionalMetadata != NULL) { input.add_var("additionalMetadata", *additionalMetadata); } + if (file != NULL) { + input.add_file("file", *file.local_filename, *file.request_filename, *file.mime_type); + } @@ -528,4 +528,107 @@ SWGPetApi::uploadFileCallback(HttpRequestWorker * worker) { emit uploadFileSignal(); } +void +SWGPetApi::getPetByIdWithByteArray(qint64 petId) { + QString fullPath; + fullPath.append(this->host).append(this->basePath).append("/pet/{petId}?testing_byte_array=true"); + + + QString petIdPathParam("{"); petIdPathParam.append("petId").append("}"); + fullPath.replace(petIdPathParam, stringValue(petId)); + + + + + HttpRequestWorker *worker = new HttpRequestWorker(); + HttpRequestInput input(fullPath, "GET"); + + + + + + + + connect(worker, + &HttpRequestWorker::on_execution_finished, + this, + &SWGPetApi::getPetByIdWithByteArrayCallback); + + worker->execute(&input); +} + +void +SWGPetApi::getPetByIdWithByteArrayCallback(HttpRequestWorker * worker) { + QString msg; + if (worker->error_type == QNetworkReply::NoError) { + msg = QString("Success! %1 bytes").arg(worker->response.length()); + } + else { + msg = "Error: " + worker->error_str; + } + + + + + + + QString json(worker->response); + QString* output = static_cast(create(json, QString("QString"))); + + + + + worker->deleteLater(); + + emit getPetByIdWithByteArraySignal(output); + +} +void +SWGPetApi::addPetUsingByteArray(QString* body) { + QString fullPath; + fullPath.append(this->host).append(this->basePath).append("/pet?testing_byte_array=true"); + + + + + + HttpRequestWorker *worker = new HttpRequestWorker(); + HttpRequestInput input(fullPath, "POST"); + + + + + + + QString output = body.asJson(); + input.request_body.append(output); + + + + + connect(worker, + &HttpRequestWorker::on_execution_finished, + this, + &SWGPetApi::addPetUsingByteArrayCallback); + + worker->execute(&input); +} + +void +SWGPetApi::addPetUsingByteArrayCallback(HttpRequestWorker * worker) { + QString msg; + if (worker->error_type == QNetworkReply::NoError) { + msg = QString("Success! %1 bytes").arg(worker->response.length()); + } + else { + msg = "Error: " + worker->error_str; + } + + + + worker->deleteLater(); + + + emit addPetUsingByteArraySignal(); +} } /* namespace Swagger */ diff --git a/samples/client/petstore/qt5cpp/client/SWGPetApi.h b/samples/client/petstore/qt5cpp/client/SWGPetApi.h index f74f8f1083..89055da3b3 100644 --- a/samples/client/petstore/qt5cpp/client/SWGPetApi.h +++ b/samples/client/petstore/qt5cpp/client/SWGPetApi.h @@ -30,6 +30,8 @@ public: void updatePetWithForm(QString* petId, QString* name, QString* status); void deletePet(qint64 petId, QString* apiKey); void uploadFile(qint64 petId, QString* additionalMetadata, SWGHttpRequestInputFileElement* file); + void getPetByIdWithByteArray(qint64 petId); + void addPetUsingByteArray(QString* body); private: void updatePetCallback (HttpRequestWorker * worker); @@ -40,6 +42,8 @@ private: void updatePetWithFormCallback (HttpRequestWorker * worker); void deletePetCallback (HttpRequestWorker * worker); void uploadFileCallback (HttpRequestWorker * worker); + void getPetByIdWithByteArrayCallback (HttpRequestWorker * worker); + void addPetUsingByteArrayCallback (HttpRequestWorker * worker); signals: void updatePetSignal(); @@ -50,6 +54,8 @@ signals: void updatePetWithFormSignal(); void deletePetSignal(); void uploadFileSignal(); + void getPetByIdWithByteArraySignal(QString* summary); + void addPetUsingByteArraySignal(); }; } diff --git a/samples/client/petstore/qt5cpp/client/SWGUserApi.cpp b/samples/client/petstore/qt5cpp/client/SWGUserApi.cpp index 6341231a88..61b92fc550 100644 --- a/samples/client/petstore/qt5cpp/client/SWGUserApi.cpp +++ b/samples/client/petstore/qt5cpp/client/SWGUserApi.cpp @@ -178,7 +178,7 @@ SWGUserApi::loginUser(QString* username, QString* password) { - if(fullPath.indexOf("?") > 0) + if (fullPath.indexOf("?") > 0) fullPath.append("&"); else fullPath.append("?"); @@ -190,7 +190,7 @@ SWGUserApi::loginUser(QString* username, QString* password) { - if(fullPath.indexOf("?") > 0) + if (fullPath.indexOf("?") > 0) fullPath.append("&"); else fullPath.append("?");