Skip to content

Commit b7d079b

Browse files
[Cpp][Qt][client] Fixed issue with unique items in OpenAPI schema (#11954)
* [cpp-qt-client] Fix CMakeLists.txt Changed: Always add Qt5::Gui to build Added: find_package for OpenSSL (if not Apple) * Revert "[cpp-qt-client] Fix CMakeLists.txt" This reverts commit db5c342. * Revert "Revert "[cpp-qt-client] Fix CMakeLists.txt"" This reverts commit c4f055f. * [Cpp][Qt][client] Fixed unique items in OpenAPI schema Added equal operator for schema objects Added qhash Operator in api template depending on unique items output.insert(val) -- QSet (unique items) or ouput.appen(val) -- QList (not unique items) * Added petstore with unique items to tests schemas, added config for [cpp][qt] and this schema * Run ./bin/generate-samples.sh bin/configs/cpp-qt-client* for new schemas and tests * Update bin/configs/cpp-qt-client-petstore-unique.yaml Co-authored-by: Martin Delille <[email protected]> * Update bin/configs/cpp-qt-client-petstore-unique.yaml Co-authored-by: Martin Delille <[email protected]> * Fixxed typo in name of spec file, too. * Moved petstore_plus_unique.json to correct directory (2_0 -> 3_0 ) moved open api specification rerun generate samples * Deleted obsolete samples output * Removed obsolete files (unique items petstore yaml definition and samples) * Updated samples output for cpp-qt Co-authored-by: Martin Delille <[email protected]>
1 parent 8511ce3 commit b7d079b

File tree

8 files changed

+114
-3
lines changed

8 files changed

+114
-3
lines changed

modules/openapi-generator/src/main/resources/cpp-qt-client/CMakeLists.txt.mustache

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ else ()
1212
endif ()
1313

1414
find_package(Qt5Core REQUIRED)
15-
find_package(Qt5Network REQUIRED){{#authMethods}}{{#isOAuth}}
16-
find_package(Qt5Gui REQUIRED){{/isOAuth}}{{/authMethods}}{{#contentCompression}}
15+
find_package(Qt5Network REQUIRED)
16+
find_package(Qt5Gui REQUIRED){{#contentCompression}}
1717
find_package(ZLIB REQUIRED){{/contentCompression}}
1818

1919
add_library(${PROJECT_NAME}
@@ -34,8 +34,9 @@ add_library(${PROJECT_NAME}
3434
{{prefix}}HttpFileElement.cpp
3535
{{prefix}}Oauth.cpp
3636
)
37-
target_link_libraries(${PROJECT_NAME} PRIVATE Qt5::Core Qt5::Network{{#authMethods}}{{#isOAuth}} Qt5::Gui{{/isOAuth}}{{/authMethods}}{{#contentCompression}} ${ZLIB_LIBRARIES}{{/contentCompression}})
37+
target_link_libraries(${PROJECT_NAME} PRIVATE Qt5::Core Qt5::Network Qt5::Gui{{#contentCompression}} ${ZLIB_LIBRARIES}{{/contentCompression}})
3838
if(NOT APPLE)
39+
find_package(OpenSSL REQUIRED)
3940
target_link_libraries(${PROJECT_NAME} PRIVATE ssl crypto)
4041
endif()
4142

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,7 +820,12 @@ void {{classname}}::{{nickname}}Callback({{prefix}}HttpRequestWorker *worker) {
820820
foreach (QJsonValue obj, jsonArray) {
821821
{{{returnBaseType}}} val;
822822
::{{cppNamespace}}::fromJsonValue(val, obj);
823+
{{#uniqueItems}}
824+
output.insert(val);
825+
{{/uniqueItems}}
826+
{{^uniqueItems}}
823827
output.append(val);
828+
{{/uniqueItems}}
824829
}
825830
{{/isArray}}
826831
{{^isArray}}

modules/openapi-generator/src/main/resources/cpp-qt-client/object.mustache

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ private:
5050
QJsonObject jObj;
5151
};
5252

53+
inline bool operator==(const {{prefix}}Object& left, const {{prefix}}Object& right){
54+
return (left.asJsonObject() == right.asJsonObject());
55+
}
56+
57+
inline uint qHash(const {{prefix}}Object& obj, uint seed = 0) noexcept{
58+
return qHash(obj.asJsonObject(), seed);
59+
}
60+
5361
{{#cppNamespaceDeclarations}}
5462
} // namespace {{this}}
5563
{{/cppNamespaceDeclarations}}

modules/openapi-generator/src/test/resources/3_0/cpp-qt/petstore.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,19 @@ paths:
5050
- petstore_auth:
5151
- write:pets
5252
- read:pets
53+
/pet/all:
54+
get:
55+
summary: Get all pets in an array
56+
operationId: allPets
57+
tags:
58+
- pet
59+
responses:
60+
'200':
61+
description: An array of all pets
62+
content:
63+
application/json:
64+
schema:
65+
$ref: '#/components/schemas/PetArray'
5366
/pet/findByStatus:
5467
get:
5568
tags:
@@ -678,6 +691,12 @@ components:
678691
- sold
679692
xml:
680693
name: Pet
694+
PetArray:
695+
title: An Array of pets
696+
type: array
697+
items:
698+
$ref: '#/components/schemas/Pet'
699+
uniqueItems: true
681700
ApiResponse:
682701
title: An uploaded response
683702
description: Describes the result of uploading an image resource

samples/client/petstore/cpp-qt/client/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ add_library(${PROJECT_NAME}
3232
)
3333
target_link_libraries(${PROJECT_NAME} PRIVATE Qt5::Core Qt5::Network Qt5::Gui)
3434
if(NOT APPLE)
35+
find_package(OpenSSL REQUIRED)
3536
target_link_libraries(${PROJECT_NAME} PRIVATE ssl crypto)
3637
endif()
3738

samples/client/petstore/cpp-qt/client/PFXObject.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ class PFXObject {
5858
QJsonObject jObj;
5959
};
6060

61+
inline bool operator==(const PFXObject& left, const PFXObject& right){
62+
return (left.asJsonObject() == right.asJsonObject());
63+
}
64+
65+
inline uint qHash(const PFXObject& obj, uint seed = 0) noexcept{
66+
return qHash(obj.asJsonObject(), seed);
67+
}
68+
6169
} // namespace test_namespace
6270

6371
Q_DECLARE_METATYPE(test_namespace::PFXObject)

samples/client/petstore/cpp-qt/client/PFXPetApi.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ void PFXPetApi::initializeServerConfigs() {
3737
QMap<QString, PFXServerVariable>()));
3838
_serverConfigs.insert("addPet", defaultConf);
3939
_serverIndices.insert("addPet", 0);
40+
_serverConfigs.insert("allPets", defaultConf);
41+
_serverIndices.insert("allPets", 0);
4042
_serverConfigs.insert("deletePet", defaultConf);
4143
_serverIndices.insert("deletePet", 0);
4244
_serverConfigs.insert("findPetsByStatus", defaultConf);
@@ -318,6 +320,64 @@ void PFXPetApi::addPetCallback(PFXHttpRequestWorker *worker) {
318320
}
319321
}
320322

323+
void PFXPetApi::allPets() {
324+
QString fullPath = QString(_serverConfigs["allPets"][_serverIndices.value("allPets")].URL()+"/pet/all");
325+
326+
PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this, _manager);
327+
worker->setTimeOut(_timeOut);
328+
worker->setWorkingDirectory(_workingDirectory);
329+
PFXHttpRequestInput input(fullPath, "GET");
330+
331+
332+
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
333+
for (auto keyValueIt = _defaultHeaders.keyValueBegin(); keyValueIt != _defaultHeaders.keyValueEnd(); keyValueIt++) {
334+
input.headers.insert(keyValueIt->first, keyValueIt->second);
335+
}
336+
#else
337+
for (auto key : _defaultHeaders.keys()) {
338+
input.headers.insert(key, _defaultHeaders[key]);
339+
}
340+
#endif
341+
342+
connect(worker, &PFXHttpRequestWorker::on_execution_finished, this, &PFXPetApi::allPetsCallback);
343+
connect(this, &PFXPetApi::abortRequestsSignal, worker, &QObject::deleteLater);
344+
connect(worker, &QObject::destroyed, this, [this]() {
345+
if (findChildren<PFXHttpRequestWorker*>().count() == 0) {
346+
emit allPendingRequestsCompleted();
347+
}
348+
});
349+
350+
worker->execute(&input);
351+
}
352+
353+
void PFXPetApi::allPetsCallback(PFXHttpRequestWorker *worker) {
354+
QString error_str = worker->error_str;
355+
QNetworkReply::NetworkError error_type = worker->error_type;
356+
357+
if (worker->error_type != QNetworkReply::NoError) {
358+
error_str = QString("%1, %2").arg(worker->error_str, QString(worker->response));
359+
}
360+
QSet<PFXPet> output;
361+
QString json(worker->response);
362+
QByteArray array(json.toStdString().c_str());
363+
QJsonDocument doc = QJsonDocument::fromJson(array);
364+
QJsonArray jsonArray = doc.array();
365+
foreach (QJsonValue obj, jsonArray) {
366+
PFXPet val;
367+
::test_namespace::fromJsonValue(val, obj);
368+
output.insert(val);
369+
}
370+
worker->deleteLater();
371+
372+
if (worker->error_type == QNetworkReply::NoError) {
373+
emit allPetsSignal(output);
374+
emit allPetsSignalFull(worker, output);
375+
} else {
376+
emit allPetsSignalE(output, error_type, error_str);
377+
emit allPetsSignalEFull(worker, error_type, error_str);
378+
}
379+
}
380+
321381
void PFXPetApi::deletePet(const qint64 &pet_id, const ::test_namespace::OptionalParam<QString> &api_key) {
322382
QString fullPath = QString(_serverConfigs["deletePet"][_serverIndices.value("deletePet")].URL()+"/pet/{petId}");
323383

samples/client/petstore/cpp-qt/client/PFXPetApi.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "PFXApiResponse.h"
2121
#include "PFXHttpFileElement.h"
2222
#include "PFXPet.h"
23+
#include <QSet>
2324
#include <QString>
2425

2526
#include <QObject>
@@ -63,6 +64,9 @@ class PFXPetApi : public QObject {
6364
*/
6465
void addPet(const PFXPet &pfx_pet);
6566

67+
68+
void allPets();
69+
6670
/**
6771
* @param[in] pet_id qint64 [required]
6872
* @param[in] api_key QString [optional]
@@ -127,6 +131,7 @@ class PFXPetApi : public QObject {
127131
int _OauthMethod = 0;
128132

129133
void addPetCallback(PFXHttpRequestWorker *worker);
134+
void allPetsCallback(PFXHttpRequestWorker *worker);
130135
void deletePetCallback(PFXHttpRequestWorker *worker);
131136
void findPetsByStatusCallback(PFXHttpRequestWorker *worker);
132137
void findPetsByTagsCallback(PFXHttpRequestWorker *worker);
@@ -138,6 +143,7 @@ class PFXPetApi : public QObject {
138143
signals:
139144

140145
void addPetSignal();
146+
void allPetsSignal(QSet<PFXPet> summary);
141147
void deletePetSignal();
142148
void findPetsByStatusSignal(QList<PFXPet> summary);
143149
void findPetsByTagsSignal(QList<PFXPet> summary);
@@ -147,6 +153,7 @@ class PFXPetApi : public QObject {
147153
void uploadFileSignal(PFXApiResponse summary);
148154

149155
void addPetSignalFull(PFXHttpRequestWorker *worker);
156+
void allPetsSignalFull(PFXHttpRequestWorker *worker, QSet<PFXPet> summary);
150157
void deletePetSignalFull(PFXHttpRequestWorker *worker);
151158
void findPetsByStatusSignalFull(PFXHttpRequestWorker *worker, QList<PFXPet> summary);
152159
void findPetsByTagsSignalFull(PFXHttpRequestWorker *worker, QList<PFXPet> summary);
@@ -156,6 +163,7 @@ class PFXPetApi : public QObject {
156163
void uploadFileSignalFull(PFXHttpRequestWorker *worker, PFXApiResponse summary);
157164

158165
void addPetSignalE(QNetworkReply::NetworkError error_type, QString error_str);
166+
void allPetsSignalE(QSet<PFXPet> summary, QNetworkReply::NetworkError error_type, QString error_str);
159167
void deletePetSignalE(QNetworkReply::NetworkError error_type, QString error_str);
160168
void findPetsByStatusSignalE(QList<PFXPet> summary, QNetworkReply::NetworkError error_type, QString error_str);
161169
void findPetsByTagsSignalE(QList<PFXPet> summary, QNetworkReply::NetworkError error_type, QString error_str);
@@ -165,6 +173,7 @@ class PFXPetApi : public QObject {
165173
void uploadFileSignalE(PFXApiResponse summary, QNetworkReply::NetworkError error_type, QString error_str);
166174

167175
void addPetSignalEFull(PFXHttpRequestWorker *worker, QNetworkReply::NetworkError error_type, QString error_str);
176+
void allPetsSignalEFull(PFXHttpRequestWorker *worker, QNetworkReply::NetworkError error_type, QString error_str);
168177
void deletePetSignalEFull(PFXHttpRequestWorker *worker, QNetworkReply::NetworkError error_type, QString error_str);
169178
void findPetsByStatusSignalEFull(PFXHttpRequestWorker *worker, QNetworkReply::NetworkError error_type, QString error_str);
170179
void findPetsByTagsSignalEFull(PFXHttpRequestWorker *worker, QNetworkReply::NetworkError error_type, QString error_str);

0 commit comments

Comments
 (0)