Skip to content

[QT5CPP] Handle of additionalProperties #6316

@stmeyer

Description

@stmeyer
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: "apiteam@example.io"
  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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions