From ea4b948428654e5c7066014bccd9843e47ab5e64 Mon Sep 17 00:00:00 2001 From: etherealjoy <33183834+etherealjoy@users.noreply.github.com> Date: Fri, 19 Jan 2018 08:36:45 +0100 Subject: [PATCH] [qt5cpp] Add nullptr guard to prevent crash when empty model is being serialized (#7434) * Small fixes to prevent crash when empty json body is provided. * Updated PetStore example for Qt5 --- .../src/main/resources/qt5cpp/helpers-header.mustache | 2 ++ .../src/main/resources/qt5cpp/model-body.mustache | 2 +- samples/client/petstore/qt5cpp/client/SWGHelpers.h | 2 ++ samples/client/petstore/qt5cpp/client/SWGPet.cpp | 2 +- 4 files changed, 6 insertions(+), 2 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/qt5cpp/helpers-header.mustache b/modules/swagger-codegen/src/main/resources/qt5cpp/helpers-header.mustache index c3569d7e7be6..b40c2ee8ba6c 100644 --- a/modules/swagger-codegen/src/main/resources/qt5cpp/helpers-header.mustache +++ b/modules/swagger-codegen/src/main/resources/qt5cpp/helpers-header.mustache @@ -3,6 +3,8 @@ #define {{prefix}}_HELPERS_H #include +#include +#include {{#cppNamespaceDeclarations}} namespace {{this}} { diff --git a/modules/swagger-codegen/src/main/resources/qt5cpp/model-body.mustache b/modules/swagger-codegen/src/main/resources/qt5cpp/model-body.mustache index f5e707f56052..011bf19fd697 100644 --- a/modules/swagger-codegen/src/main/resources/qt5cpp/model-body.mustache +++ b/modules/swagger-codegen/src/main/resources/qt5cpp/model-body.mustache @@ -105,7 +105,7 @@ QJsonObject* QJsonObject* obj = new QJsonObject(); {{#vars}} {{^isContainer}}{{#complexType}}{{^isString}}{{^isDate}}{{^isDateTime}}{{^isByteArray}} - if({{name}}->isSet()){ + if(({{name}} != nullptr) && ({{name}}->isSet())){ toJsonValue(QString("{{baseName}}"), {{name}}, obj, QString("{{complexType}}")); }{{/isByteArray}}{{/isDateTime}}{{/isDate}}{{/isString}}{{#isString}} if({{name}} != nullptr && *{{name}} != QString("")){ diff --git a/samples/client/petstore/qt5cpp/client/SWGHelpers.h b/samples/client/petstore/qt5cpp/client/SWGHelpers.h index bf721a6a1b9f..337d2150d3d2 100644 --- a/samples/client/petstore/qt5cpp/client/SWGHelpers.h +++ b/samples/client/petstore/qt5cpp/client/SWGHelpers.h @@ -14,6 +14,8 @@ #define SWG_HELPERS_H #include +#include +#include namespace Swagger { diff --git a/samples/client/petstore/qt5cpp/client/SWGPet.cpp b/samples/client/petstore/qt5cpp/client/SWGPet.cpp index a57cfdbc8d39..77a84be36a16 100644 --- a/samples/client/petstore/qt5cpp/client/SWGPet.cpp +++ b/samples/client/petstore/qt5cpp/client/SWGPet.cpp @@ -122,7 +122,7 @@ SWGPet::asJsonObject() { obj->insert("id", QJsonValue(id)); } - if(category->isSet()){ + if((category != nullptr) && (category->isSet())){ toJsonValue(QString("category"), category, obj, QString("SWGCategory")); }