Revision control

/**
* Qt Quick Demo - RESTful API client
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* Do not edit the class manually.
*/
#include "OAIDefaultApi.h"
#include "OAIServerConfiguration.h"
#include <QtCore/qanystringview.h>
#include <QtCore/qjsonarray.h>
#include <QtCore/qjsondocument.h>
#include <QtNetwork/qnetworkaccessmanager.h>
#include <QtNetwork/qnetworkreply.h>
#include <QtNetwork/qnetworkrequestfactory.h>
#include <QtNetwork/qrestaccessmanager.h>
using namespace Qt::StringLiterals;
namespace OpenAPI {
OAIDefaultApi::OAIDefaultApi(QObject *parent)
: OAIBaseApi(parent)
{
initializeServerConfigs();
}
OAIDefaultApi::~OAIDefaultApi()
{
}
void OAIDefaultApi::initializeServerConfigs()
{
//Default server
QList<OAIServerConfiguration> defaultConf = QList<OAIServerConfiguration>();
//varying endpoint server
defaultConf.append(OAIServerConfiguration(
"Public REST API test service",
QMap<QString, OAIServerVariable>()));
defaultConf.append(OAIServerConfiguration(
"Qt-based REST API server",
QMap<QString, OAIServerVariable>()));
m_serverConfigs.insert("testOperation", defaultConf);
m_serverIndices.insert("testOperation", 0);
}
/**
* \fn virtual void OAIDefaultApi::testOperation(const qint32 &pathParam, const ::OpenAPI::OptionalParam<bool> &queryParam0 = ::OpenAPI::OptionalParam<bool>(), const ::OpenAPI::OptionalParam<OAITestObject> &queryParam1 = ::OpenAPI::OptionalParam<OAITestObject>(), const ::OpenAPI::OptionalParam<QList<qint32>> &queryParam2 = ::OpenAPI::OptionalParam<QList<qint32>>(), const ::OpenAPI::OptionalParam<OAITestOperation_request> &oAITestOperationRequest = ::OpenAPI::OptionalParam<OAITestOperation_request>())
* 'testOperation' operation sends the request to a server.
* The request parameters are defined by a specification file.
* @param[in] pathParam qint32 [required]
* @param[in] queryParam0 bool [optional]
* @param[in] queryParam1 OAITestObject [optional]
* @param[in] queryParam2 QList<qint32> [optional]
* @param[in] oAITestOperationRequest OAITestOperation_request [optional]
*/
/**
* \fn template < Functor, > void OAIDefaultApi::testOperation(const qint32 &pathParam, const ::OpenAPI::OptionalParam<bool> &queryParam0 = ::OpenAPI::OptionalParam<bool>(), const ::OpenAPI::OptionalParam<OAITestObject> &queryParam1 = ::OpenAPI::OptionalParam<OAITestObject>(), const ::OpenAPI::OptionalParam<QList<qint32>> &queryParam2 = ::OpenAPI::OptionalParam<QList<qint32>>(), const ::OpenAPI::OptionalParam<OAITestOperation_request> &oAITestOperationRequest = ::OpenAPI::OptionalParam<OAITestOperation_request>(), const ContextTypeForFunctor< Functor > *context = nullptr, Functor &&callback = (){})
* 'testOperation' operation sends the request to a server.
* The request parameters are defined by a specification file.
*
* \attention Use the operation with following parameters in the callback:
* \code {c++}
* testOperation(pathParam, queryParam0, queryParam1, queryParam2, oAITestOperationRequest, this, [&](const QRestReply &reply) { if (reply.isSuccess()) ... });
* \endcode
* \note The template function can not be virtual in C++17.
* If you want to use 'makeOperationsVirtual' option for mocking API,
* please override virtual testOperationWithDataImpl() in derived class.
* The virtual testOperationWithDataImpl() is being called by the template
* function.
* @param[in] pathParam qint32 [required]
* @param[in] queryParam0 bool [optional]
* @param[in] queryParam1 OAITestObject [optional]
* @param[in] queryParam2 QList<qint32> [optional]
* @param[in] oAITestOperationRequest OAITestOperation_request [optional]
* @param[in] context const ContextTypeForFunctor< Functor > * [optional]
* @param[in] callback Functor && [optional]
*/
/**
* \fn void OAIDefaultApi::testOperationCallback(const QRestReply &reply)
* Processes a \a reply response from a server.
* The result of processed data is emitted by testOperationFinished() or
* being returned as a callback parameter of testOperation() request.
* @param[in] reply const QRestReply &
*/
/**
* \fn virtual void OAIDefaultApi::testOperationWithDataImpl(const qint32 &pathParam, const ::OpenAPI::OptionalParam<bool> &queryParam0, const ::OpenAPI::OptionalParam<OAITestObject> &queryParam1, const ::OpenAPI::OptionalParam<QList<qint32>> &queryParam2, const ::OpenAPI::OptionalParam<OAITestOperation_request> &oAITestOperationRequest, const QObject *context, QtPrivate::QSlotObjectBase *slot)
* Implements the testOperation() operation request.
* \note If 'makeOperationsVirtual' option is true, this function is declared as virtual
* and can be overloaded for mocking testOperation() operation calls.
* @param[in] pathParam qint32 [required]
* @param[in] queryParam0 bool [optional]
* @param[in] queryParam1 OAITestObject [optional]
* @param[in] queryParam2 QList<qint32> [optional]
* @param[in] oAITestOperationRequest OAITestOperation_request [optional]
* @param[in] context const QObject * [optional]
* @param[in] slot QtPrivate::QSlotObjectBase * [optional]
*/
void OAIDefaultApi::testOperationWithDataImpl(const qint32 &pathParam, const ::OpenAPI::OptionalParam<bool> &queryParam0, const ::OpenAPI::OptionalParam<OAITestObject> &queryParam1, const ::OpenAPI::OptionalParam<QList<qint32>> &queryParam2, const ::OpenAPI::OptionalParam<OAITestOperation_request> &oAITestOperationRequest, const QObject *context, QtPrivate::QSlotObjectBase *slot)
{
const QUrl serverUrl = m_serverConfigs["testOperation"][m_serverIndices.value("testOperation")].serverUrl();
QString fullPath = "/test/{path_param}";
m_networkFactory->setBaseUrl(serverUrl);
- QString pathParamPathParam("{");
- pathParamPathParam.append("path_param").append("}");
- QString pathPrefix, pathSuffix, pathDelimiter;
- QString pathStyle = "simple";
- if (pathStyle == "")
- pathStyle = "simple";
- pathPrefix = getParamStylePrefix(pathStyle);
- pathSuffix = getParamStyleSuffix(pathStyle);
- pathDelimiter = getParamStyleDelimiter(pathStyle, "path_param", false);
-
- QString paramString = (pathStyle == "matrix") ? pathPrefix + "path_param" + pathSuffix : pathPrefix;
- fullPath.replace(pathParamPathParam, QUrl::toPercentEncoding(paramString + ::OpenAPI::toStringValue(pathParam)));
- QString queryPrefix, querySuffix, queryDelimiter, queryStyle;
- queryStyle = "form";
- if (queryStyle == "")
- queryStyle = "form";
- queryPrefix = getParamStylePrefix(queryStyle);
- querySuffix = getParamStyleSuffix(queryStyle);
- if (queryParam0.hasValue()) {
- if (fullPath.indexOf("?") > 0)
- fullPath.append(queryPrefix);
- else
- fullPath.append("?");
- fullPath.append(QUrl::toPercentEncoding("query_param0") + querySuffix + QUrl::toPercentEncoding(::OpenAPI::toStringValue(queryParam0.value())));
+ {
+ QString pathParamPathParam = QString("{%1}").arg("path_param");
+ QString pathStyle = "simple";
+ if (pathStyle.isEmpty())
+ pathStyle = "simple";
+ const QString pathPrefix = getParamStylePrefix(pathStyle);
+ const QString pathDelimiter = getParamStyleDelimiter(pathStyle, false);
+ [[maybe_unused]] const QString assignOperator = getParamStyleAssignOperator(pathStyle, false, (!true && !false));
+ const QString pathSuffix = getParamStyleSuffix(pathStyle, u"path_param"_s, false, (!true && !false));
+ QString paramString = pathPrefix + pathSuffix;
+ paramString += QUrl::toPercentEncoding(::OpenAPI::toStringValue(pathParam));
+ // In case style=matrix and paramString is empty due to any reasons,
+ // we serialize it like undefined value and delete '='.
+ if ((paramString == pathPrefix + pathSuffix) && QString("simple") == "matrix"_L1)
+ paramString.chop(1);
+ fullPath.replace(pathParamPathParam, paramString);
}
- queryStyle = "form";
- if (queryStyle == "")
- queryStyle = "form";
- queryPrefix = getParamStylePrefix(queryStyle);
- querySuffix = getParamStyleSuffix(queryStyle);
- if (queryParam1.hasValue()) {
- if (fullPath.indexOf("?") > 0)
+ int queryParamCounter = 0;
+ {
+ [[maybe_unused]] QString paramString;
+ QString queryStyle = "form";
+ if (queryStyle.isEmpty())
+ queryStyle = "form";
+ const QString queryPrefix = getParamStylePrefix(queryStyle);
+ [[maybe_unused]] const QString queryDelimiter = getParamStyleDelimiter(queryStyle, true);
+ const QString querySuffix = getParamStyleSuffix(queryStyle, u"query_param0"_s, true, (!true && !false));
+ [[maybe_unused]] const QString queryAssignOperator = getParamStyleAssignOperator(queryStyle, true, (!true && !false));
+ paramString = querySuffix;
+ if ((fullPath.indexOf("?") != fullPath.size() - 1) && (queryParamCounter == 0))
fullPath.append(queryPrefix);
- else
- fullPath.append("?");
- QString paramString = (queryStyle == "form" && true) ? "" : "query_param1" + querySuffix;
- QString assignOperator;
- if (queryStyle == "form")
- assignOperator = (true) ? "=" : ",";
- else if (queryStyle == "deepObject")
- assignOperator = (true) ? "=" : "none";
- queryDelimiter = ((queryStyle == "form" || queryStyle == "deepObject") && true) ? "&" : ",";
- const QJsonObject parameter = queryParam1.value().asJsonObject();
- qint32 count = 0;
- for(const QString& key : parameter.keys()) {
- if (count > 0)
- paramString.append(queryDelimiter);
- paramString.append(::OpenAPI::optionParameterToString(((queryStyle == "form") ? key
- : QString("query_param1") + QString("[") + key + QString("]")), assignOperator, parameter.value(key)));
- count++;
+ if (queryParamCounter > 0)
+ fullPath.append("&");
+ if (queryParam0.hasValue()) {
+
+ fullPath.append(querySuffix + QUrl::toPercentEncoding(::OpenAPI::toStringValue(queryParam0.value())));
+ queryParamCounter++;
}
-
-
- fullPath.append(QUrl::toPercentEncoding(paramString));
-
- } else if (queryParam1.isNull()) {
- if (fullPath.indexOf("?") > 0)
- fullPath.append(queryPrefix);
- else
- fullPath.append("?");
-
- fullPath.append(QUrl::toPercentEncoding("query_param1")).append(querySuffix).append(QUrl::toPercentEncoding(QString("null")));
}
- queryStyle = "form";
- if (queryStyle == "")
- queryStyle = "form";
- queryPrefix = getParamStylePrefix(queryStyle);
- querySuffix = getParamStyleSuffix(queryStyle);
- if (queryParam2.hasValue()) {
+ {
+ [[maybe_unused]] QString paramString;
+ QString queryStyle = "form";
+ if (queryStyle.isEmpty())
+ queryStyle = "form";
+ const QString queryPrefix = getParamStylePrefix(queryStyle);
+ [[maybe_unused]] const QString queryDelimiter = getParamStyleDelimiter(queryStyle, true);
+ const QString querySuffix = getParamStyleSuffix(queryStyle, u"query_param1"_s, true, (!false && !false));
+ [[maybe_unused]] const QString queryAssignOperator = getParamStyleAssignOperator(queryStyle, true, (!false && !false));
+ paramString = querySuffix;
+ if ((fullPath.indexOf("?") != fullPath.size() - 1) && (queryParamCounter == 0))
+ fullPath.append(queryPrefix);
+ if (queryParamCounter > 0)
+ fullPath.append("&");
+ if (queryParam1.hasValue()) {
- queryDelimiter = getParamStyleDelimiter(queryStyle, "query_param2", true);
- if (queryParam2.value().size() > 0) {
- if (QString("multi").indexOf("multi") == 0) {
- for (qint32 t : queryParam2.value()) {
- if (fullPath.indexOf("?") > 0)
- fullPath.append(queryPrefix);
- else
- fullPath.append("?");
- fullPath.append("query_param2=").append(::OpenAPI::toStringValue(t));
- }
- } else if (QString("multi").indexOf("ssv") == 0) {
- if (fullPath.indexOf("?") > 0)
- fullPath.append("&");
- else
- fullPath.append("?").append(queryPrefix).append("query_param2").append(querySuffix);
- qint32 count = 0;
- for (qint32 t : queryParam2.value()) {
- if (count > 0) {
- fullPath.append((true)? queryDelimiter : QUrl::toPercentEncoding(queryDelimiter));
- }
- fullPath.append(::OpenAPI::toStringValue(t));
- count++;
- }
- } else if (QString("multi").indexOf("tsv") == 0) {
- if (fullPath.indexOf("?") > 0)
- fullPath.append("&");
- else
- fullPath.append("?").append(queryPrefix).append("query_param2").append(querySuffix);
- qint32 count = 0;
- for (qint32 t : queryParam2.value()) {
- if (count > 0) {
- fullPath.append("\t");
- }
- fullPath.append(::OpenAPI::toStringValue(t));
- count++;
- }
- } else if (QString("multi").indexOf("csv") == 0) {
- if (fullPath.indexOf("?") > 0)
- fullPath.append("&");
- else
- fullPath.append("?").append(queryPrefix).append("query_param2").append(querySuffix);
- qint32 count = 0;
- for (qint32 t : queryParam2.value()) {
- if (count > 0) {
- fullPath.append(queryDelimiter);
- }
- fullPath.append(::OpenAPI::toStringValue(t));
- count++;
- }
- } else if (QString("multi").indexOf("pipes") == 0) {
- if (fullPath.indexOf("?") > 0)
- fullPath.append("&");
- else
- fullPath.append("?").append(queryPrefix).append("query_param2").append(querySuffix);
- qint32 count = 0;
- for (qint32 t : queryParam2.value()) {
- if (count > 0) {
- fullPath.append(queryDelimiter);
- }
- fullPath.append(::OpenAPI::toStringValue(t));
- count++;
- }
- } else if (QString("multi").indexOf("deepObject") == 0) {
- if (fullPath.indexOf("?") > 0)
- fullPath.append("&");
- else
- fullPath.append("?").append(queryPrefix).append("query_param2").append(querySuffix);
- qint32 count = 0;
- for (qint32 t : queryParam2.value()) {
- if (count > 0) {
- fullPath.append(queryDelimiter);
- }
- fullPath.append(::OpenAPI::toStringValue(t));
- count++;
- }
+ const QJsonObject parameter = queryParam1.value().asJsonObject();
+ if (queryStyle == "deepObject") {
+ qint32 index = 0;
+ if (parameter.isEmpty())
+ qWarning() << "Serialized QJsonValue::Object is empty!";
+ for (const QString& key : parameter.keys()) {
+ if (index > 0)
+ paramString.append(queryDelimiter);
+ paramString.append(::OpenAPI::optionParameterToString(QString("query_param1") + QString("[") + key + QString("]"), queryAssignOperator, parameter.value(key)));
+ index++;
+ }
+ } else {
+ paramString = serializeJsonValue(QJsonValue(parameter), queryStyle, true, querySuffix, queryAssignOperator, queryDelimiter);
}
+ // style=form && explode=true && non-object => 'query_param1' isn't used in serialization
+ // style=form && explode=true && empty object => need to be 'query_param1='
+ if (parameter.isEmpty() && queryStyle == "form")
+ paramString = u"query_param1="_s;
+ fullPath.append(paramString);
+
+ queryParamCounter++;
+ } else if (queryParam1.isNull()) {
+ fullPath.append(querySuffix).append(QString("null"));
+ queryParamCounter++;
}
}
+ {
+ [[maybe_unused]] QString paramString;
+ QString queryStyle = "form";
+ if (queryStyle.isEmpty())
+ queryStyle = "form";
+ const QString queryPrefix = getParamStylePrefix(queryStyle);
+ [[maybe_unused]] const QString queryDelimiter = getParamStyleDelimiter(queryStyle, true);
+ const QString querySuffix = getParamStyleSuffix(queryStyle, u"query_param2"_s, true, (!false && !true));
+ [[maybe_unused]] const QString queryAssignOperator = getParamStyleAssignOperator(queryStyle, true, (!false && !true));
+ if ((fullPath.indexOf("?") != fullPath.size() - 1) && (queryParamCounter == 0))
+ fullPath.append(queryPrefix);
+ if (queryParamCounter > 0)
+ fullPath.append("&");
+ if (queryParam2.hasValue()) {
+
+ fullPath.append(serializeArrayValue(queryParam2.value(), queryStyle, true, querySuffix, queryDelimiter));
+ queryParamCounter++;
+ }
+ }
+ // set m_testOperationPath for serialization tests
+ m_testOperationPath = fullPath;
OAIHttpRequestInput input(fullPath, "POST");
if (oAITestOperationRequest.hasValue()) {
QByteArray output = oAITestOperationRequest.value().asJson().toUtf8();
input.m_requestBody.append(output);
} else if (oAITestOperationRequest.isNull()) {
input.m_requestBody.append("null");
}
QByteArray requestContent;
QNetworkRequest request
= OAIHttpRequestWorker::getNetworkRequest(input, requestContent, m_networkFactory,
m_isResponseCompressionEnabled, m_isRequestCompressionEnabled);
QNetworkReply *reply = execute(input, request, requestContent);
if (reply != nullptr) {
reply->setParent(this);
m_callerData.insert(reply, OAICallerInfo{context, slot});
connect(reply, &QNetworkReply::finished, this, [this, reply] {
testOperationCallback(QRestReply(reply));
});
connect(reply, &QNetworkReply::errorOccurred, this, [this, reply] {
if (reply) {
emit testOperationErrorOccurred(reply->error(), reply->errorString());
OAICallerInfo callerInfo = m_callerData.take(reply);
if (callerInfo.slot) {
QRestReply restRepl(reply);
void *argv[] = { nullptr, &restRepl };
QObject *context = callerInfo.contextObject ? const_cast<QObject*>(callerInfo.contextObject) : nullptr;
callerInfo.slot->call(context, argv);
}
}
});
}
}
void OAIDefaultApi::testOperationCallback(const QRestReply &reply)
{
auto netReply = reply.networkReply();
if (netReply)
disconnect(netReply, nullptr, nullptr, nullptr);
if (!reply.isSuccess())
return;
// Check if callback is provided
OAICallerInfo callerInfo = m_callerData.take(netReply);
if (callerInfo.slot) {
void *argv[] = { nullptr, const_cast<QRestReply*>(&reply) };
QObject *context = callerInfo.contextObject
? const_cast<QObject*>(callerInfo.contextObject) : nullptr;
callerInfo.slot->call(context, argv);
}
emit testOperationFinished();
}
} // namespace OpenAPI