[QT5CPP] Client not handling optional parameter
Description
If xou have not required query params this params have to be set by client. As you can see in generated code below in method SWGExampleApi::exampleGet one has to pass all parameters but parameter example2 and example3 are not marked as required.
Swagger-codegen version
Version 2.2.3
Swagger declaration file content or url
swagger: "2.0"
info:
description: "example."
version: "1.0.0"
title: "example"
license:
name: "Apache 2.0"
url: "http://www.apache.org/licenses/LICENSE-2.0.html"
host: "example.io"
basePath: "/v2"
schemes:
- "http"
paths:
/example:
get:
tags:
- "example"
consumes:
- "application/json"
produces:
- "application/json"
parameters:
- in: "query"
name: "example1"
description: "example1"
required: true
type: "integer"
- in: "query"
name: "example2"
description: "example2"
required: false
type: "integer"
- in: "query"
name: "example3"
description: "example3"
required: false
type: "integer"
responses:
405:
description: "Invalid input"
Command line used for generation
java -jar swagger-codegen-cli-2.2.3.jar generate -l qt5cpp -i swagger.json -o PathToFolder
Generated code
// SWGExampleAPI.h
/**
* example
* example.
*
* OpenAPI spec version: 1.0.0
*
*
* NOTE: This class is auto generated by the swagger code generator program.
* https://github.com/swagger-api/swagger-codegen.git
* Do not edit the class manually.
*/
#ifndef _SWG_SWGExampleApi_H_
#define _SWG_SWGExampleApi_H_
#include "SWGHttpRequest.h"
#include <QObject>
namespace Swagger {
class SWGExampleApi: public QObject {
Q_OBJECT
public:
SWGExampleApi();
SWGExampleApi(QString host, QString basePath);
~SWGExampleApi();
QString host;
QString basePath;
QMap<QString, QString> defaultHeaders;
void exampleGet(qint32 example1, qint32 example2, qint32 example3);
private:
void exampleGetCallback (HttpRequestWorker * worker);
signals:
void exampleGetSignal();
void exampleGetSignalE(QNetworkReply::NetworkError error_type, QString& error_str);
};
}
#endif
// SWGExampleApi.cpp
/**
* example
* example.
*
* OpenAPI spec version: 1.0.0
*
*
* NOTE: This class is auto generated by the swagger code generator program.
* https://github.com/swagger-api/swagger-codegen.git
* Do not edit the class manually.
*/
#include "SWGExampleApi.h"
#include "SWGHelpers.h"
#include "SWGModelFactory.h"
#include <QJsonArray>
#include <QJsonDocument>
namespace Swagger {
SWGExampleApi::SWGExampleApi() {}
SWGExampleApi::~SWGExampleApi() {}
SWGExampleApi::SWGExampleApi(QString host, QString basePath) {
this->host = host;
this->basePath = basePath;
}
void
SWGExampleApi::exampleGet(qint32 example1, qint32 example2, qint32 example3) {
QString fullPath;
fullPath.append(this->host).append(this->basePath).append("/example");
if (fullPath.indexOf("?") > 0)
fullPath.append("&");
else
fullPath.append("?");
fullPath.append(QUrl::toPercentEncoding("example1"))
.append("=")
.append(QUrl::toPercentEncoding(stringValue(example1)));
if (fullPath.indexOf("?") > 0)
fullPath.append("&");
else
fullPath.append("?");
fullPath.append(QUrl::toPercentEncoding("example2"))
.append("=")
.append(QUrl::toPercentEncoding(stringValue(example2)));
if (fullPath.indexOf("?") > 0)
fullPath.append("&");
else
fullPath.append("?");
fullPath.append(QUrl::toPercentEncoding("example3"))
.append("=")
.append(QUrl::toPercentEncoding(stringValue(example3)));
HttpRequestWorker *worker = new HttpRequestWorker();
HttpRequestInput input(fullPath, "GET");
foreach(QString key, this->defaultHeaders.keys()) {
input.headers.insert(key, this->defaultHeaders.value(key));
}
connect(worker,
&HttpRequestWorker::on_execution_finished,
this,
&SWGExampleApi::exampleGetCallback);
worker->execute(&input);
}
void
SWGExampleApi::exampleGetCallback(HttpRequestWorker * worker) {
QString msg;
QString error_str = worker->error_str;
QNetworkReply::NetworkError error_type = worker->error_type;
if (worker->error_type == QNetworkReply::NoError) {
msg = QString("Success! %1 bytes").arg(worker->response.length());
}
else {
msg = "Error: " + worker->error_str;
}
worker->deleteLater();
emit exampleGetSignal();
emit exampleGetSignalE(error_type, error_str);
}
}
Steps to reproduce
generate the same code.
Related issues/PRs
I didn't finde any
Suggest a fix/enhancement
Two suggestions:
- It could be a fix to sort the method params and first to provide all required params. All not required params are added to a new overriden method. So in the example you have 3 methods:
SWGExampleApi::exampleGet(qint32 example1)SWGExampleApi::exampleGet(qint32 example1, qint32 example2)SWGExampleApi::exampleGet(qint32 example1, qint32 example2, qint32 example3)
- Use pointers as parameters:
SWGExampleApi::exampleGet(qint32* example1, qint32* example2 = nullptr, qint32* example3 = nullptr)
That would also solve my issue #6219
@meisterlampe nice to hear. Are you able to create a PR?
@wing328 @stkrwork @fehguy I do not want to spam but it looks like you have skills in the QT5 client :-)
Does anyone have any ideas? I tried to check for nullprt but this is not possible (see example below). I get the error No conversion from "nullptr" in "qint32". Maybe we should use pointers as argument as described in #1475 to make optional parameters possible? This will break backward compatibility
void
SWGExampleApi::exampleGet(qint32 example1, qint32 example2, qint32 example3) {
QString fullPath;
fullPath.append(this->host).append(this->basePath).append("/example");
if (example1 != nullptr) {
if (fullPath.indexOf("?") > 0)
fullPath.append("&");
else
fullPath.append("?");
fullPath.append(QUrl::toPercentEncoding("example1"))
.append("=")
.append(QUrl::toPercentEncoding(stringValue(example1)));
}
if (example2 != nullptr) {
if (fullPath.indexOf("?") > 0)
fullPath.append("&");
else
fullPath.append("?");
fullPath.append(QUrl::toPercentEncoding("example2"))
.append("=")
.append(QUrl::toPercentEncoding(stringValue(example2)));
}
if (example3 != nullptr) {
if (fullPath.indexOf("?") > 0)
fullPath.append("&");
else
fullPath.append("?");
fullPath.append(QUrl::toPercentEncoding("example3"))
.append("=")
.append(QUrl::toPercentEncoding(stringValue(example3)));
}
HttpRequestWorker *worker = new HttpRequestWorker();
HttpRequestInput input(fullPath, "GET");
foreach(QString key, this->defaultHeaders.keys()) {
input.headers.insert(key, this->defaultHeaders.value(key));
}
connect(worker,
&HttpRequestWorker::on_execution_finished,
this,
&SWGExampleApi::exampleGetCallback);
worker->execute(&input);
}
nullptr can't be assigned to something that is not a pointer. if you would really want to do this you would have to do:
if (&example1 != nullptr) {...}
The reference of the variable can be seen as a pointer, as it represents the address.
If you want to work with pointers here, you could try QSharedPointer. I saw in the QT documentation that that is the equivalent to a shared_pointer in normal C++.
You could also define values for non required parameters in the function header, for example:
void SWGExampleApi::exampleGet(qint32 example1, qint32 example2 = 0, qint32 example3 = 0);
I don't think there's an option other than:
- wrap primitives like
qint32in an object, which may be null - assign a magic number that would be treated as "not existing"
My vote would be point 2, where you could #define a magic number which is specific to your system and won't collide with a real value.
Creating a wrapper class like point 1 is probably a better long-term solution, but will break compatibility