Skip to content

Commit 21a291f

Browse files
MartinDelilleetherealjoy
authored andcommitted
cpp-qt5-client: remove host since it is not well handled (#4429)
* fixup! Simpler timeout with QTimer::singleShot (#4430) * cpp-qt5-client: remove host since it is not well handled * Move disconnect again * cpp-qt5-client: handle scheme/host/port properly * Fix port change try
1 parent 9ceabc7 commit 21a291f

15 files changed

Lines changed: 277 additions & 205 deletions

File tree

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,12 +300,17 @@ public void preprocessOpenAPI(OpenAPI openAPI) {
300300
URL url = URLPathUtils.getServerURL(openAPI, serverVariableOverrides());
301301
String port = URLPathUtils.getPort(url, "");
302302
String host = url.getHost();
303+
String scheme = url.getProtocol();
304+
303305
if(!port.isEmpty()) {
304306
this.additionalProperties.put("serverPort", port);
305307
}
306308
if(!host.isEmpty()) {
307309
this.additionalProperties.put("serverHost", host);
308310
}
311+
if(!scheme.isEmpty()) {
312+
this.additionalProperties.put("scheme", scheme);
313+
}
309314
}
310315

311316
@Override

modules/openapi-generator/src/main/resources/cpp-qt5-client/api-body.mustache

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,49 +9,55 @@
99
namespace {{this}} {
1010
{{/cppNamespaceDeclarations}}
1111

12-
{{classname}}::{{classname}}() : basePath("{{{basePathWithoutHost}}}"),
13-
host("{{#serverHost}}{{#scheme}}{{scheme}}://{{/scheme}}{{serverHost}}{{#serverPort}}:{{serverPort}}{{/serverPort}}{{/serverHost}}"),
14-
timeout(0){
15-
12+
{{classname}}::{{classname}}(const QString &scheme, const QString &host, int port, const QString& basePath, const int timeOut) :
13+
_scheme(scheme),
14+
_host(host),
15+
_port(port),
16+
_basePath(basePath),
17+
_timeOut(timeOut) {
1618
}
1719

1820
{{classname}}::~{{classname}}() {
21+
}
1922

23+
void {{classname}}::setScheme(const QString& scheme){
24+
_scheme = scheme;
2025
}
2126

22-
{{classname}}::{{classname}}(const QString& host, const QString& basePath, const int tout) {
23-
this->host = host;
24-
this->basePath = basePath;
25-
this->timeout = tout;
27+
void {{classname}}::setHost(const QString& host){
28+
_host = host;
2629
}
2730

28-
void {{classname}}::setBasePath(const QString& basePath){
29-
this->basePath = basePath;
31+
void {{classname}}::setPort(int port){
32+
_port = port;
3033
}
3134

32-
void {{classname}}::setHost(const QString& host){
33-
this->host = host;
35+
void {{classname}}::setBasePath(const QString& basePath){
36+
_basePath = basePath;
3437
}
3538

36-
void {{classname}}::setApiTimeOutMs(const int tout){
37-
timeout = tout;
39+
void {{classname}}::setTimeOut(const int timeOut){
40+
_timeOut = timeOut;
3841
}
3942

4043
void {{classname}}::setWorkingDirectory(const QString& path){
41-
workingDirectory = path;
44+
_workingDirectory = path;
4245
}
4346

4447
void {{classname}}::addHeaders(const QString& key, const QString& value){
4548
defaultHeaders.insert(key, value);
4649
}
4750

48-
4951
{{#operations}}
5052
{{#operation}}
5153
void
5254
{{classname}}::{{nickname}}({{#allParams}}const {{{dataType}}}& {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) {
53-
QString fullPath;
54-
fullPath.append(this->host).append(this->basePath).append("{{{path}}}");
55+
QString fullPath = QString("%0://%1%2%3%4")
56+
.arg(_scheme)
57+
.arg(_host)
58+
.arg(_port ? ":" + QString::number(_port) : "")
59+
.arg(_basePath)
60+
.arg("{{{path}}}");
5561
{{#pathParams}}
5662
QString {{paramName}}PathParam("{");
5763
{{paramName}}PathParam.append("{{baseName}}").append("}");
@@ -107,8 +113,8 @@ void
107113
}
108114
{{/collectionFormat}}{{/queryParams}}
109115
{{prefix}}HttpRequestWorker *worker = new {{prefix}}HttpRequestWorker(this);
110-
worker->setTimeOut(timeout);
111-
worker->setWorkingDirectory(workingDirectory);
116+
worker->setTimeOut(_timeOut);
117+
worker->setWorkingDirectory(_workingDirectory);
112118
{{prefix}}HttpRequestInput input(fullPath, "{{httpMethod}}");
113119
{{#formParams}}{{^isFile}}
114120
input.add_var("{{baseName}}", ::{{cppNamespace}}::toStringValue({{paramName}}));{{/isFile}}{{#isFile}}

modules/openapi-generator/src/main/resources/cpp-qt5-client/api-header.mustache

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,23 @@ class {{classname}}: public QObject {
1717
Q_OBJECT
1818
1919
public:
20-
{{classname}}();
21-
{{classname}}(const QString& host, const QString& basePath, const int toutMs = 0);
20+
{{classname}}(const QString &scheme = "{{scheme}}", const QString &host = "{{serverHost}}", int port = {{#serverPort}}{{serverPort}}{{/serverPort}}{{^serverPort}}0{{/serverPort}}, const QString& basePath = "{{basePathWithoutHost}}", const int timeOut = 0);
2221
~{{classname}}();
2322

23+
void setScheme(const QString &scheme);
24+
void setHost(const QString &host);
25+
void setPort(int port);
2426
void setBasePath(const QString& basePath);
25-
void setHost(const QString& host);
26-
void setApiTimeOutMs(const int tout);
27+
void setTimeOut(const int timeOut);
2728
void setWorkingDirectory(const QString& path);
2829
void addHeaders(const QString& key, const QString& value);
2930

3031
{{#operations}}{{#operation}}void {{nickname}}({{#allParams}}const {{{dataType}}}& {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}});
3132
{{/operation}}{{/operations}}
3233
private:
33-
QString basePath;
34-
QString host;
35-
QString workingDirectory;
36-
int timeout;
34+
QString _scheme, _host, _basePath;
35+
int _port, _timeOut;
36+
QString _workingDirectory;
3737
QMap<QString, QString> defaultHeaders;
3838
{{#operations}}{{#operation}}void {{nickname}}Callback ({{prefix}}HttpRequestWorker * worker);
3939
{{/operation}}{{/operations}}

samples/client/petstore/cpp-qt5/PetStore/PetApiTests.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ PFXPet PetApiTests::createRandomPet() {
1414

1515
void PetApiTests::findPetsByStatusTest() {
1616
PFXPetApi api;
17-
api.setHost(PetStoreHost);
1817
QEventLoop loop;
1918
bool petFound = false;
2019

@@ -34,7 +33,6 @@ void PetApiTests::findPetsByStatusTest() {
3433

3534
void PetApiTests::createAndGetPetTest() {
3635
PFXPetApi api;
37-
api.setHost(PetStoreHost);
3836
QEventLoop loop;
3937
bool petCreated = false;
4038

@@ -65,12 +63,10 @@ void PetApiTests::createAndGetPetTest() {
6563
QTimer::singleShot(14000, &loop, &QEventLoop::quit);
6664
loop.exec();
6765
QVERIFY2(petFetched, "didn't finish within timeout");
68-
6966
}
7067

7168
void PetApiTests::updatePetTest() {
7269
PFXPetApi api;
73-
api.setHost(PetStoreHost);
7470

7571
PFXPet pet = createRandomPet();
7672
PFXPet petToCheck;
@@ -134,7 +130,6 @@ void PetApiTests::updatePetTest() {
134130

135131
void PetApiTests::updatePetWithFormTest() {
136132
PFXPetApi api;
137-
api.setHost(PetStoreHost);
138133

139134
PFXPet pet = createRandomPet();
140135
PFXPet petToCheck;

samples/client/petstore/cpp-qt5/PetStore/PetApiTests.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,4 @@ private slots:
1414
void createAndGetPetTest();
1515
void updatePetTest();
1616
void updatePetWithFormTest();
17-
private:
18-
const QString PetStoreHost = QStringLiteral("http://petstore.swagger.io");
1917
};

samples/client/petstore/cpp-qt5/PetStore/StoreApiTests.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
void StoreApiTests::placeOrderTest() {
88
PFXStoreApi api;
9-
api.setHost(PetStoreHost);
109
QEventLoop loop;
1110
bool orderPlaced = false;
1211

@@ -33,12 +32,10 @@ void StoreApiTests::placeOrderTest() {
3332
QTimer::singleShot(14000, &loop, &QEventLoop::quit);
3433
loop.exec();
3534
QVERIFY2(orderPlaced, "didn't finish within timeout");
36-
3735
}
3836

3937
void StoreApiTests::getOrderByIdTest() {
4038
PFXStoreApi api;
41-
api.setHost(PetStoreHost);
4239
QEventLoop loop;
4340
bool orderFetched = false;
4441

@@ -54,12 +51,10 @@ void StoreApiTests::getOrderByIdTest() {
5451
QTimer::singleShot(14000, &loop, &QEventLoop::quit);
5552
loop.exec();
5653
QVERIFY2(orderFetched, "didn't finish within timeout");
57-
5854
}
5955

6056
void StoreApiTests::getInventoryTest() {
6157
PFXStoreApi api;
62-
api.setHost(PetStoreHost);
6358
QEventLoop loop;
6459
bool inventoryFetched = false;
6560

@@ -75,5 +70,4 @@ void StoreApiTests::getInventoryTest() {
7570
QTimer::singleShot(14000, &loop, &QEventLoop::quit);
7671
loop.exec();
7772
QVERIFY2(inventoryFetched, "didn't finish within timeout");
78-
7973
}

samples/client/petstore/cpp-qt5/PetStore/StoreApiTests.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,4 @@ private slots:
1111
void placeOrderTest();
1212
void getOrderByIdTest();
1313
void getInventoryTest();
14-
private:
15-
const QString PetStoreHost = QStringLiteral("http://petstore.swagger.io");
1614
};

samples/client/petstore/cpp-qt5/PetStore/UserApiTests.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ PFXUser UserApiTests::createRandomUser() {
1919

2020
void UserApiTests::createUserTest(){
2121
PFXUserApi api;
22-
api.setHost(PetStoreHost);
2322
QEventLoop loop;
2423
bool userCreated = false;
2524

@@ -36,7 +35,6 @@ void UserApiTests::createUserTest(){
3635

3736
void UserApiTests::createUsersWithArrayInputTest(){
3837
PFXUserApi api;
39-
api.setHost(PetStoreHost);
4038
QEventLoop loop;
4139
bool usersCreated = false;
4240

@@ -57,7 +55,6 @@ void UserApiTests::createUsersWithArrayInputTest(){
5755

5856
void UserApiTests::createUsersWithListInputTest(){
5957
PFXUserApi api;
60-
api.setHost(PetStoreHost);
6158
QEventLoop loop;
6259
bool usersCreated = false;
6360

@@ -82,7 +79,6 @@ void UserApiTests::createUsersWithListInputTest(){
8279

8380
void UserApiTests::deleteUserTest(){
8481
PFXUserApi api;
85-
api.setHost(PetStoreHost);
8682
QEventLoop loop;
8783
bool userDeleted = false;
8884

@@ -99,7 +95,6 @@ void UserApiTests::deleteUserTest(){
9995

10096
void UserApiTests::getUserByNameTest(){
10197
PFXUserApi api;
102-
api.setHost(PetStoreHost);
10398
QEventLoop loop;
10499
bool userFetched = false;
105100

@@ -118,7 +113,6 @@ void UserApiTests::getUserByNameTest(){
118113

119114
void UserApiTests::loginUserTest(){
120115
PFXUserApi api;
121-
api.setHost(PetStoreHost);
122116
QEventLoop loop;
123117
bool userLogged = false;
124118

@@ -136,7 +130,6 @@ void UserApiTests::loginUserTest(){
136130

137131
void UserApiTests::logoutUserTest(){
138132
PFXUserApi api;
139-
api.setHost(PetStoreHost);
140133
QEventLoop loop;
141134
bool userLoggedOut = false;
142135

@@ -153,7 +146,6 @@ void UserApiTests::logoutUserTest(){
153146

154147
void UserApiTests::updateUserTest(){
155148
PFXUserApi api;
156-
api.setHost(PetStoreHost);
157149
QEventLoop loop;
158150
bool userUpdated = false;
159151

samples/client/petstore/cpp-qt5/PetStore/UserApiTests.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,4 @@ private slots:
1818
void loginUserTest();
1919
void logoutUserTest();
2020
void updateUserTest();
21-
private:
22-
const QString PetStoreHost = QStringLiteral("http://petstore.swagger.io");
2321
};

0 commit comments

Comments
 (0)