Skip to content

Commit a2c27ba

Browse files
etherealjoywing328
authored andcommitted
[cpp-qt5] Remove std::shared_ptr from Qt5 (#267)
* Remove std::shared_ptr from Qt5 Updated Petstore Update Object Template to be able to store the json body * Remove tabs
1 parent f570821 commit a2c27ba

File tree

11 files changed

+119
-54
lines changed

11 files changed

+119
-54
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,4 +442,9 @@ public String escapeUnsafeCharacters(String input) {
442442
public void setOptionalProjectFileFlag(boolean flag) {
443443
this.optionalProjectFileFlag = flag;
444444
}
445+
446+
@Override
447+
public String getTypeDeclaration(String str) {
448+
return str;
449+
}
445450
}

modules/openapi-generator/src/main/resources/qt5cpp/object.mustache

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#define _{{prefix}}_OBJECT_H_
44

55
#include <QJsonObject>
6+
#include <QJsonDocument>
67

78
{{#cppNamespaceDeclarations}}
89
namespace {{this}} {
@@ -11,22 +12,51 @@ namespace {{this}} {
1112
class {{prefix}}Object {
1213
public:
1314
virtual QJsonObject asJsonObject() {
14-
return QJsonObject();
15+
if(jObj != nullptr){
16+
return *jObj;
17+
}
18+
return QJsonObject();
1519
}
16-
virtual ~{{prefix}}Object() {}
20+
21+
{{prefix}}Object() {
22+
jObj = nullptr;
23+
}
24+
25+
virtual ~{{prefix}}Object() {
26+
if(jObj != nullptr){
27+
delete jObj;
28+
}
29+
}
30+
1731
virtual {{prefix}}Object* fromJson(QString jsonString) {
18-
Q_UNUSED(jsonString);
19-
return new {{prefix}}Object();
32+
QJsonDocument doc = QJsonDocument::fromJson(jsonString.toUtf8());
33+
auto ret = new {{prefix}}Object();
34+
ret->fromJsonObject(doc.object());
35+
return ret;
2036
}
37+
2138
virtual void fromJsonObject(QJsonObject json) {
22-
Q_UNUSED(json);
39+
if(jObj != nullptr)
40+
{
41+
delete jObj;
42+
}
43+
jObj = new QJsonObject(json);
2344
}
45+
2446
virtual QString asJson() {
25-
return QString("");
47+
if(jObj != nullptr)
48+
{
49+
QJsonDocument doc(*jObj);
50+
return doc.toJson(QJsonDocument::Compact);
51+
}
52+
return QString();
2653
}
54+
2755
virtual bool isSet() {
2856
return false;
2957
}
58+
private :
59+
QJsonObject *jObj;
3060
};
3161

3262
{{#cppNamespaceDeclarations}}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3.0.0-SNAPSHOT
1+
3.0.1-SNAPSHOT

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

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ PetApiTests::~PetApiTests () {
1111
exit(1);
1212
}
1313

14-
SWGPetApi* PetApiTests::getApi() {
15-
SWGPetApi* api = new SWGPetApi();
14+
OAIPetApi* PetApiTests::getApi() {
15+
OAIPetApi* api = new OAIPetApi();
1616
api->host = "http://petstore.swagger.io";
1717
api->basePath = "/v2";
1818
return api;
1919
}
2020

21-
SWGPet* PetApiTests::createRandomPet() {
22-
SWGPet* pet = new SWGPet();
21+
OAIPet* PetApiTests::createRandomPet() {
22+
OAIPet* pet = new OAIPet();
2323
qint64 id = QDateTime::currentMSecsSinceEpoch();
2424

2525
pet->setName(new QString("monster"));
@@ -36,21 +36,21 @@ void PetApiTests::runTests() {
3636
}
3737

3838
void PetApiTests::findPetsByStatusTest() {
39-
SWGPetApi* api = getApi();
39+
OAIPetApi* api = getApi();
4040

4141
static QEventLoop loop;
4242
QTimer timer;
4343
timer.setInterval(14000);
4444
timer.setSingleShot(true);
4545

46-
auto validator = [](QList<SWGPet*>* pets) {
47-
foreach(SWGPet* pet, *pets) {
46+
auto validator = [](QList<OAIPet*>* pets) {
47+
foreach(OAIPet* pet, *pets) {
4848
QVERIFY(pet->getStatus()->startsWith("available") || pet->getStatus()->startsWith("sold"));
4949
}
5050
loop.quit();
5151
};
5252

53-
connect(api, &SWGPetApi::findPetsByStatusSignal, this, validator);
53+
connect(api, &OAIPetApi::findPetsByStatusSignal, this, validator);
5454
connect(&timer, &QTimer::timeout, &loop, &QEventLoop::quit);
5555

5656
QList<QString*>* status = new QList<QString*>();
@@ -64,7 +64,7 @@ void PetApiTests::findPetsByStatusTest() {
6464
}
6565

6666
void PetApiTests::createAndGetPetTest() {
67-
SWGPetApi* api = getApi();
67+
OAIPetApi* api = getApi();
6868

6969
static QEventLoop loop;
7070
QTimer timer;
@@ -76,10 +76,10 @@ void PetApiTests::createAndGetPetTest() {
7676
loop.quit();
7777
};
7878

79-
connect(api, &SWGPetApi::addPetSignal, this, validator);
79+
connect(api, &OAIPetApi::addPetSignal, this, validator);
8080
connect(&timer, &QTimer::timeout, &loop, &QEventLoop::quit);
8181

82-
SWGPet* pet = createRandomPet();
82+
OAIPet* pet = createRandomPet();
8383
qint64 id = pet->getId();
8484

8585
api->addPet(*pet);
@@ -90,13 +90,13 @@ void PetApiTests::createAndGetPetTest() {
9090
timer.setInterval(1000);
9191
timer.setSingleShot(true);
9292

93-
auto getPetValidator = [](SWGPet* pet) {
93+
auto getPetValidator = [](OAIPet* pet) {
9494
QVERIFY(pet->getId() > 0);
9595
QVERIFY(pet->getStatus()->compare("freaky") == 0);
9696
loop.quit();
9797
};
9898

99-
connect(api, &SWGPetApi::getPetByIdSignal, this, getPetValidator);
99+
connect(api, &OAIPetApi::getPetByIdSignal, this, getPetValidator);
100100
connect(&timer, &QTimer::timeout, &loop, &QEventLoop::quit);
101101

102102
api->getPetById(id);
@@ -107,10 +107,10 @@ void PetApiTests::createAndGetPetTest() {
107107
}
108108

109109
void PetApiTests::updatePetTest() {
110-
static SWGPetApi* api = getApi();
110+
static OAIPetApi* api = getApi();
111111

112-
SWGPet* pet = createRandomPet();
113-
static SWGPet* petToCheck;
112+
OAIPet* pet = createRandomPet();
113+
static OAIPet* petToCheck;
114114
qint64 id = pet->getId();
115115
static QEventLoop loop;
116116
QTimer timer;
@@ -121,7 +121,7 @@ void PetApiTests::updatePetTest() {
121121
loop.quit();
122122
};
123123

124-
connect(api, &SWGPetApi::addPetSignal, this, validator);
124+
connect(api, &OAIPetApi::addPetSignal, this, validator);
125125
connect(&timer, &QTimer::timeout, &loop, &QEventLoop::quit);
126126

127127
// create pet
@@ -134,11 +134,11 @@ void PetApiTests::updatePetTest() {
134134
timer.setInterval(1000);
135135
timer.setSingleShot(true);
136136

137-
auto fetchPet = [](SWGPet* pet) {
137+
auto fetchPet = [](OAIPet* pet) {
138138
petToCheck = pet;
139139
loop.quit();
140140
};
141-
connect(api, &SWGPetApi::getPetByIdSignal, this, fetchPet);
141+
connect(api, &OAIPetApi::getPetByIdSignal, this, fetchPet);
142142
connect(&timer, &QTimer::timeout, &loop, &QEventLoop::quit);
143143

144144
// create pet
@@ -154,7 +154,7 @@ void PetApiTests::updatePetTest() {
154154
loop.quit();
155155
};
156156

157-
connect(api, &SWGPetApi::updatePetSignal, this, updatePetTest);
157+
connect(api, &OAIPetApi::updatePetSignal, this, updatePetTest);
158158
connect(&timer, &QTimer::timeout, &loop, &QEventLoop::quit);
159159

160160
// update pet
@@ -168,12 +168,12 @@ void PetApiTests::updatePetTest() {
168168
timer.setInterval(1000);
169169
timer.setSingleShot(true);
170170

171-
auto fetchPet2 = [](SWGPet* pet) {
171+
auto fetchPet2 = [](OAIPet* pet) {
172172
QVERIFY(pet->getId() == petToCheck->getId());
173173
QVERIFY(pet->getStatus()->compare(petToCheck->getStatus()) == 0);
174174
loop.quit();
175175
};
176-
connect(api, &SWGPetApi::getPetByIdSignal, this, fetchPet2);
176+
connect(api, &OAIPetApi::getPetByIdSignal, this, fetchPet2);
177177
connect(&timer, &QTimer::timeout, &loop, &QEventLoop::quit);
178178
api->getPetById(id);
179179
timer.start();
@@ -182,10 +182,10 @@ void PetApiTests::updatePetTest() {
182182
}
183183

184184
void PetApiTests::updatePetWithFormTest() {
185-
static SWGPetApi* api = getApi();
185+
static OAIPetApi* api = getApi();
186186

187-
SWGPet* pet = createRandomPet();
188-
SWGPet* petToCheck;
187+
OAIPet* pet = createRandomPet();
188+
OAIPet* petToCheck;
189189
qint64 id = pet->getId();
190190
static QEventLoop loop;
191191
QTimer timer;
@@ -198,7 +198,7 @@ void PetApiTests::updatePetWithFormTest() {
198198
loop.quit();
199199
};
200200

201-
connect(api, &SWGPetApi::addPetSignal, this, validator);
201+
connect(api, &OAIPetApi::addPetSignal, this, validator);
202202
connect(&timer, &QTimer::timeout, &loop, &QEventLoop::quit);
203203
api->addPet(*pet);
204204
timer.start();
@@ -209,11 +209,11 @@ void PetApiTests::updatePetWithFormTest() {
209209
timer.setInterval(1000);
210210
timer.setSingleShot(true);
211211

212-
auto fetchPet = [&](SWGPet* pet) {
212+
auto fetchPet = [&](OAIPet* pet) {
213213
petToCheck = pet;
214214
loop.quit();
215215
};
216-
connect(api, &SWGPetApi::getPetByIdSignal, this, fetchPet);
216+
connect(api, &OAIPetApi::getPetByIdSignal, this, fetchPet);
217217
connect(&timer, &QTimer::timeout, &loop, &QEventLoop::quit);
218218

219219
api->getPetById(id);
@@ -225,7 +225,7 @@ void PetApiTests::updatePetWithFormTest() {
225225
timer.setInterval(1000);
226226
timer.setSingleShot(true);
227227

228-
connect(api, &SWGPetApi::updatePetWithFormSignal, this, [](){loop.quit();});
228+
connect(api, &OAIPetApi::updatePetWithFormSignal, this, [](){loop.quit();});
229229
connect(&timer, &QTimer::timeout, &loop, &QEventLoop::quit);
230230

231231
api->updatePetWithForm(id, new QString("gorilla"), NULL);
@@ -237,11 +237,11 @@ void PetApiTests::updatePetWithFormTest() {
237237
timer.setInterval(1000);
238238
timer.setSingleShot(true);
239239

240-
auto fetchUpdatedPet = [](SWGPet* pet) {
240+
auto fetchUpdatedPet = [](OAIPet* pet) {
241241
QVERIFY(pet->getName()->compare(QString("gorilla")) == 0);
242242
loop.quit();
243243
};
244-
connect(api, &SWGPetApi::getPetByIdSignal, this, fetchUpdatedPet);
244+
connect(api, &OAIPetApi::getPetByIdSignal, this, fetchUpdatedPet);
245245
connect(&timer, &QTimer::timeout, &loop, &QEventLoop::quit);
246246

247247
api->getPetById(id);

samples/client/petstore/cpp-qt5/client/OAIObject.h

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,58 @@
1414
#define _OAI_OBJECT_H_
1515

1616
#include <QJsonObject>
17+
#include <QJsonDocument>
1718

1819
namespace OpenAPI {
1920

2021
class OAIObject {
2122
public:
2223
virtual QJsonObject asJsonObject() {
23-
return QJsonObject();
24+
if(jObj != nullptr){
25+
return *jObj;
26+
}
27+
return QJsonObject();
2428
}
25-
virtual ~OAIObject() {}
29+
30+
OAIObject() {
31+
jObj = nullptr;
32+
}
33+
34+
virtual ~OAIObject() {
35+
if(jObj != nullptr){
36+
delete jObj;
37+
}
38+
}
39+
2640
virtual OAIObject* fromJson(QString jsonString) {
27-
Q_UNUSED(jsonString);
28-
return new OAIObject();
41+
QJsonDocument doc = QJsonDocument::fromJson(jsonString.toUtf8());
42+
auto ret = new OAIObject();
43+
ret->fromJsonObject(doc.object());
44+
return ret;
2945
}
46+
3047
virtual void fromJsonObject(QJsonObject json) {
31-
Q_UNUSED(json);
48+
if(jObj != nullptr)
49+
{
50+
delete jObj;
51+
}
52+
jObj = new QJsonObject(json);
3253
}
54+
3355
virtual QString asJson() {
34-
return QString("");
56+
if(jObj != nullptr)
57+
{
58+
QJsonDocument doc(*jObj);
59+
return doc.toJson(QJsonDocument::Compact);
60+
}
61+
return QString();
3562
}
63+
3664
virtual bool isSet() {
3765
return false;
3866
}
67+
private :
68+
QJsonObject *jObj;
3969
};
4070

4171
}

samples/client/petstore/cpp-qt5/client/OAIPetApi.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ OAIPetApi::OAIPetApi(QString host, QString basePath) {
3030
}
3131

3232
void
33-
OAIPetApi::addPet(std::shared_ptr<OAIOAIPet>& oai_pet) {
33+
OAIPetApi::addPet(OAIPet& oai_pet) {
3434
QString fullPath;
3535
fullPath.append(this->host).append(this->basePath).append("/pet");
3636

@@ -405,7 +405,7 @@ OAIPetApi::getPetByIdCallback(OAIHttpRequestWorker * worker) {
405405
}
406406

407407
void
408-
OAIPetApi::updatePet(std::shared_ptr<OAIOAIPet>& oai_pet) {
408+
OAIPetApi::updatePet(OAIPet& oai_pet) {
409409
QString fullPath;
410410
fullPath.append(this->host).append(this->basePath).append("/pet");
411411

samples/client/petstore/cpp-qt5/client/OAIPetApi.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ class OAIPetApi: public QObject {
3636
QString basePath;
3737
QMap<QString, QString> defaultHeaders;
3838

39-
void addPet(std::shared_ptr<OAIOAIPet>& oai_pet);
39+
void addPet(OAIPet& oai_pet);
4040
void deletePet(qint64 pet_id, QString* api_key);
4141
void findPetsByStatus(QList<QString*>* status);
4242
void findPetsByTags(QList<QString*>* tags);
4343
void getPetById(qint64 pet_id);
44-
void updatePet(std::shared_ptr<OAIOAIPet>& oai_pet);
44+
void updatePet(OAIPet& oai_pet);
4545
void updatePetWithForm(qint64 pet_id, QString* name, QString* status);
4646
void uploadFile(qint64 pet_id, QString* additional_metadata, OAIHttpRequestInputFileElement* file);
4747

samples/client/petstore/cpp-qt5/client/OAIStoreApi.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ OAIStoreApi::getOrderByIdCallback(OAIHttpRequestWorker * worker) {
196196
}
197197

198198
void
199-
OAIStoreApi::placeOrder(std::shared_ptr<OAIOAIOrder>& oai_order) {
199+
OAIStoreApi::placeOrder(OAIOrder& oai_order) {
200200
QString fullPath;
201201
fullPath.append(this->host).append(this->basePath).append("/store/order");
202202

0 commit comments

Comments
 (0)