From 8cd73e473300a37cdd76eda808684f178ca92a26 Mon Sep 17 00:00:00 2001 From: hirishh Date: Wed, 25 Feb 2026 01:42:34 +0100 Subject: [PATCH 1/6] [c] move model from int to int* --- .../resources/C-libcurl/model-body.mustache | 101 ++++++++++++++++-- .../resources/C-libcurl/model-header.mustache | 8 +- 2 files changed, 97 insertions(+), 12 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/C-libcurl/model-body.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/model-body.mustache index 70f2106ba847..4d52cf79698c 100644 --- a/modules/openapi-generator/src/main/resources/C-libcurl/model-body.mustache +++ b/modules/openapi-generator/src/main/resources/C-libcurl/model-body.mustache @@ -153,10 +153,10 @@ static {{classname}}_t *{{classname}}_create_internal( {{/isPrimitiveType}} {{#isPrimitiveType}} {{#isNumeric}} - {{datatype}} {{name}}{{^-last}},{{/-last}} + {{datatype}} *{{name}}{{^-last}},{{/-last}} {{/isNumeric}} {{#isBoolean}} - {{datatype}} {{name}}{{^-last}},{{/-last}} + {{datatype}} *{{name}}{{^-last}},{{/-last}} {{/isBoolean}} {{#isEnum}} {{#isString}} @@ -201,8 +201,43 @@ static {{classname}}_t *{{classname}}_create_internal( if (!{{classname}}_local_var) { return NULL; } + memset({{classname}}_local_var, 0, sizeof({{classname}}_t)); {{#vars}} + {{^isContainer}} + {{#isPrimitiveType}} + {{#isNumeric}} + if ({{{name}}}) { + {{classname}}_local_var->{{{name}}} = malloc(sizeof({{datatype}})); + if (!{{classname}}_local_var->{{{name}}}) { + {{classname}}_free({{classname}}_local_var); + return NULL; + } + *{{classname}}_local_var->{{{name}}} = *{{{name}}}; + } + {{/isNumeric}} + {{#isBoolean}} + if ({{{name}}}) { + {{classname}}_local_var->{{{name}}} = malloc(sizeof({{datatype}})); + if (!{{classname}}_local_var->{{{name}}}) { + {{classname}}_free({{classname}}_local_var); + return NULL; + } + *{{classname}}_local_var->{{{name}}} = *{{{name}}}; + } + {{/isBoolean}} + {{^isNumeric}} + {{^isBoolean}} + {{classname}}_local_var->{{{name}}} = {{{name}}}; + {{/isBoolean}} + {{/isNumeric}} + {{/isPrimitiveType}} + {{^isPrimitiveType}} + {{classname}}_local_var->{{{name}}} = {{{name}}}; + {{/isPrimitiveType}} + {{/isContainer}} + {{#isContainer}} {{classname}}_local_var->{{{name}}} = {{{name}}}; + {{/isContainer}} {{/vars}} {{classname}}_local_var->_library_owned = 1; @@ -243,10 +278,10 @@ __attribute__((deprecated)) {{classname}}_t *{{classname}}_create( {{/isPrimitiveType}} {{#isPrimitiveType}} {{#isNumeric}} - {{datatype}} {{name}}{{^-last}},{{/-last}} + {{datatype}} *{{name}}{{^-last}},{{/-last}} {{/isNumeric}} {{#isBoolean}} - {{datatype}} {{name}}{{^-last}},{{/-last}} + {{datatype}} *{{name}}{{^-last}},{{/-last}} {{/isBoolean}} {{#isEnum}} {{#isString}} @@ -344,6 +379,18 @@ void {{classname}}_free({{classname}}_t *{{classname}}) { {{/isFreeFormObject}} {{/isPrimitiveType}} {{#isPrimitiveType}} + {{#isNumeric}} + if ({{{classname}}}->{{{name}}}) { + free({{{classname}}}->{{{name}}}); + {{classname}}->{{name}} = NULL; + } + {{/isNumeric}} + {{#isBoolean}} + if ({{{classname}}}->{{{name}}}) { + free({{{classname}}}->{{{name}}}); + {{classname}}->{{name}} = NULL; + } + {{/isBoolean}} {{^isEnum}} {{#isString}} if ({{{classname}}}->{{{name}}}) { @@ -454,12 +501,12 @@ cJSON *{{classname}}_convertToJSON({{classname}}_t *{{classname}}) { {{^isContainer}} {{#isPrimitiveType}} {{#isNumeric}} - if(cJSON_AddNumberToObject(item, "{{{baseName}}}", {{{classname}}}->{{{name}}}) == NULL) { + if(cJSON_AddNumberToObject(item, "{{{baseName}}}", *{{{classname}}}->{{{name}}}) == NULL) { goto fail; //Numeric } {{/isNumeric}} {{#isBoolean}} - if(cJSON_AddBoolToObject(item, "{{{baseName}}}", {{{classname}}}->{{{name}}}) == NULL) { + if(cJSON_AddBoolToObject(item, "{{{baseName}}}", *{{{classname}}}->{{{name}}}) == NULL) { goto fail; //Bool } {{/isBoolean}} @@ -700,6 +747,18 @@ fail: {{/isFreeFormObject}} {{/isModel}} {{/isPrimitiveType}} + {{#isPrimitiveType}} + {{#isNumeric}} + // define the local variable for {{{classname}}}->{{{name}}} + {{datatype}} *{{name}}_local_var = NULL; + + {{/isNumeric}} + {{#isBoolean}} + // define the local variable for {{{classname}}}->{{{name}}} + {{datatype}} *{{name}}_local_var = NULL; + + {{/isBoolean}} + {{/isPrimitiveType}} {{/isContainer}} {{/vars}} {{#vars}} @@ -722,6 +781,12 @@ fail: { goto end; //Numeric } + {{name}}_local_var = malloc(sizeof({{datatype}})); + if(!{{name}}_local_var) + { + goto end; + } + *{{name}}_local_var = {{{name}}}->valuedouble; {{/isNumeric}} {{#isBoolean}} {{^required}}if ({{{name}}}) { {{/required}} @@ -729,6 +794,12 @@ fail: { goto end; //Bool } + {{name}}_local_var = malloc(sizeof({{datatype}})); + if(!{{name}}_local_var) + { + goto end; + } + *{{name}}_local_var = {{{name}}}->valueint; {{/isBoolean}} {{#isEnum}} {{#isString}} @@ -977,10 +1048,10 @@ fail: {{/isPrimitiveType}} {{#isPrimitiveType}} {{#isNumeric}} - {{^required}}{{{name}}} ? {{/required}}{{{name}}}->valuedouble{{^required}} : 0{{/required}}{{^-last}},{{/-last}} + {{name}}_local_var{{^-last}},{{/-last}} {{/isNumeric}} {{#isBoolean}} - {{^required}}{{{name}}} ? {{/required}}{{{name}}}->valueint{{^required}} : 0{{/required}}{{^-last}},{{/-last}} + {{name}}_local_var{{^-last}},{{/-last}} {{/isBoolean}} {{#isEnum}} {{#isString}} @@ -1046,6 +1117,20 @@ end: {{/isFreeFormObject}} {{/isModel}} {{/isPrimitiveType}} + {{#isPrimitiveType}} + {{#isNumeric}} + if ({{{name}}}_local_var) { + free({{{name}}}_local_var); + {{{name}}}_local_var = NULL; + } + {{/isNumeric}} + {{#isBoolean}} + if ({{{name}}}_local_var) { + free({{{name}}}_local_var); + {{{name}}}_local_var = NULL; + } + {{/isBoolean}} + {{/isPrimitiveType}} {{/isContainer}} {{#isContainer}} {{#isArray}} diff --git a/modules/openapi-generator/src/main/resources/C-libcurl/model-header.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/model-header.mustache index 236d9e8c3cba..c2013de9e3ac 100644 --- a/modules/openapi-generator/src/main/resources/C-libcurl/model-header.mustache +++ b/modules/openapi-generator/src/main/resources/C-libcurl/model-header.mustache @@ -110,10 +110,10 @@ typedef struct {{classname}}_t { {{/isPrimitiveType}} {{#isPrimitiveType}} {{#isNumeric}} - {{datatype}} {{name}}; //numeric + {{datatype}} *{{name}}; //numeric {{/isNumeric}} {{#isBoolean}} - {{datatype}} {{name}}; //boolean + {{datatype}} *{{name}}; //boolean {{/isBoolean}} {{#isEnum}} {{#isString}} @@ -191,10 +191,10 @@ __attribute__((deprecated)) {{classname}}_t *{{classname}}_create( {{/isPrimitiveType}} {{#isPrimitiveType}} {{#isNumeric}} - {{datatype}} {{name}}{{^-last}},{{/-last}} + {{datatype}} *{{name}}{{^-last}},{{/-last}} {{/isNumeric}} {{#isBoolean}} - {{datatype}} {{name}}{{^-last}},{{/-last}} + {{datatype}} *{{name}}{{^-last}},{{/-last}} {{/isBoolean}} {{#isEnum}} {{#isString}} From 613cbfe8327c0403eb80e03c767e7b2124eac297 Mon Sep 17 00:00:00 2001 From: hirishh Date: Wed, 25 Feb 2026 01:42:42 +0100 Subject: [PATCH 2/6] [c] regenerate petstore and fix manual unit-tests --- .../client/petstore/c/model/api_response.c | 35 ++++- .../client/petstore/c/model/api_response.h | 4 +- samples/client/petstore/c/model/category.c | 35 ++++- samples/client/petstore/c/model/category.h | 4 +- .../client/petstore/c/model/mapped_model.c | 35 ++++- .../client/petstore/c/model/mapped_model.h | 4 +- .../c/model/model_with_set_propertes.c | 1 + samples/client/petstore/c/model/order.c | 137 +++++++++++++++--- samples/client/petstore/c/model/order.h | 16 +- samples/client/petstore/c/model/pet.c | 35 ++++- samples/client/petstore/c/model/pet.h | 4 +- samples/client/petstore/c/model/tag.c | 35 ++++- samples/client/petstore/c/model/tag.h | 4 +- samples/client/petstore/c/model/user.c | 69 +++++++-- samples/client/petstore/c/model/user.h | 8 +- .../petstore/c/unit-tests/manual-PetAPI.c | 18 ++- .../petstore/c/unit-tests/manual-StoreAPI.c | 12 +- .../petstore/c/unit-tests/manual-UserAPI.c | 12 +- .../petstore/c/unit-tests/manual-order.c | 8 +- .../petstore/c/unit-tests/manual-user.c | 6 +- 20 files changed, 387 insertions(+), 95 deletions(-) diff --git a/samples/client/petstore/c/model/api_response.c b/samples/client/petstore/c/model/api_response.c index d0169c84f5b2..3541fbaf191b 100644 --- a/samples/client/petstore/c/model/api_response.c +++ b/samples/client/petstore/c/model/api_response.c @@ -6,7 +6,7 @@ static api_response_t *api_response_create_internal( - int code, + int *code, char *type, char *message ) { @@ -14,7 +14,15 @@ static api_response_t *api_response_create_internal( if (!api_response_local_var) { return NULL; } - api_response_local_var->code = code; + memset(api_response_local_var, 0, sizeof(api_response_t)); + if (code) { + api_response_local_var->code = malloc(sizeof(int)); + if (!api_response_local_var->code) { + api_response_free(api_response_local_var); + return NULL; + } + *api_response_local_var->code = *code; + } api_response_local_var->type = type; api_response_local_var->message = message; @@ -23,7 +31,7 @@ static api_response_t *api_response_create_internal( } __attribute__((deprecated)) api_response_t *api_response_create( - int code, + int *code, char *type, char *message ) { @@ -43,6 +51,10 @@ void api_response_free(api_response_t *api_response) { return ; } listEntry_t *listEntry; + if (api_response->code) { + free(api_response->code); + api_response->code = NULL; + } if (api_response->type) { free(api_response->type); api_response->type = NULL; @@ -59,7 +71,7 @@ cJSON *api_response_convertToJSON(api_response_t *api_response) { // api_response->code if(api_response->code) { - if(cJSON_AddNumberToObject(item, "code", api_response->code) == NULL) { + if(cJSON_AddNumberToObject(item, "code", *api_response->code) == NULL) { goto fail; //Numeric } } @@ -92,6 +104,9 @@ api_response_t *api_response_parseFromJSON(cJSON *api_responseJSON){ api_response_t *api_response_local_var = NULL; + // define the local variable for api_response->code + int *code_local_var = NULL; + // api_response->code cJSON *code = cJSON_GetObjectItemCaseSensitive(api_responseJSON, "code"); if (cJSON_IsNull(code)) { @@ -102,6 +117,12 @@ api_response_t *api_response_parseFromJSON(cJSON *api_responseJSON){ { goto end; //Numeric } + code_local_var = malloc(sizeof(int)); + if(!code_local_var) + { + goto end; + } + *code_local_var = code->valuedouble; } // api_response->type @@ -130,13 +151,17 @@ api_response_t *api_response_parseFromJSON(cJSON *api_responseJSON){ api_response_local_var = api_response_create_internal ( - code ? code->valuedouble : 0, + code_local_var, type && !cJSON_IsNull(type) ? strdup(type->valuestring) : NULL, message && !cJSON_IsNull(message) ? strdup(message->valuestring) : NULL ); return api_response_local_var; end: + if (code_local_var) { + free(code_local_var); + code_local_var = NULL; + } return NULL; } diff --git a/samples/client/petstore/c/model/api_response.h b/samples/client/petstore/c/model/api_response.h index 3d9eb71ff5d5..10d364f3c39b 100644 --- a/samples/client/petstore/c/model/api_response.h +++ b/samples/client/petstore/c/model/api_response.h @@ -19,7 +19,7 @@ typedef struct api_response_t api_response_t; typedef struct api_response_t { - int code; //numeric + int *code; //numeric char *type; // string char *message; // string @@ -27,7 +27,7 @@ typedef struct api_response_t { } api_response_t; __attribute__((deprecated)) api_response_t *api_response_create( - int code, + int *code, char *type, char *message ); diff --git a/samples/client/petstore/c/model/category.c b/samples/client/petstore/c/model/category.c index 2b060a568015..b59bdfb5c8c5 100644 --- a/samples/client/petstore/c/model/category.c +++ b/samples/client/petstore/c/model/category.c @@ -6,14 +6,22 @@ static category_t *category_create_internal( - long id, + long *id, char *name ) { category_t *category_local_var = malloc(sizeof(category_t)); if (!category_local_var) { return NULL; } - category_local_var->id = id; + memset(category_local_var, 0, sizeof(category_t)); + if (id) { + category_local_var->id = malloc(sizeof(long)); + if (!category_local_var->id) { + category_free(category_local_var); + return NULL; + } + *category_local_var->id = *id; + } category_local_var->name = name; category_local_var->_library_owned = 1; @@ -21,7 +29,7 @@ static category_t *category_create_internal( } __attribute__((deprecated)) category_t *category_create( - long id, + long *id, char *name ) { return category_create_internal ( @@ -39,6 +47,10 @@ void category_free(category_t *category) { return ; } listEntry_t *listEntry; + if (category->id) { + free(category->id); + category->id = NULL; + } if (category->name) { free(category->name); category->name = NULL; @@ -51,7 +63,7 @@ cJSON *category_convertToJSON(category_t *category) { // category->id if(category->id) { - if(cJSON_AddNumberToObject(item, "id", category->id) == NULL) { + if(cJSON_AddNumberToObject(item, "id", *category->id) == NULL) { goto fail; //Numeric } } @@ -76,6 +88,9 @@ category_t *category_parseFromJSON(cJSON *categoryJSON){ category_t *category_local_var = NULL; + // define the local variable for category->id + long *id_local_var = NULL; + // category->id cJSON *id = cJSON_GetObjectItemCaseSensitive(categoryJSON, "id"); if (cJSON_IsNull(id)) { @@ -86,6 +101,12 @@ category_t *category_parseFromJSON(cJSON *categoryJSON){ { goto end; //Numeric } + id_local_var = malloc(sizeof(long)); + if(!id_local_var) + { + goto end; + } + *id_local_var = id->valuedouble; } // category->name @@ -102,12 +123,16 @@ category_t *category_parseFromJSON(cJSON *categoryJSON){ category_local_var = category_create_internal ( - id ? id->valuedouble : 0, + id_local_var, name && !cJSON_IsNull(name) ? strdup(name->valuestring) : NULL ); return category_local_var; end: + if (id_local_var) { + free(id_local_var); + id_local_var = NULL; + } return NULL; } diff --git a/samples/client/petstore/c/model/category.h b/samples/client/petstore/c/model/category.h index bd27e27e35a3..dcc265955832 100644 --- a/samples/client/petstore/c/model/category.h +++ b/samples/client/petstore/c/model/category.h @@ -19,14 +19,14 @@ typedef struct category_t category_t; typedef struct category_t { - long id; //numeric + long *id; //numeric char *name; // string int _library_owned; // Is the library responsible for freeing this object? } category_t; __attribute__((deprecated)) category_t *category_create( - long id, + long *id, char *name ); diff --git a/samples/client/petstore/c/model/mapped_model.c b/samples/client/petstore/c/model/mapped_model.c index 3ab1e861c326..38e58473400c 100644 --- a/samples/client/petstore/c/model/mapped_model.c +++ b/samples/client/petstore/c/model/mapped_model.c @@ -6,14 +6,22 @@ static MappedModel_t *MappedModel_create_internal( - int another_property, + int *another_property, char *uuid_property ) { MappedModel_t *MappedModel_local_var = malloc(sizeof(MappedModel_t)); if (!MappedModel_local_var) { return NULL; } - MappedModel_local_var->another_property = another_property; + memset(MappedModel_local_var, 0, sizeof(MappedModel_t)); + if (another_property) { + MappedModel_local_var->another_property = malloc(sizeof(int)); + if (!MappedModel_local_var->another_property) { + MappedModel_free(MappedModel_local_var); + return NULL; + } + *MappedModel_local_var->another_property = *another_property; + } MappedModel_local_var->uuid_property = uuid_property; MappedModel_local_var->_library_owned = 1; @@ -21,7 +29,7 @@ static MappedModel_t *MappedModel_create_internal( } __attribute__((deprecated)) MappedModel_t *MappedModel_create( - int another_property, + int *another_property, char *uuid_property ) { return MappedModel_create_internal ( @@ -39,6 +47,10 @@ void MappedModel_free(MappedModel_t *MappedModel) { return ; } listEntry_t *listEntry; + if (MappedModel->another_property) { + free(MappedModel->another_property); + MappedModel->another_property = NULL; + } if (MappedModel->uuid_property) { free(MappedModel->uuid_property); MappedModel->uuid_property = NULL; @@ -51,7 +63,7 @@ cJSON *MappedModel_convertToJSON(MappedModel_t *MappedModel) { // MappedModel->another_property if(MappedModel->another_property) { - if(cJSON_AddNumberToObject(item, "another_property", MappedModel->another_property) == NULL) { + if(cJSON_AddNumberToObject(item, "another_property", *MappedModel->another_property) == NULL) { goto fail; //Numeric } } @@ -76,6 +88,9 @@ MappedModel_t *MappedModel_parseFromJSON(cJSON *MappedModelJSON){ MappedModel_t *MappedModel_local_var = NULL; + // define the local variable for MappedModel->another_property + int *another_property_local_var = NULL; + // MappedModel->another_property cJSON *another_property = cJSON_GetObjectItemCaseSensitive(MappedModelJSON, "another_property"); if (cJSON_IsNull(another_property)) { @@ -86,6 +101,12 @@ MappedModel_t *MappedModel_parseFromJSON(cJSON *MappedModelJSON){ { goto end; //Numeric } + another_property_local_var = malloc(sizeof(int)); + if(!another_property_local_var) + { + goto end; + } + *another_property_local_var = another_property->valuedouble; } // MappedModel->uuid_property @@ -102,12 +123,16 @@ MappedModel_t *MappedModel_parseFromJSON(cJSON *MappedModelJSON){ MappedModel_local_var = MappedModel_create_internal ( - another_property ? another_property->valuedouble : 0, + another_property_local_var, uuid_property && !cJSON_IsNull(uuid_property) ? strdup(uuid_property->valuestring) : NULL ); return MappedModel_local_var; end: + if (another_property_local_var) { + free(another_property_local_var); + another_property_local_var = NULL; + } return NULL; } diff --git a/samples/client/petstore/c/model/mapped_model.h b/samples/client/petstore/c/model/mapped_model.h index b962632d647d..b2a2baab9741 100644 --- a/samples/client/petstore/c/model/mapped_model.h +++ b/samples/client/petstore/c/model/mapped_model.h @@ -19,14 +19,14 @@ typedef struct MappedModel_t MappedModel_t; typedef struct MappedModel_t { - int another_property; //numeric + int *another_property; //numeric char *uuid_property; // string int _library_owned; // Is the library responsible for freeing this object? } MappedModel_t; __attribute__((deprecated)) MappedModel_t *MappedModel_create( - int another_property, + int *another_property, char *uuid_property ); diff --git a/samples/client/petstore/c/model/model_with_set_propertes.c b/samples/client/petstore/c/model/model_with_set_propertes.c index 934cdd587c85..94f3b581190b 100644 --- a/samples/client/petstore/c/model/model_with_set_propertes.c +++ b/samples/client/petstore/c/model/model_with_set_propertes.c @@ -13,6 +13,7 @@ static model_with_set_propertes_t *model_with_set_propertes_create_internal( if (!model_with_set_propertes_local_var) { return NULL; } + memset(model_with_set_propertes_local_var, 0, sizeof(model_with_set_propertes_t)); model_with_set_propertes_local_var->tag_set = tag_set; model_with_set_propertes_local_var->string_set = string_set; diff --git a/samples/client/petstore/c/model/order.c b/samples/client/petstore/c/model/order.c index d67d1d47c714..7b96a4f77bc0 100644 --- a/samples/client/petstore/c/model/order.c +++ b/samples/client/petstore/c/model/order.c @@ -23,35 +23,64 @@ openapi_petstore_order_STATUS_e order_status_FromString(char* status){ } static order_t *order_create_internal( - long id, - long pet_id, - int quantity, + long *id, + long *pet_id, + int *quantity, char *ship_date, openapi_petstore_order_STATUS_e status, - int complete + int *complete ) { order_t *order_local_var = malloc(sizeof(order_t)); if (!order_local_var) { return NULL; } - order_local_var->id = id; - order_local_var->pet_id = pet_id; - order_local_var->quantity = quantity; + memset(order_local_var, 0, sizeof(order_t)); + if (id) { + order_local_var->id = malloc(sizeof(long)); + if (!order_local_var->id) { + order_free(order_local_var); + return NULL; + } + *order_local_var->id = *id; + } + if (pet_id) { + order_local_var->pet_id = malloc(sizeof(long)); + if (!order_local_var->pet_id) { + order_free(order_local_var); + return NULL; + } + *order_local_var->pet_id = *pet_id; + } + if (quantity) { + order_local_var->quantity = malloc(sizeof(int)); + if (!order_local_var->quantity) { + order_free(order_local_var); + return NULL; + } + *order_local_var->quantity = *quantity; + } order_local_var->ship_date = ship_date; order_local_var->status = status; - order_local_var->complete = complete; + if (complete) { + order_local_var->complete = malloc(sizeof(int)); + if (!order_local_var->complete) { + order_free(order_local_var); + return NULL; + } + *order_local_var->complete = *complete; + } order_local_var->_library_owned = 1; return order_local_var; } __attribute__((deprecated)) order_t *order_create( - long id, - long pet_id, - int quantity, + long *id, + long *pet_id, + int *quantity, char *ship_date, openapi_petstore_order_STATUS_e status, - int complete + int *complete ) { return order_create_internal ( id, @@ -72,10 +101,26 @@ void order_free(order_t *order) { return ; } listEntry_t *listEntry; + if (order->id) { + free(order->id); + order->id = NULL; + } + if (order->pet_id) { + free(order->pet_id); + order->pet_id = NULL; + } + if (order->quantity) { + free(order->quantity); + order->quantity = NULL; + } if (order->ship_date) { free(order->ship_date); order->ship_date = NULL; } + if (order->complete) { + free(order->complete); + order->complete = NULL; + } free(order); } @@ -84,7 +129,7 @@ cJSON *order_convertToJSON(order_t *order) { // order->id if(order->id) { - if(cJSON_AddNumberToObject(item, "id", order->id) == NULL) { + if(cJSON_AddNumberToObject(item, "id", *order->id) == NULL) { goto fail; //Numeric } } @@ -92,7 +137,7 @@ cJSON *order_convertToJSON(order_t *order) { // order->pet_id if(order->pet_id) { - if(cJSON_AddNumberToObject(item, "petId", order->pet_id) == NULL) { + if(cJSON_AddNumberToObject(item, "petId", *order->pet_id) == NULL) { goto fail; //Numeric } } @@ -100,7 +145,7 @@ cJSON *order_convertToJSON(order_t *order) { // order->quantity if(order->quantity) { - if(cJSON_AddNumberToObject(item, "quantity", order->quantity) == NULL) { + if(cJSON_AddNumberToObject(item, "quantity", *order->quantity) == NULL) { goto fail; //Numeric } } @@ -125,7 +170,7 @@ cJSON *order_convertToJSON(order_t *order) { // order->complete if(order->complete) { - if(cJSON_AddBoolToObject(item, "complete", order->complete) == NULL) { + if(cJSON_AddBoolToObject(item, "complete", *order->complete) == NULL) { goto fail; //Bool } } @@ -142,6 +187,18 @@ order_t *order_parseFromJSON(cJSON *orderJSON){ order_t *order_local_var = NULL; + // define the local variable for order->id + long *id_local_var = NULL; + + // define the local variable for order->pet_id + long *pet_id_local_var = NULL; + + // define the local variable for order->quantity + int *quantity_local_var = NULL; + + // define the local variable for order->complete + int *complete_local_var = NULL; + // order->id cJSON *id = cJSON_GetObjectItemCaseSensitive(orderJSON, "id"); if (cJSON_IsNull(id)) { @@ -152,6 +209,12 @@ order_t *order_parseFromJSON(cJSON *orderJSON){ { goto end; //Numeric } + id_local_var = malloc(sizeof(long)); + if(!id_local_var) + { + goto end; + } + *id_local_var = id->valuedouble; } // order->pet_id @@ -164,6 +227,12 @@ order_t *order_parseFromJSON(cJSON *orderJSON){ { goto end; //Numeric } + pet_id_local_var = malloc(sizeof(long)); + if(!pet_id_local_var) + { + goto end; + } + *pet_id_local_var = pet_id->valuedouble; } // order->quantity @@ -176,6 +245,12 @@ order_t *order_parseFromJSON(cJSON *orderJSON){ { goto end; //Numeric } + quantity_local_var = malloc(sizeof(int)); + if(!quantity_local_var) + { + goto end; + } + *quantity_local_var = quantity->valuedouble; } // order->ship_date @@ -214,20 +289,42 @@ order_t *order_parseFromJSON(cJSON *orderJSON){ { goto end; //Bool } + complete_local_var = malloc(sizeof(int)); + if(!complete_local_var) + { + goto end; + } + *complete_local_var = complete->valueint; } order_local_var = order_create_internal ( - id ? id->valuedouble : 0, - pet_id ? pet_id->valuedouble : 0, - quantity ? quantity->valuedouble : 0, + id_local_var, + pet_id_local_var, + quantity_local_var, ship_date && !cJSON_IsNull(ship_date) ? strdup(ship_date->valuestring) : NULL, status ? statusVariable : openapi_petstore_order_STATUS_NULL, - complete ? complete->valueint : 0 + complete_local_var ); return order_local_var; end: + if (id_local_var) { + free(id_local_var); + id_local_var = NULL; + } + if (pet_id_local_var) { + free(pet_id_local_var); + pet_id_local_var = NULL; + } + if (quantity_local_var) { + free(quantity_local_var); + quantity_local_var = NULL; + } + if (complete_local_var) { + free(complete_local_var); + complete_local_var = NULL; + } return NULL; } diff --git a/samples/client/petstore/c/model/order.h b/samples/client/petstore/c/model/order.h index 1b0a47b3028e..59ede1731710 100644 --- a/samples/client/petstore/c/model/order.h +++ b/samples/client/petstore/c/model/order.h @@ -27,23 +27,23 @@ openapi_petstore_order_STATUS_e order_status_FromString(char* status); typedef struct order_t { - long id; //numeric - long pet_id; //numeric - int quantity; //numeric + long *id; //numeric + long *pet_id; //numeric + int *quantity; //numeric char *ship_date; //date time openapi_petstore_order_STATUS_e status; //enum - int complete; //boolean + int *complete; //boolean int _library_owned; // Is the library responsible for freeing this object? } order_t; __attribute__((deprecated)) order_t *order_create( - long id, - long pet_id, - int quantity, + long *id, + long *pet_id, + int *quantity, char *ship_date, openapi_petstore_order_STATUS_e status, - int complete + int *complete ); void order_free(order_t *order); diff --git a/samples/client/petstore/c/model/pet.c b/samples/client/petstore/c/model/pet.c index b561634cb529..7c91bc7541a5 100644 --- a/samples/client/petstore/c/model/pet.c +++ b/samples/client/petstore/c/model/pet.c @@ -23,7 +23,7 @@ openapi_petstore_pet_STATUS_e pet_status_FromString(char* status){ } static pet_t *pet_create_internal( - long id, + long *id, category_t *category, char *name, list_t *photo_urls, @@ -34,7 +34,15 @@ static pet_t *pet_create_internal( if (!pet_local_var) { return NULL; } - pet_local_var->id = id; + memset(pet_local_var, 0, sizeof(pet_t)); + if (id) { + pet_local_var->id = malloc(sizeof(long)); + if (!pet_local_var->id) { + pet_free(pet_local_var); + return NULL; + } + *pet_local_var->id = *id; + } pet_local_var->category = category; pet_local_var->name = name; pet_local_var->photo_urls = photo_urls; @@ -46,7 +54,7 @@ static pet_t *pet_create_internal( } __attribute__((deprecated)) pet_t *pet_create( - long id, + long *id, category_t *category, char *name, list_t *photo_urls, @@ -72,6 +80,10 @@ void pet_free(pet_t *pet) { return ; } listEntry_t *listEntry; + if (pet->id) { + free(pet->id); + pet->id = NULL; + } if (pet->category) { category_free(pet->category); pet->category = NULL; @@ -102,7 +114,7 @@ cJSON *pet_convertToJSON(pet_t *pet) { // pet->id if(pet->id) { - if(cJSON_AddNumberToObject(item, "id", pet->id) == NULL) { + if(cJSON_AddNumberToObject(item, "id", *pet->id) == NULL) { goto fail; //Numeric } } @@ -188,6 +200,9 @@ pet_t *pet_parseFromJSON(cJSON *petJSON){ pet_t *pet_local_var = NULL; + // define the local variable for pet->id + long *id_local_var = NULL; + // define the local variable for pet->category category_t *category_local_nonprim = NULL; @@ -207,6 +222,12 @@ pet_t *pet_parseFromJSON(cJSON *petJSON){ { goto end; //Numeric } + id_local_var = malloc(sizeof(long)); + if(!id_local_var) + { + goto end; + } + *id_local_var = id->valuedouble; } // pet->category @@ -298,7 +319,7 @@ pet_t *pet_parseFromJSON(cJSON *petJSON){ pet_local_var = pet_create_internal ( - id ? id->valuedouble : 0, + id_local_var, category ? category_local_nonprim : NULL, strdup(name->valuestring), photo_urlsList, @@ -308,6 +329,10 @@ pet_t *pet_parseFromJSON(cJSON *petJSON){ return pet_local_var; end: + if (id_local_var) { + free(id_local_var); + id_local_var = NULL; + } if (category_local_nonprim) { category_free(category_local_nonprim); category_local_nonprim = NULL; diff --git a/samples/client/petstore/c/model/pet.h b/samples/client/petstore/c/model/pet.h index 860197e63a53..e86cda79d16e 100644 --- a/samples/client/petstore/c/model/pet.h +++ b/samples/client/petstore/c/model/pet.h @@ -29,7 +29,7 @@ openapi_petstore_pet_STATUS_e pet_status_FromString(char* status); typedef struct pet_t { - long id; //numeric + long *id; //numeric struct category_t *category; //model char *name; // string list_t *photo_urls; //primitive container @@ -40,7 +40,7 @@ typedef struct pet_t { } pet_t; __attribute__((deprecated)) pet_t *pet_create( - long id, + long *id, category_t *category, char *name, list_t *photo_urls, diff --git a/samples/client/petstore/c/model/tag.c b/samples/client/petstore/c/model/tag.c index e4b4f94af53d..0b54379df60c 100644 --- a/samples/client/petstore/c/model/tag.c +++ b/samples/client/petstore/c/model/tag.c @@ -6,14 +6,22 @@ static tag_t *tag_create_internal( - long id, + long *id, char *name ) { tag_t *tag_local_var = malloc(sizeof(tag_t)); if (!tag_local_var) { return NULL; } - tag_local_var->id = id; + memset(tag_local_var, 0, sizeof(tag_t)); + if (id) { + tag_local_var->id = malloc(sizeof(long)); + if (!tag_local_var->id) { + tag_free(tag_local_var); + return NULL; + } + *tag_local_var->id = *id; + } tag_local_var->name = name; tag_local_var->_library_owned = 1; @@ -21,7 +29,7 @@ static tag_t *tag_create_internal( } __attribute__((deprecated)) tag_t *tag_create( - long id, + long *id, char *name ) { return tag_create_internal ( @@ -39,6 +47,10 @@ void tag_free(tag_t *tag) { return ; } listEntry_t *listEntry; + if (tag->id) { + free(tag->id); + tag->id = NULL; + } if (tag->name) { free(tag->name); tag->name = NULL; @@ -51,7 +63,7 @@ cJSON *tag_convertToJSON(tag_t *tag) { // tag->id if(tag->id) { - if(cJSON_AddNumberToObject(item, "id", tag->id) == NULL) { + if(cJSON_AddNumberToObject(item, "id", *tag->id) == NULL) { goto fail; //Numeric } } @@ -76,6 +88,9 @@ tag_t *tag_parseFromJSON(cJSON *tagJSON){ tag_t *tag_local_var = NULL; + // define the local variable for tag->id + long *id_local_var = NULL; + // tag->id cJSON *id = cJSON_GetObjectItemCaseSensitive(tagJSON, "id"); if (cJSON_IsNull(id)) { @@ -86,6 +101,12 @@ tag_t *tag_parseFromJSON(cJSON *tagJSON){ { goto end; //Numeric } + id_local_var = malloc(sizeof(long)); + if(!id_local_var) + { + goto end; + } + *id_local_var = id->valuedouble; } // tag->name @@ -102,12 +123,16 @@ tag_t *tag_parseFromJSON(cJSON *tagJSON){ tag_local_var = tag_create_internal ( - id ? id->valuedouble : 0, + id_local_var, name && !cJSON_IsNull(name) ? strdup(name->valuestring) : NULL ); return tag_local_var; end: + if (id_local_var) { + free(id_local_var); + id_local_var = NULL; + } return NULL; } diff --git a/samples/client/petstore/c/model/tag.h b/samples/client/petstore/c/model/tag.h index d4b29e4d2e04..0e39017928c1 100644 --- a/samples/client/petstore/c/model/tag.h +++ b/samples/client/petstore/c/model/tag.h @@ -19,14 +19,14 @@ typedef struct tag_t tag_t; typedef struct tag_t { - long id; //numeric + long *id; //numeric char *name; // string int _library_owned; // Is the library responsible for freeing this object? } tag_t; __attribute__((deprecated)) tag_t *tag_create( - long id, + long *id, char *name ); diff --git a/samples/client/petstore/c/model/user.c b/samples/client/petstore/c/model/user.c index 780347915886..c81976dabcac 100644 --- a/samples/client/petstore/c/model/user.c +++ b/samples/client/petstore/c/model/user.c @@ -6,14 +6,14 @@ static user_t *user_create_internal( - long id, + long *id, char *username, char *first_name, char *last_name, char *email, char *password, char *phone, - int user_status, + int *user_status, list_t* extra, openapi_petstore_preference__e preference ) { @@ -21,14 +21,29 @@ static user_t *user_create_internal( if (!user_local_var) { return NULL; } - user_local_var->id = id; + memset(user_local_var, 0, sizeof(user_t)); + if (id) { + user_local_var->id = malloc(sizeof(long)); + if (!user_local_var->id) { + user_free(user_local_var); + return NULL; + } + *user_local_var->id = *id; + } user_local_var->username = username; user_local_var->first_name = first_name; user_local_var->last_name = last_name; user_local_var->email = email; user_local_var->password = password; user_local_var->phone = phone; - user_local_var->user_status = user_status; + if (user_status) { + user_local_var->user_status = malloc(sizeof(int)); + if (!user_local_var->user_status) { + user_free(user_local_var); + return NULL; + } + *user_local_var->user_status = *user_status; + } user_local_var->extra = extra; user_local_var->preference = preference; @@ -37,14 +52,14 @@ static user_t *user_create_internal( } __attribute__((deprecated)) user_t *user_create( - long id, + long *id, char *username, char *first_name, char *last_name, char *email, char *password, char *phone, - int user_status, + int *user_status, list_t* extra, openapi_petstore_preference__e preference ) { @@ -71,6 +86,10 @@ void user_free(user_t *user) { return ; } listEntry_t *listEntry; + if (user->id) { + free(user->id); + user->id = NULL; + } if (user->username) { free(user->username); user->username = NULL; @@ -95,6 +114,10 @@ void user_free(user_t *user) { free(user->phone); user->phone = NULL; } + if (user->user_status) { + free(user->user_status); + user->user_status = NULL; + } if (user->extra) { list_ForEach(listEntry, user->extra) { keyValuePair_t *localKeyValue = listEntry->data; @@ -113,7 +136,7 @@ cJSON *user_convertToJSON(user_t *user) { // user->id if(user->id) { - if(cJSON_AddNumberToObject(item, "id", user->id) == NULL) { + if(cJSON_AddNumberToObject(item, "id", *user->id) == NULL) { goto fail; //Numeric } } @@ -169,7 +192,7 @@ cJSON *user_convertToJSON(user_t *user) { // user->user_status if(user->user_status) { - if(cJSON_AddNumberToObject(item, "userStatus", user->user_status) == NULL) { + if(cJSON_AddNumberToObject(item, "userStatus", *user->user_status) == NULL) { goto fail; //Numeric } } @@ -215,6 +238,12 @@ user_t *user_parseFromJSON(cJSON *userJSON){ user_t *user_local_var = NULL; + // define the local variable for user->id + long *id_local_var = NULL; + + // define the local variable for user->user_status + int *user_status_local_var = NULL; + // define the local map for user->extra list_t *extraList = NULL; @@ -231,6 +260,12 @@ user_t *user_parseFromJSON(cJSON *userJSON){ { goto end; //Numeric } + id_local_var = malloc(sizeof(long)); + if(!id_local_var) + { + goto end; + } + *id_local_var = id->valuedouble; } // user->username @@ -315,6 +350,12 @@ user_t *user_parseFromJSON(cJSON *userJSON){ { goto end; //Numeric } + user_status_local_var = malloc(sizeof(int)); + if(!user_status_local_var) + { + goto end; + } + *user_status_local_var = user_status->valuedouble; } // user->extra @@ -351,20 +392,28 @@ user_t *user_parseFromJSON(cJSON *userJSON){ user_local_var = user_create_internal ( - id ? id->valuedouble : 0, + id_local_var, username && !cJSON_IsNull(username) ? strdup(username->valuestring) : NULL, first_name && !cJSON_IsNull(first_name) ? strdup(first_name->valuestring) : NULL, last_name && !cJSON_IsNull(last_name) ? strdup(last_name->valuestring) : NULL, email && !cJSON_IsNull(email) ? strdup(email->valuestring) : NULL, password && !cJSON_IsNull(password) ? strdup(password->valuestring) : NULL, phone && !cJSON_IsNull(phone) ? strdup(phone->valuestring) : NULL, - user_status ? user_status->valuedouble : 0, + user_status_local_var, extra ? extraList : NULL, preference ? preference_local_nonprim : 0 ); return user_local_var; end: + if (id_local_var) { + free(id_local_var); + id_local_var = NULL; + } + if (user_status_local_var) { + free(user_status_local_var); + user_status_local_var = NULL; + } if (extraList) { listEntry_t *listEntry = NULL; list_ForEach(listEntry, extraList) { diff --git a/samples/client/petstore/c/model/user.h b/samples/client/petstore/c/model/user.h index 4643e020a5fe..333195656bb0 100644 --- a/samples/client/petstore/c/model/user.h +++ b/samples/client/petstore/c/model/user.h @@ -21,14 +21,14 @@ typedef struct user_t user_t; typedef struct user_t { - long id; //numeric + long *id; //numeric char *username; // string char *first_name; // string char *last_name; // string char *email; // string char *password; // string char *phone; // string - int user_status; //numeric + int *user_status; //numeric list_t* extra; //map openapi_petstore_preference__e preference; //referenced enum @@ -36,14 +36,14 @@ typedef struct user_t { } user_t; __attribute__((deprecated)) user_t *user_create( - long id, + long *id, char *username, char *first_name, char *last_name, char *email, char *password, char *phone, - int user_status, + int *user_status, list_t* extra, openapi_petstore_preference__e preference ); diff --git a/samples/client/petstore/c/unit-tests/manual-PetAPI.c b/samples/client/petstore/c/unit-tests/manual-PetAPI.c index a510b79214a5..ffd2e8201225 100644 --- a/samples/client/petstore/c/unit-tests/manual-PetAPI.c +++ b/samples/client/petstore/c/unit-tests/manual-PetAPI.c @@ -31,8 +31,9 @@ int main() { char *categoryName = malloc(strlen(EXAMPLE_CATEGORY_NAME) + 1); strcpy(categoryName, EXAMPLE_CATEGORY_NAME); + long categoryId = EXAMPLE_CATEGORY_ID; category_t *category = - category_create(EXAMPLE_CATEGORY_ID, categoryName); + category_create(&categoryId, categoryName); char *petName = malloc(strlen(EXAMPLE_PET_NAME) + 1); strcpy(petName, EXAMPLE_PET_NAME); @@ -50,11 +51,13 @@ int main() { char *exampleTag1Name = malloc(strlen(EXAMPLE_TAG_1_NAME) + 1); strcpy(exampleTag1Name, EXAMPLE_TAG_1_NAME); - tag_t *exampleTag1 = tag_create(EXAMPLE_TAG_1_ID, exampleTag1Name); + long tag1Id = EXAMPLE_TAG_1_ID; + tag_t *exampleTag1 = tag_create(&tag1Id, exampleTag1Name); char *exampleTag2Name = malloc(strlen(EXAMPLE_TAG_2_NAME) + 1); strcpy(exampleTag2Name, EXAMPLE_TAG_2_NAME); - tag_t *exampleTag2 = tag_create(EXAMPLE_TAG_2_ID, exampleTag2Name); + long tag2Id = EXAMPLE_TAG_2_ID; + tag_t *exampleTag2 = tag_create(&tag2Id, exampleTag2Name); list_t *tags = list_createList(); @@ -62,8 +65,9 @@ int main() { list_addElement(tags, exampleTag2); + long petId = EXAMPLE_PET_ID; pet_t *pet = - pet_create(EXAMPLE_PET_ID, + pet_create(&petId, category, petName, photoUrls, @@ -98,16 +102,18 @@ int main() { printf("Data is:%s\n", petJson); assert(strcmp(mypet->name, "Rocky Handsome") == 0); - assert(mypet->id == EXAMPLE_PET_ID); + assert(mypet->id && *mypet->id == EXAMPLE_PET_ID); assert(strcmp(mypet->category->name, EXAMPLE_CATEGORY_NAME) == 0); - assert(mypet->category->id == EXAMPLE_CATEGORY_ID); + assert(mypet->category->id && *mypet->category->id == EXAMPLE_CATEGORY_ID); assert(strcmp(list_getElementAt(mypet->photo_urls, 0)->data, EXAMPLE_URL_1) == 0); assert(strcmp(list_getElementAt(mypet->photo_urls, 1)->data, EXAMPLE_URL_2) == 0); assert(((tag_t *) list_getElementAt(mypet->tags, + 0)->data)->id && *((tag_t *) list_getElementAt(mypet->tags, 0)->data)->id == EXAMPLE_TAG_1_ID); assert(((tag_t *) list_getElementAt(mypet->tags, + 1)->data)->id && *((tag_t *) list_getElementAt(mypet->tags, 1)->data)->id == EXAMPLE_TAG_2_ID); assert(strcmp(((tag_t *) list_getElementAt(mypet->tags, 0)->data)->name, EXAMPLE_TAG_1_NAME) == 0); diff --git a/samples/client/petstore/c/unit-tests/manual-StoreAPI.c b/samples/client/petstore/c/unit-tests/manual-StoreAPI.c index baadca77f82a..7bc12e0627b2 100644 --- a/samples/client/petstore/c/unit-tests/manual-StoreAPI.c +++ b/samples/client/petstore/c/unit-tests/manual-StoreAPI.c @@ -22,12 +22,16 @@ int main() { char *shipdate = malloc(strlen(SHIP_DATE) + 1); strcpy(shipdate, SHIP_DATE); - order_t *neworder = order_create(ORDER_ID, - PET_ID, - QUANTITY, + long orderId = ORDER_ID; + long petId = PET_ID; + int quantity = QUANTITY; + int complete = COMPLETE; + order_t *neworder = order_create(&orderId, + &petId, + &quantity, shipdate, openapi_petstore_order_STATUS_placed, - COMPLETE); + &complete); order_t *returnorder = StoreAPI_placeOrder(apiClient, neworder); diff --git a/samples/client/petstore/c/unit-tests/manual-UserAPI.c b/samples/client/petstore/c/unit-tests/manual-UserAPI.c index e205bf6cf01e..468efcd5d9f0 100644 --- a/samples/client/petstore/c/unit-tests/manual-UserAPI.c +++ b/samples/client/petstore/c/unit-tests/manual-UserAPI.c @@ -32,14 +32,16 @@ int main() { char *phone = malloc(strlen(PHONE) + 1); strcpy(phone, PHONE); - user_t *newuser = user_create(USER_ID, + long userId = USER_ID; + int userStatus = USER_STATUS; + user_t *newuser = user_create(&userId, username, firstname, lastname, email, password, phone, - USER_STATUS, + &userStatus, NULL, openapi_petstore_preference__cats); @@ -77,14 +79,16 @@ int main() { char *phone = malloc(strlen(PHONE) + 1); strcpy(phone, PHONE); - user_t *newuser1 = user_create(USER_ID, + long userId1 = USER_ID; + int userStatus1 = USER_STATUS; + user_t *newuser1 = user_create(&userId1, username1, firstname, lastname, email, password, phone, - USER_STATUS, + &userStatus1, NULL, openapi_petstore_preference__cats); diff --git a/samples/client/petstore/c/unit-tests/manual-order.c b/samples/client/petstore/c/unit-tests/manual-order.c index ae4a5d3d071b..4c996c66f0b8 100644 --- a/samples/client/petstore/c/unit-tests/manual-order.c +++ b/samples/client/petstore/c/unit-tests/manual-order.c @@ -15,9 +15,13 @@ int main() { char *shipDate = malloc(strlen(SHIP_DATE) + 1); strcpy(shipDate, SHIP_DATE); - order_t *neworder = order_create(ORDER_ID, PET_ID, QUANTITY, shipDate, + long orderId = ORDER_ID; + long petId = PET_ID; + int quantity = QUANTITY; + int complete = COMPLETE; + order_t *neworder = order_create(&orderId, &petId, &quantity, shipDate, openapi_petstore_order_STATUS_placed, - COMPLETE); + &complete); cJSON *JSONNODE = order_convertToJSON(neworder); diff --git a/samples/client/petstore/c/unit-tests/manual-user.c b/samples/client/petstore/c/unit-tests/manual-user.c index da1ad928fb69..6d168be90ee4 100644 --- a/samples/client/petstore/c/unit-tests/manual-user.c +++ b/samples/client/petstore/c/unit-tests/manual-user.c @@ -28,14 +28,16 @@ int main() { char *phone = malloc(strlen(PHONE) + 1); strcpy(phone, PHONE); - user_t *newuser = user_create(USER_ID, + long userId = USER_ID; + int userStatus = USER_STATUS; + user_t *newuser = user_create(&userId, username, firstname, lastname, email, password, phone, - USER_STATUS, + &userStatus, NULL, openapi_petstore_preference__cats); From ca2d452909924c4cd11bb8be5740f71da370408d Mon Sep 17 00:00:00 2001 From: hirishh Date: Wed, 25 Feb 2026 01:42:49 +0100 Subject: [PATCH 3/6] [c]-useJsonUnformatted regenerate petstore samples --- .../c-useJsonUnformatted/model/api_response.c | 35 ++++- .../c-useJsonUnformatted/model/api_response.h | 4 +- .../c-useJsonUnformatted/model/category.c | 35 ++++- .../c-useJsonUnformatted/model/category.h | 4 +- .../c-useJsonUnformatted/model/mapped_model.c | 35 ++++- .../c-useJsonUnformatted/model/mapped_model.h | 4 +- .../model/model_with_set_propertes.c | 1 + .../c-useJsonUnformatted/model/order.c | 137 +++++++++++++++--- .../c-useJsonUnformatted/model/order.h | 16 +- .../petstore/c-useJsonUnformatted/model/pet.c | 35 ++++- .../petstore/c-useJsonUnformatted/model/pet.h | 4 +- .../petstore/c-useJsonUnformatted/model/tag.c | 35 ++++- .../petstore/c-useJsonUnformatted/model/tag.h | 4 +- .../c-useJsonUnformatted/model/user.c | 69 +++++++-- .../c-useJsonUnformatted/model/user.h | 8 +- 15 files changed, 349 insertions(+), 77 deletions(-) diff --git a/samples/client/petstore/c-useJsonUnformatted/model/api_response.c b/samples/client/petstore/c-useJsonUnformatted/model/api_response.c index d0169c84f5b2..3541fbaf191b 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/api_response.c +++ b/samples/client/petstore/c-useJsonUnformatted/model/api_response.c @@ -6,7 +6,7 @@ static api_response_t *api_response_create_internal( - int code, + int *code, char *type, char *message ) { @@ -14,7 +14,15 @@ static api_response_t *api_response_create_internal( if (!api_response_local_var) { return NULL; } - api_response_local_var->code = code; + memset(api_response_local_var, 0, sizeof(api_response_t)); + if (code) { + api_response_local_var->code = malloc(sizeof(int)); + if (!api_response_local_var->code) { + api_response_free(api_response_local_var); + return NULL; + } + *api_response_local_var->code = *code; + } api_response_local_var->type = type; api_response_local_var->message = message; @@ -23,7 +31,7 @@ static api_response_t *api_response_create_internal( } __attribute__((deprecated)) api_response_t *api_response_create( - int code, + int *code, char *type, char *message ) { @@ -43,6 +51,10 @@ void api_response_free(api_response_t *api_response) { return ; } listEntry_t *listEntry; + if (api_response->code) { + free(api_response->code); + api_response->code = NULL; + } if (api_response->type) { free(api_response->type); api_response->type = NULL; @@ -59,7 +71,7 @@ cJSON *api_response_convertToJSON(api_response_t *api_response) { // api_response->code if(api_response->code) { - if(cJSON_AddNumberToObject(item, "code", api_response->code) == NULL) { + if(cJSON_AddNumberToObject(item, "code", *api_response->code) == NULL) { goto fail; //Numeric } } @@ -92,6 +104,9 @@ api_response_t *api_response_parseFromJSON(cJSON *api_responseJSON){ api_response_t *api_response_local_var = NULL; + // define the local variable for api_response->code + int *code_local_var = NULL; + // api_response->code cJSON *code = cJSON_GetObjectItemCaseSensitive(api_responseJSON, "code"); if (cJSON_IsNull(code)) { @@ -102,6 +117,12 @@ api_response_t *api_response_parseFromJSON(cJSON *api_responseJSON){ { goto end; //Numeric } + code_local_var = malloc(sizeof(int)); + if(!code_local_var) + { + goto end; + } + *code_local_var = code->valuedouble; } // api_response->type @@ -130,13 +151,17 @@ api_response_t *api_response_parseFromJSON(cJSON *api_responseJSON){ api_response_local_var = api_response_create_internal ( - code ? code->valuedouble : 0, + code_local_var, type && !cJSON_IsNull(type) ? strdup(type->valuestring) : NULL, message && !cJSON_IsNull(message) ? strdup(message->valuestring) : NULL ); return api_response_local_var; end: + if (code_local_var) { + free(code_local_var); + code_local_var = NULL; + } return NULL; } diff --git a/samples/client/petstore/c-useJsonUnformatted/model/api_response.h b/samples/client/petstore/c-useJsonUnformatted/model/api_response.h index 3d9eb71ff5d5..10d364f3c39b 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/api_response.h +++ b/samples/client/petstore/c-useJsonUnformatted/model/api_response.h @@ -19,7 +19,7 @@ typedef struct api_response_t api_response_t; typedef struct api_response_t { - int code; //numeric + int *code; //numeric char *type; // string char *message; // string @@ -27,7 +27,7 @@ typedef struct api_response_t { } api_response_t; __attribute__((deprecated)) api_response_t *api_response_create( - int code, + int *code, char *type, char *message ); diff --git a/samples/client/petstore/c-useJsonUnformatted/model/category.c b/samples/client/petstore/c-useJsonUnformatted/model/category.c index 2b060a568015..b59bdfb5c8c5 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/category.c +++ b/samples/client/petstore/c-useJsonUnformatted/model/category.c @@ -6,14 +6,22 @@ static category_t *category_create_internal( - long id, + long *id, char *name ) { category_t *category_local_var = malloc(sizeof(category_t)); if (!category_local_var) { return NULL; } - category_local_var->id = id; + memset(category_local_var, 0, sizeof(category_t)); + if (id) { + category_local_var->id = malloc(sizeof(long)); + if (!category_local_var->id) { + category_free(category_local_var); + return NULL; + } + *category_local_var->id = *id; + } category_local_var->name = name; category_local_var->_library_owned = 1; @@ -21,7 +29,7 @@ static category_t *category_create_internal( } __attribute__((deprecated)) category_t *category_create( - long id, + long *id, char *name ) { return category_create_internal ( @@ -39,6 +47,10 @@ void category_free(category_t *category) { return ; } listEntry_t *listEntry; + if (category->id) { + free(category->id); + category->id = NULL; + } if (category->name) { free(category->name); category->name = NULL; @@ -51,7 +63,7 @@ cJSON *category_convertToJSON(category_t *category) { // category->id if(category->id) { - if(cJSON_AddNumberToObject(item, "id", category->id) == NULL) { + if(cJSON_AddNumberToObject(item, "id", *category->id) == NULL) { goto fail; //Numeric } } @@ -76,6 +88,9 @@ category_t *category_parseFromJSON(cJSON *categoryJSON){ category_t *category_local_var = NULL; + // define the local variable for category->id + long *id_local_var = NULL; + // category->id cJSON *id = cJSON_GetObjectItemCaseSensitive(categoryJSON, "id"); if (cJSON_IsNull(id)) { @@ -86,6 +101,12 @@ category_t *category_parseFromJSON(cJSON *categoryJSON){ { goto end; //Numeric } + id_local_var = malloc(sizeof(long)); + if(!id_local_var) + { + goto end; + } + *id_local_var = id->valuedouble; } // category->name @@ -102,12 +123,16 @@ category_t *category_parseFromJSON(cJSON *categoryJSON){ category_local_var = category_create_internal ( - id ? id->valuedouble : 0, + id_local_var, name && !cJSON_IsNull(name) ? strdup(name->valuestring) : NULL ); return category_local_var; end: + if (id_local_var) { + free(id_local_var); + id_local_var = NULL; + } return NULL; } diff --git a/samples/client/petstore/c-useJsonUnformatted/model/category.h b/samples/client/petstore/c-useJsonUnformatted/model/category.h index bd27e27e35a3..dcc265955832 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/category.h +++ b/samples/client/petstore/c-useJsonUnformatted/model/category.h @@ -19,14 +19,14 @@ typedef struct category_t category_t; typedef struct category_t { - long id; //numeric + long *id; //numeric char *name; // string int _library_owned; // Is the library responsible for freeing this object? } category_t; __attribute__((deprecated)) category_t *category_create( - long id, + long *id, char *name ); diff --git a/samples/client/petstore/c-useJsonUnformatted/model/mapped_model.c b/samples/client/petstore/c-useJsonUnformatted/model/mapped_model.c index 3ab1e861c326..38e58473400c 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/mapped_model.c +++ b/samples/client/petstore/c-useJsonUnformatted/model/mapped_model.c @@ -6,14 +6,22 @@ static MappedModel_t *MappedModel_create_internal( - int another_property, + int *another_property, char *uuid_property ) { MappedModel_t *MappedModel_local_var = malloc(sizeof(MappedModel_t)); if (!MappedModel_local_var) { return NULL; } - MappedModel_local_var->another_property = another_property; + memset(MappedModel_local_var, 0, sizeof(MappedModel_t)); + if (another_property) { + MappedModel_local_var->another_property = malloc(sizeof(int)); + if (!MappedModel_local_var->another_property) { + MappedModel_free(MappedModel_local_var); + return NULL; + } + *MappedModel_local_var->another_property = *another_property; + } MappedModel_local_var->uuid_property = uuid_property; MappedModel_local_var->_library_owned = 1; @@ -21,7 +29,7 @@ static MappedModel_t *MappedModel_create_internal( } __attribute__((deprecated)) MappedModel_t *MappedModel_create( - int another_property, + int *another_property, char *uuid_property ) { return MappedModel_create_internal ( @@ -39,6 +47,10 @@ void MappedModel_free(MappedModel_t *MappedModel) { return ; } listEntry_t *listEntry; + if (MappedModel->another_property) { + free(MappedModel->another_property); + MappedModel->another_property = NULL; + } if (MappedModel->uuid_property) { free(MappedModel->uuid_property); MappedModel->uuid_property = NULL; @@ -51,7 +63,7 @@ cJSON *MappedModel_convertToJSON(MappedModel_t *MappedModel) { // MappedModel->another_property if(MappedModel->another_property) { - if(cJSON_AddNumberToObject(item, "another_property", MappedModel->another_property) == NULL) { + if(cJSON_AddNumberToObject(item, "another_property", *MappedModel->another_property) == NULL) { goto fail; //Numeric } } @@ -76,6 +88,9 @@ MappedModel_t *MappedModel_parseFromJSON(cJSON *MappedModelJSON){ MappedModel_t *MappedModel_local_var = NULL; + // define the local variable for MappedModel->another_property + int *another_property_local_var = NULL; + // MappedModel->another_property cJSON *another_property = cJSON_GetObjectItemCaseSensitive(MappedModelJSON, "another_property"); if (cJSON_IsNull(another_property)) { @@ -86,6 +101,12 @@ MappedModel_t *MappedModel_parseFromJSON(cJSON *MappedModelJSON){ { goto end; //Numeric } + another_property_local_var = malloc(sizeof(int)); + if(!another_property_local_var) + { + goto end; + } + *another_property_local_var = another_property->valuedouble; } // MappedModel->uuid_property @@ -102,12 +123,16 @@ MappedModel_t *MappedModel_parseFromJSON(cJSON *MappedModelJSON){ MappedModel_local_var = MappedModel_create_internal ( - another_property ? another_property->valuedouble : 0, + another_property_local_var, uuid_property && !cJSON_IsNull(uuid_property) ? strdup(uuid_property->valuestring) : NULL ); return MappedModel_local_var; end: + if (another_property_local_var) { + free(another_property_local_var); + another_property_local_var = NULL; + } return NULL; } diff --git a/samples/client/petstore/c-useJsonUnformatted/model/mapped_model.h b/samples/client/petstore/c-useJsonUnformatted/model/mapped_model.h index b962632d647d..b2a2baab9741 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/mapped_model.h +++ b/samples/client/petstore/c-useJsonUnformatted/model/mapped_model.h @@ -19,14 +19,14 @@ typedef struct MappedModel_t MappedModel_t; typedef struct MappedModel_t { - int another_property; //numeric + int *another_property; //numeric char *uuid_property; // string int _library_owned; // Is the library responsible for freeing this object? } MappedModel_t; __attribute__((deprecated)) MappedModel_t *MappedModel_create( - int another_property, + int *another_property, char *uuid_property ); diff --git a/samples/client/petstore/c-useJsonUnformatted/model/model_with_set_propertes.c b/samples/client/petstore/c-useJsonUnformatted/model/model_with_set_propertes.c index 934cdd587c85..94f3b581190b 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/model_with_set_propertes.c +++ b/samples/client/petstore/c-useJsonUnformatted/model/model_with_set_propertes.c @@ -13,6 +13,7 @@ static model_with_set_propertes_t *model_with_set_propertes_create_internal( if (!model_with_set_propertes_local_var) { return NULL; } + memset(model_with_set_propertes_local_var, 0, sizeof(model_with_set_propertes_t)); model_with_set_propertes_local_var->tag_set = tag_set; model_with_set_propertes_local_var->string_set = string_set; diff --git a/samples/client/petstore/c-useJsonUnformatted/model/order.c b/samples/client/petstore/c-useJsonUnformatted/model/order.c index d67d1d47c714..7b96a4f77bc0 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/order.c +++ b/samples/client/petstore/c-useJsonUnformatted/model/order.c @@ -23,35 +23,64 @@ openapi_petstore_order_STATUS_e order_status_FromString(char* status){ } static order_t *order_create_internal( - long id, - long pet_id, - int quantity, + long *id, + long *pet_id, + int *quantity, char *ship_date, openapi_petstore_order_STATUS_e status, - int complete + int *complete ) { order_t *order_local_var = malloc(sizeof(order_t)); if (!order_local_var) { return NULL; } - order_local_var->id = id; - order_local_var->pet_id = pet_id; - order_local_var->quantity = quantity; + memset(order_local_var, 0, sizeof(order_t)); + if (id) { + order_local_var->id = malloc(sizeof(long)); + if (!order_local_var->id) { + order_free(order_local_var); + return NULL; + } + *order_local_var->id = *id; + } + if (pet_id) { + order_local_var->pet_id = malloc(sizeof(long)); + if (!order_local_var->pet_id) { + order_free(order_local_var); + return NULL; + } + *order_local_var->pet_id = *pet_id; + } + if (quantity) { + order_local_var->quantity = malloc(sizeof(int)); + if (!order_local_var->quantity) { + order_free(order_local_var); + return NULL; + } + *order_local_var->quantity = *quantity; + } order_local_var->ship_date = ship_date; order_local_var->status = status; - order_local_var->complete = complete; + if (complete) { + order_local_var->complete = malloc(sizeof(int)); + if (!order_local_var->complete) { + order_free(order_local_var); + return NULL; + } + *order_local_var->complete = *complete; + } order_local_var->_library_owned = 1; return order_local_var; } __attribute__((deprecated)) order_t *order_create( - long id, - long pet_id, - int quantity, + long *id, + long *pet_id, + int *quantity, char *ship_date, openapi_petstore_order_STATUS_e status, - int complete + int *complete ) { return order_create_internal ( id, @@ -72,10 +101,26 @@ void order_free(order_t *order) { return ; } listEntry_t *listEntry; + if (order->id) { + free(order->id); + order->id = NULL; + } + if (order->pet_id) { + free(order->pet_id); + order->pet_id = NULL; + } + if (order->quantity) { + free(order->quantity); + order->quantity = NULL; + } if (order->ship_date) { free(order->ship_date); order->ship_date = NULL; } + if (order->complete) { + free(order->complete); + order->complete = NULL; + } free(order); } @@ -84,7 +129,7 @@ cJSON *order_convertToJSON(order_t *order) { // order->id if(order->id) { - if(cJSON_AddNumberToObject(item, "id", order->id) == NULL) { + if(cJSON_AddNumberToObject(item, "id", *order->id) == NULL) { goto fail; //Numeric } } @@ -92,7 +137,7 @@ cJSON *order_convertToJSON(order_t *order) { // order->pet_id if(order->pet_id) { - if(cJSON_AddNumberToObject(item, "petId", order->pet_id) == NULL) { + if(cJSON_AddNumberToObject(item, "petId", *order->pet_id) == NULL) { goto fail; //Numeric } } @@ -100,7 +145,7 @@ cJSON *order_convertToJSON(order_t *order) { // order->quantity if(order->quantity) { - if(cJSON_AddNumberToObject(item, "quantity", order->quantity) == NULL) { + if(cJSON_AddNumberToObject(item, "quantity", *order->quantity) == NULL) { goto fail; //Numeric } } @@ -125,7 +170,7 @@ cJSON *order_convertToJSON(order_t *order) { // order->complete if(order->complete) { - if(cJSON_AddBoolToObject(item, "complete", order->complete) == NULL) { + if(cJSON_AddBoolToObject(item, "complete", *order->complete) == NULL) { goto fail; //Bool } } @@ -142,6 +187,18 @@ order_t *order_parseFromJSON(cJSON *orderJSON){ order_t *order_local_var = NULL; + // define the local variable for order->id + long *id_local_var = NULL; + + // define the local variable for order->pet_id + long *pet_id_local_var = NULL; + + // define the local variable for order->quantity + int *quantity_local_var = NULL; + + // define the local variable for order->complete + int *complete_local_var = NULL; + // order->id cJSON *id = cJSON_GetObjectItemCaseSensitive(orderJSON, "id"); if (cJSON_IsNull(id)) { @@ -152,6 +209,12 @@ order_t *order_parseFromJSON(cJSON *orderJSON){ { goto end; //Numeric } + id_local_var = malloc(sizeof(long)); + if(!id_local_var) + { + goto end; + } + *id_local_var = id->valuedouble; } // order->pet_id @@ -164,6 +227,12 @@ order_t *order_parseFromJSON(cJSON *orderJSON){ { goto end; //Numeric } + pet_id_local_var = malloc(sizeof(long)); + if(!pet_id_local_var) + { + goto end; + } + *pet_id_local_var = pet_id->valuedouble; } // order->quantity @@ -176,6 +245,12 @@ order_t *order_parseFromJSON(cJSON *orderJSON){ { goto end; //Numeric } + quantity_local_var = malloc(sizeof(int)); + if(!quantity_local_var) + { + goto end; + } + *quantity_local_var = quantity->valuedouble; } // order->ship_date @@ -214,20 +289,42 @@ order_t *order_parseFromJSON(cJSON *orderJSON){ { goto end; //Bool } + complete_local_var = malloc(sizeof(int)); + if(!complete_local_var) + { + goto end; + } + *complete_local_var = complete->valueint; } order_local_var = order_create_internal ( - id ? id->valuedouble : 0, - pet_id ? pet_id->valuedouble : 0, - quantity ? quantity->valuedouble : 0, + id_local_var, + pet_id_local_var, + quantity_local_var, ship_date && !cJSON_IsNull(ship_date) ? strdup(ship_date->valuestring) : NULL, status ? statusVariable : openapi_petstore_order_STATUS_NULL, - complete ? complete->valueint : 0 + complete_local_var ); return order_local_var; end: + if (id_local_var) { + free(id_local_var); + id_local_var = NULL; + } + if (pet_id_local_var) { + free(pet_id_local_var); + pet_id_local_var = NULL; + } + if (quantity_local_var) { + free(quantity_local_var); + quantity_local_var = NULL; + } + if (complete_local_var) { + free(complete_local_var); + complete_local_var = NULL; + } return NULL; } diff --git a/samples/client/petstore/c-useJsonUnformatted/model/order.h b/samples/client/petstore/c-useJsonUnformatted/model/order.h index 1b0a47b3028e..59ede1731710 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/order.h +++ b/samples/client/petstore/c-useJsonUnformatted/model/order.h @@ -27,23 +27,23 @@ openapi_petstore_order_STATUS_e order_status_FromString(char* status); typedef struct order_t { - long id; //numeric - long pet_id; //numeric - int quantity; //numeric + long *id; //numeric + long *pet_id; //numeric + int *quantity; //numeric char *ship_date; //date time openapi_petstore_order_STATUS_e status; //enum - int complete; //boolean + int *complete; //boolean int _library_owned; // Is the library responsible for freeing this object? } order_t; __attribute__((deprecated)) order_t *order_create( - long id, - long pet_id, - int quantity, + long *id, + long *pet_id, + int *quantity, char *ship_date, openapi_petstore_order_STATUS_e status, - int complete + int *complete ); void order_free(order_t *order); diff --git a/samples/client/petstore/c-useJsonUnformatted/model/pet.c b/samples/client/petstore/c-useJsonUnformatted/model/pet.c index b561634cb529..7c91bc7541a5 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/pet.c +++ b/samples/client/petstore/c-useJsonUnformatted/model/pet.c @@ -23,7 +23,7 @@ openapi_petstore_pet_STATUS_e pet_status_FromString(char* status){ } static pet_t *pet_create_internal( - long id, + long *id, category_t *category, char *name, list_t *photo_urls, @@ -34,7 +34,15 @@ static pet_t *pet_create_internal( if (!pet_local_var) { return NULL; } - pet_local_var->id = id; + memset(pet_local_var, 0, sizeof(pet_t)); + if (id) { + pet_local_var->id = malloc(sizeof(long)); + if (!pet_local_var->id) { + pet_free(pet_local_var); + return NULL; + } + *pet_local_var->id = *id; + } pet_local_var->category = category; pet_local_var->name = name; pet_local_var->photo_urls = photo_urls; @@ -46,7 +54,7 @@ static pet_t *pet_create_internal( } __attribute__((deprecated)) pet_t *pet_create( - long id, + long *id, category_t *category, char *name, list_t *photo_urls, @@ -72,6 +80,10 @@ void pet_free(pet_t *pet) { return ; } listEntry_t *listEntry; + if (pet->id) { + free(pet->id); + pet->id = NULL; + } if (pet->category) { category_free(pet->category); pet->category = NULL; @@ -102,7 +114,7 @@ cJSON *pet_convertToJSON(pet_t *pet) { // pet->id if(pet->id) { - if(cJSON_AddNumberToObject(item, "id", pet->id) == NULL) { + if(cJSON_AddNumberToObject(item, "id", *pet->id) == NULL) { goto fail; //Numeric } } @@ -188,6 +200,9 @@ pet_t *pet_parseFromJSON(cJSON *petJSON){ pet_t *pet_local_var = NULL; + // define the local variable for pet->id + long *id_local_var = NULL; + // define the local variable for pet->category category_t *category_local_nonprim = NULL; @@ -207,6 +222,12 @@ pet_t *pet_parseFromJSON(cJSON *petJSON){ { goto end; //Numeric } + id_local_var = malloc(sizeof(long)); + if(!id_local_var) + { + goto end; + } + *id_local_var = id->valuedouble; } // pet->category @@ -298,7 +319,7 @@ pet_t *pet_parseFromJSON(cJSON *petJSON){ pet_local_var = pet_create_internal ( - id ? id->valuedouble : 0, + id_local_var, category ? category_local_nonprim : NULL, strdup(name->valuestring), photo_urlsList, @@ -308,6 +329,10 @@ pet_t *pet_parseFromJSON(cJSON *petJSON){ return pet_local_var; end: + if (id_local_var) { + free(id_local_var); + id_local_var = NULL; + } if (category_local_nonprim) { category_free(category_local_nonprim); category_local_nonprim = NULL; diff --git a/samples/client/petstore/c-useJsonUnformatted/model/pet.h b/samples/client/petstore/c-useJsonUnformatted/model/pet.h index 860197e63a53..e86cda79d16e 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/pet.h +++ b/samples/client/petstore/c-useJsonUnformatted/model/pet.h @@ -29,7 +29,7 @@ openapi_petstore_pet_STATUS_e pet_status_FromString(char* status); typedef struct pet_t { - long id; //numeric + long *id; //numeric struct category_t *category; //model char *name; // string list_t *photo_urls; //primitive container @@ -40,7 +40,7 @@ typedef struct pet_t { } pet_t; __attribute__((deprecated)) pet_t *pet_create( - long id, + long *id, category_t *category, char *name, list_t *photo_urls, diff --git a/samples/client/petstore/c-useJsonUnformatted/model/tag.c b/samples/client/petstore/c-useJsonUnformatted/model/tag.c index e4b4f94af53d..0b54379df60c 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/tag.c +++ b/samples/client/petstore/c-useJsonUnformatted/model/tag.c @@ -6,14 +6,22 @@ static tag_t *tag_create_internal( - long id, + long *id, char *name ) { tag_t *tag_local_var = malloc(sizeof(tag_t)); if (!tag_local_var) { return NULL; } - tag_local_var->id = id; + memset(tag_local_var, 0, sizeof(tag_t)); + if (id) { + tag_local_var->id = malloc(sizeof(long)); + if (!tag_local_var->id) { + tag_free(tag_local_var); + return NULL; + } + *tag_local_var->id = *id; + } tag_local_var->name = name; tag_local_var->_library_owned = 1; @@ -21,7 +29,7 @@ static tag_t *tag_create_internal( } __attribute__((deprecated)) tag_t *tag_create( - long id, + long *id, char *name ) { return tag_create_internal ( @@ -39,6 +47,10 @@ void tag_free(tag_t *tag) { return ; } listEntry_t *listEntry; + if (tag->id) { + free(tag->id); + tag->id = NULL; + } if (tag->name) { free(tag->name); tag->name = NULL; @@ -51,7 +63,7 @@ cJSON *tag_convertToJSON(tag_t *tag) { // tag->id if(tag->id) { - if(cJSON_AddNumberToObject(item, "id", tag->id) == NULL) { + if(cJSON_AddNumberToObject(item, "id", *tag->id) == NULL) { goto fail; //Numeric } } @@ -76,6 +88,9 @@ tag_t *tag_parseFromJSON(cJSON *tagJSON){ tag_t *tag_local_var = NULL; + // define the local variable for tag->id + long *id_local_var = NULL; + // tag->id cJSON *id = cJSON_GetObjectItemCaseSensitive(tagJSON, "id"); if (cJSON_IsNull(id)) { @@ -86,6 +101,12 @@ tag_t *tag_parseFromJSON(cJSON *tagJSON){ { goto end; //Numeric } + id_local_var = malloc(sizeof(long)); + if(!id_local_var) + { + goto end; + } + *id_local_var = id->valuedouble; } // tag->name @@ -102,12 +123,16 @@ tag_t *tag_parseFromJSON(cJSON *tagJSON){ tag_local_var = tag_create_internal ( - id ? id->valuedouble : 0, + id_local_var, name && !cJSON_IsNull(name) ? strdup(name->valuestring) : NULL ); return tag_local_var; end: + if (id_local_var) { + free(id_local_var); + id_local_var = NULL; + } return NULL; } diff --git a/samples/client/petstore/c-useJsonUnformatted/model/tag.h b/samples/client/petstore/c-useJsonUnformatted/model/tag.h index d4b29e4d2e04..0e39017928c1 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/tag.h +++ b/samples/client/petstore/c-useJsonUnformatted/model/tag.h @@ -19,14 +19,14 @@ typedef struct tag_t tag_t; typedef struct tag_t { - long id; //numeric + long *id; //numeric char *name; // string int _library_owned; // Is the library responsible for freeing this object? } tag_t; __attribute__((deprecated)) tag_t *tag_create( - long id, + long *id, char *name ); diff --git a/samples/client/petstore/c-useJsonUnformatted/model/user.c b/samples/client/petstore/c-useJsonUnformatted/model/user.c index 780347915886..c81976dabcac 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/user.c +++ b/samples/client/petstore/c-useJsonUnformatted/model/user.c @@ -6,14 +6,14 @@ static user_t *user_create_internal( - long id, + long *id, char *username, char *first_name, char *last_name, char *email, char *password, char *phone, - int user_status, + int *user_status, list_t* extra, openapi_petstore_preference__e preference ) { @@ -21,14 +21,29 @@ static user_t *user_create_internal( if (!user_local_var) { return NULL; } - user_local_var->id = id; + memset(user_local_var, 0, sizeof(user_t)); + if (id) { + user_local_var->id = malloc(sizeof(long)); + if (!user_local_var->id) { + user_free(user_local_var); + return NULL; + } + *user_local_var->id = *id; + } user_local_var->username = username; user_local_var->first_name = first_name; user_local_var->last_name = last_name; user_local_var->email = email; user_local_var->password = password; user_local_var->phone = phone; - user_local_var->user_status = user_status; + if (user_status) { + user_local_var->user_status = malloc(sizeof(int)); + if (!user_local_var->user_status) { + user_free(user_local_var); + return NULL; + } + *user_local_var->user_status = *user_status; + } user_local_var->extra = extra; user_local_var->preference = preference; @@ -37,14 +52,14 @@ static user_t *user_create_internal( } __attribute__((deprecated)) user_t *user_create( - long id, + long *id, char *username, char *first_name, char *last_name, char *email, char *password, char *phone, - int user_status, + int *user_status, list_t* extra, openapi_petstore_preference__e preference ) { @@ -71,6 +86,10 @@ void user_free(user_t *user) { return ; } listEntry_t *listEntry; + if (user->id) { + free(user->id); + user->id = NULL; + } if (user->username) { free(user->username); user->username = NULL; @@ -95,6 +114,10 @@ void user_free(user_t *user) { free(user->phone); user->phone = NULL; } + if (user->user_status) { + free(user->user_status); + user->user_status = NULL; + } if (user->extra) { list_ForEach(listEntry, user->extra) { keyValuePair_t *localKeyValue = listEntry->data; @@ -113,7 +136,7 @@ cJSON *user_convertToJSON(user_t *user) { // user->id if(user->id) { - if(cJSON_AddNumberToObject(item, "id", user->id) == NULL) { + if(cJSON_AddNumberToObject(item, "id", *user->id) == NULL) { goto fail; //Numeric } } @@ -169,7 +192,7 @@ cJSON *user_convertToJSON(user_t *user) { // user->user_status if(user->user_status) { - if(cJSON_AddNumberToObject(item, "userStatus", user->user_status) == NULL) { + if(cJSON_AddNumberToObject(item, "userStatus", *user->user_status) == NULL) { goto fail; //Numeric } } @@ -215,6 +238,12 @@ user_t *user_parseFromJSON(cJSON *userJSON){ user_t *user_local_var = NULL; + // define the local variable for user->id + long *id_local_var = NULL; + + // define the local variable for user->user_status + int *user_status_local_var = NULL; + // define the local map for user->extra list_t *extraList = NULL; @@ -231,6 +260,12 @@ user_t *user_parseFromJSON(cJSON *userJSON){ { goto end; //Numeric } + id_local_var = malloc(sizeof(long)); + if(!id_local_var) + { + goto end; + } + *id_local_var = id->valuedouble; } // user->username @@ -315,6 +350,12 @@ user_t *user_parseFromJSON(cJSON *userJSON){ { goto end; //Numeric } + user_status_local_var = malloc(sizeof(int)); + if(!user_status_local_var) + { + goto end; + } + *user_status_local_var = user_status->valuedouble; } // user->extra @@ -351,20 +392,28 @@ user_t *user_parseFromJSON(cJSON *userJSON){ user_local_var = user_create_internal ( - id ? id->valuedouble : 0, + id_local_var, username && !cJSON_IsNull(username) ? strdup(username->valuestring) : NULL, first_name && !cJSON_IsNull(first_name) ? strdup(first_name->valuestring) : NULL, last_name && !cJSON_IsNull(last_name) ? strdup(last_name->valuestring) : NULL, email && !cJSON_IsNull(email) ? strdup(email->valuestring) : NULL, password && !cJSON_IsNull(password) ? strdup(password->valuestring) : NULL, phone && !cJSON_IsNull(phone) ? strdup(phone->valuestring) : NULL, - user_status ? user_status->valuedouble : 0, + user_status_local_var, extra ? extraList : NULL, preference ? preference_local_nonprim : 0 ); return user_local_var; end: + if (id_local_var) { + free(id_local_var); + id_local_var = NULL; + } + if (user_status_local_var) { + free(user_status_local_var); + user_status_local_var = NULL; + } if (extraList) { listEntry_t *listEntry = NULL; list_ForEach(listEntry, extraList) { diff --git a/samples/client/petstore/c-useJsonUnformatted/model/user.h b/samples/client/petstore/c-useJsonUnformatted/model/user.h index 4643e020a5fe..333195656bb0 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/user.h +++ b/samples/client/petstore/c-useJsonUnformatted/model/user.h @@ -21,14 +21,14 @@ typedef struct user_t user_t; typedef struct user_t { - long id; //numeric + long *id; //numeric char *username; // string char *first_name; // string char *last_name; // string char *email; // string char *password; // string char *phone; // string - int user_status; //numeric + int *user_status; //numeric list_t* extra; //map openapi_petstore_preference__e preference; //referenced enum @@ -36,14 +36,14 @@ typedef struct user_t { } user_t; __attribute__((deprecated)) user_t *user_create( - long id, + long *id, char *username, char *first_name, char *last_name, char *email, char *password, char *phone, - int user_status, + int *user_status, list_t* extra, openapi_petstore_preference__e preference ); From 246853353a711af33565c195a42ef4daf6981394 Mon Sep 17 00:00:00 2001 From: hirishh Date: Wed, 25 Feb 2026 03:05:03 +0100 Subject: [PATCH 4/6] [c] fix memory leaks --- .../resources/C-libcurl/model-body.mustache | 70 +++++++++---------- .../c-useJsonUnformatted/model/api_response.c | 19 +++-- .../c-useJsonUnformatted/model/category.c | 19 +++-- .../c-useJsonUnformatted/model/mapped_model.c | 19 +++-- .../model/model_with_set_propertes.c | 3 +- .../c-useJsonUnformatted/model/order.c | 67 ++++++++---------- .../petstore/c-useJsonUnformatted/model/pet.c | 19 +++-- .../petstore/c-useJsonUnformatted/model/tag.c | 19 +++-- .../c-useJsonUnformatted/model/user.c | 35 ++++------ .../client/petstore/c/model/api_response.c | 19 +++-- samples/client/petstore/c/model/category.c | 19 +++-- .../client/petstore/c/model/mapped_model.c | 19 +++-- .../c/model/model_with_set_propertes.c | 3 +- samples/client/petstore/c/model/order.c | 67 ++++++++---------- samples/client/petstore/c/model/pet.c | 19 +++-- samples/client/petstore/c/model/tag.c | 19 +++-- samples/client/petstore/c/model/user.c | 35 ++++------ 17 files changed, 204 insertions(+), 266 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/C-libcurl/model-body.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/model-body.mustache index 4d52cf79698c..788a413068d6 100644 --- a/modules/openapi-generator/src/main/resources/C-libcurl/model-body.mustache +++ b/modules/openapi-generator/src/main/resources/C-libcurl/model-body.mustache @@ -202,45 +202,10 @@ static {{classname}}_t *{{classname}}_create_internal( return NULL; } memset({{classname}}_local_var, 0, sizeof({{classname}}_t)); + {{classname}}_local_var->_library_owned = 1; {{#vars}} - {{^isContainer}} - {{#isPrimitiveType}} - {{#isNumeric}} - if ({{{name}}}) { - {{classname}}_local_var->{{{name}}} = malloc(sizeof({{datatype}})); - if (!{{classname}}_local_var->{{{name}}}) { - {{classname}}_free({{classname}}_local_var); - return NULL; - } - *{{classname}}_local_var->{{{name}}} = *{{{name}}}; - } - {{/isNumeric}} - {{#isBoolean}} - if ({{{name}}}) { - {{classname}}_local_var->{{{name}}} = malloc(sizeof({{datatype}})); - if (!{{classname}}_local_var->{{{name}}}) { - {{classname}}_free({{classname}}_local_var); - return NULL; - } - *{{classname}}_local_var->{{{name}}} = *{{{name}}}; - } - {{/isBoolean}} - {{^isNumeric}} - {{^isBoolean}} - {{classname}}_local_var->{{{name}}} = {{{name}}}; - {{/isBoolean}} - {{/isNumeric}} - {{/isPrimitiveType}} - {{^isPrimitiveType}} {{classname}}_local_var->{{{name}}} = {{{name}}}; - {{/isPrimitiveType}} - {{/isContainer}} - {{#isContainer}} - {{classname}}_local_var->{{{name}}} = {{{name}}}; - {{/isContainer}} {{/vars}} - - {{classname}}_local_var->_library_owned = 1; return {{classname}}_local_var; } @@ -322,9 +287,42 @@ __attribute__((deprecated)) {{classname}}_t *{{classname}}_create( {{/isContainer}} {{/vars}} ) { + {{#vars}} + {{#isPrimitiveType}} + {{#isNumeric}} + {{datatype}} *{{name}}_copy = NULL; + if ({{{name}}}) { + {{name}}_copy = malloc(sizeof({{datatype}})); + if ({{name}}_copy) *{{name}}_copy = *{{{name}}}; + } + {{/isNumeric}} + {{#isBoolean}} + {{datatype}} *{{name}}_copy = NULL; + if ({{{name}}}) { + {{name}}_copy = malloc(sizeof({{datatype}})); + if ({{name}}_copy) *{{name}}_copy = *{{{name}}}; + } + {{/isBoolean}} + {{/isPrimitiveType}} + {{/vars}} return {{classname}}_create_internal ( {{#vars}} + {{#isPrimitiveType}} + {{#isNumeric}} + {{name}}_copy{{^-last}},{{/-last}} + {{/isNumeric}} + {{#isBoolean}} + {{name}}_copy{{^-last}},{{/-last}} + {{/isBoolean}} + {{^isNumeric}} + {{^isBoolean}} {{name}}{{^-last}},{{/-last}} + {{/isBoolean}} + {{/isNumeric}} + {{/isPrimitiveType}} + {{^isPrimitiveType}} + {{name}}{{^-last}},{{/-last}} + {{/isPrimitiveType}} {{/vars}} ); } diff --git a/samples/client/petstore/c-useJsonUnformatted/model/api_response.c b/samples/client/petstore/c-useJsonUnformatted/model/api_response.c index 3541fbaf191b..a71a5ef95e9a 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/api_response.c +++ b/samples/client/petstore/c-useJsonUnformatted/model/api_response.c @@ -15,18 +15,10 @@ static api_response_t *api_response_create_internal( return NULL; } memset(api_response_local_var, 0, sizeof(api_response_t)); - if (code) { - api_response_local_var->code = malloc(sizeof(int)); - if (!api_response_local_var->code) { - api_response_free(api_response_local_var); - return NULL; - } - *api_response_local_var->code = *code; - } + api_response_local_var->_library_owned = 1; + api_response_local_var->code = code; api_response_local_var->type = type; api_response_local_var->message = message; - - api_response_local_var->_library_owned = 1; return api_response_local_var; } @@ -35,8 +27,13 @@ __attribute__((deprecated)) api_response_t *api_response_create( char *type, char *message ) { + int *code_copy = NULL; + if (code) { + code_copy = malloc(sizeof(int)); + if (code_copy) *code_copy = *code; + } return api_response_create_internal ( - code, + code_copy, type, message ); diff --git a/samples/client/petstore/c-useJsonUnformatted/model/category.c b/samples/client/petstore/c-useJsonUnformatted/model/category.c index b59bdfb5c8c5..3a2294c7205f 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/category.c +++ b/samples/client/petstore/c-useJsonUnformatted/model/category.c @@ -14,17 +14,9 @@ static category_t *category_create_internal( return NULL; } memset(category_local_var, 0, sizeof(category_t)); - if (id) { - category_local_var->id = malloc(sizeof(long)); - if (!category_local_var->id) { - category_free(category_local_var); - return NULL; - } - *category_local_var->id = *id; - } - category_local_var->name = name; - category_local_var->_library_owned = 1; + category_local_var->id = id; + category_local_var->name = name; return category_local_var; } @@ -32,8 +24,13 @@ __attribute__((deprecated)) category_t *category_create( long *id, char *name ) { + long *id_copy = NULL; + if (id) { + id_copy = malloc(sizeof(long)); + if (id_copy) *id_copy = *id; + } return category_create_internal ( - id, + id_copy, name ); } diff --git a/samples/client/petstore/c-useJsonUnformatted/model/mapped_model.c b/samples/client/petstore/c-useJsonUnformatted/model/mapped_model.c index 38e58473400c..bcff94b95bf1 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/mapped_model.c +++ b/samples/client/petstore/c-useJsonUnformatted/model/mapped_model.c @@ -14,17 +14,9 @@ static MappedModel_t *MappedModel_create_internal( return NULL; } memset(MappedModel_local_var, 0, sizeof(MappedModel_t)); - if (another_property) { - MappedModel_local_var->another_property = malloc(sizeof(int)); - if (!MappedModel_local_var->another_property) { - MappedModel_free(MappedModel_local_var); - return NULL; - } - *MappedModel_local_var->another_property = *another_property; - } - MappedModel_local_var->uuid_property = uuid_property; - MappedModel_local_var->_library_owned = 1; + MappedModel_local_var->another_property = another_property; + MappedModel_local_var->uuid_property = uuid_property; return MappedModel_local_var; } @@ -32,8 +24,13 @@ __attribute__((deprecated)) MappedModel_t *MappedModel_create( int *another_property, char *uuid_property ) { + int *another_property_copy = NULL; + if (another_property) { + another_property_copy = malloc(sizeof(int)); + if (another_property_copy) *another_property_copy = *another_property; + } return MappedModel_create_internal ( - another_property, + another_property_copy, uuid_property ); } diff --git a/samples/client/petstore/c-useJsonUnformatted/model/model_with_set_propertes.c b/samples/client/petstore/c-useJsonUnformatted/model/model_with_set_propertes.c index 94f3b581190b..b46d7659391f 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/model_with_set_propertes.c +++ b/samples/client/petstore/c-useJsonUnformatted/model/model_with_set_propertes.c @@ -14,10 +14,9 @@ static model_with_set_propertes_t *model_with_set_propertes_create_internal( return NULL; } memset(model_with_set_propertes_local_var, 0, sizeof(model_with_set_propertes_t)); + model_with_set_propertes_local_var->_library_owned = 1; model_with_set_propertes_local_var->tag_set = tag_set; model_with_set_propertes_local_var->string_set = string_set; - - model_with_set_propertes_local_var->_library_owned = 1; return model_with_set_propertes_local_var; } diff --git a/samples/client/petstore/c-useJsonUnformatted/model/order.c b/samples/client/petstore/c-useJsonUnformatted/model/order.c index 7b96a4f77bc0..0016968669cb 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/order.c +++ b/samples/client/petstore/c-useJsonUnformatted/model/order.c @@ -35,42 +35,13 @@ static order_t *order_create_internal( return NULL; } memset(order_local_var, 0, sizeof(order_t)); - if (id) { - order_local_var->id = malloc(sizeof(long)); - if (!order_local_var->id) { - order_free(order_local_var); - return NULL; - } - *order_local_var->id = *id; - } - if (pet_id) { - order_local_var->pet_id = malloc(sizeof(long)); - if (!order_local_var->pet_id) { - order_free(order_local_var); - return NULL; - } - *order_local_var->pet_id = *pet_id; - } - if (quantity) { - order_local_var->quantity = malloc(sizeof(int)); - if (!order_local_var->quantity) { - order_free(order_local_var); - return NULL; - } - *order_local_var->quantity = *quantity; - } + order_local_var->_library_owned = 1; + order_local_var->id = id; + order_local_var->pet_id = pet_id; + order_local_var->quantity = quantity; order_local_var->ship_date = ship_date; order_local_var->status = status; - if (complete) { - order_local_var->complete = malloc(sizeof(int)); - if (!order_local_var->complete) { - order_free(order_local_var); - return NULL; - } - *order_local_var->complete = *complete; - } - - order_local_var->_library_owned = 1; + order_local_var->complete = complete; return order_local_var; } @@ -82,13 +53,33 @@ __attribute__((deprecated)) order_t *order_create( openapi_petstore_order_STATUS_e status, int *complete ) { + long *id_copy = NULL; + if (id) { + id_copy = malloc(sizeof(long)); + if (id_copy) *id_copy = *id; + } + long *pet_id_copy = NULL; + if (pet_id) { + pet_id_copy = malloc(sizeof(long)); + if (pet_id_copy) *pet_id_copy = *pet_id; + } + int *quantity_copy = NULL; + if (quantity) { + quantity_copy = malloc(sizeof(int)); + if (quantity_copy) *quantity_copy = *quantity; + } + int *complete_copy = NULL; + if (complete) { + complete_copy = malloc(sizeof(int)); + if (complete_copy) *complete_copy = *complete; + } return order_create_internal ( - id, - pet_id, - quantity, + id_copy, + pet_id_copy, + quantity_copy, ship_date, status, - complete + complete_copy ); } diff --git a/samples/client/petstore/c-useJsonUnformatted/model/pet.c b/samples/client/petstore/c-useJsonUnformatted/model/pet.c index 7c91bc7541a5..add38126542e 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/pet.c +++ b/samples/client/petstore/c-useJsonUnformatted/model/pet.c @@ -35,21 +35,13 @@ static pet_t *pet_create_internal( return NULL; } memset(pet_local_var, 0, sizeof(pet_t)); - if (id) { - pet_local_var->id = malloc(sizeof(long)); - if (!pet_local_var->id) { - pet_free(pet_local_var); - return NULL; - } - *pet_local_var->id = *id; - } + pet_local_var->_library_owned = 1; + pet_local_var->id = id; pet_local_var->category = category; pet_local_var->name = name; pet_local_var->photo_urls = photo_urls; pet_local_var->tags = tags; pet_local_var->status = status; - - pet_local_var->_library_owned = 1; return pet_local_var; } @@ -61,8 +53,13 @@ __attribute__((deprecated)) pet_t *pet_create( list_t *tags, openapi_petstore_pet_STATUS_e status ) { + long *id_copy = NULL; + if (id) { + id_copy = malloc(sizeof(long)); + if (id_copy) *id_copy = *id; + } return pet_create_internal ( - id, + id_copy, category, name, photo_urls, diff --git a/samples/client/petstore/c-useJsonUnformatted/model/tag.c b/samples/client/petstore/c-useJsonUnformatted/model/tag.c index 0b54379df60c..951b01edd248 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/tag.c +++ b/samples/client/petstore/c-useJsonUnformatted/model/tag.c @@ -14,17 +14,9 @@ static tag_t *tag_create_internal( return NULL; } memset(tag_local_var, 0, sizeof(tag_t)); - if (id) { - tag_local_var->id = malloc(sizeof(long)); - if (!tag_local_var->id) { - tag_free(tag_local_var); - return NULL; - } - *tag_local_var->id = *id; - } - tag_local_var->name = name; - tag_local_var->_library_owned = 1; + tag_local_var->id = id; + tag_local_var->name = name; return tag_local_var; } @@ -32,8 +24,13 @@ __attribute__((deprecated)) tag_t *tag_create( long *id, char *name ) { + long *id_copy = NULL; + if (id) { + id_copy = malloc(sizeof(long)); + if (id_copy) *id_copy = *id; + } return tag_create_internal ( - id, + id_copy, name ); } diff --git a/samples/client/petstore/c-useJsonUnformatted/model/user.c b/samples/client/petstore/c-useJsonUnformatted/model/user.c index c81976dabcac..5cd454449f94 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/user.c +++ b/samples/client/petstore/c-useJsonUnformatted/model/user.c @@ -22,32 +22,17 @@ static user_t *user_create_internal( return NULL; } memset(user_local_var, 0, sizeof(user_t)); - if (id) { - user_local_var->id = malloc(sizeof(long)); - if (!user_local_var->id) { - user_free(user_local_var); - return NULL; - } - *user_local_var->id = *id; - } + user_local_var->_library_owned = 1; + user_local_var->id = id; user_local_var->username = username; user_local_var->first_name = first_name; user_local_var->last_name = last_name; user_local_var->email = email; user_local_var->password = password; user_local_var->phone = phone; - if (user_status) { - user_local_var->user_status = malloc(sizeof(int)); - if (!user_local_var->user_status) { - user_free(user_local_var); - return NULL; - } - *user_local_var->user_status = *user_status; - } + user_local_var->user_status = user_status; user_local_var->extra = extra; user_local_var->preference = preference; - - user_local_var->_library_owned = 1; return user_local_var; } @@ -63,15 +48,25 @@ __attribute__((deprecated)) user_t *user_create( list_t* extra, openapi_petstore_preference__e preference ) { + long *id_copy = NULL; + if (id) { + id_copy = malloc(sizeof(long)); + if (id_copy) *id_copy = *id; + } + int *user_status_copy = NULL; + if (user_status) { + user_status_copy = malloc(sizeof(int)); + if (user_status_copy) *user_status_copy = *user_status; + } return user_create_internal ( - id, + id_copy, username, first_name, last_name, email, password, phone, - user_status, + user_status_copy, extra, preference ); diff --git a/samples/client/petstore/c/model/api_response.c b/samples/client/petstore/c/model/api_response.c index 3541fbaf191b..a71a5ef95e9a 100644 --- a/samples/client/petstore/c/model/api_response.c +++ b/samples/client/petstore/c/model/api_response.c @@ -15,18 +15,10 @@ static api_response_t *api_response_create_internal( return NULL; } memset(api_response_local_var, 0, sizeof(api_response_t)); - if (code) { - api_response_local_var->code = malloc(sizeof(int)); - if (!api_response_local_var->code) { - api_response_free(api_response_local_var); - return NULL; - } - *api_response_local_var->code = *code; - } + api_response_local_var->_library_owned = 1; + api_response_local_var->code = code; api_response_local_var->type = type; api_response_local_var->message = message; - - api_response_local_var->_library_owned = 1; return api_response_local_var; } @@ -35,8 +27,13 @@ __attribute__((deprecated)) api_response_t *api_response_create( char *type, char *message ) { + int *code_copy = NULL; + if (code) { + code_copy = malloc(sizeof(int)); + if (code_copy) *code_copy = *code; + } return api_response_create_internal ( - code, + code_copy, type, message ); diff --git a/samples/client/petstore/c/model/category.c b/samples/client/petstore/c/model/category.c index b59bdfb5c8c5..3a2294c7205f 100644 --- a/samples/client/petstore/c/model/category.c +++ b/samples/client/petstore/c/model/category.c @@ -14,17 +14,9 @@ static category_t *category_create_internal( return NULL; } memset(category_local_var, 0, sizeof(category_t)); - if (id) { - category_local_var->id = malloc(sizeof(long)); - if (!category_local_var->id) { - category_free(category_local_var); - return NULL; - } - *category_local_var->id = *id; - } - category_local_var->name = name; - category_local_var->_library_owned = 1; + category_local_var->id = id; + category_local_var->name = name; return category_local_var; } @@ -32,8 +24,13 @@ __attribute__((deprecated)) category_t *category_create( long *id, char *name ) { + long *id_copy = NULL; + if (id) { + id_copy = malloc(sizeof(long)); + if (id_copy) *id_copy = *id; + } return category_create_internal ( - id, + id_copy, name ); } diff --git a/samples/client/petstore/c/model/mapped_model.c b/samples/client/petstore/c/model/mapped_model.c index 38e58473400c..bcff94b95bf1 100644 --- a/samples/client/petstore/c/model/mapped_model.c +++ b/samples/client/petstore/c/model/mapped_model.c @@ -14,17 +14,9 @@ static MappedModel_t *MappedModel_create_internal( return NULL; } memset(MappedModel_local_var, 0, sizeof(MappedModel_t)); - if (another_property) { - MappedModel_local_var->another_property = malloc(sizeof(int)); - if (!MappedModel_local_var->another_property) { - MappedModel_free(MappedModel_local_var); - return NULL; - } - *MappedModel_local_var->another_property = *another_property; - } - MappedModel_local_var->uuid_property = uuid_property; - MappedModel_local_var->_library_owned = 1; + MappedModel_local_var->another_property = another_property; + MappedModel_local_var->uuid_property = uuid_property; return MappedModel_local_var; } @@ -32,8 +24,13 @@ __attribute__((deprecated)) MappedModel_t *MappedModel_create( int *another_property, char *uuid_property ) { + int *another_property_copy = NULL; + if (another_property) { + another_property_copy = malloc(sizeof(int)); + if (another_property_copy) *another_property_copy = *another_property; + } return MappedModel_create_internal ( - another_property, + another_property_copy, uuid_property ); } diff --git a/samples/client/petstore/c/model/model_with_set_propertes.c b/samples/client/petstore/c/model/model_with_set_propertes.c index 94f3b581190b..b46d7659391f 100644 --- a/samples/client/petstore/c/model/model_with_set_propertes.c +++ b/samples/client/petstore/c/model/model_with_set_propertes.c @@ -14,10 +14,9 @@ static model_with_set_propertes_t *model_with_set_propertes_create_internal( return NULL; } memset(model_with_set_propertes_local_var, 0, sizeof(model_with_set_propertes_t)); + model_with_set_propertes_local_var->_library_owned = 1; model_with_set_propertes_local_var->tag_set = tag_set; model_with_set_propertes_local_var->string_set = string_set; - - model_with_set_propertes_local_var->_library_owned = 1; return model_with_set_propertes_local_var; } diff --git a/samples/client/petstore/c/model/order.c b/samples/client/petstore/c/model/order.c index 7b96a4f77bc0..0016968669cb 100644 --- a/samples/client/petstore/c/model/order.c +++ b/samples/client/petstore/c/model/order.c @@ -35,42 +35,13 @@ static order_t *order_create_internal( return NULL; } memset(order_local_var, 0, sizeof(order_t)); - if (id) { - order_local_var->id = malloc(sizeof(long)); - if (!order_local_var->id) { - order_free(order_local_var); - return NULL; - } - *order_local_var->id = *id; - } - if (pet_id) { - order_local_var->pet_id = malloc(sizeof(long)); - if (!order_local_var->pet_id) { - order_free(order_local_var); - return NULL; - } - *order_local_var->pet_id = *pet_id; - } - if (quantity) { - order_local_var->quantity = malloc(sizeof(int)); - if (!order_local_var->quantity) { - order_free(order_local_var); - return NULL; - } - *order_local_var->quantity = *quantity; - } + order_local_var->_library_owned = 1; + order_local_var->id = id; + order_local_var->pet_id = pet_id; + order_local_var->quantity = quantity; order_local_var->ship_date = ship_date; order_local_var->status = status; - if (complete) { - order_local_var->complete = malloc(sizeof(int)); - if (!order_local_var->complete) { - order_free(order_local_var); - return NULL; - } - *order_local_var->complete = *complete; - } - - order_local_var->_library_owned = 1; + order_local_var->complete = complete; return order_local_var; } @@ -82,13 +53,33 @@ __attribute__((deprecated)) order_t *order_create( openapi_petstore_order_STATUS_e status, int *complete ) { + long *id_copy = NULL; + if (id) { + id_copy = malloc(sizeof(long)); + if (id_copy) *id_copy = *id; + } + long *pet_id_copy = NULL; + if (pet_id) { + pet_id_copy = malloc(sizeof(long)); + if (pet_id_copy) *pet_id_copy = *pet_id; + } + int *quantity_copy = NULL; + if (quantity) { + quantity_copy = malloc(sizeof(int)); + if (quantity_copy) *quantity_copy = *quantity; + } + int *complete_copy = NULL; + if (complete) { + complete_copy = malloc(sizeof(int)); + if (complete_copy) *complete_copy = *complete; + } return order_create_internal ( - id, - pet_id, - quantity, + id_copy, + pet_id_copy, + quantity_copy, ship_date, status, - complete + complete_copy ); } diff --git a/samples/client/petstore/c/model/pet.c b/samples/client/petstore/c/model/pet.c index 7c91bc7541a5..add38126542e 100644 --- a/samples/client/petstore/c/model/pet.c +++ b/samples/client/petstore/c/model/pet.c @@ -35,21 +35,13 @@ static pet_t *pet_create_internal( return NULL; } memset(pet_local_var, 0, sizeof(pet_t)); - if (id) { - pet_local_var->id = malloc(sizeof(long)); - if (!pet_local_var->id) { - pet_free(pet_local_var); - return NULL; - } - *pet_local_var->id = *id; - } + pet_local_var->_library_owned = 1; + pet_local_var->id = id; pet_local_var->category = category; pet_local_var->name = name; pet_local_var->photo_urls = photo_urls; pet_local_var->tags = tags; pet_local_var->status = status; - - pet_local_var->_library_owned = 1; return pet_local_var; } @@ -61,8 +53,13 @@ __attribute__((deprecated)) pet_t *pet_create( list_t *tags, openapi_petstore_pet_STATUS_e status ) { + long *id_copy = NULL; + if (id) { + id_copy = malloc(sizeof(long)); + if (id_copy) *id_copy = *id; + } return pet_create_internal ( - id, + id_copy, category, name, photo_urls, diff --git a/samples/client/petstore/c/model/tag.c b/samples/client/petstore/c/model/tag.c index 0b54379df60c..951b01edd248 100644 --- a/samples/client/petstore/c/model/tag.c +++ b/samples/client/petstore/c/model/tag.c @@ -14,17 +14,9 @@ static tag_t *tag_create_internal( return NULL; } memset(tag_local_var, 0, sizeof(tag_t)); - if (id) { - tag_local_var->id = malloc(sizeof(long)); - if (!tag_local_var->id) { - tag_free(tag_local_var); - return NULL; - } - *tag_local_var->id = *id; - } - tag_local_var->name = name; - tag_local_var->_library_owned = 1; + tag_local_var->id = id; + tag_local_var->name = name; return tag_local_var; } @@ -32,8 +24,13 @@ __attribute__((deprecated)) tag_t *tag_create( long *id, char *name ) { + long *id_copy = NULL; + if (id) { + id_copy = malloc(sizeof(long)); + if (id_copy) *id_copy = *id; + } return tag_create_internal ( - id, + id_copy, name ); } diff --git a/samples/client/petstore/c/model/user.c b/samples/client/petstore/c/model/user.c index c81976dabcac..5cd454449f94 100644 --- a/samples/client/petstore/c/model/user.c +++ b/samples/client/petstore/c/model/user.c @@ -22,32 +22,17 @@ static user_t *user_create_internal( return NULL; } memset(user_local_var, 0, sizeof(user_t)); - if (id) { - user_local_var->id = malloc(sizeof(long)); - if (!user_local_var->id) { - user_free(user_local_var); - return NULL; - } - *user_local_var->id = *id; - } + user_local_var->_library_owned = 1; + user_local_var->id = id; user_local_var->username = username; user_local_var->first_name = first_name; user_local_var->last_name = last_name; user_local_var->email = email; user_local_var->password = password; user_local_var->phone = phone; - if (user_status) { - user_local_var->user_status = malloc(sizeof(int)); - if (!user_local_var->user_status) { - user_free(user_local_var); - return NULL; - } - *user_local_var->user_status = *user_status; - } + user_local_var->user_status = user_status; user_local_var->extra = extra; user_local_var->preference = preference; - - user_local_var->_library_owned = 1; return user_local_var; } @@ -63,15 +48,25 @@ __attribute__((deprecated)) user_t *user_create( list_t* extra, openapi_petstore_preference__e preference ) { + long *id_copy = NULL; + if (id) { + id_copy = malloc(sizeof(long)); + if (id_copy) *id_copy = *id; + } + int *user_status_copy = NULL; + if (user_status) { + user_status_copy = malloc(sizeof(int)); + if (user_status_copy) *user_status_copy = *user_status; + } return user_create_internal ( - id, + id_copy, username, first_name, last_name, email, password, phone, - user_status, + user_status_copy, extra, preference ); From 9b1227db7be83d4fbdb88940160970c923d07e49 Mon Sep 17 00:00:00 2001 From: hirishh Date: Wed, 25 Feb 2026 03:31:12 +0100 Subject: [PATCH 5/6] [c] fix memory leaks: second round --- .../resources/C-libcurl/model-body.mustache | 19 ++++++++++++++++++- .../c-useJsonUnformatted/model/api_response.c | 10 +++++++++- .../c-useJsonUnformatted/model/category.c | 10 +++++++++- .../c-useJsonUnformatted/model/mapped_model.c | 10 +++++++++- .../model/model_with_set_propertes.c | 9 ++++++++- .../c-useJsonUnformatted/model/order.c | 13 ++++++++++++- .../petstore/c-useJsonUnformatted/model/pet.c | 10 +++++++++- .../petstore/c-useJsonUnformatted/model/tag.c | 10 +++++++++- .../c-useJsonUnformatted/model/user.c | 11 ++++++++++- .../client/petstore/c/model/api_response.c | 10 +++++++++- samples/client/petstore/c/model/category.c | 10 +++++++++- .../client/petstore/c/model/mapped_model.c | 10 +++++++++- .../c/model/model_with_set_propertes.c | 9 ++++++++- samples/client/petstore/c/model/order.c | 13 ++++++++++++- samples/client/petstore/c/model/pet.c | 10 +++++++++- samples/client/petstore/c/model/tag.c | 10 +++++++++- samples/client/petstore/c/model/user.c | 11 ++++++++++- 17 files changed, 168 insertions(+), 17 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/C-libcurl/model-body.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/model-body.mustache index 788a413068d6..09def564f8d1 100644 --- a/modules/openapi-generator/src/main/resources/C-libcurl/model-body.mustache +++ b/modules/openapi-generator/src/main/resources/C-libcurl/model-body.mustache @@ -305,7 +305,7 @@ __attribute__((deprecated)) {{classname}}_t *{{classname}}_create( {{/isBoolean}} {{/isPrimitiveType}} {{/vars}} - return {{classname}}_create_internal ( + {{classname}}_t *result = {{classname}}_create_internal ( {{#vars}} {{#isPrimitiveType}} {{#isNumeric}} @@ -325,6 +325,19 @@ __attribute__((deprecated)) {{classname}}_t *{{classname}}_create( {{/isPrimitiveType}} {{/vars}} ); + if (!result) { + {{#vars}} + {{#isPrimitiveType}} + {{#isNumeric}} + free({{name}}_copy); + {{/isNumeric}} + {{#isBoolean}} + free({{name}}_copy); + {{/isBoolean}} + {{/isPrimitiveType}} + {{/vars}} + } + return result; } void {{classname}}_free({{classname}}_t *{{classname}}) { @@ -1091,6 +1104,10 @@ fail: {{/vars}} ); + if (!{{classname}}_local_var) { + goto end; + } + return {{classname}}_local_var; end: {{#vars}} diff --git a/samples/client/petstore/c-useJsonUnformatted/model/api_response.c b/samples/client/petstore/c-useJsonUnformatted/model/api_response.c index a71a5ef95e9a..d1dffbcac049 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/api_response.c +++ b/samples/client/petstore/c-useJsonUnformatted/model/api_response.c @@ -32,11 +32,15 @@ __attribute__((deprecated)) api_response_t *api_response_create( code_copy = malloc(sizeof(int)); if (code_copy) *code_copy = *code; } - return api_response_create_internal ( + api_response_t *result = api_response_create_internal ( code_copy, type, message ); + if (!result) { + free(code_copy); + } + return result; } void api_response_free(api_response_t *api_response) { @@ -153,6 +157,10 @@ api_response_t *api_response_parseFromJSON(cJSON *api_responseJSON){ message && !cJSON_IsNull(message) ? strdup(message->valuestring) : NULL ); + if (!api_response_local_var) { + goto end; + } + return api_response_local_var; end: if (code_local_var) { diff --git a/samples/client/petstore/c-useJsonUnformatted/model/category.c b/samples/client/petstore/c-useJsonUnformatted/model/category.c index 3a2294c7205f..5249af704edb 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/category.c +++ b/samples/client/petstore/c-useJsonUnformatted/model/category.c @@ -29,10 +29,14 @@ __attribute__((deprecated)) category_t *category_create( id_copy = malloc(sizeof(long)); if (id_copy) *id_copy = *id; } - return category_create_internal ( + category_t *result = category_create_internal ( id_copy, name ); + if (!result) { + free(id_copy); + } + return result; } void category_free(category_t *category) { @@ -124,6 +128,10 @@ category_t *category_parseFromJSON(cJSON *categoryJSON){ name && !cJSON_IsNull(name) ? strdup(name->valuestring) : NULL ); + if (!category_local_var) { + goto end; + } + return category_local_var; end: if (id_local_var) { diff --git a/samples/client/petstore/c-useJsonUnformatted/model/mapped_model.c b/samples/client/petstore/c-useJsonUnformatted/model/mapped_model.c index bcff94b95bf1..12523dfcf068 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/mapped_model.c +++ b/samples/client/petstore/c-useJsonUnformatted/model/mapped_model.c @@ -29,10 +29,14 @@ __attribute__((deprecated)) MappedModel_t *MappedModel_create( another_property_copy = malloc(sizeof(int)); if (another_property_copy) *another_property_copy = *another_property; } - return MappedModel_create_internal ( + MappedModel_t *result = MappedModel_create_internal ( another_property_copy, uuid_property ); + if (!result) { + free(another_property_copy); + } + return result; } void MappedModel_free(MappedModel_t *MappedModel) { @@ -124,6 +128,10 @@ MappedModel_t *MappedModel_parseFromJSON(cJSON *MappedModelJSON){ uuid_property && !cJSON_IsNull(uuid_property) ? strdup(uuid_property->valuestring) : NULL ); + if (!MappedModel_local_var) { + goto end; + } + return MappedModel_local_var; end: if (another_property_local_var) { diff --git a/samples/client/petstore/c-useJsonUnformatted/model/model_with_set_propertes.c b/samples/client/petstore/c-useJsonUnformatted/model/model_with_set_propertes.c index b46d7659391f..9ec6fc63894f 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/model_with_set_propertes.c +++ b/samples/client/petstore/c-useJsonUnformatted/model/model_with_set_propertes.c @@ -24,10 +24,13 @@ __attribute__((deprecated)) model_with_set_propertes_t *model_with_set_propertes list_t *tag_set, list_t *string_set ) { - return model_with_set_propertes_create_internal ( + model_with_set_propertes_t *result = model_with_set_propertes_create_internal ( tag_set, string_set ); + if (!result) { + } + return result; } void model_with_set_propertes_free(model_with_set_propertes_t *model_with_set_propertes) { @@ -165,6 +168,10 @@ model_with_set_propertes_t *model_with_set_propertes_parseFromJSON(cJSON *model_ string_set ? string_setList : NULL ); + if (!model_with_set_propertes_local_var) { + goto end; + } + return model_with_set_propertes_local_var; end: if (tag_setList) { diff --git a/samples/client/petstore/c-useJsonUnformatted/model/order.c b/samples/client/petstore/c-useJsonUnformatted/model/order.c index 0016968669cb..e7acce111588 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/order.c +++ b/samples/client/petstore/c-useJsonUnformatted/model/order.c @@ -73,7 +73,7 @@ __attribute__((deprecated)) order_t *order_create( complete_copy = malloc(sizeof(int)); if (complete_copy) *complete_copy = *complete; } - return order_create_internal ( + order_t *result = order_create_internal ( id_copy, pet_id_copy, quantity_copy, @@ -81,6 +81,13 @@ __attribute__((deprecated)) order_t *order_create( status, complete_copy ); + if (!result) { + free(id_copy); + free(pet_id_copy); + free(quantity_copy); + free(complete_copy); + } + return result; } void order_free(order_t *order) { @@ -298,6 +305,10 @@ order_t *order_parseFromJSON(cJSON *orderJSON){ complete_local_var ); + if (!order_local_var) { + goto end; + } + return order_local_var; end: if (id_local_var) { diff --git a/samples/client/petstore/c-useJsonUnformatted/model/pet.c b/samples/client/petstore/c-useJsonUnformatted/model/pet.c index add38126542e..bb5180bf739e 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/pet.c +++ b/samples/client/petstore/c-useJsonUnformatted/model/pet.c @@ -58,7 +58,7 @@ __attribute__((deprecated)) pet_t *pet_create( id_copy = malloc(sizeof(long)); if (id_copy) *id_copy = *id; } - return pet_create_internal ( + pet_t *result = pet_create_internal ( id_copy, category, name, @@ -66,6 +66,10 @@ __attribute__((deprecated)) pet_t *pet_create( tags, status ); + if (!result) { + free(id_copy); + } + return result; } void pet_free(pet_t *pet) { @@ -324,6 +328,10 @@ pet_t *pet_parseFromJSON(cJSON *petJSON){ status ? statusVariable : openapi_petstore_pet_STATUS_NULL ); + if (!pet_local_var) { + goto end; + } + return pet_local_var; end: if (id_local_var) { diff --git a/samples/client/petstore/c-useJsonUnformatted/model/tag.c b/samples/client/petstore/c-useJsonUnformatted/model/tag.c index 951b01edd248..1cd664239343 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/tag.c +++ b/samples/client/petstore/c-useJsonUnformatted/model/tag.c @@ -29,10 +29,14 @@ __attribute__((deprecated)) tag_t *tag_create( id_copy = malloc(sizeof(long)); if (id_copy) *id_copy = *id; } - return tag_create_internal ( + tag_t *result = tag_create_internal ( id_copy, name ); + if (!result) { + free(id_copy); + } + return result; } void tag_free(tag_t *tag) { @@ -124,6 +128,10 @@ tag_t *tag_parseFromJSON(cJSON *tagJSON){ name && !cJSON_IsNull(name) ? strdup(name->valuestring) : NULL ); + if (!tag_local_var) { + goto end; + } + return tag_local_var; end: if (id_local_var) { diff --git a/samples/client/petstore/c-useJsonUnformatted/model/user.c b/samples/client/petstore/c-useJsonUnformatted/model/user.c index 5cd454449f94..9ac7fb0a748d 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/user.c +++ b/samples/client/petstore/c-useJsonUnformatted/model/user.c @@ -58,7 +58,7 @@ __attribute__((deprecated)) user_t *user_create( user_status_copy = malloc(sizeof(int)); if (user_status_copy) *user_status_copy = *user_status; } - return user_create_internal ( + user_t *result = user_create_internal ( id_copy, username, first_name, @@ -70,6 +70,11 @@ __attribute__((deprecated)) user_t *user_create( extra, preference ); + if (!result) { + free(id_copy); + free(user_status_copy); + } + return result; } void user_free(user_t *user) { @@ -399,6 +404,10 @@ user_t *user_parseFromJSON(cJSON *userJSON){ preference ? preference_local_nonprim : 0 ); + if (!user_local_var) { + goto end; + } + return user_local_var; end: if (id_local_var) { diff --git a/samples/client/petstore/c/model/api_response.c b/samples/client/petstore/c/model/api_response.c index a71a5ef95e9a..d1dffbcac049 100644 --- a/samples/client/petstore/c/model/api_response.c +++ b/samples/client/petstore/c/model/api_response.c @@ -32,11 +32,15 @@ __attribute__((deprecated)) api_response_t *api_response_create( code_copy = malloc(sizeof(int)); if (code_copy) *code_copy = *code; } - return api_response_create_internal ( + api_response_t *result = api_response_create_internal ( code_copy, type, message ); + if (!result) { + free(code_copy); + } + return result; } void api_response_free(api_response_t *api_response) { @@ -153,6 +157,10 @@ api_response_t *api_response_parseFromJSON(cJSON *api_responseJSON){ message && !cJSON_IsNull(message) ? strdup(message->valuestring) : NULL ); + if (!api_response_local_var) { + goto end; + } + return api_response_local_var; end: if (code_local_var) { diff --git a/samples/client/petstore/c/model/category.c b/samples/client/petstore/c/model/category.c index 3a2294c7205f..5249af704edb 100644 --- a/samples/client/petstore/c/model/category.c +++ b/samples/client/petstore/c/model/category.c @@ -29,10 +29,14 @@ __attribute__((deprecated)) category_t *category_create( id_copy = malloc(sizeof(long)); if (id_copy) *id_copy = *id; } - return category_create_internal ( + category_t *result = category_create_internal ( id_copy, name ); + if (!result) { + free(id_copy); + } + return result; } void category_free(category_t *category) { @@ -124,6 +128,10 @@ category_t *category_parseFromJSON(cJSON *categoryJSON){ name && !cJSON_IsNull(name) ? strdup(name->valuestring) : NULL ); + if (!category_local_var) { + goto end; + } + return category_local_var; end: if (id_local_var) { diff --git a/samples/client/petstore/c/model/mapped_model.c b/samples/client/petstore/c/model/mapped_model.c index bcff94b95bf1..12523dfcf068 100644 --- a/samples/client/petstore/c/model/mapped_model.c +++ b/samples/client/petstore/c/model/mapped_model.c @@ -29,10 +29,14 @@ __attribute__((deprecated)) MappedModel_t *MappedModel_create( another_property_copy = malloc(sizeof(int)); if (another_property_copy) *another_property_copy = *another_property; } - return MappedModel_create_internal ( + MappedModel_t *result = MappedModel_create_internal ( another_property_copy, uuid_property ); + if (!result) { + free(another_property_copy); + } + return result; } void MappedModel_free(MappedModel_t *MappedModel) { @@ -124,6 +128,10 @@ MappedModel_t *MappedModel_parseFromJSON(cJSON *MappedModelJSON){ uuid_property && !cJSON_IsNull(uuid_property) ? strdup(uuid_property->valuestring) : NULL ); + if (!MappedModel_local_var) { + goto end; + } + return MappedModel_local_var; end: if (another_property_local_var) { diff --git a/samples/client/petstore/c/model/model_with_set_propertes.c b/samples/client/petstore/c/model/model_with_set_propertes.c index b46d7659391f..9ec6fc63894f 100644 --- a/samples/client/petstore/c/model/model_with_set_propertes.c +++ b/samples/client/petstore/c/model/model_with_set_propertes.c @@ -24,10 +24,13 @@ __attribute__((deprecated)) model_with_set_propertes_t *model_with_set_propertes list_t *tag_set, list_t *string_set ) { - return model_with_set_propertes_create_internal ( + model_with_set_propertes_t *result = model_with_set_propertes_create_internal ( tag_set, string_set ); + if (!result) { + } + return result; } void model_with_set_propertes_free(model_with_set_propertes_t *model_with_set_propertes) { @@ -165,6 +168,10 @@ model_with_set_propertes_t *model_with_set_propertes_parseFromJSON(cJSON *model_ string_set ? string_setList : NULL ); + if (!model_with_set_propertes_local_var) { + goto end; + } + return model_with_set_propertes_local_var; end: if (tag_setList) { diff --git a/samples/client/petstore/c/model/order.c b/samples/client/petstore/c/model/order.c index 0016968669cb..e7acce111588 100644 --- a/samples/client/petstore/c/model/order.c +++ b/samples/client/petstore/c/model/order.c @@ -73,7 +73,7 @@ __attribute__((deprecated)) order_t *order_create( complete_copy = malloc(sizeof(int)); if (complete_copy) *complete_copy = *complete; } - return order_create_internal ( + order_t *result = order_create_internal ( id_copy, pet_id_copy, quantity_copy, @@ -81,6 +81,13 @@ __attribute__((deprecated)) order_t *order_create( status, complete_copy ); + if (!result) { + free(id_copy); + free(pet_id_copy); + free(quantity_copy); + free(complete_copy); + } + return result; } void order_free(order_t *order) { @@ -298,6 +305,10 @@ order_t *order_parseFromJSON(cJSON *orderJSON){ complete_local_var ); + if (!order_local_var) { + goto end; + } + return order_local_var; end: if (id_local_var) { diff --git a/samples/client/petstore/c/model/pet.c b/samples/client/petstore/c/model/pet.c index add38126542e..bb5180bf739e 100644 --- a/samples/client/petstore/c/model/pet.c +++ b/samples/client/petstore/c/model/pet.c @@ -58,7 +58,7 @@ __attribute__((deprecated)) pet_t *pet_create( id_copy = malloc(sizeof(long)); if (id_copy) *id_copy = *id; } - return pet_create_internal ( + pet_t *result = pet_create_internal ( id_copy, category, name, @@ -66,6 +66,10 @@ __attribute__((deprecated)) pet_t *pet_create( tags, status ); + if (!result) { + free(id_copy); + } + return result; } void pet_free(pet_t *pet) { @@ -324,6 +328,10 @@ pet_t *pet_parseFromJSON(cJSON *petJSON){ status ? statusVariable : openapi_petstore_pet_STATUS_NULL ); + if (!pet_local_var) { + goto end; + } + return pet_local_var; end: if (id_local_var) { diff --git a/samples/client/petstore/c/model/tag.c b/samples/client/petstore/c/model/tag.c index 951b01edd248..1cd664239343 100644 --- a/samples/client/petstore/c/model/tag.c +++ b/samples/client/petstore/c/model/tag.c @@ -29,10 +29,14 @@ __attribute__((deprecated)) tag_t *tag_create( id_copy = malloc(sizeof(long)); if (id_copy) *id_copy = *id; } - return tag_create_internal ( + tag_t *result = tag_create_internal ( id_copy, name ); + if (!result) { + free(id_copy); + } + return result; } void tag_free(tag_t *tag) { @@ -124,6 +128,10 @@ tag_t *tag_parseFromJSON(cJSON *tagJSON){ name && !cJSON_IsNull(name) ? strdup(name->valuestring) : NULL ); + if (!tag_local_var) { + goto end; + } + return tag_local_var; end: if (id_local_var) { diff --git a/samples/client/petstore/c/model/user.c b/samples/client/petstore/c/model/user.c index 5cd454449f94..9ac7fb0a748d 100644 --- a/samples/client/petstore/c/model/user.c +++ b/samples/client/petstore/c/model/user.c @@ -58,7 +58,7 @@ __attribute__((deprecated)) user_t *user_create( user_status_copy = malloc(sizeof(int)); if (user_status_copy) *user_status_copy = *user_status; } - return user_create_internal ( + user_t *result = user_create_internal ( id_copy, username, first_name, @@ -70,6 +70,11 @@ __attribute__((deprecated)) user_t *user_create( extra, preference ); + if (!result) { + free(id_copy); + free(user_status_copy); + } + return result; } void user_free(user_t *user) { @@ -399,6 +404,10 @@ user_t *user_parseFromJSON(cJSON *userJSON){ preference ? preference_local_nonprim : 0 ); + if (!user_local_var) { + goto end; + } + return user_local_var; end: if (id_local_var) { From f79341f8641df2ca5ac6c545e80865c8591e1f1a Mon Sep 17 00:00:00 2001 From: hirishh Date: Wed, 25 Feb 2026 04:01:40 +0100 Subject: [PATCH 6/6] [c] fix memory leaks: third round --- .../resources/C-libcurl/model-body.mustache | 105 +++++++++++++++++- .../c-useJsonUnformatted/model/api_response.c | 19 +++- .../c-useJsonUnformatted/model/category.c | 10 +- .../c-useJsonUnformatted/model/mapped_model.c | 10 +- .../model/model_with_set_propertes.c | 1 + .../c-useJsonUnformatted/model/order.c | 10 +- .../petstore/c-useJsonUnformatted/model/pet.c | 10 +- .../petstore/c-useJsonUnformatted/model/tag.c | 10 +- .../c-useJsonUnformatted/model/user.c | 55 ++++++++- .../client/petstore/c/model/api_response.c | 19 +++- samples/client/petstore/c/model/category.c | 10 +- .../client/petstore/c/model/mapped_model.c | 10 +- .../c/model/model_with_set_propertes.c | 1 + samples/client/petstore/c/model/order.c | 10 +- samples/client/petstore/c/model/pet.c | 10 +- samples/client/petstore/c/model/tag.c | 10 +- samples/client/petstore/c/model/user.c | 55 ++++++++- 17 files changed, 323 insertions(+), 32 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/C-libcurl/model-body.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/model-body.mustache index 09def564f8d1..8602abd550e9 100644 --- a/modules/openapi-generator/src/main/resources/C-libcurl/model-body.mustache +++ b/modules/openapi-generator/src/main/resources/C-libcurl/model-body.mustache @@ -757,6 +757,14 @@ fail: {{/isFreeFormObject}} {{/isModel}} + {{#isUuid}} + char *{{name}}_local_str = NULL; + + {{/isUuid}} + {{#isEmail}} + char *{{name}}_local_str = NULL; + + {{/isEmail}} {{/isPrimitiveType}} {{#isPrimitiveType}} {{#isNumeric}} @@ -769,6 +777,24 @@ fail: {{datatype}} *{{name}}_local_var = NULL; {{/isBoolean}} + {{^isEnum}} + {{#isString}} + char *{{name}}_local_str = NULL; + + {{/isString}} + {{/isEnum}} + {{#isByteArray}} + char *{{name}}_local_str = NULL; + + {{/isByteArray}} + {{#isDate}} + char *{{name}}_local_str = NULL; + + {{/isDate}} + {{#isDateTime}} + char *{{name}}_local_str = NULL; + + {{/isDateTime}} {{/isPrimitiveType}} {{/isContainer}} {{/vars}} @@ -1030,6 +1056,35 @@ fail: {{/vars}} + {{#vars}} + {{^isContainer}} + {{^isPrimitiveType}} + {{#isUuid}} + if ({{{name}}}) {{name}}_local_str = strdup({{{name}}}->valuestring); + {{/isUuid}} + {{#isEmail}} + if ({{{name}}}) {{name}}_local_str = strdup({{{name}}}->valuestring); + {{/isEmail}} + {{/isPrimitiveType}} + {{#isPrimitiveType}} + {{^isEnum}} + {{#isString}} + if ({{{name}}} && !cJSON_IsNull({{{name}}})) {{name}}_local_str = strdup({{{name}}}->valuestring); + {{/isString}} + {{/isEnum}} + {{#isByteArray}} + if ({{{name}}}) {{name}}_local_str = strdup({{{name}}}->valuestring); + {{/isByteArray}} + {{#isDate}} + if ({{{name}}}) {{name}}_local_str = strdup({{{name}}}->valuestring); + {{/isDate}} + {{#isDateTime}} + if ({{{name}}} && !cJSON_IsNull({{{name}}})) {{name}}_local_str = strdup({{{name}}}->valuestring); + {{/isDateTime}} + {{/isPrimitiveType}} + {{/isContainer}} + {{/vars}} + {{classname}}_local_var = {{classname}}_create_internal ( {{#vars}} {{^isContainer}} @@ -1048,10 +1103,10 @@ fail: {{/isFreeFormObject}} {{/isModel}} {{#isUuid}} - {{^required}}{{{name}}} ? {{/required}}strdup({{{name}}}->valuestring){{^required}} : NULL{{/required}}{{^-last}},{{/-last}} + {{name}}_local_str{{^-last}},{{/-last}} {{/isUuid}} {{#isEmail}} - {{^required}}{{{name}}} ? {{/required}}strdup({{{name}}}->valuestring){{^required}} : NULL{{/required}}{{^-last}},{{/-last}} + {{name}}_local_str{{^-last}},{{/-last}} {{/isEmail}} {{#isFreeFormObject}} {{^required}}{{{name}}} ? {{/required}}{{{name}}}_local_object{{^required}} : NULL{{/required}}{{^-last}},{{/-last}} @@ -1071,20 +1126,20 @@ fail: {{/isEnum}} {{^isEnum}} {{#isString}} - {{^required}}{{{name}}} && !cJSON_IsNull({{{name}}}) ? {{/required}}strdup({{{name}}}->valuestring){{^required}} : NULL{{/required}}{{^-last}},{{/-last}} + {{name}}_local_str{{^-last}},{{/-last}} {{/isString}} {{/isEnum}} {{#isByteArray}} - {{^required}}{{{name}}} ? {{/required}}strdup({{{name}}}->valuestring){{^required}} : NULL{{/required}}{{^-last}},{{/-last}} + {{name}}_local_str{{^-last}},{{/-last}} {{/isByteArray}} {{#isBinary}} {{^required}}{{{name}}} ? {{/required}}decoded_str_{{{name}}}{{^required}} : NULL{{/required}}{{^-last}},{{/-last}} {{/isBinary}} {{#isDate}} - {{^required}}{{{name}}} ? {{/required}}strdup({{{name}}}->valuestring){{^required}} : NULL{{/required}}{{^-last}},{{/-last}} + {{name}}_local_str{{^-last}},{{/-last}} {{/isDate}} {{#isDateTime}} - {{^required}}{{{name}}} && !cJSON_IsNull({{{name}}}) ? {{/required}}strdup({{{name}}}->valuestring){{^required}} : NULL{{/required}}{{^-last}},{{/-last}} + {{name}}_local_str{{^-last}},{{/-last}} {{/isDateTime}} {{/isPrimitiveType}} {{/isContainer}} @@ -1131,6 +1186,18 @@ end: } {{/isFreeFormObject}} {{/isModel}} + {{#isUuid}} + if ({{{name}}}_local_str) { + free({{{name}}}_local_str); + {{{name}}}_local_str = NULL; + } + {{/isUuid}} + {{#isEmail}} + if ({{{name}}}_local_str) { + free({{{name}}}_local_str); + {{{name}}}_local_str = NULL; + } + {{/isEmail}} {{/isPrimitiveType}} {{#isPrimitiveType}} {{#isNumeric}} @@ -1145,6 +1212,32 @@ end: {{{name}}}_local_var = NULL; } {{/isBoolean}} + {{^isEnum}} + {{#isString}} + if ({{{name}}}_local_str) { + free({{{name}}}_local_str); + {{{name}}}_local_str = NULL; + } + {{/isString}} + {{/isEnum}} + {{#isByteArray}} + if ({{{name}}}_local_str) { + free({{{name}}}_local_str); + {{{name}}}_local_str = NULL; + } + {{/isByteArray}} + {{#isDate}} + if ({{{name}}}_local_str) { + free({{{name}}}_local_str); + {{{name}}}_local_str = NULL; + } + {{/isDate}} + {{#isDateTime}} + if ({{{name}}}_local_str) { + free({{{name}}}_local_str); + {{{name}}}_local_str = NULL; + } + {{/isDateTime}} {{/isPrimitiveType}} {{/isContainer}} {{#isContainer}} diff --git a/samples/client/petstore/c-useJsonUnformatted/model/api_response.c b/samples/client/petstore/c-useJsonUnformatted/model/api_response.c index d1dffbcac049..e77d4101158e 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/api_response.c +++ b/samples/client/petstore/c-useJsonUnformatted/model/api_response.c @@ -108,6 +108,10 @@ api_response_t *api_response_parseFromJSON(cJSON *api_responseJSON){ // define the local variable for api_response->code int *code_local_var = NULL; + char *type_local_str = NULL; + + char *message_local_str = NULL; + // api_response->code cJSON *code = cJSON_GetObjectItemCaseSensitive(api_responseJSON, "code"); if (cJSON_IsNull(code)) { @@ -151,10 +155,13 @@ api_response_t *api_response_parseFromJSON(cJSON *api_responseJSON){ } + if (type && !cJSON_IsNull(type)) type_local_str = strdup(type->valuestring); + if (message && !cJSON_IsNull(message)) message_local_str = strdup(message->valuestring); + api_response_local_var = api_response_create_internal ( code_local_var, - type && !cJSON_IsNull(type) ? strdup(type->valuestring) : NULL, - message && !cJSON_IsNull(message) ? strdup(message->valuestring) : NULL + type_local_str, + message_local_str ); if (!api_response_local_var) { @@ -167,6 +174,14 @@ api_response_t *api_response_parseFromJSON(cJSON *api_responseJSON){ free(code_local_var); code_local_var = NULL; } + if (type_local_str) { + free(type_local_str); + type_local_str = NULL; + } + if (message_local_str) { + free(message_local_str); + message_local_str = NULL; + } return NULL; } diff --git a/samples/client/petstore/c-useJsonUnformatted/model/category.c b/samples/client/petstore/c-useJsonUnformatted/model/category.c index 5249af704edb..de6198a3a4bf 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/category.c +++ b/samples/client/petstore/c-useJsonUnformatted/model/category.c @@ -92,6 +92,8 @@ category_t *category_parseFromJSON(cJSON *categoryJSON){ // define the local variable for category->id long *id_local_var = NULL; + char *name_local_str = NULL; + // category->id cJSON *id = cJSON_GetObjectItemCaseSensitive(categoryJSON, "id"); if (cJSON_IsNull(id)) { @@ -123,9 +125,11 @@ category_t *category_parseFromJSON(cJSON *categoryJSON){ } + if (name && !cJSON_IsNull(name)) name_local_str = strdup(name->valuestring); + category_local_var = category_create_internal ( id_local_var, - name && !cJSON_IsNull(name) ? strdup(name->valuestring) : NULL + name_local_str ); if (!category_local_var) { @@ -138,6 +142,10 @@ category_t *category_parseFromJSON(cJSON *categoryJSON){ free(id_local_var); id_local_var = NULL; } + if (name_local_str) { + free(name_local_str); + name_local_str = NULL; + } return NULL; } diff --git a/samples/client/petstore/c-useJsonUnformatted/model/mapped_model.c b/samples/client/petstore/c-useJsonUnformatted/model/mapped_model.c index 12523dfcf068..b4bb82e1889d 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/mapped_model.c +++ b/samples/client/petstore/c-useJsonUnformatted/model/mapped_model.c @@ -92,6 +92,8 @@ MappedModel_t *MappedModel_parseFromJSON(cJSON *MappedModelJSON){ // define the local variable for MappedModel->another_property int *another_property_local_var = NULL; + char *uuid_property_local_str = NULL; + // MappedModel->another_property cJSON *another_property = cJSON_GetObjectItemCaseSensitive(MappedModelJSON, "another_property"); if (cJSON_IsNull(another_property)) { @@ -123,9 +125,11 @@ MappedModel_t *MappedModel_parseFromJSON(cJSON *MappedModelJSON){ } + if (uuid_property && !cJSON_IsNull(uuid_property)) uuid_property_local_str = strdup(uuid_property->valuestring); + MappedModel_local_var = MappedModel_create_internal ( another_property_local_var, - uuid_property && !cJSON_IsNull(uuid_property) ? strdup(uuid_property->valuestring) : NULL + uuid_property_local_str ); if (!MappedModel_local_var) { @@ -138,6 +142,10 @@ MappedModel_t *MappedModel_parseFromJSON(cJSON *MappedModelJSON){ free(another_property_local_var); another_property_local_var = NULL; } + if (uuid_property_local_str) { + free(uuid_property_local_str); + uuid_property_local_str = NULL; + } return NULL; } diff --git a/samples/client/petstore/c-useJsonUnformatted/model/model_with_set_propertes.c b/samples/client/petstore/c-useJsonUnformatted/model/model_with_set_propertes.c index 9ec6fc63894f..5a9d85ab0fb6 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/model_with_set_propertes.c +++ b/samples/client/petstore/c-useJsonUnformatted/model/model_with_set_propertes.c @@ -163,6 +163,7 @@ model_with_set_propertes_t *model_with_set_propertes_parseFromJSON(cJSON *model_ } + model_with_set_propertes_local_var = model_with_set_propertes_create_internal ( tag_set ? tag_setList : NULL, string_set ? string_setList : NULL diff --git a/samples/client/petstore/c-useJsonUnformatted/model/order.c b/samples/client/petstore/c-useJsonUnformatted/model/order.c index e7acce111588..dab67dd0dbec 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/order.c +++ b/samples/client/petstore/c-useJsonUnformatted/model/order.c @@ -194,6 +194,8 @@ order_t *order_parseFromJSON(cJSON *orderJSON){ // define the local variable for order->quantity int *quantity_local_var = NULL; + char *ship_date_local_str = NULL; + // define the local variable for order->complete int *complete_local_var = NULL; @@ -296,11 +298,13 @@ order_t *order_parseFromJSON(cJSON *orderJSON){ } + if (ship_date && !cJSON_IsNull(ship_date)) ship_date_local_str = strdup(ship_date->valuestring); + order_local_var = order_create_internal ( id_local_var, pet_id_local_var, quantity_local_var, - ship_date && !cJSON_IsNull(ship_date) ? strdup(ship_date->valuestring) : NULL, + ship_date_local_str, status ? statusVariable : openapi_petstore_order_STATUS_NULL, complete_local_var ); @@ -323,6 +327,10 @@ order_t *order_parseFromJSON(cJSON *orderJSON){ free(quantity_local_var); quantity_local_var = NULL; } + if (ship_date_local_str) { + free(ship_date_local_str); + ship_date_local_str = NULL; + } if (complete_local_var) { free(complete_local_var); complete_local_var = NULL; diff --git a/samples/client/petstore/c-useJsonUnformatted/model/pet.c b/samples/client/petstore/c-useJsonUnformatted/model/pet.c index bb5180bf739e..861a0167662e 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/pet.c +++ b/samples/client/petstore/c-useJsonUnformatted/model/pet.c @@ -207,6 +207,8 @@ pet_t *pet_parseFromJSON(cJSON *petJSON){ // define the local variable for pet->category category_t *category_local_nonprim = NULL; + char *name_local_str = NULL; + // define the local list for pet->photo_urls list_t *photo_urlsList = NULL; @@ -319,10 +321,12 @@ pet_t *pet_parseFromJSON(cJSON *petJSON){ } + if (name && !cJSON_IsNull(name)) name_local_str = strdup(name->valuestring); + pet_local_var = pet_create_internal ( id_local_var, category ? category_local_nonprim : NULL, - strdup(name->valuestring), + name_local_str, photo_urlsList, tags ? tagsList : NULL, status ? statusVariable : openapi_petstore_pet_STATUS_NULL @@ -342,6 +346,10 @@ pet_t *pet_parseFromJSON(cJSON *petJSON){ category_free(category_local_nonprim); category_local_nonprim = NULL; } + if (name_local_str) { + free(name_local_str); + name_local_str = NULL; + } if (photo_urlsList) { listEntry_t *listEntry = NULL; list_ForEach(listEntry, photo_urlsList) { diff --git a/samples/client/petstore/c-useJsonUnformatted/model/tag.c b/samples/client/petstore/c-useJsonUnformatted/model/tag.c index 1cd664239343..ce7c30b8f49f 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/tag.c +++ b/samples/client/petstore/c-useJsonUnformatted/model/tag.c @@ -92,6 +92,8 @@ tag_t *tag_parseFromJSON(cJSON *tagJSON){ // define the local variable for tag->id long *id_local_var = NULL; + char *name_local_str = NULL; + // tag->id cJSON *id = cJSON_GetObjectItemCaseSensitive(tagJSON, "id"); if (cJSON_IsNull(id)) { @@ -123,9 +125,11 @@ tag_t *tag_parseFromJSON(cJSON *tagJSON){ } + if (name && !cJSON_IsNull(name)) name_local_str = strdup(name->valuestring); + tag_local_var = tag_create_internal ( id_local_var, - name && !cJSON_IsNull(name) ? strdup(name->valuestring) : NULL + name_local_str ); if (!tag_local_var) { @@ -138,6 +142,10 @@ tag_t *tag_parseFromJSON(cJSON *tagJSON){ free(id_local_var); id_local_var = NULL; } + if (name_local_str) { + free(name_local_str); + name_local_str = NULL; + } return NULL; } diff --git a/samples/client/petstore/c-useJsonUnformatted/model/user.c b/samples/client/petstore/c-useJsonUnformatted/model/user.c index 9ac7fb0a748d..9a560c7fea41 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/user.c +++ b/samples/client/petstore/c-useJsonUnformatted/model/user.c @@ -241,6 +241,18 @@ user_t *user_parseFromJSON(cJSON *userJSON){ // define the local variable for user->id long *id_local_var = NULL; + char *username_local_str = NULL; + + char *first_name_local_str = NULL; + + char *last_name_local_str = NULL; + + char *email_local_str = NULL; + + char *password_local_str = NULL; + + char *phone_local_str = NULL; + // define the local variable for user->user_status int *user_status_local_var = NULL; @@ -391,14 +403,21 @@ user_t *user_parseFromJSON(cJSON *userJSON){ } + if (username && !cJSON_IsNull(username)) username_local_str = strdup(username->valuestring); + if (first_name && !cJSON_IsNull(first_name)) first_name_local_str = strdup(first_name->valuestring); + if (last_name && !cJSON_IsNull(last_name)) last_name_local_str = strdup(last_name->valuestring); + if (email && !cJSON_IsNull(email)) email_local_str = strdup(email->valuestring); + if (password && !cJSON_IsNull(password)) password_local_str = strdup(password->valuestring); + if (phone && !cJSON_IsNull(phone)) phone_local_str = strdup(phone->valuestring); + user_local_var = user_create_internal ( id_local_var, - username && !cJSON_IsNull(username) ? strdup(username->valuestring) : NULL, - first_name && !cJSON_IsNull(first_name) ? strdup(first_name->valuestring) : NULL, - last_name && !cJSON_IsNull(last_name) ? strdup(last_name->valuestring) : NULL, - email && !cJSON_IsNull(email) ? strdup(email->valuestring) : NULL, - password && !cJSON_IsNull(password) ? strdup(password->valuestring) : NULL, - phone && !cJSON_IsNull(phone) ? strdup(phone->valuestring) : NULL, + username_local_str, + first_name_local_str, + last_name_local_str, + email_local_str, + password_local_str, + phone_local_str, user_status_local_var, extra ? extraList : NULL, preference ? preference_local_nonprim : 0 @@ -414,6 +433,30 @@ user_t *user_parseFromJSON(cJSON *userJSON){ free(id_local_var); id_local_var = NULL; } + if (username_local_str) { + free(username_local_str); + username_local_str = NULL; + } + if (first_name_local_str) { + free(first_name_local_str); + first_name_local_str = NULL; + } + if (last_name_local_str) { + free(last_name_local_str); + last_name_local_str = NULL; + } + if (email_local_str) { + free(email_local_str); + email_local_str = NULL; + } + if (password_local_str) { + free(password_local_str); + password_local_str = NULL; + } + if (phone_local_str) { + free(phone_local_str); + phone_local_str = NULL; + } if (user_status_local_var) { free(user_status_local_var); user_status_local_var = NULL; diff --git a/samples/client/petstore/c/model/api_response.c b/samples/client/petstore/c/model/api_response.c index d1dffbcac049..e77d4101158e 100644 --- a/samples/client/petstore/c/model/api_response.c +++ b/samples/client/petstore/c/model/api_response.c @@ -108,6 +108,10 @@ api_response_t *api_response_parseFromJSON(cJSON *api_responseJSON){ // define the local variable for api_response->code int *code_local_var = NULL; + char *type_local_str = NULL; + + char *message_local_str = NULL; + // api_response->code cJSON *code = cJSON_GetObjectItemCaseSensitive(api_responseJSON, "code"); if (cJSON_IsNull(code)) { @@ -151,10 +155,13 @@ api_response_t *api_response_parseFromJSON(cJSON *api_responseJSON){ } + if (type && !cJSON_IsNull(type)) type_local_str = strdup(type->valuestring); + if (message && !cJSON_IsNull(message)) message_local_str = strdup(message->valuestring); + api_response_local_var = api_response_create_internal ( code_local_var, - type && !cJSON_IsNull(type) ? strdup(type->valuestring) : NULL, - message && !cJSON_IsNull(message) ? strdup(message->valuestring) : NULL + type_local_str, + message_local_str ); if (!api_response_local_var) { @@ -167,6 +174,14 @@ api_response_t *api_response_parseFromJSON(cJSON *api_responseJSON){ free(code_local_var); code_local_var = NULL; } + if (type_local_str) { + free(type_local_str); + type_local_str = NULL; + } + if (message_local_str) { + free(message_local_str); + message_local_str = NULL; + } return NULL; } diff --git a/samples/client/petstore/c/model/category.c b/samples/client/petstore/c/model/category.c index 5249af704edb..de6198a3a4bf 100644 --- a/samples/client/petstore/c/model/category.c +++ b/samples/client/petstore/c/model/category.c @@ -92,6 +92,8 @@ category_t *category_parseFromJSON(cJSON *categoryJSON){ // define the local variable for category->id long *id_local_var = NULL; + char *name_local_str = NULL; + // category->id cJSON *id = cJSON_GetObjectItemCaseSensitive(categoryJSON, "id"); if (cJSON_IsNull(id)) { @@ -123,9 +125,11 @@ category_t *category_parseFromJSON(cJSON *categoryJSON){ } + if (name && !cJSON_IsNull(name)) name_local_str = strdup(name->valuestring); + category_local_var = category_create_internal ( id_local_var, - name && !cJSON_IsNull(name) ? strdup(name->valuestring) : NULL + name_local_str ); if (!category_local_var) { @@ -138,6 +142,10 @@ category_t *category_parseFromJSON(cJSON *categoryJSON){ free(id_local_var); id_local_var = NULL; } + if (name_local_str) { + free(name_local_str); + name_local_str = NULL; + } return NULL; } diff --git a/samples/client/petstore/c/model/mapped_model.c b/samples/client/petstore/c/model/mapped_model.c index 12523dfcf068..b4bb82e1889d 100644 --- a/samples/client/petstore/c/model/mapped_model.c +++ b/samples/client/petstore/c/model/mapped_model.c @@ -92,6 +92,8 @@ MappedModel_t *MappedModel_parseFromJSON(cJSON *MappedModelJSON){ // define the local variable for MappedModel->another_property int *another_property_local_var = NULL; + char *uuid_property_local_str = NULL; + // MappedModel->another_property cJSON *another_property = cJSON_GetObjectItemCaseSensitive(MappedModelJSON, "another_property"); if (cJSON_IsNull(another_property)) { @@ -123,9 +125,11 @@ MappedModel_t *MappedModel_parseFromJSON(cJSON *MappedModelJSON){ } + if (uuid_property && !cJSON_IsNull(uuid_property)) uuid_property_local_str = strdup(uuid_property->valuestring); + MappedModel_local_var = MappedModel_create_internal ( another_property_local_var, - uuid_property && !cJSON_IsNull(uuid_property) ? strdup(uuid_property->valuestring) : NULL + uuid_property_local_str ); if (!MappedModel_local_var) { @@ -138,6 +142,10 @@ MappedModel_t *MappedModel_parseFromJSON(cJSON *MappedModelJSON){ free(another_property_local_var); another_property_local_var = NULL; } + if (uuid_property_local_str) { + free(uuid_property_local_str); + uuid_property_local_str = NULL; + } return NULL; } diff --git a/samples/client/petstore/c/model/model_with_set_propertes.c b/samples/client/petstore/c/model/model_with_set_propertes.c index 9ec6fc63894f..5a9d85ab0fb6 100644 --- a/samples/client/petstore/c/model/model_with_set_propertes.c +++ b/samples/client/petstore/c/model/model_with_set_propertes.c @@ -163,6 +163,7 @@ model_with_set_propertes_t *model_with_set_propertes_parseFromJSON(cJSON *model_ } + model_with_set_propertes_local_var = model_with_set_propertes_create_internal ( tag_set ? tag_setList : NULL, string_set ? string_setList : NULL diff --git a/samples/client/petstore/c/model/order.c b/samples/client/petstore/c/model/order.c index e7acce111588..dab67dd0dbec 100644 --- a/samples/client/petstore/c/model/order.c +++ b/samples/client/petstore/c/model/order.c @@ -194,6 +194,8 @@ order_t *order_parseFromJSON(cJSON *orderJSON){ // define the local variable for order->quantity int *quantity_local_var = NULL; + char *ship_date_local_str = NULL; + // define the local variable for order->complete int *complete_local_var = NULL; @@ -296,11 +298,13 @@ order_t *order_parseFromJSON(cJSON *orderJSON){ } + if (ship_date && !cJSON_IsNull(ship_date)) ship_date_local_str = strdup(ship_date->valuestring); + order_local_var = order_create_internal ( id_local_var, pet_id_local_var, quantity_local_var, - ship_date && !cJSON_IsNull(ship_date) ? strdup(ship_date->valuestring) : NULL, + ship_date_local_str, status ? statusVariable : openapi_petstore_order_STATUS_NULL, complete_local_var ); @@ -323,6 +327,10 @@ order_t *order_parseFromJSON(cJSON *orderJSON){ free(quantity_local_var); quantity_local_var = NULL; } + if (ship_date_local_str) { + free(ship_date_local_str); + ship_date_local_str = NULL; + } if (complete_local_var) { free(complete_local_var); complete_local_var = NULL; diff --git a/samples/client/petstore/c/model/pet.c b/samples/client/petstore/c/model/pet.c index bb5180bf739e..861a0167662e 100644 --- a/samples/client/petstore/c/model/pet.c +++ b/samples/client/petstore/c/model/pet.c @@ -207,6 +207,8 @@ pet_t *pet_parseFromJSON(cJSON *petJSON){ // define the local variable for pet->category category_t *category_local_nonprim = NULL; + char *name_local_str = NULL; + // define the local list for pet->photo_urls list_t *photo_urlsList = NULL; @@ -319,10 +321,12 @@ pet_t *pet_parseFromJSON(cJSON *petJSON){ } + if (name && !cJSON_IsNull(name)) name_local_str = strdup(name->valuestring); + pet_local_var = pet_create_internal ( id_local_var, category ? category_local_nonprim : NULL, - strdup(name->valuestring), + name_local_str, photo_urlsList, tags ? tagsList : NULL, status ? statusVariable : openapi_petstore_pet_STATUS_NULL @@ -342,6 +346,10 @@ pet_t *pet_parseFromJSON(cJSON *petJSON){ category_free(category_local_nonprim); category_local_nonprim = NULL; } + if (name_local_str) { + free(name_local_str); + name_local_str = NULL; + } if (photo_urlsList) { listEntry_t *listEntry = NULL; list_ForEach(listEntry, photo_urlsList) { diff --git a/samples/client/petstore/c/model/tag.c b/samples/client/petstore/c/model/tag.c index 1cd664239343..ce7c30b8f49f 100644 --- a/samples/client/petstore/c/model/tag.c +++ b/samples/client/petstore/c/model/tag.c @@ -92,6 +92,8 @@ tag_t *tag_parseFromJSON(cJSON *tagJSON){ // define the local variable for tag->id long *id_local_var = NULL; + char *name_local_str = NULL; + // tag->id cJSON *id = cJSON_GetObjectItemCaseSensitive(tagJSON, "id"); if (cJSON_IsNull(id)) { @@ -123,9 +125,11 @@ tag_t *tag_parseFromJSON(cJSON *tagJSON){ } + if (name && !cJSON_IsNull(name)) name_local_str = strdup(name->valuestring); + tag_local_var = tag_create_internal ( id_local_var, - name && !cJSON_IsNull(name) ? strdup(name->valuestring) : NULL + name_local_str ); if (!tag_local_var) { @@ -138,6 +142,10 @@ tag_t *tag_parseFromJSON(cJSON *tagJSON){ free(id_local_var); id_local_var = NULL; } + if (name_local_str) { + free(name_local_str); + name_local_str = NULL; + } return NULL; } diff --git a/samples/client/petstore/c/model/user.c b/samples/client/petstore/c/model/user.c index 9ac7fb0a748d..9a560c7fea41 100644 --- a/samples/client/petstore/c/model/user.c +++ b/samples/client/petstore/c/model/user.c @@ -241,6 +241,18 @@ user_t *user_parseFromJSON(cJSON *userJSON){ // define the local variable for user->id long *id_local_var = NULL; + char *username_local_str = NULL; + + char *first_name_local_str = NULL; + + char *last_name_local_str = NULL; + + char *email_local_str = NULL; + + char *password_local_str = NULL; + + char *phone_local_str = NULL; + // define the local variable for user->user_status int *user_status_local_var = NULL; @@ -391,14 +403,21 @@ user_t *user_parseFromJSON(cJSON *userJSON){ } + if (username && !cJSON_IsNull(username)) username_local_str = strdup(username->valuestring); + if (first_name && !cJSON_IsNull(first_name)) first_name_local_str = strdup(first_name->valuestring); + if (last_name && !cJSON_IsNull(last_name)) last_name_local_str = strdup(last_name->valuestring); + if (email && !cJSON_IsNull(email)) email_local_str = strdup(email->valuestring); + if (password && !cJSON_IsNull(password)) password_local_str = strdup(password->valuestring); + if (phone && !cJSON_IsNull(phone)) phone_local_str = strdup(phone->valuestring); + user_local_var = user_create_internal ( id_local_var, - username && !cJSON_IsNull(username) ? strdup(username->valuestring) : NULL, - first_name && !cJSON_IsNull(first_name) ? strdup(first_name->valuestring) : NULL, - last_name && !cJSON_IsNull(last_name) ? strdup(last_name->valuestring) : NULL, - email && !cJSON_IsNull(email) ? strdup(email->valuestring) : NULL, - password && !cJSON_IsNull(password) ? strdup(password->valuestring) : NULL, - phone && !cJSON_IsNull(phone) ? strdup(phone->valuestring) : NULL, + username_local_str, + first_name_local_str, + last_name_local_str, + email_local_str, + password_local_str, + phone_local_str, user_status_local_var, extra ? extraList : NULL, preference ? preference_local_nonprim : 0 @@ -414,6 +433,30 @@ user_t *user_parseFromJSON(cJSON *userJSON){ free(id_local_var); id_local_var = NULL; } + if (username_local_str) { + free(username_local_str); + username_local_str = NULL; + } + if (first_name_local_str) { + free(first_name_local_str); + first_name_local_str = NULL; + } + if (last_name_local_str) { + free(last_name_local_str); + last_name_local_str = NULL; + } + if (email_local_str) { + free(email_local_str); + email_local_str = NULL; + } + if (password_local_str) { + free(password_local_str); + password_local_str = NULL; + } + if (phone_local_str) { + free(phone_local_str); + phone_local_str = NULL; + } if (user_status_local_var) { free(user_status_local_var); user_status_local_var = NULL;