[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 db5c3423b92410115f4c3d5f8b6ca0cabb58f9f6.

* Revert "Revert "[cpp-qt-client] Fix CMakeLists.txt""

This reverts commit c4f055f3cdf666b3ce265fa14e297c3b11d803d9.

* [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 <martin@delille.org>

* Update bin/configs/cpp-qt-client-petstore-unique.yaml

Co-authored-by: Martin Delille <martin@delille.org>

* 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 <martin@delille.org>
This commit is contained in:
Thi 2022-05-08 09:14:41 +02:00 committed by GitHub
parent 8511ce360c
commit b7d079b7e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 114 additions and 3 deletions

View File

@ -12,8 +12,8 @@ else ()
endif ()
find_package(Qt5Core REQUIRED)
find_package(Qt5Network REQUIRED){{#authMethods}}{{#isOAuth}}
find_package(Qt5Gui REQUIRED){{/isOAuth}}{{/authMethods}}{{#contentCompression}}
find_package(Qt5Network REQUIRED)
find_package(Qt5Gui REQUIRED){{#contentCompression}}
find_package(ZLIB REQUIRED){{/contentCompression}}
add_library(${PROJECT_NAME}
@ -34,8 +34,9 @@ add_library(${PROJECT_NAME}
{{prefix}}HttpFileElement.cpp
{{prefix}}Oauth.cpp
)
target_link_libraries(${PROJECT_NAME} PRIVATE Qt5::Core Qt5::Network{{#authMethods}}{{#isOAuth}} Qt5::Gui{{/isOAuth}}{{/authMethods}}{{#contentCompression}} ${ZLIB_LIBRARIES}{{/contentCompression}})
target_link_libraries(${PROJECT_NAME} PRIVATE Qt5::Core Qt5::Network Qt5::Gui{{#contentCompression}} ${ZLIB_LIBRARIES}{{/contentCompression}})
if(NOT APPLE)
find_package(OpenSSL REQUIRED)
target_link_libraries(${PROJECT_NAME} PRIVATE ssl crypto)
endif()

View File

@ -820,7 +820,12 @@ void {{classname}}::{{nickname}}Callback({{prefix}}HttpRequestWorker *worker) {
foreach (QJsonValue obj, jsonArray) {
{{{returnBaseType}}} val;
::{{cppNamespace}}::fromJsonValue(val, obj);
{{#uniqueItems}}
output.insert(val);
{{/uniqueItems}}
{{^uniqueItems}}
output.append(val);
{{/uniqueItems}}
}
{{/isArray}}
{{^isArray}}

View File

@ -50,6 +50,14 @@ private:
QJsonObject jObj;
};
inline bool operator==(const {{prefix}}Object& left, const {{prefix}}Object& right){
return (left.asJsonObject() == right.asJsonObject());
}
inline uint qHash(const {{prefix}}Object& obj, uint seed = 0) noexcept{
return qHash(obj.asJsonObject(), seed);
}
{{#cppNamespaceDeclarations}}
} // namespace {{this}}
{{/cppNamespaceDeclarations}}

View File

@ -50,6 +50,19 @@ paths:
- petstore_auth:
- write:pets
- read:pets
/pet/all:
get:
summary: Get all pets in an array
operationId: allPets
tags:
- pet
responses:
'200':
description: An array of all pets
content:
application/json:
schema:
$ref: '#/components/schemas/PetArray'
/pet/findByStatus:
get:
tags:
@ -678,6 +691,12 @@ components:
- sold
xml:
name: Pet
PetArray:
title: An Array of pets
type: array
items:
$ref: '#/components/schemas/Pet'
uniqueItems: true
ApiResponse:
title: An uploaded response
description: Describes the result of uploading an image resource

View File

@ -32,6 +32,7 @@ add_library(${PROJECT_NAME}
)
target_link_libraries(${PROJECT_NAME} PRIVATE Qt5::Core Qt5::Network Qt5::Gui)
if(NOT APPLE)
find_package(OpenSSL REQUIRED)
target_link_libraries(${PROJECT_NAME} PRIVATE ssl crypto)
endif()

View File

@ -58,6 +58,14 @@ private:
QJsonObject jObj;
};
inline bool operator==(const PFXObject& left, const PFXObject& right){
return (left.asJsonObject() == right.asJsonObject());
}
inline uint qHash(const PFXObject& obj, uint seed = 0) noexcept{
return qHash(obj.asJsonObject(), seed);
}
} // namespace test_namespace
Q_DECLARE_METATYPE(test_namespace::PFXObject)

View File

@ -37,6 +37,8 @@ void PFXPetApi::initializeServerConfigs() {
QMap<QString, PFXServerVariable>()));
_serverConfigs.insert("addPet", defaultConf);
_serverIndices.insert("addPet", 0);
_serverConfigs.insert("allPets", defaultConf);
_serverIndices.insert("allPets", 0);
_serverConfigs.insert("deletePet", defaultConf);
_serverIndices.insert("deletePet", 0);
_serverConfigs.insert("findPetsByStatus", defaultConf);
@ -318,6 +320,64 @@ void PFXPetApi::addPetCallback(PFXHttpRequestWorker *worker) {
}
}
void PFXPetApi::allPets() {
QString fullPath = QString(_serverConfigs["allPets"][_serverIndices.value("allPets")].URL()+"/pet/all");
PFXHttpRequestWorker *worker = new PFXHttpRequestWorker(this, _manager);
worker->setTimeOut(_timeOut);
worker->setWorkingDirectory(_workingDirectory);
PFXHttpRequestInput input(fullPath, "GET");
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
for (auto keyValueIt = _defaultHeaders.keyValueBegin(); keyValueIt != _defaultHeaders.keyValueEnd(); keyValueIt++) {
input.headers.insert(keyValueIt->first, keyValueIt->second);
}
#else
for (auto key : _defaultHeaders.keys()) {
input.headers.insert(key, _defaultHeaders[key]);
}
#endif
connect(worker, &PFXHttpRequestWorker::on_execution_finished, this, &PFXPetApi::allPetsCallback);
connect(this, &PFXPetApi::abortRequestsSignal, worker, &QObject::deleteLater);
connect(worker, &QObject::destroyed, this, [this]() {
if (findChildren<PFXHttpRequestWorker*>().count() == 0) {
emit allPendingRequestsCompleted();
}
});
worker->execute(&input);
}
void PFXPetApi::allPetsCallback(PFXHttpRequestWorker *worker) {
QString error_str = worker->error_str;
QNetworkReply::NetworkError error_type = worker->error_type;
if (worker->error_type != QNetworkReply::NoError) {
error_str = QString("%1, %2").arg(worker->error_str, QString(worker->response));
}
QSet<PFXPet> output;
QString json(worker->response);
QByteArray array(json.toStdString().c_str());
QJsonDocument doc = QJsonDocument::fromJson(array);
QJsonArray jsonArray = doc.array();
foreach (QJsonValue obj, jsonArray) {
PFXPet val;
::test_namespace::fromJsonValue(val, obj);
output.insert(val);
}
worker->deleteLater();
if (worker->error_type == QNetworkReply::NoError) {
emit allPetsSignal(output);
emit allPetsSignalFull(worker, output);
} else {
emit allPetsSignalE(output, error_type, error_str);
emit allPetsSignalEFull(worker, error_type, error_str);
}
}
void PFXPetApi::deletePet(const qint64 &pet_id, const ::test_namespace::OptionalParam<QString> &api_key) {
QString fullPath = QString(_serverConfigs["deletePet"][_serverIndices.value("deletePet")].URL()+"/pet/{petId}");

View File

@ -20,6 +20,7 @@
#include "PFXApiResponse.h"
#include "PFXHttpFileElement.h"
#include "PFXPet.h"
#include <QSet>
#include <QString>
#include <QObject>
@ -63,6 +64,9 @@ public:
*/
void addPet(const PFXPet &pfx_pet);
void allPets();
/**
* @param[in] pet_id qint64 [required]
* @param[in] api_key QString [optional]
@ -127,6 +131,7 @@ private:
int _OauthMethod = 0;
void addPetCallback(PFXHttpRequestWorker *worker);
void allPetsCallback(PFXHttpRequestWorker *worker);
void deletePetCallback(PFXHttpRequestWorker *worker);
void findPetsByStatusCallback(PFXHttpRequestWorker *worker);
void findPetsByTagsCallback(PFXHttpRequestWorker *worker);
@ -138,6 +143,7 @@ private:
signals:
void addPetSignal();
void allPetsSignal(QSet<PFXPet> summary);
void deletePetSignal();
void findPetsByStatusSignal(QList<PFXPet> summary);
void findPetsByTagsSignal(QList<PFXPet> summary);
@ -147,6 +153,7 @@ signals:
void uploadFileSignal(PFXApiResponse summary);
void addPetSignalFull(PFXHttpRequestWorker *worker);
void allPetsSignalFull(PFXHttpRequestWorker *worker, QSet<PFXPet> summary);
void deletePetSignalFull(PFXHttpRequestWorker *worker);
void findPetsByStatusSignalFull(PFXHttpRequestWorker *worker, QList<PFXPet> summary);
void findPetsByTagsSignalFull(PFXHttpRequestWorker *worker, QList<PFXPet> summary);
@ -156,6 +163,7 @@ signals:
void uploadFileSignalFull(PFXHttpRequestWorker *worker, PFXApiResponse summary);
void addPetSignalE(QNetworkReply::NetworkError error_type, QString error_str);
void allPetsSignalE(QSet<PFXPet> summary, QNetworkReply::NetworkError error_type, QString error_str);
void deletePetSignalE(QNetworkReply::NetworkError error_type, QString error_str);
void findPetsByStatusSignalE(QList<PFXPet> summary, QNetworkReply::NetworkError error_type, QString error_str);
void findPetsByTagsSignalE(QList<PFXPet> summary, QNetworkReply::NetworkError error_type, QString error_str);
@ -165,6 +173,7 @@ signals:
void uploadFileSignalE(PFXApiResponse summary, QNetworkReply::NetworkError error_type, QString error_str);
void addPetSignalEFull(PFXHttpRequestWorker *worker, QNetworkReply::NetworkError error_type, QString error_str);
void allPetsSignalEFull(PFXHttpRequestWorker *worker, QNetworkReply::NetworkError error_type, QString error_str);
void deletePetSignalEFull(PFXHttpRequestWorker *worker, QNetworkReply::NetworkError error_type, QString error_str);
void findPetsByStatusSignalEFull(PFXHttpRequestWorker *worker, QNetworkReply::NetworkError error_type, QString error_str);
void findPetsByTagsSignalEFull(PFXHttpRequestWorker *worker, QNetworkReply::NetworkError error_type, QString error_str);