fix NPE with cpp qt5, add logic to avoid NPE with composed schema (#267)

This commit is contained in:
William Cheng 2018-05-01 12:05:57 +08:00 committed by GitHub
parent acb63fd5e8
commit 0b3ec6b1f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 111 additions and 24 deletions

View File

@ -1148,6 +1148,18 @@ public class DefaultCodegen implements CodegenConfig {
**/ **/
@SuppressWarnings("static-method") @SuppressWarnings("static-method")
public String getSchemaType(Schema schema) { public String getSchemaType(Schema schema) {
// TODO better logic to handle compose schema
if (schema instanceof ComposedSchema) { // composed schema
ComposedSchema cs = (ComposedSchema) schema;
for (Schema s : cs.getAllOf()) {
if (s != null) {
// using the first schema defined in allOf
schema = s;
break;
}
}
}
if (StringUtils.isNotBlank(schema.get$ref())) { // object if (StringUtils.isNotBlank(schema.get$ref())) { // object
// get the schema/model name from $ref // get the schema/model name from $ref
String schemaName = ModelUtils.getSimpleRef(schema.get$ref()); String schemaName = ModelUtils.getSimpleRef(schema.get$ref());

View File

@ -345,6 +345,7 @@ public class CppQt5ClientCodegen extends AbstractCppCodegen implements CodegenCo
@Override @Override
public String getSchemaType(Schema p) { public String getSchemaType(Schema p) {
String openAPIType = super.getSchemaType(p); String openAPIType = super.getSchemaType(p);
String type = null; String type = null;
if (typeMapping.containsKey(openAPIType)) { if (typeMapping.containsKey(openAPIType)) {
type = typeMapping.get(openAPIType); type = typeMapping.get(openAPIType);
@ -362,6 +363,11 @@ public class CppQt5ClientCodegen extends AbstractCppCodegen implements CodegenCo
@Override @Override
public String toModelName(String type) { public String toModelName(String type) {
if (type == null) {
LOGGER.warn("Model name can't be null. Defaul to 'UnknownModel'.");
type = "UnknownModel";
}
if (typeMapping.keySet().contains(type) || if (typeMapping.keySet().contains(type) ||
typeMapping.values().contains(type) || typeMapping.values().contains(type) ||
importMapping.values().contains(type) || importMapping.values().contains(type) ||

View File

@ -30,7 +30,7 @@ SWGPetApi::SWGPetApi(QString host, QString basePath) {
} }
void void
SWGPetApi::addPet(SWGPet& swg_pet) { SWGPetApi::addPet(std::shared_ptr<SWGSWGPet>& swg_pet) {
QString fullPath; QString fullPath;
fullPath.append(this->host).append(this->basePath).append("/pet"); fullPath.append(this->host).append(this->basePath).append("/pet");
@ -141,13 +141,47 @@ SWGPetApi::findPetsByStatus(QList<QString*>* status) {
fullPath.append(this->host).append(this->basePath).append("/pet/findByStatus"); fullPath.append(this->host).append(this->basePath).append("/pet/findByStatus");
if (fullPath.indexOf("?") > 0)
fullPath.append("&");
else if (status->size() > 0) {
fullPath.append("?"); if (QString("csv").indexOf("multi") == 0) {
fullPath.append(QUrl::toPercentEncoding("status")) foreach(QString* t, *status) {
.append("=") if (fullPath.indexOf("?") > 0)
.append(QUrl::toPercentEncoding(stringValue(status))); fullPath.append("&");
else
fullPath.append("?");
fullPath.append("status=").append(stringValue(t));
}
}
else if (QString("csv").indexOf("ssv") == 0) {
if (fullPath.indexOf("?") > 0)
fullPath.append("&");
else
fullPath.append("?");
fullPath.append("status=");
qint32 count = 0;
foreach(QString* t, *status) {
if (count > 0) {
fullPath.append(" ");
}
fullPath.append(stringValue(t));
}
}
else if (QString("csv").indexOf("tsv") == 0) {
if (fullPath.indexOf("?") > 0)
fullPath.append("&");
else
fullPath.append("?");
fullPath.append("status=");
qint32 count = 0;
foreach(QString* t, *status) {
if (count > 0) {
fullPath.append("\t");
}
fullPath.append(stringValue(t));
}
}
}
SWGHttpRequestWorker *worker = new SWGHttpRequestWorker(); SWGHttpRequestWorker *worker = new SWGHttpRequestWorker();
@ -214,13 +248,47 @@ SWGPetApi::findPetsByTags(QList<QString*>* tags) {
fullPath.append(this->host).append(this->basePath).append("/pet/findByTags"); fullPath.append(this->host).append(this->basePath).append("/pet/findByTags");
if (fullPath.indexOf("?") > 0)
fullPath.append("&");
else if (tags->size() > 0) {
fullPath.append("?"); if (QString("csv").indexOf("multi") == 0) {
fullPath.append(QUrl::toPercentEncoding("tags")) foreach(QString* t, *tags) {
.append("=") if (fullPath.indexOf("?") > 0)
.append(QUrl::toPercentEncoding(stringValue(tags))); fullPath.append("&");
else
fullPath.append("?");
fullPath.append("tags=").append(stringValue(t));
}
}
else if (QString("csv").indexOf("ssv") == 0) {
if (fullPath.indexOf("?") > 0)
fullPath.append("&");
else
fullPath.append("?");
fullPath.append("tags=");
qint32 count = 0;
foreach(QString* t, *tags) {
if (count > 0) {
fullPath.append(" ");
}
fullPath.append(stringValue(t));
}
}
else if (QString("csv").indexOf("tsv") == 0) {
if (fullPath.indexOf("?") > 0)
fullPath.append("&");
else
fullPath.append("?");
fullPath.append("tags=");
qint32 count = 0;
foreach(QString* t, *tags) {
if (count > 0) {
fullPath.append("\t");
}
fullPath.append(stringValue(t));
}
}
}
SWGHttpRequestWorker *worker = new SWGHttpRequestWorker(); SWGHttpRequestWorker *worker = new SWGHttpRequestWorker();
@ -337,7 +405,7 @@ SWGPetApi::getPetByIdCallback(SWGHttpRequestWorker * worker) {
} }
void void
SWGPetApi::updatePet(SWGPet& swg_pet) { SWGPetApi::updatePet(std::shared_ptr<SWGSWGPet>& swg_pet) {
QString fullPath; QString fullPath;
fullPath.append(this->host).append(this->basePath).append("/pet"); fullPath.append(this->host).append(this->basePath).append("/pet");

View File

@ -36,12 +36,12 @@ public:
QString basePath; QString basePath;
QMap<QString, QString> defaultHeaders; QMap<QString, QString> defaultHeaders;
void addPet(SWGPet& swg_pet); void addPet(std::shared_ptr<SWGSWGPet>& swg_pet);
void deletePet(qint64 pet_id, QString* api_key); void deletePet(qint64 pet_id, QString* api_key);
void findPetsByStatus(QList<QString*>* status); void findPetsByStatus(QList<QString*>* status);
void findPetsByTags(QList<QString*>* tags); void findPetsByTags(QList<QString*>* tags);
void getPetById(qint64 pet_id); void getPetById(qint64 pet_id);
void updatePet(SWGPet& swg_pet); void updatePet(std::shared_ptr<SWGSWGPet>& swg_pet);
void updatePetWithForm(qint64 pet_id, QString* name, QString* status); void updatePetWithForm(qint64 pet_id, QString* name, QString* status);
void uploadFile(qint64 pet_id, QString* additional_metadata, SWGHttpRequestInputFileElement* file); void uploadFile(qint64 pet_id, QString* additional_metadata, SWGHttpRequestInputFileElement* file);

View File

@ -196,7 +196,7 @@ SWGStoreApi::getOrderByIdCallback(SWGHttpRequestWorker * worker) {
} }
void void
SWGStoreApi::placeOrder(SWGOrder& swg_order) { SWGStoreApi::placeOrder(std::shared_ptr<SWGSWGOrder>& swg_order) {
QString fullPath; QString fullPath;
fullPath.append(this->host).append(this->basePath).append("/store/order"); fullPath.append(this->host).append(this->basePath).append("/store/order");

View File

@ -38,7 +38,7 @@ public:
void deleteOrder(QString* order_id); void deleteOrder(QString* order_id);
void getInventory(); void getInventory();
void getOrderById(qint64 order_id); void getOrderById(qint64 order_id);
void placeOrder(SWGOrder& swg_order); void placeOrder(std::shared_ptr<SWGSWGOrder>& swg_order);
private: private:
void deleteOrderCallback (SWGHttpRequestWorker * worker); void deleteOrderCallback (SWGHttpRequestWorker * worker);

View File

@ -30,7 +30,7 @@ SWGUserApi::SWGUserApi(QString host, QString basePath) {
} }
void void
SWGUserApi::createUser(SWGUser& swg_user) { SWGUserApi::createUser(std::shared_ptr<SWGSWGUser>& swg_user) {
QString fullPath; QString fullPath;
fullPath.append(this->host).append(this->basePath).append("/user"); fullPath.append(this->host).append(this->basePath).append("/user");
@ -418,7 +418,7 @@ SWGUserApi::logoutUserCallback(SWGHttpRequestWorker * worker) {
} }
void void
SWGUserApi::updateUser(QString* username, SWGUser& swg_user) { SWGUserApi::updateUser(QString* username, std::shared_ptr<SWGSWGUser>& swg_user) {
QString fullPath; QString fullPath;
fullPath.append(this->host).append(this->basePath).append("/user/{username}"); fullPath.append(this->host).append(this->basePath).append("/user/{username}");

View File

@ -15,6 +15,7 @@
#include "SWGHttpRequest.h" #include "SWGHttpRequest.h"
#include <QList>
#include <QString> #include <QString>
#include "SWGUser.h" #include "SWGUser.h"
@ -34,14 +35,14 @@ public:
QString basePath; QString basePath;
QMap<QString, QString> defaultHeaders; QMap<QString, QString> defaultHeaders;
void createUser(SWGUser& swg_user); void createUser(std::shared_ptr<SWGSWGUser>& swg_user);
void createUsersWithArrayInput(QList<SWGUser*>*& swg_user); void createUsersWithArrayInput(QList<SWGUser*>*& swg_user);
void createUsersWithListInput(QList<SWGUser*>*& swg_user); void createUsersWithListInput(QList<SWGUser*>*& swg_user);
void deleteUser(QString* username); void deleteUser(QString* username);
void getUserByName(QString* username); void getUserByName(QString* username);
void loginUser(QString* username, QString* password); void loginUser(QString* username, QString* password);
void logoutUser(); void logoutUser();
void updateUser(QString* username, SWGUser& swg_user); void updateUser(QString* username, std::shared_ptr<SWGSWGUser>& swg_user);
private: private:
void createUserCallback (SWGHttpRequestWorker * worker); void createUserCallback (SWGHttpRequestWorker * worker);