swagger-api / swagger-api/swagger-codegen

[QT5CPP] Client not handling optional parameter

Open
#6,314 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

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:
  1. SWGExampleApi::exampleGet(qint32 example1)
  2. SWGExampleApi::exampleGet(qint32 example1, qint32 example2)
  3. SWGExampleApi::exampleGet(qint32 example1, qint32 example2, qint32 example3)
  • Use pointers as parameters: SWGExampleApi::exampleGet(qint32* example1, qint32* example2 = nullptr, qint32* example3 = nullptr)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Swagger YAML and the generated SWGExampleApi.h and SWGExampleApi.cpp, then locate the qt5cpp generator templates or entry point that produced the method signature and query handling. Regenerate the example and verify that required parameters remain usable while optional parameters can be omitted without being sent.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
api, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.