Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.swagger.v3.parser.util.SchemaTypeUtil;
import org.apache.commons.lang3.StringUtils;
import org.openapitools.codegen.CodegenConfig;
import org.openapitools.codegen.CodegenModel;
import org.openapitools.codegen.CodegenOperation;
import org.openapitools.codegen.CodegenParameter;
import org.openapitools.codegen.utils.ModelUtils;
Expand Down Expand Up @@ -280,7 +281,19 @@ public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> o
List<CodegenOperation> operations = (List<CodegenOperation>) objectMap.get("operation");

List<Map<String, String>> imports = (List<Map<String, String>>) objs.get("imports");
Map<String, CodegenModel> codegenModels = new HashMap<String, CodegenModel> ();
for(Object moObj : allModels) {
CodegenModel mo = ((Map<String, CodegenModel>) moObj).get("model");
if(mo.isEnum) {
codegenModels.put(mo.classname, mo);
}
}
for (CodegenOperation operation : operations) {
if(operation.returnType != null) {
if(codegenModels.containsKey(operation.returnType)){
operation.vendorExtensions.put("returnsEnum", true);
}
}
// Check all return parameter baseType if there is a necessity to include, include it if not
// already done
if (operation.returnBaseType != null && needToImport(operation.returnBaseType)) {
Expand Down Expand Up @@ -316,6 +329,21 @@ public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> o
return objs;
}

@Override
public Map<String, Object> postProcessModels(Map<String, Object> objs) {
return postProcessModelsEnum(objs);
}

@Override
public String toEnumValue(String value, String datatype) {
return escapeText(value);
}

@Override
public boolean isDataTypeString(String dataType) {
return "QString".equals(dataType);
}

private Map<String, String> createMapping(String key, String value) {
Map<String, String> customImport = new HashMap<String, String>();
customImport.put(key, toModelImport(value));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public CppQt5ClientCodegen() {
supportingFiles.add(new SupportingFile("HttpRequest.h.mustache", sourceFolder, PREFIX + "HttpRequest.h"));
supportingFiles.add(new SupportingFile("HttpRequest.cpp.mustache", sourceFolder, PREFIX + "HttpRequest.cpp"));
supportingFiles.add(new SupportingFile("object.mustache", sourceFolder, PREFIX + "Object.h"));
supportingFiles.add(new SupportingFile("enum.mustache", sourceFolder, PREFIX + "Enum.h"));
if (optionalProjectFileFlag) {
supportingFiles.add(new SupportingFile("Project.mustache", sourceFolder, "client.pri"));
}
Expand All @@ -94,6 +95,7 @@ public void processOpts() {
supportingFiles.add(new SupportingFile("HttpRequest.h.mustache", sourceFolder, modelNamePrefix + "HttpRequest.h"));
supportingFiles.add(new SupportingFile("HttpRequest.cpp.mustache", sourceFolder, modelNamePrefix + "HttpRequest.cpp"));
supportingFiles.add(new SupportingFile("object.mustache", sourceFolder, modelNamePrefix + "Object.h"));
supportingFiles.add(new SupportingFile("enum.mustache", sourceFolder, modelNamePrefix + "Enum.h"));

typeMapping.put("file", modelNamePrefix + "HttpRequestInputFileElement");
typeMapping.put("binary", modelNamePrefix + "HttpRequestInputFileElement");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public CppQt5QHttpEngineServerCodegen() {
supportingFiles.add(new SupportingFile("helpers-header.mustache", sourceFolder + MODEL_DIR, PREFIX + "Helpers.h"));
supportingFiles.add(new SupportingFile("helpers-body.mustache", sourceFolder + MODEL_DIR, PREFIX + "Helpers.cpp"));
supportingFiles.add(new SupportingFile("object.mustache", sourceFolder + MODEL_DIR, PREFIX + "Object.h"));
supportingFiles.add(new SupportingFile("enum.mustache", sourceFolder + MODEL_DIR, PREFIX + "Enum.h"));
supportingFiles.add(new SupportingFile("apirouter.h.mustache", sourceFolder + APIHANDLER_DIR, PREFIX + "ApiRouter.h"));
supportingFiles.add(new SupportingFile("apirouter.cpp.mustache", sourceFolder + APIHANDLER_DIR, PREFIX + "ApiRouter.cpp"));

Expand All @@ -106,6 +107,7 @@ public void processOpts() {
supportingFiles.add(new SupportingFile("helpers-header.mustache", sourceFolder + MODEL_DIR, modelNamePrefix + "Helpers.h"));
supportingFiles.add(new SupportingFile("helpers-body.mustache", sourceFolder + MODEL_DIR, modelNamePrefix + "Helpers.cpp"));
supportingFiles.add(new SupportingFile("object.mustache", sourceFolder + MODEL_DIR, modelNamePrefix + "Object.h"));
supportingFiles.add(new SupportingFile("enum.mustache", sourceFolder + MODEL_DIR, modelNamePrefix + "Enum.h"));
supportingFiles.add(new SupportingFile("apirouter.h.mustache", sourceFolder + APIHANDLER_DIR, modelNamePrefix + "ApiRouter.h"));
supportingFiles.add(new SupportingFile("apirouter.cpp.mustache", sourceFolder + APIHANDLER_DIR, modelNamePrefix + "ApiRouter.cpp"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ HEADERS += \
$${PWD}/{{prefix}}Helpers.h \
$${PWD}/{{prefix}}HttpRequest.h \
$${PWD}/{{prefix}}Object.h
$${PWD}/{{prefix}}Enum.h

SOURCES += \
# Models
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{{>licenseInfo}}
#ifndef {{prefix}}_ENUM_H
#define {{prefix}}_ENUM_H

#include <QString>
#include <QJsonValue>

{{#cppNamespaceDeclarations}}
namespace {{this}} {
{{/cppNamespaceDeclarations}}

class {{prefix}}Enum {
public:
{{prefix}}Enum() {

}

{{prefix}}Enum(QString jsonString) {
fromJson(jsonString);
}

virtual ~{{prefix}}Enum(){

}

virtual QJsonValue asJsonValue() const {
return QJsonValue(jstr);
}

virtual QString asJson() const {
return jstr;
}

virtual void fromJson(QString jsonString) {
jstr = jsonString;
}

virtual void fromJsonValue(QJsonValue jval) {
jstr = jval.toString();
}

virtual bool isSet() const {
return false;
}

virtual bool isValid() const {
return true;
}
private :
QString jstr;
};

{{#cppNamespaceDeclarations}}
}
{{/cppNamespaceDeclarations}}

#endif // {{prefix}}_ENUM_H
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{{>licenseInfo}}
#include <QDebug>
#include "{{prefix}}Helpers.h"
#include "{{prefix}}Object.h"


{{#cppNamespaceDeclarations}}
namespace {{this}} {
Expand Down Expand Up @@ -55,6 +55,11 @@ toStringValue(const double &value){
return QString::number(value);
}

QString
toStringValue(const {{prefix}}Enum &value){
return value.asJson();
}

QJsonValue
toJsonValue(const QString &value){
return QJsonValue(value);
Expand Down Expand Up @@ -105,6 +110,11 @@ toJsonValue(const {{prefix}}Object &value){
return value.asJsonObject();
}

QJsonValue
toJsonValue(const {{prefix}}Enum &value){
return value.asJsonValue();
}

bool
fromStringValue(const QString &inStr, QString &value){
value.clear();
Expand Down Expand Up @@ -193,6 +203,12 @@ fromStringValue(const QString &inStr, double &value){
return ok;
}

bool
fromStringValue(const QString &inStr, {{prefix}}Enum &value){
value.fromJson(inStr);
return true;
}

bool
fromJsonValue(QString &value, const QJsonValue &jval){
bool ok = true;
Expand Down Expand Up @@ -315,6 +331,12 @@ fromJsonValue({{prefix}}Object &value, const QJsonValue &jval){
return ok;
}

bool
fromJsonValue({{prefix}}Enum &value, const QJsonValue &jval){
value.fromJsonValue(jval);
return true;
}

{{#cppNamespaceDeclarations}}
}
{{/cppNamespaceDeclarations}}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <QDate>
#include <QVariant>
#include "{{prefix}}Object.h"
#include "{{prefix}}Enum.h"

{{#cppNamespaceDeclarations}}
namespace {{this}} {
Expand All @@ -26,6 +27,7 @@ namespace {{this}} {
QString toStringValue(const bool &value);
QString toStringValue(const float &value);
QString toStringValue(const double &value);
QString toStringValue(const {{prefix}}Enum &value);

template <typename T>
QString toStringValue(const QList<T> &val) {
Expand All @@ -49,6 +51,7 @@ namespace {{this}} {
QJsonValue toJsonValue(const float &value);
QJsonValue toJsonValue(const double &value);
QJsonValue toJsonValue(const {{prefix}}Object &value);
QJsonValue toJsonValue(const {{prefix}}Enum &value);

template <typename T>
QJsonValue toJsonValue(const QList<T> &val) {
Expand Down Expand Up @@ -77,6 +80,7 @@ namespace {{this}} {
bool fromStringValue(const QString &inStr, bool &value);
bool fromStringValue(const QString &inStr, float &value);
bool fromStringValue(const QString &inStr, double &value);
bool fromStringValue(const QString &inStr, {{prefix}}Enum &value);

template <typename T>
bool fromStringValue(const QList<QString> &inStr, QList<T> &val) {
Expand Down Expand Up @@ -110,6 +114,7 @@ namespace {{this}} {
bool fromJsonValue(float &value, const QJsonValue &jval);
bool fromJsonValue(double &value, const QJsonValue &jval);
bool fromJsonValue({{prefix}}Object &value, const QJsonValue &jval);
bool fromJsonValue({{prefix}}Enum &value, const QJsonValue &jval);

template <typename T>
bool fromJsonValue(QList<T> &val, const QJsonValue &jval) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,31 @@ namespace {{this}} {

void
{{classname}}::init() {
{{#vars}}
{{^isEnum}}{{#vars}}
m_{{name}}_isSet = false;
m_{{name}}_isValid = false;
{{/vars}}
{{/vars}}{{/isEnum}}{{#isEnum}}
m_value_isSet = false;
m_value_isValid = false;
m_value = e{{classname}}::INVALID_VALUE_OPENAPI_GENERATED;
{{/isEnum}}
}

void
{{classname}}::fromJson(QString jsonString) {
QByteArray array (jsonString.toStdString().c_str());
{{^isEnum}}QByteArray array (jsonString.toStdString().c_str());
QJsonDocument doc = QJsonDocument::fromJson(array);
QJsonObject jsonObject = doc.object();
this->fromJsonObject(jsonObject);
this->fromJsonObject(jsonObject);{{/isEnum}}{{#isEnum}}{{#allowableValues}}{{#enumVars}}
{{#-first}}if{{/-first}}{{^-first}}else if{{/-first}} ( jsonString.compare({{#isString}}"{{value}}"{{/isString}}{{^isString}}QString::number({{value}}){{/isString}}, Qt::CaseInsensitive) == 0) {
m_value = e{{classname}}::{{name}};
m_value_isValid = true;
}{{/enumVars}}{{/allowableValues}}{{/isEnum}}
}

void
{{classname}}::fromJsonObject(QJsonObject json) {
{{#vars}}
{{classname}}::fromJson{{^isEnum}}Object{{/isEnum}}{{#isEnum}}Value{{/isEnum}}(QJson{{^isEnum}}Object{{/isEnum}}{{#isEnum}}Value{{/isEnum}} json) {
{{^isEnum}}{{#vars}}
{{^isContainer}}m_{{name}}_isValid = ::{{cppNamespace}}::fromJsonValue({{name}}, json[QString("{{baseName}}")]);{{/isContainer}}
{{#isContainer}}{{^items.isContainer}}m_{{name}}_isValid = ::{{cppNamespace}}::fromJsonValue({{name}}, json[QString("{{baseName}}")]);{{/items.isContainer}}{{#items.isContainer}}{{#isListContainer}}
if(json["{{baseName}}"].isArray()){
Expand All @@ -68,33 +76,45 @@ void
}
}
}{{/isMapContainer}}{{/items.isContainer}}{{/isContainer}}
{{/vars}}
{{/vars}}{{/isEnum}}{{#isEnum}}
{{#allowableValues}}{{#enumVars}}{{#-first}}{{#isString}}fromJson(json.toString());{{/isString}}{{^isString}}m_value = static_cast<e{{classname}}>(json.toInt());{{/isString}}{{/-first}}{{/enumVars}}{{/allowableValues}}{{/isEnum}}
}

QString
{{classname}}::asJson () const {
QJsonObject obj = this->asJsonObject();
{{^isEnum}}QJsonObject obj = this->asJsonObject();
QJsonDocument doc(obj);
QByteArray bytes = doc.toJson();
return QString(bytes);
return QString(bytes);{{/isEnum}}{{#isEnum}}
QString val;
{{#allowableValues}}
switch (m_value){
{{#enumVars}}
case e{{classname}}::{{name}}:
val = {{#isString}}"{{value}}"{{/isString}}{{^isString}}QString::number({{value}}){{/isString}};
break;{{#-last}}
default:
break;{{/-last}}
{{/enumVars}}
}{{/allowableValues}}
return val;{{/isEnum}}
}

QJsonObject
{{classname}}::asJsonObject() const {
QJsonObject obj;
{{#vars}}
QJson{{^isEnum}}Object{{/isEnum}}{{#isEnum}}Value{{/isEnum}}
{{classname}}::asJson{{^isEnum}}Object{{/isEnum}}{{#isEnum}}Value{{/isEnum}}() const {
{{^isEnum}}QJsonObject obj;{{#vars}}
{{^isContainer}}{{#complexType}}if({{name}}.isSet()){{/complexType}}{{^complexType}}if(m_{{name}}_isSet){{/complexType}}{
obj.insert(QString("{{baseName}}"), ::{{cppNamespace}}::toJsonValue({{name}}));
}{{/isContainer}}{{#isContainer}}
if({{name}}.size() > 0){
{{^items.isContainer}}obj.insert(QString("{{baseName}}"), ::{{cppNamespace}}::toJsonValue({{name}}));{{/items.isContainer}}{{#items.isContainer}}
obj.insert(QString("{{baseName}}"), toJsonValue({{name}}));{{/items.isContainer}}
} {{/isContainer}}
{{/vars}}
return obj;
} {{/isContainer}}{{/vars}}
return obj;{{/isEnum}}{{#isEnum}}
{{#allowableValues}}{{#enumVars}}{{#-first}}{{^isString}}return QJsonValue(static_cast<int>(m_value));{{/isString}}{{#isString}}return QJsonValue(asJson());{{/isString}}{{/-first}}{{/enumVars}}{{/allowableValues}}{{/isEnum}}
}

{{#vars}}
{{^isEnum}}{{#vars}}
{{{dataType}}}
{{classname}}::{{getter}}() const {
return {{name}};
Expand All @@ -105,20 +125,30 @@ void
this->m_{{name}}_isSet = true;
}

{{/vars}}
{{/vars}}{{/isEnum}}{{#isEnum}}
{{classname}}::e{{classname}} {{classname}}::getValue() const {
return m_value;
}

void {{classname}}::setValue(const {{classname}}::e{{classname}}& value){
m_value = value;
m_value_isSet = true;
}
{{/isEnum}}
bool
{{classname}}::isSet() const {
bool isObjectUpdated = false;
{{^isEnum}}bool isObjectUpdated = false;
do{ {{#vars}}
{{#isContainer}}if({{name}}.size() > 0){{/isContainer}}{{^isContainer}}{{#complexType}}if({{name}}.isSet()){{/complexType}}{{^complexType}}if(m_{{name}}_isSet){{/complexType}}{{/isContainer}}{ isObjectUpdated = true; break;}
{{/vars}}}while(false);
return isObjectUpdated;
return isObjectUpdated;{{/isEnum}}{{#isEnum}}
return m_value_isSet;{{/isEnum}}
}

bool
{{classname}}::isValid() const {
// only required properties are required for the object to be considered valid
return {{#vars}}{{#required}}m_{{name}}_isValid && {{/required}}{{/vars}}true;
return {{^isEnum}}{{#vars}}{{#required}}m_{{name}}_isValid && {{/required}}{{/vars}}true{{/isEnum}}{{#isEnum}}m_value_isValid{{/isEnum}};
}

{{#cppNamespaceDeclarations}}
Expand Down
Loading