forked from loafle/openapi-generator-original
Fixes segmentation faults for arrays of primitive types (#7099)
[C++][Qt5] Fixes segmentation faults for arrays of primitive types
This commit is contained in:
parent
f1c52dc97e
commit
4b4528317c
@ -315,19 +315,13 @@ public class Qt5CPPGenerator extends AbstractCppCodegen implements CodegenConfig
|
||||
} else if (p instanceof DecimalProperty) {
|
||||
return "0.0";
|
||||
} else if (p instanceof MapProperty) {
|
||||
MapProperty ap = (MapProperty) p;
|
||||
String inner = getSwaggerType(ap.getAdditionalProperties());
|
||||
if (!languageSpecificPrimitives.contains(inner)) {
|
||||
inner += "*";
|
||||
}
|
||||
return "new QMap<QString, " + inner + ">()";
|
||||
MapProperty mp = (MapProperty) p;
|
||||
Property inner = mp.getAdditionalProperties();
|
||||
return "new QMap<QString, " + getTypeDeclaration(inner) + ">()";
|
||||
} else if (p instanceof ArrayProperty) {
|
||||
ArrayProperty ap = (ArrayProperty) p;
|
||||
String inner = getSwaggerType(ap.getItems());
|
||||
if (!languageSpecificPrimitives.contains(inner)) {
|
||||
inner += "*";
|
||||
}
|
||||
return "new QList<" + inner + ">()";
|
||||
Property inner = ap.getItems();
|
||||
return "new QList<" + getTypeDeclaration(inner) + ">()";
|
||||
}
|
||||
// else
|
||||
if (p instanceof RefProperty) {
|
||||
|
@ -132,10 +132,10 @@ setValue(void* value, QJsonValue obj, QString type, QString complexType) {
|
||||
}
|
||||
else if(type.startsWith("QList") && QString("").compare(complexType) != 0 && obj.isArray()) {
|
||||
// list of values
|
||||
QList<void*>* output = new QList<void*>();
|
||||
QJsonArray arr = obj.toArray();
|
||||
foreach (const QJsonValue & jval, arr) {
|
||||
if(complexType.startsWith("SWG")) {
|
||||
if(complexType.startsWith("SWG")) {
|
||||
QList<SWGObject*>* output = new QList<SWGObject*>();
|
||||
QJsonArray arr = obj.toArray();
|
||||
for (const QJsonValue & jval : arr) {
|
||||
// it's an object
|
||||
SWGObject * val = (SWGObject*)create(complexType);
|
||||
QJsonObject t = jval.toObject();
|
||||
@ -143,53 +143,102 @@ setValue(void* value, QJsonValue obj, QString type, QString complexType) {
|
||||
val->fromJsonObject(t);
|
||||
output->append(val);
|
||||
}
|
||||
else {
|
||||
// primitives
|
||||
if(QStringLiteral("qint32").compare(complexType) == 0) {
|
||||
qint32 val;
|
||||
setValue(&val, jval, QStringLiteral("qint32"), QStringLiteral(""));
|
||||
output->append((void*)&val);
|
||||
}
|
||||
else if(QStringLiteral("qint64").compare(complexType) == 0) {
|
||||
qint64 val;
|
||||
setValue(&val, jval, QStringLiteral("qint64"), QStringLiteral(""));
|
||||
output->append((void*)&val);
|
||||
}
|
||||
else if(QStringLiteral("bool").compare(complexType) == 0) {
|
||||
bool val;
|
||||
setValue(&val, jval, QStringLiteral("bool"), QStringLiteral(""));
|
||||
output->append((void*)&val);
|
||||
}
|
||||
else if(QStringLiteral("float").compare(complexType) == 0) {
|
||||
float val;
|
||||
setValue(&val, jval, QStringLiteral("float"), QStringLiteral(""));
|
||||
output->append((void*)&val);
|
||||
}
|
||||
else if(QStringLiteral("double").compare(complexType) == 0) {
|
||||
double val;
|
||||
setValue(&val, jval, QStringLiteral("double"), QStringLiteral(""));
|
||||
output->append((void*)&val);
|
||||
}
|
||||
else if(QStringLiteral("QString").compare(complexType) == 0) {
|
||||
QString * val = new QString();
|
||||
setValue(&val, jval, QStringLiteral("QString"), QStringLiteral(""));
|
||||
output->append((void*)val);
|
||||
}
|
||||
else if(QStringLiteral("QDate").compare(complexType) == 0) {
|
||||
QDate * val = new QDate();
|
||||
setValue(&val, jval, QStringLiteral("QDate"), QStringLiteral(""));
|
||||
output->append((void*)val);
|
||||
}
|
||||
else if(QStringLiteral("QDateTime").compare(complexType) == 0) {
|
||||
QDateTime * val = new QDateTime();
|
||||
setValue(&val, jval, QStringLiteral("QDateTime"), QStringLiteral(""));
|
||||
output->append((void*)val);
|
||||
}
|
||||
QList<SWGObject*> **val = static_cast<QList<SWGObject*>**>(value);
|
||||
for (auto item : **val) {
|
||||
delete item;
|
||||
}
|
||||
delete *val;
|
||||
*val = output;
|
||||
}
|
||||
else if(QStringLiteral("qint32").compare(complexType) == 0) {
|
||||
QList<qint32> **output = reinterpret_cast<QList<qint32> **> (value);
|
||||
(*output)->clear();
|
||||
QJsonArray arr = obj.toArray();
|
||||
for (const QJsonValue & jval : arr){
|
||||
qint32 val;
|
||||
setValue(&val, jval, QStringLiteral("qint32"), QStringLiteral(""));
|
||||
(*output)->push_back(val);
|
||||
}
|
||||
}
|
||||
else if(QStringLiteral("qint64").compare(complexType) == 0) {
|
||||
QList<qint64> **output = reinterpret_cast<QList<qint64> **> (value);
|
||||
(*output)->clear();
|
||||
QJsonArray arr = obj.toArray();
|
||||
for (const QJsonValue & jval : arr){
|
||||
qint64 val;
|
||||
setValue(&val, jval, QStringLiteral("qint64"), QStringLiteral(""));
|
||||
(*output)->push_back(val);
|
||||
}
|
||||
}
|
||||
else if(QStringLiteral("bool").compare(complexType) == 0) {
|
||||
QList<bool> **output = reinterpret_cast<QList<bool> **> (value);
|
||||
(*output)->clear();
|
||||
QJsonArray arr = obj.toArray();
|
||||
for (const QJsonValue & jval : arr){
|
||||
bool val;
|
||||
setValue(&val, jval, QStringLiteral("bool"), QStringLiteral(""));
|
||||
(*output)->push_back(val);
|
||||
}
|
||||
}
|
||||
else if(QStringLiteral("float").compare(complexType) == 0) {
|
||||
QList<float> **output = reinterpret_cast<QList<float> **> (value);
|
||||
(*output)->clear();
|
||||
QJsonArray arr = obj.toArray();
|
||||
for (const QJsonValue & jval : arr){
|
||||
float val;
|
||||
setValue(&val, jval, QStringLiteral("float"), QStringLiteral(""));
|
||||
(*output)->push_back(val);
|
||||
}
|
||||
}
|
||||
else if(QStringLiteral("double").compare(complexType) == 0) {
|
||||
QList<double> **output = reinterpret_cast<QList<double> **> (value);
|
||||
(*output)->clear();
|
||||
QJsonArray arr = obj.toArray();
|
||||
for (const QJsonValue & jval : arr){
|
||||
double val;
|
||||
setValue(&val, jval, QStringLiteral("double"), QStringLiteral(""));
|
||||
(*output)->push_back(val);
|
||||
}
|
||||
}
|
||||
else if(QStringLiteral("QString").compare(complexType) == 0) {
|
||||
QList<QString*> **output = reinterpret_cast<QList<QString*> **> (value);
|
||||
for (auto item : **output) {
|
||||
delete item;
|
||||
}
|
||||
(*output)->clear();
|
||||
QJsonArray arr = obj.toArray();
|
||||
for (const QJsonValue & jval : arr){
|
||||
QString * val = new QString();
|
||||
setValue(&val, jval, QStringLiteral("QString"), QStringLiteral(""));
|
||||
(*output)->push_back(val);
|
||||
}
|
||||
}
|
||||
else if(QStringLiteral("QDate").compare(complexType) == 0) {
|
||||
QList<QDate*> **output = reinterpret_cast<QList<QDate*> **> (value);
|
||||
for (auto item : **output) {
|
||||
delete item;
|
||||
}
|
||||
(*output)->clear();
|
||||
QJsonArray arr = obj.toArray();
|
||||
for (const QJsonValue & jval : arr){
|
||||
QDate * val = new QDate();
|
||||
setValue(&val, jval, QStringLiteral("QDate"), QStringLiteral(""));
|
||||
(*output)->push_back(val);
|
||||
}
|
||||
}
|
||||
else if(QStringLiteral("QDateTime").compare(complexType) == 0) {
|
||||
QList<QDateTime*> **output = reinterpret_cast<QList<QDateTime*> **> (value);
|
||||
for (auto item : **output) {
|
||||
delete item;
|
||||
}
|
||||
(*output)->clear();
|
||||
QJsonArray arr = obj.toArray();
|
||||
for (const QJsonValue & jval : arr){
|
||||
QDateTime * val = new QDateTime();
|
||||
setValue(&val, jval, QStringLiteral("QDateTime"), QStringLiteral(""));
|
||||
(*output)->push_back(val);
|
||||
}
|
||||
}
|
||||
QList<void*> **val = static_cast<QList<void*>**>(value);
|
||||
delete *val;
|
||||
*val = output;
|
||||
}
|
||||
}
|
||||
|
||||
@ -208,7 +257,7 @@ toJsonValue(QString name, void* value, QJsonObject* output, QString type) {
|
||||
}
|
||||
else {
|
||||
output->empty();
|
||||
foreach(QString key, o->keys()) {
|
||||
for(QString key : o->keys()) {
|
||||
output->insert(key, o->value(key));
|
||||
}
|
||||
}
|
||||
@ -254,11 +303,52 @@ toJsonValue(QString name, void* value, QJsonObject* output, QString type) {
|
||||
|
||||
void
|
||||
toJsonArray(QList<void*>* value, QJsonArray* output, QString innerName, QString innerType) {
|
||||
foreach(void* obj, *value) {
|
||||
QJsonObject element;
|
||||
|
||||
toJsonValue(nullptr, obj, &element, innerType);
|
||||
output->append(element);
|
||||
if(innerType.startsWith("SWG")){
|
||||
for(void* obj : *value) {
|
||||
SWGObject *swgObject = reinterpret_cast<SWGObject *>(obj);
|
||||
if(swgObject != nullptr) {
|
||||
output->append(*(swgObject->asJsonObject()));
|
||||
}
|
||||
}
|
||||
}
|
||||
else if(QStringLiteral("QString").compare(innerType) == 0) {
|
||||
for(QString* obj : *(reinterpret_cast<QList<QString*>*>(value))){
|
||||
output->append(QJsonValue(*obj));
|
||||
}
|
||||
}
|
||||
else if(QStringLiteral("QDate").compare(innerType) == 0) {
|
||||
for(QDate* obj : *(reinterpret_cast<QList<QDate*>*>(value))){
|
||||
output->append(QJsonValue(obj->toString(Qt::ISODate)));
|
||||
}
|
||||
}
|
||||
else if(QStringLiteral("QDateTime").compare(innerType) == 0) {
|
||||
for(QDateTime* obj : *(reinterpret_cast<QList<QDateTime*>*>(value))){
|
||||
output->append(QJsonValue(obj->toString(Qt::ISODate))); }
|
||||
}
|
||||
else if(QStringLiteral("QByteArray").compare(innerType) == 0) {
|
||||
for(QByteArray* obj : *(reinterpret_cast<QList<QByteArray*>*>(value))){
|
||||
output->append(QJsonValue(QString(obj->toBase64())));
|
||||
}
|
||||
}
|
||||
else if(QStringLiteral("qint32").compare(innerType) == 0) {
|
||||
for(qint32 obj : *(reinterpret_cast<QList<qint32>*>(value)))
|
||||
output->append(QJsonValue(obj));
|
||||
}
|
||||
else if(QStringLiteral("qint64").compare(innerType) == 0) {
|
||||
for(qint64 obj : *(reinterpret_cast<QList<qint64>*>(value)))
|
||||
output->append(QJsonValue(obj));
|
||||
}
|
||||
else if(QStringLiteral("bool").compare(innerType) == 0) {
|
||||
for(bool obj : *(reinterpret_cast<QList<bool>*>(value)))
|
||||
output->append(QJsonValue(obj));
|
||||
}
|
||||
else if(QStringLiteral("float").compare(innerType) == 0) {
|
||||
for(float obj : *(reinterpret_cast<QList<float>*>(value)))
|
||||
output->append(QJsonValue(obj));
|
||||
}
|
||||
else if(QStringLiteral("double").compare(innerType) == 0) {
|
||||
for(double obj : *(reinterpret_cast<QList<double>*>(value)))
|
||||
output->append(QJsonValue(obj));
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user