[Qt5][C++] Removed deprecated functions to support Qt6 (#8234)

* removed depricated functions

* using preprocessor variable. Updated samples

* added version check for random functions

* added another version check to Json functions

* Update modules/openapi-generator/src/main/resources/cpp-qt5-client/HttpFileElement.cpp.mustache

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

* Update modules/openapi-generator/src/main/resources/cpp-qt5-client/HttpFileElement.cpp.mustache

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

* Update modules/openapi-generator/src/main/resources/cpp-qt5-client/HttpRequest.cpp.mustache

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

* Update modules/openapi-generator/src/main/resources/cpp-qt5-client/HttpRequest.cpp.mustache

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

* Update modules/openapi-generator/src/main/resources/cpp-qt5-client/HttpRequest.cpp.mustache

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

* Update modules/openapi-generator/src/main/resources/cpp-qt5-client/HttpRequest.h.mustache

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

* Apply suggestions from code review

Changed version check to Qt 5.15 to remove the warning of the deprecated functions when compiling with Qt 5.15

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

Co-authored-by: Martin Delille <martin@delille.org>
This commit is contained in:
basyskom-dege
2021-01-25 08:32:43 +01:00
committed by GitHub
parent 3a56e3818f
commit 58f486651e
10 changed files with 132 additions and 72 deletions

View File

@@ -57,9 +57,13 @@ QJsonValue {{prefix}}HttpFileElement::asJsonValue() const {
if (!result) {
qDebug() << "Error opening file " << local_filename;
}
return QJsonDocument::fromBinaryData(bArray.data()).object();
#if QT_VERSION >= 0x051500
return QJsonDocument::fromJson(bArray.data()).object();
#else
return QJsonDocument::fromBinaryData(bArray.data()).object();
#endif
}
bool {{prefix}}HttpFileElement::fromStringValue(const QString &instr) {
QFile file(local_filename);
bool result = false;
@@ -82,7 +86,11 @@ bool {{prefix}}HttpFileElement::fromJsonValue(const QJsonValue &jval) {
file.remove();
}
result = file.open(QIODevice::WriteOnly);
#if QT_VERSION >= 0x051500
file.write(QJsonDocument(jval.toObject()).toJson());
#else
file.write(QJsonDocument(jval.toObject()).toBinaryData());
#endif
file.close();
if (!result) {
qDebug() << "Error creating file " << local_filename;

View File

@@ -6,7 +6,12 @@
#include <QTimer>
#include <QUrl>
#include <QUuid>
#include <QtGlobal>{{#contentCompression}}
#include <QtGlobal>
#if QT_VERSION >= 0x051500
#define SKIP_EMPTY_PARTS Qt::SkipEmptyParts
#else
#define SKIP_EMPTY_PARTS QString::SkipEmptyParts
#endif{{#contentCompression}}
#include <zlib.h>{{/contentCompression}}
#include "{{prefix}}HttpRequest.h"
@@ -46,7 +51,13 @@ void {{prefix}}HttpRequestInput::add_file(QString variable_name, QString local_f
{{prefix}}HttpRequestWorker::{{prefix}}HttpRequestWorker(QObject *parent, QNetworkAccessManager *_manager)
: QObject(parent), manager(_manager), timeOutTimer(this), isResponseCompressionEnabled(false), isRequestCompressionEnabled(false), httpResponseCode(-1) {
#if QT_VERSION >= 0x051500
randomGenerator = QRandomGenerator(QDateTime::currentDateTime().toSecsSinceEpoch());
#else
qsrand(QDateTime::currentDateTime().toTime_t());
#endif
if (manager == nullptr) {
manager = new QNetworkAccessManager(this);
}
@@ -205,31 +216,36 @@ void {{prefix}}HttpRequestWorker::execute({{prefix}}HttpRequestInput *input) {
// variable layout is MULTIPART
boundary = QString("__-----------------------%1%2")
.arg(QDateTime::currentDateTime().toTime_t())
.arg(qrand());
#if QT_VERSION >= 0x051500
.arg(QDateTime::currentDateTime().toSecsSinceEpoch())
.arg(randomGenerator.generate());
#else
.arg(QDateTime::currentDateTime().toTime_t())
.arg(qrand());
#endif
QString boundary_delimiter = "--";
QString new_line = "\r\n";
// add variables
foreach (QString key, input->vars.keys()) {
// add boundary
request_content.append(boundary_delimiter);
request_content.append(boundary);
request_content.append(new_line);
request_content.append(boundary_delimiter.toUtf8());
request_content.append(boundary.toUtf8());
request_content.append(new_line.toUtf8());
// add header
request_content.append("Content-Disposition: form-data; ");
request_content.append(http_attribute_encode("name", key));
request_content.append(new_line);
request_content.append(http_attribute_encode("name", key).toUtf8());
request_content.append(new_line.toUtf8());
request_content.append("Content-Type: text/plain");
request_content.append(new_line);
request_content.append(new_line.toUtf8());
// add header to body splitter
request_content.append(new_line);
request_content.append(new_line.toUtf8());
// add variable content
request_content.append(input->vars.value(key));
request_content.append(new_line);
request_content.append(input->vars.value(key).toUtf8());
request_content.append(new_line.toUtf8());
}
// add files
@@ -263,38 +279,38 @@ void {{prefix}}HttpRequestWorker::execute({{prefix}}HttpRequestInput *input) {
}
// add boundary
request_content.append(boundary_delimiter);
request_content.append(boundary);
request_content.append(new_line);
request_content.append(boundary_delimiter.toUtf8());
request_content.append(boundary.toUtf8());
request_content.append(new_line.toUtf8());
// add header
request_content.append(
QString("Content-Disposition: form-data; %1; %2").arg(http_attribute_encode("name", file_info->variable_name), http_attribute_encode("filename", file_info->request_filename)));
request_content.append(new_line);
QString("Content-Disposition: form-data; %1; %2").arg(http_attribute_encode("name", file_info->variable_name), http_attribute_encode("filename", file_info->request_filename)).toUtf8());
request_content.append(new_line.toUtf8());
if (file_info->mime_type != nullptr && !file_info->mime_type.isEmpty()) {
request_content.append("Content-Type: ");
request_content.append(file_info->mime_type);
request_content.append(new_line);
request_content.append(file_info->mime_type.toUtf8());
request_content.append(new_line.toUtf8());
}
request_content.append("Content-Transfer-Encoding: binary");
request_content.append(new_line);
request_content.append(new_line.toUtf8());
// add header to body splitter
request_content.append(new_line);
request_content.append(new_line.toUtf8());
// add file content
request_content.append(file.readAll());
request_content.append(new_line);
request_content.append(new_line.toUtf8());
file.close();
}
// add end of body
request_content.append(boundary_delimiter);
request_content.append(boundary);
request_content.append(boundary_delimiter);
request_content.append(boundary_delimiter.toUtf8());
request_content.append(boundary.toUtf8());
request_content.append(boundary_delimiter.toUtf8());
}
if (input->request_body.size() > 0) {
@@ -408,14 +424,14 @@ void {{prefix}}HttpRequestWorker::on_reply_timeout(QNetworkReply *reply) {
void {{prefix}}HttpRequestWorker::process_response(QNetworkReply *reply) {
if (getResponseHeaders().contains(QString("Content-Disposition"))) {
auto contentDisposition = getResponseHeaders().value(QString("Content-Disposition").toUtf8()).split(QString(";"), QString::SkipEmptyParts);
auto contentDisposition = getResponseHeaders().value(QString("Content-Disposition").toUtf8()).split(QString(";"), SKIP_EMPTY_PARTS);
auto contentType =
getResponseHeaders().contains(QString("Content-Type")) ? getResponseHeaders().value(QString("Content-Type").toUtf8()).split(QString(";"), QString::SkipEmptyParts).first() : QString();
getResponseHeaders().contains(QString("Content-Type")) ? getResponseHeaders().value(QString("Content-Type").toUtf8()).split(QString(";"), SKIP_EMPTY_PARTS).first() : QString();
if ((contentDisposition.count() > 0) && (contentDisposition.first() == QString("attachment"))) {
QString filename = QUuid::createUuid().toString();
for (const auto &file : contentDisposition) {
if (file.contains(QString("filename"))) {
filename = file.split(QString("="), QString::SkipEmptyParts).at(1);
filename = file.split(QString("="), SKIP_EMPTY_PARTS).at(1);
break;
}
}
@@ -425,14 +441,14 @@ void {{prefix}}HttpRequestWorker::process_response(QNetworkReply *reply) {
}
} else if (getResponseHeaders().contains(QString("Content-Type"))) {
auto contentType = getResponseHeaders().value(QString("Content-Type").toUtf8()).split(QString(";"), QString::SkipEmptyParts);
auto contentType = getResponseHeaders().value(QString("Content-Type").toUtf8()).split(QString(";"), SKIP_EMPTY_PARTS);
if ((contentType.count() > 0) && (contentType.first() == QString("multipart/form-data"))) {
// TODO : Handle Multipart responses
} else {
if(headers.contains("Content-Encoding")){
auto encoding = headers.value("Content-Encoding").split(QString(";"), QString::SkipEmptyParts);
auto encoding = headers.value("Content-Encoding").split(QString(";"), SKIP_EMPTY_PARTS);
if(encoding.count() > 0){
auto compressionTypes = encoding.first().split(',', QString::SkipEmptyParts);
auto compressionTypes = encoding.first().split(',', SKIP_EMPTY_PARTS);
if(compressionTypes.contains("gzip", Qt::CaseInsensitive) || compressionTypes.contains("deflate", Qt::CaseInsensitive)){
response = decompress(reply->readAll());
} else if(compressionTypes.contains("identity", Qt::CaseInsensitive)){

View File

@@ -14,6 +14,9 @@
#include <QObject>
#include <QString>
#include <QTimer>
#if QT_VERSION >= 0x051500
#include <QRandomGenerator>
#endif
#include "{{prefix}}HttpFileElement.h"
@@ -86,6 +89,9 @@ private:
bool isResponseCompressionEnabled;
bool isRequestCompressionEnabled;
int httpResponseCode;
#if QT_VERSION >= 0x051500
QRandomGenerator randomGenerator;
#endif
void on_reply_timeout(QNetworkReply *reply);
void on_reply_finished(QNetworkReply *reply);

View File

@@ -165,7 +165,7 @@ void {{classname}}::{{nickname}}({{#allParams}}const {{{dataType}}} &{{paramName
{{/isBasicBearer}}{{#isBasicBasic}}
if(!_username.isEmpty() && !_password.isEmpty()){
QByteArray b64;
b64.append(_username + ":" + _password);
b64.append(_username.toUtf8() + ":" + _password.toUtf8());
addHeaders("Authorization","Basic " + b64.toBase64());
}{{/isBasicBasic}}{{/authMethods}}
{{#queryParams}}{{^collectionFormat}}
@@ -230,7 +230,7 @@ void {{classname}}::{{nickname}}({{#allParams}}const {{{dataType}}} &{{paramName
QString output({{paramName}});{{/isString}}{{#isByteArray}}QString output({{paramName}});{{/isByteArray}}{{^isString}}{{^isByteArray}}{{^isFile}}
QString output = {{paramName}}.asJson();{{/isFile}}{{/isByteArray}}{{/isString}}{{#isFile}}{{#hasConsumes}}input.headers.insert("Content-Type", {{#consumes}}{{^-first}}, {{/-first}}"{{mediaType}}"{{/consumes}});{{/hasConsumes}}
QByteArray output = {{paramName}}.asByteArray();{{/isFile}}
input.request_body.append(output);
input.request_body.append(output.toUtf8());
{{/isContainer}}{{/bodyParams}}{{#headerParams}}
if (!::{{cppNamespace}}::toStringValue({{paramName}}).isEmpty()) {
input.headers.insert("{{baseName}}", ::{{cppNamespace}}::toStringValue({{paramName}}));

View File

@@ -65,9 +65,13 @@ QJsonValue PFXHttpFileElement::asJsonValue() const {
if (!result) {
qDebug() << "Error opening file " << local_filename;
}
return QJsonDocument::fromBinaryData(bArray.data()).object();
#if QT_VERSION >= 0x051500
return QJsonDocument::fromJson(bArray.data()).object();
#else
return QJsonDocument::fromBinaryData(bArray.data()).object();
#endif
}
bool PFXHttpFileElement::fromStringValue(const QString &instr) {
QFile file(local_filename);
bool result = false;
@@ -90,7 +94,11 @@ bool PFXHttpFileElement::fromJsonValue(const QJsonValue &jval) {
file.remove();
}
result = file.open(QIODevice::WriteOnly);
#if QT_VERSION >= 0x051500
file.write(QJsonDocument(jval.toObject()).toJson());
#else
file.write(QJsonDocument(jval.toObject()).toBinaryData());
#endif
file.close();
if (!result) {
qDebug() << "Error creating file " << local_filename;

View File

@@ -17,6 +17,11 @@
#include <QUrl>
#include <QUuid>
#include <QtGlobal>
#if QT_VERSION >= 0x051500
#define SKIP_EMPTY_PARTS Qt::SkipEmptyParts
#else
#define SKIP_EMPTY_PARTS QString::SkipEmptyParts
#endif
#include "PFXHttpRequest.h"
@@ -53,7 +58,13 @@ void PFXHttpRequestInput::add_file(QString variable_name, QString local_filename
PFXHttpRequestWorker::PFXHttpRequestWorker(QObject *parent, QNetworkAccessManager *_manager)
: QObject(parent), manager(_manager), timeOutTimer(this), isResponseCompressionEnabled(false), isRequestCompressionEnabled(false), httpResponseCode(-1) {
#if QT_VERSION >= 0x051500
randomGenerator = QRandomGenerator(QDateTime::currentDateTime().toSecsSinceEpoch());
#else
qsrand(QDateTime::currentDateTime().toTime_t());
#endif
if (manager == nullptr) {
manager = new QNetworkAccessManager(this);
}
@@ -212,31 +223,36 @@ void PFXHttpRequestWorker::execute(PFXHttpRequestInput *input) {
// variable layout is MULTIPART
boundary = QString("__-----------------------%1%2")
.arg(QDateTime::currentDateTime().toTime_t())
.arg(qrand());
#if QT_VERSION >= 0x051500
.arg(QDateTime::currentDateTime().toSecsSinceEpoch())
.arg(randomGenerator.generate());
#else
.arg(QDateTime::currentDateTime().toTime_t())
.arg(qrand());
#endif
QString boundary_delimiter = "--";
QString new_line = "\r\n";
// add variables
foreach (QString key, input->vars.keys()) {
// add boundary
request_content.append(boundary_delimiter);
request_content.append(boundary);
request_content.append(new_line);
request_content.append(boundary_delimiter.toUtf8());
request_content.append(boundary.toUtf8());
request_content.append(new_line.toUtf8());
// add header
request_content.append("Content-Disposition: form-data; ");
request_content.append(http_attribute_encode("name", key));
request_content.append(new_line);
request_content.append(http_attribute_encode("name", key).toUtf8());
request_content.append(new_line.toUtf8());
request_content.append("Content-Type: text/plain");
request_content.append(new_line);
request_content.append(new_line.toUtf8());
// add header to body splitter
request_content.append(new_line);
request_content.append(new_line.toUtf8());
// add variable content
request_content.append(input->vars.value(key));
request_content.append(new_line);
request_content.append(input->vars.value(key).toUtf8());
request_content.append(new_line.toUtf8());
}
// add files
@@ -270,38 +286,38 @@ void PFXHttpRequestWorker::execute(PFXHttpRequestInput *input) {
}
// add boundary
request_content.append(boundary_delimiter);
request_content.append(boundary);
request_content.append(new_line);
request_content.append(boundary_delimiter.toUtf8());
request_content.append(boundary.toUtf8());
request_content.append(new_line.toUtf8());
// add header
request_content.append(
QString("Content-Disposition: form-data; %1; %2").arg(http_attribute_encode("name", file_info->variable_name), http_attribute_encode("filename", file_info->request_filename)));
request_content.append(new_line);
QString("Content-Disposition: form-data; %1; %2").arg(http_attribute_encode("name", file_info->variable_name), http_attribute_encode("filename", file_info->request_filename)).toUtf8());
request_content.append(new_line.toUtf8());
if (file_info->mime_type != nullptr && !file_info->mime_type.isEmpty()) {
request_content.append("Content-Type: ");
request_content.append(file_info->mime_type);
request_content.append(new_line);
request_content.append(file_info->mime_type.toUtf8());
request_content.append(new_line.toUtf8());
}
request_content.append("Content-Transfer-Encoding: binary");
request_content.append(new_line);
request_content.append(new_line.toUtf8());
// add header to body splitter
request_content.append(new_line);
request_content.append(new_line.toUtf8());
// add file content
request_content.append(file.readAll());
request_content.append(new_line);
request_content.append(new_line.toUtf8());
file.close();
}
// add end of body
request_content.append(boundary_delimiter);
request_content.append(boundary);
request_content.append(boundary_delimiter);
request_content.append(boundary_delimiter.toUtf8());
request_content.append(boundary.toUtf8());
request_content.append(boundary_delimiter.toUtf8());
}
if (input->request_body.size() > 0) {
@@ -415,14 +431,14 @@ void PFXHttpRequestWorker::on_reply_timeout(QNetworkReply *reply) {
void PFXHttpRequestWorker::process_response(QNetworkReply *reply) {
if (getResponseHeaders().contains(QString("Content-Disposition"))) {
auto contentDisposition = getResponseHeaders().value(QString("Content-Disposition").toUtf8()).split(QString(";"), QString::SkipEmptyParts);
auto contentDisposition = getResponseHeaders().value(QString("Content-Disposition").toUtf8()).split(QString(";"), SKIP_EMPTY_PARTS);
auto contentType =
getResponseHeaders().contains(QString("Content-Type")) ? getResponseHeaders().value(QString("Content-Type").toUtf8()).split(QString(";"), QString::SkipEmptyParts).first() : QString();
getResponseHeaders().contains(QString("Content-Type")) ? getResponseHeaders().value(QString("Content-Type").toUtf8()).split(QString(";"), SKIP_EMPTY_PARTS).first() : QString();
if ((contentDisposition.count() > 0) && (contentDisposition.first() == QString("attachment"))) {
QString filename = QUuid::createUuid().toString();
for (const auto &file : contentDisposition) {
if (file.contains(QString("filename"))) {
filename = file.split(QString("="), QString::SkipEmptyParts).at(1);
filename = file.split(QString("="), SKIP_EMPTY_PARTS).at(1);
break;
}
}
@@ -432,14 +448,14 @@ void PFXHttpRequestWorker::process_response(QNetworkReply *reply) {
}
} else if (getResponseHeaders().contains(QString("Content-Type"))) {
auto contentType = getResponseHeaders().value(QString("Content-Type").toUtf8()).split(QString(";"), QString::SkipEmptyParts);
auto contentType = getResponseHeaders().value(QString("Content-Type").toUtf8()).split(QString(";"), SKIP_EMPTY_PARTS);
if ((contentType.count() > 0) && (contentType.first() == QString("multipart/form-data"))) {
// TODO : Handle Multipart responses
} else {
if(headers.contains("Content-Encoding")){
auto encoding = headers.value("Content-Encoding").split(QString(";"), QString::SkipEmptyParts);
auto encoding = headers.value("Content-Encoding").split(QString(";"), SKIP_EMPTY_PARTS);
if(encoding.count() > 0){
auto compressionTypes = encoding.first().split(',', QString::SkipEmptyParts);
auto compressionTypes = encoding.first().split(',', SKIP_EMPTY_PARTS);
if(compressionTypes.contains("gzip", Qt::CaseInsensitive) || compressionTypes.contains("deflate", Qt::CaseInsensitive)){
response = decompress(reply->readAll());
} else if(compressionTypes.contains("identity", Qt::CaseInsensitive)){

View File

@@ -24,6 +24,9 @@
#include <QObject>
#include <QString>
#include <QTimer>
#if QT_VERSION >= 0x051500
#include <QRandomGenerator>
#endif
#include "PFXHttpFileElement.h"
@@ -94,6 +97,9 @@ private:
bool isResponseCompressionEnabled;
bool isRequestCompressionEnabled;
int httpResponseCode;
#if QT_VERSION >= 0x051500
QRandomGenerator randomGenerator;
#endif
void on_reply_timeout(QNetworkReply *reply);
void on_reply_finished(QNetworkReply *reply);

View File

@@ -155,7 +155,7 @@ void PFXPetApi::addPet(const PFXPet &body) {
PFXHttpRequestInput input(fullPath, "POST");
QString output = body.asJson();
input.request_body.append(output);
input.request_body.append(output.toUtf8());
foreach (QString key, this->defaultHeaders.keys()) { input.headers.insert(key, this->defaultHeaders.value(key)); }
@@ -459,7 +459,7 @@ void PFXPetApi::updatePet(const PFXPet &body) {
PFXHttpRequestInput input(fullPath, "PUT");
QString output = body.asJson();
input.request_body.append(output);
input.request_body.append(output.toUtf8());
foreach (QString key, this->defaultHeaders.keys()) { input.headers.insert(key, this->defaultHeaders.value(key)); }

View File

@@ -278,7 +278,7 @@ void PFXStoreApi::placeOrder(const PFXOrder &body) {
PFXHttpRequestInput input(fullPath, "POST");
QString output = body.asJson();
input.request_body.append(output);
input.request_body.append(output.toUtf8());
foreach (QString key, this->defaultHeaders.keys()) { input.headers.insert(key, this->defaultHeaders.value(key)); }

View File

@@ -155,7 +155,7 @@ void PFXUserApi::createUser(const PFXUser &body) {
PFXHttpRequestInput input(fullPath, "POST");
QString output = body.asJson();
input.request_body.append(output);
input.request_body.append(output.toUtf8());
foreach (QString key, this->defaultHeaders.keys()) { input.headers.insert(key, this->defaultHeaders.value(key)); }
@@ -456,7 +456,7 @@ void PFXUserApi::updateUser(const QString &username, const PFXUser &body) {
PFXHttpRequestInput input(fullPath, "PUT");
QString output = body.asJson();
input.request_body.append(output);
input.request_body.append(output.toUtf8());
foreach (QString key, this->defaultHeaders.keys()) { input.headers.insert(key, this->defaultHeaders.value(key)); }