Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[QT5CPP] Handle of additionalProperties #6316

Closed
stmeyer opened this issue Aug 15, 2017 · 4 comments
Closed

[QT5CPP] Handle of additionalProperties #6316

stmeyer opened this issue Aug 15, 2017 · 4 comments

Comments

@stmeyer
Copy link
Contributor

stmeyer commented Aug 15, 2017

Description

If you have a model definition with additionalProperties the generated code could not be compiles. You can see in generated code below that in the header file the variable is defined as QMap<QString, QString*>* values. In the CPP class file in there are two errors:

  • in method init it is tried to instanciate the value with values = new QMap<QString, QString>();. It has to be values = new QMap<QString, QString*>;
  • in method cleanup it is used QList<QString*>* arr = values; but is should be QMap<QString, QString*>* arr = values;
Swagger-codegen version

2.2.3 and current master

Swagger declaration file content or url
swagger: "2.0"
info:
  description: "example"
  version: "1.0.0"
  title: "exmaple"
  termsOfService: "http://example.io/terms/"
  contact:
    email: "[email protected]"
  license:
    name: "Apache 2.0"
    url: "http://www.apache.org/licenses/LICENSE-2.0.html"
host: "example.io"
basePath: "/v2"
tags:
- name: "example"
schemes:
- "http"
paths:
  /pet:
    get:
      tags:
      - "example"
      summary: "example"
      description: "example"
      consumes:
      - "application/json"
      - "application/xml"
      produces:
      - "application/xml"
      - "application/json"
      responses:
        405:
          description: "Invalid input"
        200:
          description: "successful operation"
          schema:
            type: "array"
            items:
              $ref: "#/definitions/Example"
definitions:
  Example:
    title: "example"
    properties:
      success:
        type: "boolean"
        default: true
      data:
        type: "object"
        properties:
          id:
            description: "id of entity"
            type: "integer"
            format: int64
          values:
            type: "object"
            additionalProperties:
              type: "string"
Command line used for generation

java -jar swagger-codegen-cli-2.2.3.jar generate -l qt5cpp -i swagger.json -o PathToFolder

Steps to reproduce

generate code with the swagger definition in this issue

Generated source code
/*
 * SWGPet_data.h
 * 
 * 
 */

#ifndef SWGPet_data_H_
#define SWGPet_data_H_

#include <QJsonObject>


#include <QList>
#include <QMap>
#include <QString>

#include "SWGObject.h"


namespace Swagger {

class SWGPet_data: public SWGObject {
public:
    SWGPet_data();
    SWGPet_data(QString* json);
    virtual ~SWGPet_data();
    void init();
    void cleanup();

    QString asJson ();
    QJsonObject* asJsonObject();
    void fromJsonObject(QJsonObject &json);
    SWGPet_data* fromJson(QString &jsonString);

    qint64 getId();
    void setId(qint64 id);

    QMap<QString, QString*>* getValues();
    void setValues(QMap<QString, QString*>* values);


private:
    qint64 id;
    QMap<QString, QString*>* values;
};

}


/*
 * SWGPet_data.cpp
 * 
 * 
 */
#include "SWGPet_data.h"

#include "SWGHelpers.h"

#include <QJsonDocument>
#include <QJsonArray>
#include <QObject>
#include <QDebug>

namespace Swagger {

SWGPet_data::SWGPet_data(QString* json) {
    init();
    this->fromJson(*json);
}

SWGPet_data::SWGPet_data() {
    init();
}

SWGPet_data::~SWGPet_data() {
    this->cleanup();
}

void
SWGPet_data::init() {
    id = 0L;
    values = new QMap<QString, QString>();
}

void
SWGPet_data::cleanup() {
    

    if(values != nullptr) {
        QList<QString*>* arr = values;
        foreach(QString* o, *arr) {
            delete o;
        }
        delete values;
    }
}

SWGPet_data*
SWGPet_data::fromJson(QString &json) {
    QByteArray array (json.toStdString().c_str());
    QJsonDocument doc = QJsonDocument::fromJson(array);
    QJsonObject jsonObject = doc.object();
    this->fromJsonObject(jsonObject);
    return this;
}

void
SWGPet_data::fromJsonObject(QJsonObject &pJson) {
    ::Swagger::setValue(&id, pJson["id"], "qint64", "");
    
    ::Swagger::setValue(&values, pJson["values"], "QMap", "QString");
    
}

QString
SWGPet_data::asJson ()
{
    QJsonObject* obj = this->asJsonObject();
    
    QJsonDocument doc(*obj);
    QByteArray bytes = doc.toJson();
    return QString(bytes);
}

QJsonObject*
SWGPet_data::asJsonObject() {
    QJsonObject* obj = new QJsonObject();
    
    obj->insert("id", QJsonValue(id));

    QJsonArray valuesJsonArray;
    toJsonArray((QList<void*>*)values, &valuesJsonArray, "values", "QString");
    obj->insert("values", valuesJsonArray);

    return obj;
}

qint64
SWGPet_data::getId() {
    return id;
}
void
SWGPet_data::setId(qint64 id) {
    this->id = id;
}

QMap<QString, QString*>*
SWGPet_data::getValues() {
    return values;
}
void
SWGPet_data::setValues(QMap<QString, QString*>* values) {
    this->values = values;
}


}
#endif /* SWGPet_data_H_ */
Related issues/PRs

maybe #5876

Suggest a fix/enhancement

generate code like described at the beginning of the issue

@wing328
Copy link
Contributor

wing328 commented Sep 5, 2017

cc @ravinikam @stkrwork

@wing328 wing328 added this to the v2.3.0 milestone Sep 5, 2017
@stkrwork
Copy link
Contributor

stkrwork commented Sep 7, 2017

I'll look into this

@stkrwork
Copy link
Contributor

The adjustments you suggested, are now in PR #6479

@wing328
Copy link
Contributor

wing328 commented Sep 19, 2017

@stmeyer the fix has been merged into master. Please pull the latest master to give it a try.

@stkrwork thanks for the PR to address the issue.

@wing328 wing328 closed this as completed Sep 19, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants