From 82f779e094330a53d4af7aa96a13fe1d277148fb Mon Sep 17 00:00:00 2001 From: etherealjoy Date: Sun, 10 Jun 2018 08:24:25 +0200 Subject: [PATCH 1/2] Remove std::shared_ptr from Qt5 Updated Petstore Update Object Template to be able to store the json body --- .../languages/CppQt5ClientCodegen.java | 5 ++ .../src/main/resources/qt5cpp/object.mustache | 42 +++++++++++-- .../cpp-qt5/.openapi-generator/VERSION | 2 +- .../petstore/cpp-qt5/PetStore/PetApiTests.cpp | 62 +++++++++---------- .../petstore/cpp-qt5/client/OAIObject.h | 42 +++++++++++-- .../petstore/cpp-qt5/client/OAIPetApi.cpp | 4 +- .../petstore/cpp-qt5/client/OAIPetApi.h | 4 +- .../petstore/cpp-qt5/client/OAIStoreApi.cpp | 2 +- .../petstore/cpp-qt5/client/OAIStoreApi.h | 2 +- .../petstore/cpp-qt5/client/OAIUserApi.cpp | 4 +- .../petstore/cpp-qt5/client/OAIUserApi.h | 4 +- 11 files changed, 119 insertions(+), 54 deletions(-) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CppQt5ClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CppQt5ClientCodegen.java index 9a45d0e8d101..46425fcdcbb7 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CppQt5ClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CppQt5ClientCodegen.java @@ -442,4 +442,9 @@ public String escapeUnsafeCharacters(String input) { public void setOptionalProjectFileFlag(boolean flag) { this.optionalProjectFileFlag = flag; } + + @Override + public String getTypeDeclaration(String str) { + return str; + } } diff --git a/modules/openapi-generator/src/main/resources/qt5cpp/object.mustache b/modules/openapi-generator/src/main/resources/qt5cpp/object.mustache index 87d3a828d0c7..9d6f17cab6c7 100644 --- a/modules/openapi-generator/src/main/resources/qt5cpp/object.mustache +++ b/modules/openapi-generator/src/main/resources/qt5cpp/object.mustache @@ -3,6 +3,7 @@ #define _{{prefix}}_OBJECT_H_ #include +#include {{#cppNamespaceDeclarations}} namespace {{this}} { @@ -11,22 +12,51 @@ namespace {{this}} { class {{prefix}}Object { public: virtual QJsonObject asJsonObject() { - return QJsonObject(); + if(jObj != nullptr){ + return *jObj; + } + return QJsonObject(); } - virtual ~{{prefix}}Object() {} + + {{prefix}}Object() { + jObj = nullptr; + } + + virtual ~{{prefix}}Object() { + if(jObj != nullptr){ + delete jObj; + } + } + virtual {{prefix}}Object* fromJson(QString jsonString) { - Q_UNUSED(jsonString); - return new {{prefix}}Object(); + QJsonDocument doc = QJsonDocument::fromJson(jsonString.toUtf8()); + auto ret = new {{prefix}}Object(); + ret->fromJsonObject(doc.object()); + return ret; } + virtual void fromJsonObject(QJsonObject json) { - Q_UNUSED(json); + if(jObj != nullptr) + { + delete jObj; + } + jObj = new QJsonObject(json); } + virtual QString asJson() { - return QString(""); + if(jObj != nullptr) + { + QJsonDocument doc(*jObj); + return doc.toJson(QJsonDocument::Compact); + } + return QString(); } + virtual bool isSet() { return false; } +private : + QJsonObject *jObj; }; {{#cppNamespaceDeclarations}} diff --git a/samples/client/petstore/cpp-qt5/.openapi-generator/VERSION b/samples/client/petstore/cpp-qt5/.openapi-generator/VERSION index 096bf47efe31..ad121e8340e0 100644 --- a/samples/client/petstore/cpp-qt5/.openapi-generator/VERSION +++ b/samples/client/petstore/cpp-qt5/.openapi-generator/VERSION @@ -1 +1 @@ -3.0.0-SNAPSHOT \ No newline at end of file +3.0.1-SNAPSHOT \ No newline at end of file diff --git a/samples/client/petstore/cpp-qt5/PetStore/PetApiTests.cpp b/samples/client/petstore/cpp-qt5/PetStore/PetApiTests.cpp index 252f15bcf54c..0933da296784 100644 --- a/samples/client/petstore/cpp-qt5/PetStore/PetApiTests.cpp +++ b/samples/client/petstore/cpp-qt5/PetStore/PetApiTests.cpp @@ -11,15 +11,15 @@ PetApiTests::~PetApiTests () { exit(1); } -SWGPetApi* PetApiTests::getApi() { - SWGPetApi* api = new SWGPetApi(); +OAIPetApi* PetApiTests::getApi() { + OAIPetApi* api = new OAIPetApi(); api->host = "http://petstore.swagger.io"; api->basePath = "/v2"; return api; } -SWGPet* PetApiTests::createRandomPet() { - SWGPet* pet = new SWGPet(); +OAIPet* PetApiTests::createRandomPet() { + OAIPet* pet = new OAIPet(); qint64 id = QDateTime::currentMSecsSinceEpoch(); pet->setName(new QString("monster")); @@ -36,21 +36,21 @@ void PetApiTests::runTests() { } void PetApiTests::findPetsByStatusTest() { - SWGPetApi* api = getApi(); + OAIPetApi* api = getApi(); static QEventLoop loop; QTimer timer; timer.setInterval(14000); timer.setSingleShot(true); - auto validator = [](QList* pets) { - foreach(SWGPet* pet, *pets) { + auto validator = [](QList* pets) { + foreach(OAIPet* pet, *pets) { QVERIFY(pet->getStatus()->startsWith("available") || pet->getStatus()->startsWith("sold")); } loop.quit(); }; - connect(api, &SWGPetApi::findPetsByStatusSignal, this, validator); + connect(api, &OAIPetApi::findPetsByStatusSignal, this, validator); connect(&timer, &QTimer::timeout, &loop, &QEventLoop::quit); QList* status = new QList(); @@ -64,7 +64,7 @@ void PetApiTests::findPetsByStatusTest() { } void PetApiTests::createAndGetPetTest() { - SWGPetApi* api = getApi(); + OAIPetApi* api = getApi(); static QEventLoop loop; QTimer timer; @@ -76,10 +76,10 @@ void PetApiTests::createAndGetPetTest() { loop.quit(); }; - connect(api, &SWGPetApi::addPetSignal, this, validator); + connect(api, &OAIPetApi::addPetSignal, this, validator); connect(&timer, &QTimer::timeout, &loop, &QEventLoop::quit); - SWGPet* pet = createRandomPet(); + OAIPet* pet = createRandomPet(); qint64 id = pet->getId(); api->addPet(*pet); @@ -90,13 +90,13 @@ void PetApiTests::createAndGetPetTest() { timer.setInterval(1000); timer.setSingleShot(true); - auto getPetValidator = [](SWGPet* pet) { + auto getPetValidator = [](OAIPet* pet) { QVERIFY(pet->getId() > 0); QVERIFY(pet->getStatus()->compare("freaky") == 0); loop.quit(); }; - connect(api, &SWGPetApi::getPetByIdSignal, this, getPetValidator); + connect(api, &OAIPetApi::getPetByIdSignal, this, getPetValidator); connect(&timer, &QTimer::timeout, &loop, &QEventLoop::quit); api->getPetById(id); @@ -107,10 +107,10 @@ void PetApiTests::createAndGetPetTest() { } void PetApiTests::updatePetTest() { - static SWGPetApi* api = getApi(); + static OAIPetApi* api = getApi(); - SWGPet* pet = createRandomPet(); - static SWGPet* petToCheck; + OAIPet* pet = createRandomPet(); + static OAIPet* petToCheck; qint64 id = pet->getId(); static QEventLoop loop; QTimer timer; @@ -121,7 +121,7 @@ void PetApiTests::updatePetTest() { loop.quit(); }; - connect(api, &SWGPetApi::addPetSignal, this, validator); + connect(api, &OAIPetApi::addPetSignal, this, validator); connect(&timer, &QTimer::timeout, &loop, &QEventLoop::quit); // create pet @@ -134,11 +134,11 @@ void PetApiTests::updatePetTest() { timer.setInterval(1000); timer.setSingleShot(true); - auto fetchPet = [](SWGPet* pet) { + auto fetchPet = [](OAIPet* pet) { petToCheck = pet; loop.quit(); }; - connect(api, &SWGPetApi::getPetByIdSignal, this, fetchPet); + connect(api, &OAIPetApi::getPetByIdSignal, this, fetchPet); connect(&timer, &QTimer::timeout, &loop, &QEventLoop::quit); // create pet @@ -154,7 +154,7 @@ void PetApiTests::updatePetTest() { loop.quit(); }; - connect(api, &SWGPetApi::updatePetSignal, this, updatePetTest); + connect(api, &OAIPetApi::updatePetSignal, this, updatePetTest); connect(&timer, &QTimer::timeout, &loop, &QEventLoop::quit); // update pet @@ -168,12 +168,12 @@ void PetApiTests::updatePetTest() { timer.setInterval(1000); timer.setSingleShot(true); - auto fetchPet2 = [](SWGPet* pet) { + auto fetchPet2 = [](OAIPet* pet) { QVERIFY(pet->getId() == petToCheck->getId()); QVERIFY(pet->getStatus()->compare(petToCheck->getStatus()) == 0); loop.quit(); }; - connect(api, &SWGPetApi::getPetByIdSignal, this, fetchPet2); + connect(api, &OAIPetApi::getPetByIdSignal, this, fetchPet2); connect(&timer, &QTimer::timeout, &loop, &QEventLoop::quit); api->getPetById(id); timer.start(); @@ -182,10 +182,10 @@ void PetApiTests::updatePetTest() { } void PetApiTests::updatePetWithFormTest() { - static SWGPetApi* api = getApi(); + static OAIPetApi* api = getApi(); - SWGPet* pet = createRandomPet(); - SWGPet* petToCheck; + OAIPet* pet = createRandomPet(); + OAIPet* petToCheck; qint64 id = pet->getId(); static QEventLoop loop; QTimer timer; @@ -198,7 +198,7 @@ void PetApiTests::updatePetWithFormTest() { loop.quit(); }; - connect(api, &SWGPetApi::addPetSignal, this, validator); + connect(api, &OAIPetApi::addPetSignal, this, validator); connect(&timer, &QTimer::timeout, &loop, &QEventLoop::quit); api->addPet(*pet); timer.start(); @@ -209,11 +209,11 @@ void PetApiTests::updatePetWithFormTest() { timer.setInterval(1000); timer.setSingleShot(true); - auto fetchPet = [&](SWGPet* pet) { + auto fetchPet = [&](OAIPet* pet) { petToCheck = pet; loop.quit(); }; - connect(api, &SWGPetApi::getPetByIdSignal, this, fetchPet); + connect(api, &OAIPetApi::getPetByIdSignal, this, fetchPet); connect(&timer, &QTimer::timeout, &loop, &QEventLoop::quit); api->getPetById(id); @@ -225,7 +225,7 @@ void PetApiTests::updatePetWithFormTest() { timer.setInterval(1000); timer.setSingleShot(true); - connect(api, &SWGPetApi::updatePetWithFormSignal, this, [](){loop.quit();}); + connect(api, &OAIPetApi::updatePetWithFormSignal, this, [](){loop.quit();}); connect(&timer, &QTimer::timeout, &loop, &QEventLoop::quit); api->updatePetWithForm(id, new QString("gorilla"), NULL); @@ -237,11 +237,11 @@ void PetApiTests::updatePetWithFormTest() { timer.setInterval(1000); timer.setSingleShot(true); - auto fetchUpdatedPet = [](SWGPet* pet) { + auto fetchUpdatedPet = [](OAIPet* pet) { QVERIFY(pet->getName()->compare(QString("gorilla")) == 0); loop.quit(); }; - connect(api, &SWGPetApi::getPetByIdSignal, this, fetchUpdatedPet); + connect(api, &OAIPetApi::getPetByIdSignal, this, fetchUpdatedPet); connect(&timer, &QTimer::timeout, &loop, &QEventLoop::quit); api->getPetById(id); diff --git a/samples/client/petstore/cpp-qt5/client/OAIObject.h b/samples/client/petstore/cpp-qt5/client/OAIObject.h index 434c8fd0bc3f..6ce563184c44 100644 --- a/samples/client/petstore/cpp-qt5/client/OAIObject.h +++ b/samples/client/petstore/cpp-qt5/client/OAIObject.h @@ -14,28 +14,58 @@ #define _OAI_OBJECT_H_ #include +#include namespace OpenAPI { class OAIObject { public: virtual QJsonObject asJsonObject() { - return QJsonObject(); + if(jObj != nullptr){ + return *jObj; + } + return QJsonObject(); } - virtual ~OAIObject() {} + + OAIObject() { + jObj = nullptr; + } + + virtual ~OAIObject() { + if(jObj != nullptr){ + delete jObj; + } + } + virtual OAIObject* fromJson(QString jsonString) { - Q_UNUSED(jsonString); - return new OAIObject(); + QJsonDocument doc = QJsonDocument::fromJson(jsonString.toUtf8()); + auto ret = new OAIObject(); + ret->fromJsonObject(doc.object()); + return ret; } + virtual void fromJsonObject(QJsonObject json) { - Q_UNUSED(json); + if(jObj != nullptr) + { + delete jObj; + } + jObj = new QJsonObject(json); } + virtual QString asJson() { - return QString(""); + if(jObj != nullptr) + { + QJsonDocument doc(*jObj); + return doc.toJson(QJsonDocument::Compact); + } + return QString(); } + virtual bool isSet() { return false; } +private : + QJsonObject *jObj; }; } diff --git a/samples/client/petstore/cpp-qt5/client/OAIPetApi.cpp b/samples/client/petstore/cpp-qt5/client/OAIPetApi.cpp index 7edfa0223ea3..e0be3d09bc91 100644 --- a/samples/client/petstore/cpp-qt5/client/OAIPetApi.cpp +++ b/samples/client/petstore/cpp-qt5/client/OAIPetApi.cpp @@ -30,7 +30,7 @@ OAIPetApi::OAIPetApi(QString host, QString basePath) { } void -OAIPetApi::addPet(std::shared_ptr& oai_pet) { +OAIPetApi::addPet(OAIPet& oai_pet) { QString fullPath; fullPath.append(this->host).append(this->basePath).append("/pet"); @@ -405,7 +405,7 @@ OAIPetApi::getPetByIdCallback(OAIHttpRequestWorker * worker) { } void -OAIPetApi::updatePet(std::shared_ptr& oai_pet) { +OAIPetApi::updatePet(OAIPet& oai_pet) { QString fullPath; fullPath.append(this->host).append(this->basePath).append("/pet"); diff --git a/samples/client/petstore/cpp-qt5/client/OAIPetApi.h b/samples/client/petstore/cpp-qt5/client/OAIPetApi.h index ab244af6edec..497e4ad01132 100644 --- a/samples/client/petstore/cpp-qt5/client/OAIPetApi.h +++ b/samples/client/petstore/cpp-qt5/client/OAIPetApi.h @@ -36,12 +36,12 @@ class OAIPetApi: public QObject { QString basePath; QMap defaultHeaders; - void addPet(std::shared_ptr& oai_pet); + void addPet(OAIPet& oai_pet); void deletePet(qint64 pet_id, QString* api_key); void findPetsByStatus(QList* status); void findPetsByTags(QList* tags); void getPetById(qint64 pet_id); - void updatePet(std::shared_ptr& oai_pet); + void updatePet(OAIPet& oai_pet); void updatePetWithForm(qint64 pet_id, QString* name, QString* status); void uploadFile(qint64 pet_id, QString* additional_metadata, OAIHttpRequestInputFileElement* file); diff --git a/samples/client/petstore/cpp-qt5/client/OAIStoreApi.cpp b/samples/client/petstore/cpp-qt5/client/OAIStoreApi.cpp index 4ef64c68c2ac..1ad514f372ad 100644 --- a/samples/client/petstore/cpp-qt5/client/OAIStoreApi.cpp +++ b/samples/client/petstore/cpp-qt5/client/OAIStoreApi.cpp @@ -196,7 +196,7 @@ OAIStoreApi::getOrderByIdCallback(OAIHttpRequestWorker * worker) { } void -OAIStoreApi::placeOrder(std::shared_ptr& oai_order) { +OAIStoreApi::placeOrder(OAIOrder& oai_order) { QString fullPath; fullPath.append(this->host).append(this->basePath).append("/store/order"); diff --git a/samples/client/petstore/cpp-qt5/client/OAIStoreApi.h b/samples/client/petstore/cpp-qt5/client/OAIStoreApi.h index d2fb9d34cbd3..7f08be814d64 100644 --- a/samples/client/petstore/cpp-qt5/client/OAIStoreApi.h +++ b/samples/client/petstore/cpp-qt5/client/OAIStoreApi.h @@ -38,7 +38,7 @@ class OAIStoreApi: public QObject { void deleteOrder(QString* order_id); void getInventory(); void getOrderById(qint64 order_id); - void placeOrder(std::shared_ptr& oai_order); + void placeOrder(OAIOrder& oai_order); private: void deleteOrderCallback (OAIHttpRequestWorker * worker); diff --git a/samples/client/petstore/cpp-qt5/client/OAIUserApi.cpp b/samples/client/petstore/cpp-qt5/client/OAIUserApi.cpp index 5b59256cfd82..6ba2797ff903 100644 --- a/samples/client/petstore/cpp-qt5/client/OAIUserApi.cpp +++ b/samples/client/petstore/cpp-qt5/client/OAIUserApi.cpp @@ -30,7 +30,7 @@ OAIUserApi::OAIUserApi(QString host, QString basePath) { } void -OAIUserApi::createUser(std::shared_ptr& oai_user) { +OAIUserApi::createUser(OAIUser& oai_user) { QString fullPath; fullPath.append(this->host).append(this->basePath).append("/user"); @@ -418,7 +418,7 @@ OAIUserApi::logoutUserCallback(OAIHttpRequestWorker * worker) { } void -OAIUserApi::updateUser(QString* username, std::shared_ptr& oai_user) { +OAIUserApi::updateUser(QString* username, OAIUser& oai_user) { QString fullPath; fullPath.append(this->host).append(this->basePath).append("/user/{username}"); diff --git a/samples/client/petstore/cpp-qt5/client/OAIUserApi.h b/samples/client/petstore/cpp-qt5/client/OAIUserApi.h index 7dbfa8f5a6bb..137b964bde90 100644 --- a/samples/client/petstore/cpp-qt5/client/OAIUserApi.h +++ b/samples/client/petstore/cpp-qt5/client/OAIUserApi.h @@ -35,14 +35,14 @@ class OAIUserApi: public QObject { QString basePath; QMap defaultHeaders; - void createUser(std::shared_ptr& oai_user); + void createUser(OAIUser& oai_user); void createUsersWithArrayInput(QList*& oai_user); void createUsersWithListInput(QList*& oai_user); void deleteUser(QString* username); void getUserByName(QString* username); void loginUser(QString* username, QString* password); void logoutUser(); - void updateUser(QString* username, std::shared_ptr& oai_user); + void updateUser(QString* username, OAIUser& oai_user); private: void createUserCallback (OAIHttpRequestWorker * worker); From 129901d5a7c2353158b88e581790411e3c4bf643 Mon Sep 17 00:00:00 2001 From: etherealjoy Date: Sun, 10 Jun 2018 08:48:26 +0200 Subject: [PATCH 2/2] Remove tabs --- .../org/openapitools/codegen/languages/CppQt5ClientCodegen.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CppQt5ClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CppQt5ClientCodegen.java index 46425fcdcbb7..dab947d9dd7f 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CppQt5ClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CppQt5ClientCodegen.java @@ -445,6 +445,6 @@ public void setOptionalProjectFileFlag(boolean flag) { @Override public String getTypeDeclaration(String str) { - return str; + return str; } }