Skip to content

Commit 53e74a0

Browse files
committed
Merge pull request #2006 from wing328/qt5_file_support
[Qt5 C++] Fix file support in form parameter
2 parents 70cce74 + d5aa285 commit 53e74a0

File tree

5 files changed

+144
-36
lines changed

5 files changed

+144
-36
lines changed

modules/swagger-codegen/src/main/resources/qt5cpp/api-body.mustache

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ void
2929

3030
{{#queryParams}}
3131
{{^collectionFormat}}
32-
if(fullPath.indexOf("?") > 0)
32+
if (fullPath.indexOf("?") > 0)
3333
fullPath.append("&");
3434
else
3535
fullPath.append("?");
@@ -40,39 +40,39 @@ void
4040

4141
{{#collectionFormat}}
4242

43-
if({{{paramName}}}->size() > 0) {
44-
if(QString("{{collectionFormat}}").indexOf("multi") == 0) {
43+
if ({{{paramName}}}->size() > 0) {
44+
if (QString("{{collectionFormat}}").indexOf("multi") == 0) {
4545
foreach({{{baseType}}} t, *{{paramName}}) {
46-
if(fullPath.indexOf("?") > 0)
46+
if (fullPath.indexOf("?") > 0)
4747
fullPath.append("&");
4848
else
4949
fullPath.append("?");
5050
fullPath.append("{{{paramName}}}=").append(stringValue(t));
5151
}
5252
}
5353
else if (QString("{{collectionFormat}}").indexOf("ssv") == 0) {
54-
if(fullPath.indexOf("?") > 0)
54+
if (fullPath.indexOf("?") > 0)
5555
fullPath.append("&");
5656
else
5757
fullPath.append("?");
5858
fullPath.append("{{paramName}}=");
5959
qint32 count = 0;
6060
foreach({{{baseType}}} t, *{{paramName}}) {
61-
if(count > 0) {
61+
if (count > 0) {
6262
fullPath.append(" ");
6363
}
6464
fullPath.append(stringValue(t));
6565
}
6666
}
6767
else if (QString("{{collectionFormat}}").indexOf("tsv") == 0) {
68-
if(fullPath.indexOf("?") > 0)
68+
if (fullPath.indexOf("?") > 0)
6969
fullPath.append("&");
7070
else
7171
fullPath.append("?");
7272
fullPath.append("{{paramName}}=");
7373
qint32 count = 0;
7474
foreach({{{baseType}}} t, *{{paramName}}) {
75-
if(count > 0) {
75+
if (count > 0) {
7676
fullPath.append("\t");
7777
}
7878
fullPath.append(stringValue(t));
@@ -86,11 +86,10 @@ void
8686
HttpRequestWorker *worker = new HttpRequestWorker();
8787
HttpRequestInput input(fullPath, "{{httpMethod}}");
8888

89-
{{#formParams}}{{^isFile}}
90-
if({{paramName}} != NULL) {
91-
input.add_var("{{paramName}}", *{{paramName}});
89+
{{#formParams}}if ({{paramName}} != NULL) {
90+
{{^isFile}}input.add_var("{{paramName}}", *{{paramName}});{{/isFile}}{{#isFile}}input.add_file("{{paramName}}", *{{paramName}}.local_filename, *{{paramName}}.request_filename, *{{paramName}}.mime_type);{{/isFile}}
9291
}
93-
{{/isFile}}{{/formParams}}
92+
{{/formParams}}
9493

9594
{{#bodyParams}}
9695
{{#isContainer}}

samples/client/petstore/qt5cpp/client/SWGPet.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
#include <QJsonObject>
1111

1212

13-
#include <QString>
1413
#include "SWGCategory.h"
15-
#include <QList>
1614
#include "SWGTag.h"
15+
#include <QList>
16+
#include <QString>
1717

1818
#include "SWGObject.h"
1919

samples/client/petstore/qt5cpp/client/SWGPetApi.cpp

Lines changed: 123 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -123,39 +123,39 @@ SWGPetApi::findPetsByStatus(QList<QString*>* status) {
123123

124124

125125

126-
if(status->size() > 0) {
127-
if(QString("multi").indexOf("multi") == 0) {
126+
if (status->size() > 0) {
127+
if (QString("multi").indexOf("multi") == 0) {
128128
foreach(QString* t, *status) {
129-
if(fullPath.indexOf("?") > 0)
129+
if (fullPath.indexOf("?") > 0)
130130
fullPath.append("&");
131131
else
132132
fullPath.append("?");
133133
fullPath.append("status=").append(stringValue(t));
134134
}
135135
}
136136
else if (QString("multi").indexOf("ssv") == 0) {
137-
if(fullPath.indexOf("?") > 0)
137+
if (fullPath.indexOf("?") > 0)
138138
fullPath.append("&");
139139
else
140140
fullPath.append("?");
141141
fullPath.append("status=");
142142
qint32 count = 0;
143143
foreach(QString* t, *status) {
144-
if(count > 0) {
144+
if (count > 0) {
145145
fullPath.append(" ");
146146
}
147147
fullPath.append(stringValue(t));
148148
}
149149
}
150150
else if (QString("multi").indexOf("tsv") == 0) {
151-
if(fullPath.indexOf("?") > 0)
151+
if (fullPath.indexOf("?") > 0)
152152
fullPath.append("&");
153153
else
154154
fullPath.append("?");
155155
fullPath.append("status=");
156156
qint32 count = 0;
157157
foreach(QString* t, *status) {
158-
if(count > 0) {
158+
if (count > 0) {
159159
fullPath.append("\t");
160160
}
161161
fullPath.append(stringValue(t));
@@ -228,39 +228,39 @@ SWGPetApi::findPetsByTags(QList<QString*>* tags) {
228228

229229

230230

231-
if(tags->size() > 0) {
232-
if(QString("multi").indexOf("multi") == 0) {
231+
if (tags->size() > 0) {
232+
if (QString("multi").indexOf("multi") == 0) {
233233
foreach(QString* t, *tags) {
234-
if(fullPath.indexOf("?") > 0)
234+
if (fullPath.indexOf("?") > 0)
235235
fullPath.append("&");
236236
else
237237
fullPath.append("?");
238238
fullPath.append("tags=").append(stringValue(t));
239239
}
240240
}
241241
else if (QString("multi").indexOf("ssv") == 0) {
242-
if(fullPath.indexOf("?") > 0)
242+
if (fullPath.indexOf("?") > 0)
243243
fullPath.append("&");
244244
else
245245
fullPath.append("?");
246246
fullPath.append("tags=");
247247
qint32 count = 0;
248248
foreach(QString* t, *tags) {
249-
if(count > 0) {
249+
if (count > 0) {
250250
fullPath.append(" ");
251251
}
252252
fullPath.append(stringValue(t));
253253
}
254254
}
255255
else if (QString("multi").indexOf("tsv") == 0) {
256-
if(fullPath.indexOf("?") > 0)
256+
if (fullPath.indexOf("?") > 0)
257257
fullPath.append("&");
258258
else
259259
fullPath.append("?");
260260
fullPath.append("tags=");
261261
qint32 count = 0;
262262
foreach(QString* t, *tags) {
263-
if(count > 0) {
263+
if (count > 0) {
264264
fullPath.append("\t");
265265
}
266266
fullPath.append(stringValue(t));
@@ -391,12 +391,10 @@ SWGPetApi::updatePetWithForm(QString* petId, QString* name, QString* status) {
391391
HttpRequestWorker *worker = new HttpRequestWorker();
392392
HttpRequestInput input(fullPath, "POST");
393393

394-
395-
if(name != NULL) {
394+
if (name != NULL) {
396395
input.add_var("name", *name);
397396
}
398-
399-
if(status != NULL) {
397+
if (status != NULL) {
400398
input.add_var("status", *status);
401399
}
402400

@@ -493,10 +491,12 @@ SWGPetApi::uploadFile(qint64 petId, QString* additionalMetadata, SWGHttpRequestI
493491
HttpRequestWorker *worker = new HttpRequestWorker();
494492
HttpRequestInput input(fullPath, "POST");
495493

496-
497-
if(additionalMetadata != NULL) {
494+
if (additionalMetadata != NULL) {
498495
input.add_var("additionalMetadata", *additionalMetadata);
499496
}
497+
if (file != NULL) {
498+
input.add_file("file", *file.local_filename, *file.request_filename, *file.mime_type);
499+
}
500500

501501

502502

@@ -528,4 +528,107 @@ SWGPetApi::uploadFileCallback(HttpRequestWorker * worker) {
528528

529529
emit uploadFileSignal();
530530
}
531+
void
532+
SWGPetApi::getPetByIdWithByteArray(qint64 petId) {
533+
QString fullPath;
534+
fullPath.append(this->host).append(this->basePath).append("/pet/{petId}?testing_byte_array=true");
535+
536+
537+
QString petIdPathParam("{"); petIdPathParam.append("petId").append("}");
538+
fullPath.replace(petIdPathParam, stringValue(petId));
539+
540+
541+
542+
543+
HttpRequestWorker *worker = new HttpRequestWorker();
544+
HttpRequestInput input(fullPath, "GET");
545+
546+
547+
548+
549+
550+
551+
552+
connect(worker,
553+
&HttpRequestWorker::on_execution_finished,
554+
this,
555+
&SWGPetApi::getPetByIdWithByteArrayCallback);
556+
557+
worker->execute(&input);
558+
}
559+
560+
void
561+
SWGPetApi::getPetByIdWithByteArrayCallback(HttpRequestWorker * worker) {
562+
QString msg;
563+
if (worker->error_type == QNetworkReply::NoError) {
564+
msg = QString("Success! %1 bytes").arg(worker->response.length());
565+
}
566+
else {
567+
msg = "Error: " + worker->error_str;
568+
}
569+
570+
571+
572+
573+
574+
575+
QString json(worker->response);
576+
QString* output = static_cast<QString*>(create(json, QString("QString")));
577+
578+
579+
580+
581+
worker->deleteLater();
582+
583+
emit getPetByIdWithByteArraySignal(output);
584+
585+
}
586+
void
587+
SWGPetApi::addPetUsingByteArray(QString* body) {
588+
QString fullPath;
589+
fullPath.append(this->host).append(this->basePath).append("/pet?testing_byte_array=true");
590+
591+
592+
593+
594+
595+
HttpRequestWorker *worker = new HttpRequestWorker();
596+
HttpRequestInput input(fullPath, "POST");
597+
598+
599+
600+
601+
602+
603+
QString output = body.asJson();
604+
input.request_body.append(output);
605+
606+
607+
608+
609+
connect(worker,
610+
&HttpRequestWorker::on_execution_finished,
611+
this,
612+
&SWGPetApi::addPetUsingByteArrayCallback);
613+
614+
worker->execute(&input);
615+
}
616+
617+
void
618+
SWGPetApi::addPetUsingByteArrayCallback(HttpRequestWorker * worker) {
619+
QString msg;
620+
if (worker->error_type == QNetworkReply::NoError) {
621+
msg = QString("Success! %1 bytes").arg(worker->response.length());
622+
}
623+
else {
624+
msg = "Error: " + worker->error_str;
625+
}
626+
627+
628+
629+
worker->deleteLater();
630+
631+
632+
emit addPetUsingByteArraySignal();
633+
}
531634
} /* namespace Swagger */

samples/client/petstore/qt5cpp/client/SWGPetApi.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ class SWGPetApi: public QObject {
3030
void updatePetWithForm(QString* petId, QString* name, QString* status);
3131
void deletePet(qint64 petId, QString* apiKey);
3232
void uploadFile(qint64 petId, QString* additionalMetadata, SWGHttpRequestInputFileElement* file);
33+
void getPetByIdWithByteArray(qint64 petId);
34+
void addPetUsingByteArray(QString* body);
3335

3436
private:
3537
void updatePetCallback (HttpRequestWorker * worker);
@@ -40,6 +42,8 @@ class SWGPetApi: public QObject {
4042
void updatePetWithFormCallback (HttpRequestWorker * worker);
4143
void deletePetCallback (HttpRequestWorker * worker);
4244
void uploadFileCallback (HttpRequestWorker * worker);
45+
void getPetByIdWithByteArrayCallback (HttpRequestWorker * worker);
46+
void addPetUsingByteArrayCallback (HttpRequestWorker * worker);
4347

4448
signals:
4549
void updatePetSignal();
@@ -50,6 +54,8 @@ class SWGPetApi: public QObject {
5054
void updatePetWithFormSignal();
5155
void deletePetSignal();
5256
void uploadFileSignal();
57+
void getPetByIdWithByteArraySignal(QString* summary);
58+
void addPetUsingByteArraySignal();
5359

5460
};
5561
}

samples/client/petstore/qt5cpp/client/SWGUserApi.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ SWGUserApi::loginUser(QString* username, QString* password) {
178178

179179

180180

181-
if(fullPath.indexOf("?") > 0)
181+
if (fullPath.indexOf("?") > 0)
182182
fullPath.append("&");
183183
else
184184
fullPath.append("?");
@@ -190,7 +190,7 @@ SWGUserApi::loginUser(QString* username, QString* password) {
190190

191191

192192

193-
if(fullPath.indexOf("?") > 0)
193+
if (fullPath.indexOf("?") > 0)
194194
fullPath.append("&");
195195
else
196196
fullPath.append("?");

0 commit comments

Comments
 (0)