From 24c55d1f0edce4284fe793deae61a428cf43a06b Mon Sep 17 00:00:00 2001 From: sabras75 Date: Mon, 19 Jun 2017 17:08:32 +0200 Subject: [PATCH] Fix#5856 - Add support for PATCH (#5875) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Changing QBuffer to use a QByteArray solves the issue for me since there is no real use-case for using a QBuffer. Documentation of QT5 states: QBuffer::QBuffer(QByteArray *byteArray, QObject *parent = Q_NULLPTR) Constructs a QBuffer that uses the QByteArray pointed to by byteArray as its internal buffer, and with the given parent. The caller is responsible for ensuring that byteArray remains valid until the QBuffer is destroyed, or until setBuffer() is called to change the buffer. QBuffer doesn't take ownership of the QByteArray. Since the variable “request_content” is allocated on the stack, this is clearly wrong and a bug. The construction of QBuffer is designed this way so that whenever you write to the buffer, it is also written to the byte array that it is pointing to * Add a retro-compatible solution based on QNetworkAccessManager SourceCode * update samples --- .../main/resources/qt5cpp/HttpRequest.cpp.mustache | 13 +++++++++++-- .../qt5cpp/client/SWGHttpRequest.cpp | 13 +++++++++++-- .../petstore/qt5cpp/client/SWGHttpRequest.cpp | 13 +++++++++++-- 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/qt5cpp/HttpRequest.cpp.mustache b/modules/swagger-codegen/src/main/resources/qt5cpp/HttpRequest.cpp.mustache index 3efdde28cc3..998defc4828 100644 --- a/modules/swagger-codegen/src/main/resources/qt5cpp/HttpRequest.cpp.mustache +++ b/modules/swagger-codegen/src/main/resources/qt5cpp/HttpRequest.cpp.mustache @@ -4,6 +4,7 @@ #include #include #include +#include {{#cppNamespaceDeclarations}} @@ -283,8 +284,16 @@ void HttpRequestWorker::execute(HttpRequestInput *input) { manager->deleteResource(request); } else { - QBuffer buff(&request_content); - manager->sendCustomRequest(request, input->http_method.toLatin1(), &buff); +#if (QT_VERSION >= 0x050800) + manager->sendCustomRequest(request, input->http_method.toLatin1(), request_content); +#else + QBuffer *buffer = new QBuffer; + buffer->setData(request_content); + buffer->open(QIODevice::ReadOnly); + + QNetworkReply* reply = manager->sendCustomRequest(request, input->http_method.toLatin1(), buffer); + buffer->setParent(reply); +#endif } } diff --git a/samples/client/petstore-security-test/qt5cpp/client/SWGHttpRequest.cpp b/samples/client/petstore-security-test/qt5cpp/client/SWGHttpRequest.cpp index 589921db798..2300fb2753b 100644 --- a/samples/client/petstore-security-test/qt5cpp/client/SWGHttpRequest.cpp +++ b/samples/client/petstore-security-test/qt5cpp/client/SWGHttpRequest.cpp @@ -15,6 +15,7 @@ #include #include #include +#include namespace Swagger { @@ -292,8 +293,16 @@ void HttpRequestWorker::execute(HttpRequestInput *input) { manager->deleteResource(request); } else { - QBuffer buff(&request_content); - manager->sendCustomRequest(request, input->http_method.toLatin1(), &buff); +#if (QT_VERSION >= 0x050800) + manager->sendCustomRequest(request, input->http_method.toLatin1(), request_content); +#else + QBuffer *buffer = new QBuffer; + buffer->setData(request_content); + buffer->open(QIODevice::ReadOnly); + + QNetworkReply* reply = manager->sendCustomRequest(request, input->http_method.toLatin1(), buffer); + buffer->setParent(reply); +#endif } } diff --git a/samples/client/petstore/qt5cpp/client/SWGHttpRequest.cpp b/samples/client/petstore/qt5cpp/client/SWGHttpRequest.cpp index fa57cc9c8a9..01e05241bda 100644 --- a/samples/client/petstore/qt5cpp/client/SWGHttpRequest.cpp +++ b/samples/client/petstore/qt5cpp/client/SWGHttpRequest.cpp @@ -15,6 +15,7 @@ #include #include #include +#include namespace Swagger { @@ -292,8 +293,16 @@ void HttpRequestWorker::execute(HttpRequestInput *input) { manager->deleteResource(request); } else { - QBuffer buff(&request_content); - manager->sendCustomRequest(request, input->http_method.toLatin1(), &buff); +#if (QT_VERSION >= 0x050800) + manager->sendCustomRequest(request, input->http_method.toLatin1(), request_content); +#else + QBuffer *buffer = new QBuffer; + buffer->setData(request_content); + buffer->open(QIODevice::ReadOnly); + + QNetworkReply* reply = manager->sendCustomRequest(request, input->http_method.toLatin1(), buffer); + buffer->setParent(reply); +#endif } }