Skip to content

Commit

Permalink
fix NPE with cpp qt5, add logic to avoid NPE with composed schema (#267)
Browse files Browse the repository at this point in the history
  • Loading branch information
wing328 committed May 1, 2018
1 parent acb63fd commit 0b3ec6b
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1148,6 +1148,18 @@ public String toDefaultValueWithParam(String name, Schema schema) {
**/
@SuppressWarnings("static-method")
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
// get the schema/model name from $ref
String schemaName = ModelUtils.getSimpleRef(schema.get$ref());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ public String toDefaultValue(Schema p) {
@Override
public String getSchemaType(Schema p) {
String openAPIType = super.getSchemaType(p);

String type = null;
if (typeMapping.containsKey(openAPIType)) {
type = typeMapping.get(openAPIType);
Expand All @@ -362,6 +363,11 @@ public String getSchemaType(Schema p) {

@Override
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) ||
typeMapping.values().contains(type) ||
importMapping.values().contains(type) ||
Expand Down
100 changes: 84 additions & 16 deletions samples/client/petstore/qt5cpp/client/SWGPetApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ SWGPetApi::SWGPetApi(QString host, QString basePath) {
}

void
SWGPetApi::addPet(SWGPet& swg_pet) {
SWGPetApi::addPet(std::shared_ptr<SWGSWGPet>& swg_pet) {
QString fullPath;
fullPath.append(this->host).append(this->basePath).append("/pet");

Expand Down Expand Up @@ -141,13 +141,47 @@ SWGPetApi::findPetsByStatus(QList<QString*>* status) {
fullPath.append(this->host).append(this->basePath).append("/pet/findByStatus");


if (fullPath.indexOf("?") > 0)
fullPath.append("&");
else
fullPath.append("?");
fullPath.append(QUrl::toPercentEncoding("status"))
.append("=")
.append(QUrl::toPercentEncoding(stringValue(status)));


if (status->size() > 0) {
if (QString("csv").indexOf("multi") == 0) {
foreach(QString* t, *status) {
if (fullPath.indexOf("?") > 0)
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();
Expand Down Expand Up @@ -214,13 +248,47 @@ SWGPetApi::findPetsByTags(QList<QString*>* tags) {
fullPath.append(this->host).append(this->basePath).append("/pet/findByTags");


if (fullPath.indexOf("?") > 0)
fullPath.append("&");
else
fullPath.append("?");
fullPath.append(QUrl::toPercentEncoding("tags"))
.append("=")
.append(QUrl::toPercentEncoding(stringValue(tags)));


if (tags->size() > 0) {
if (QString("csv").indexOf("multi") == 0) {
foreach(QString* t, *tags) {
if (fullPath.indexOf("?") > 0)
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();
Expand Down Expand Up @@ -337,7 +405,7 @@ SWGPetApi::getPetByIdCallback(SWGHttpRequestWorker * worker) {
}

void
SWGPetApi::updatePet(SWGPet& swg_pet) {
SWGPetApi::updatePet(std::shared_ptr<SWGSWGPet>& swg_pet) {
QString fullPath;
fullPath.append(this->host).append(this->basePath).append("/pet");

Expand Down
4 changes: 2 additions & 2 deletions samples/client/petstore/qt5cpp/client/SWGPetApi.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ class SWGPetApi: public QObject {
QString basePath;
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 findPetsByStatus(QList<QString*>* status);
void findPetsByTags(QList<QString*>* tags);
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 uploadFile(qint64 pet_id, QString* additional_metadata, SWGHttpRequestInputFileElement* file);

Expand Down
2 changes: 1 addition & 1 deletion samples/client/petstore/qt5cpp/client/SWGStoreApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ SWGStoreApi::getOrderByIdCallback(SWGHttpRequestWorker * worker) {
}

void
SWGStoreApi::placeOrder(SWGOrder& swg_order) {
SWGStoreApi::placeOrder(std::shared_ptr<SWGSWGOrder>& swg_order) {
QString fullPath;
fullPath.append(this->host).append(this->basePath).append("/store/order");

Expand Down
2 changes: 1 addition & 1 deletion samples/client/petstore/qt5cpp/client/SWGStoreApi.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class SWGStoreApi: public QObject {
void deleteOrder(QString* order_id);
void getInventory();
void getOrderById(qint64 order_id);
void placeOrder(SWGOrder& swg_order);
void placeOrder(std::shared_ptr<SWGSWGOrder>& swg_order);

private:
void deleteOrderCallback (SWGHttpRequestWorker * worker);
Expand Down
4 changes: 2 additions & 2 deletions samples/client/petstore/qt5cpp/client/SWGUserApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ SWGUserApi::SWGUserApi(QString host, QString basePath) {
}

void
SWGUserApi::createUser(SWGUser& swg_user) {
SWGUserApi::createUser(std::shared_ptr<SWGSWGUser>& swg_user) {
QString fullPath;
fullPath.append(this->host).append(this->basePath).append("/user");

Expand Down Expand Up @@ -418,7 +418,7 @@ SWGUserApi::logoutUserCallback(SWGHttpRequestWorker * worker) {
}

void
SWGUserApi::updateUser(QString* username, SWGUser& swg_user) {
SWGUserApi::updateUser(QString* username, std::shared_ptr<SWGSWGUser>& swg_user) {
QString fullPath;
fullPath.append(this->host).append(this->basePath).append("/user/{username}");

Expand Down
5 changes: 3 additions & 2 deletions samples/client/petstore/qt5cpp/client/SWGUserApi.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#include "SWGHttpRequest.h"

#include <QList>
#include <QString>
#include "SWGUser.h"

Expand All @@ -34,14 +35,14 @@ class SWGUserApi: public QObject {
QString basePath;
QMap<QString, QString> defaultHeaders;

void createUser(SWGUser& swg_user);
void createUser(std::shared_ptr<SWGSWGUser>& swg_user);
void createUsersWithArrayInput(QList<SWGUser*>*& swg_user);
void createUsersWithListInput(QList<SWGUser*>*& swg_user);
void deleteUser(QString* username);
void getUserByName(QString* username);
void loginUser(QString* username, QString* password);
void logoutUser();
void updateUser(QString* username, SWGUser& swg_user);
void updateUser(QString* username, std::shared_ptr<SWGSWGUser>& swg_user);

private:
void createUserCallback (SWGHttpRequestWorker * worker);
Expand Down

0 comments on commit 0b3ec6b

Please sign in to comment.