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..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 @@ -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,11 +201,11 @@ static {{classname}}_t *{{classname}}_create_internal( if (!{{classname}}_local_var) { return NULL; } + memset({{classname}}_local_var, 0, sizeof({{classname}}_t)); + {{classname}}_local_var->_library_owned = 1; {{#vars}} {{classname}}_local_var->{{{name}}} = {{{name}}}; {{/vars}} - - {{classname}}_local_var->_library_owned = 1; return {{classname}}_local_var; } @@ -243,10 +243,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}} @@ -287,11 +287,57 @@ __attribute__((deprecated)) {{classname}}_t *{{classname}}_create( {{/isContainer}} {{/vars}} ) { - return {{classname}}_create_internal ( + {{#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}} + {{classname}}_t *result = {{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}} ); + if (!result) { + {{#vars}} + {{#isPrimitiveType}} + {{#isNumeric}} + free({{name}}_copy); + {{/isNumeric}} + {{#isBoolean}} + free({{name}}_copy); + {{/isBoolean}} + {{/isPrimitiveType}} + {{/vars}} + } + return result; } void {{classname}}_free({{classname}}_t *{{classname}}) { @@ -344,6 +390,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 +512,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}} @@ -699,6 +757,44 @@ fail: {{/isFreeFormObject}} {{/isModel}} + {{#isUuid}} + char *{{name}}_local_str = NULL; + + {{/isUuid}} + {{#isEmail}} + char *{{name}}_local_str = NULL; + + {{/isEmail}} + {{/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}} + {{^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}} @@ -722,6 +818,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 +831,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}} @@ -948,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}} @@ -966,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}} @@ -977,10 +1114,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}} @@ -989,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}} @@ -1022,6 +1159,10 @@ fail: {{/vars}} ); + if (!{{classname}}_local_var) { + goto end; + } + return {{classname}}_local_var; end: {{#vars}} @@ -1045,6 +1186,58 @@ 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}} + 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}} + {{^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/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}} diff --git a/samples/client/petstore/c-useJsonUnformatted/model/api_response.c b/samples/client/petstore/c-useJsonUnformatted/model/api_response.c index d0169c84f5b2..e77d4101158e 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,24 +14,33 @@ static api_response_t *api_response_create_internal( if (!api_response_local_var) { return NULL; } + memset(api_response_local_var, 0, sizeof(api_response_t)); + 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; } __attribute__((deprecated)) api_response_t *api_response_create( - int code, + int *code, char *type, char *message ) { - return api_response_create_internal ( - code, + int *code_copy = NULL; + if (code) { + code_copy = malloc(sizeof(int)); + if (code_copy) *code_copy = *code; + } + 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) { @@ -43,6 +52,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 +72,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 +105,13 @@ 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; + + char *type_local_str = NULL; + + char *message_local_str = NULL; + // api_response->code cJSON *code = cJSON_GetObjectItemCaseSensitive(api_responseJSON, "code"); if (cJSON_IsNull(code)) { @@ -102,6 +122,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 @@ -129,14 +155,33 @@ 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 ? code->valuedouble : 0, - type && !cJSON_IsNull(type) ? strdup(type->valuestring) : NULL, - message && !cJSON_IsNull(message) ? strdup(message->valuestring) : NULL + code_local_var, + type_local_str, + message_local_str ); + if (!api_response_local_var) { + goto end; + } + return api_response_local_var; end: + if (code_local_var) { + 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/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..de6198a3a4bf 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/category.c +++ b/samples/client/petstore/c-useJsonUnformatted/model/category.c @@ -6,28 +6,37 @@ 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; } + memset(category_local_var, 0, sizeof(category_t)); + category_local_var->_library_owned = 1; category_local_var->id = id; category_local_var->name = name; - - category_local_var->_library_owned = 1; return category_local_var; } __attribute__((deprecated)) category_t *category_create( - long id, + long *id, char *name ) { - return category_create_internal ( - id, + long *id_copy = NULL; + if (id) { + id_copy = malloc(sizeof(long)); + if (id_copy) *id_copy = *id; + } + category_t *result = category_create_internal ( + id_copy, name ); + if (!result) { + free(id_copy); + } + return result; } void category_free(category_t *category) { @@ -39,6 +48,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 +64,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 +89,11 @@ category_t *category_parseFromJSON(cJSON *categoryJSON){ category_t *category_local_var = NULL; + // 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)) { @@ -86,6 +104,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 @@ -101,13 +125,27 @@ category_t *category_parseFromJSON(cJSON *categoryJSON){ } + if (name && !cJSON_IsNull(name)) name_local_str = strdup(name->valuestring); + category_local_var = category_create_internal ( - id ? id->valuedouble : 0, - name && !cJSON_IsNull(name) ? strdup(name->valuestring) : NULL + id_local_var, + name_local_str ); + if (!category_local_var) { + goto end; + } + return category_local_var; end: + if (id_local_var) { + 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/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..b4bb82e1889d 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/mapped_model.c +++ b/samples/client/petstore/c-useJsonUnformatted/model/mapped_model.c @@ -6,28 +6,37 @@ 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; } + memset(MappedModel_local_var, 0, sizeof(MappedModel_t)); + MappedModel_local_var->_library_owned = 1; MappedModel_local_var->another_property = another_property; MappedModel_local_var->uuid_property = uuid_property; - - MappedModel_local_var->_library_owned = 1; return MappedModel_local_var; } __attribute__((deprecated)) MappedModel_t *MappedModel_create( - int another_property, + int *another_property, char *uuid_property ) { - return MappedModel_create_internal ( - another_property, + int *another_property_copy = NULL; + if (another_property) { + another_property_copy = malloc(sizeof(int)); + if (another_property_copy) *another_property_copy = *another_property; + } + 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) { @@ -39,6 +48,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 +64,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 +89,11 @@ 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; + + char *uuid_property_local_str = NULL; + // MappedModel->another_property cJSON *another_property = cJSON_GetObjectItemCaseSensitive(MappedModelJSON, "another_property"); if (cJSON_IsNull(another_property)) { @@ -86,6 +104,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 @@ -101,13 +125,27 @@ 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 ? another_property->valuedouble : 0, - uuid_property && !cJSON_IsNull(uuid_property) ? strdup(uuid_property->valuestring) : NULL + another_property_local_var, + uuid_property_local_str ); + if (!MappedModel_local_var) { + goto end; + } + return MappedModel_local_var; end: + if (another_property_local_var) { + 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/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..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 @@ -13,10 +13,10 @@ 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->_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; } @@ -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) { @@ -160,11 +163,16 @@ 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 ); + 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 d67d1d47c714..dab67dd0dbec 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/order.c +++ b/samples/client/petstore/c-useJsonUnformatted/model/order.c @@ -23,44 +23,71 @@ 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; } + memset(order_local_var, 0, sizeof(order_t)); + 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; 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, - pet_id, - quantity, + 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; + } + order_t *result = order_create_internal ( + id_copy, + pet_id_copy, + quantity_copy, ship_date, status, - complete + 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) { @@ -72,10 +99,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 +127,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 +135,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 +143,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 +168,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 +185,20 @@ 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; + + char *ship_date_local_str = 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,52 @@ 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; } + if (ship_date && !cJSON_IsNull(ship_date)) ship_date_local_str = strdup(ship_date->valuestring); + order_local_var = order_create_internal ( - id ? id->valuedouble : 0, - pet_id ? pet_id->valuedouble : 0, - quantity ? quantity->valuedouble : 0, - ship_date && !cJSON_IsNull(ship_date) ? strdup(ship_date->valuestring) : NULL, + id_local_var, + pet_id_local_var, + quantity_local_var, + ship_date_local_str, status ? statusVariable : openapi_petstore_order_STATUS_NULL, - complete ? complete->valueint : 0 + complete_local_var ); + if (!order_local_var) { + goto end; + } + 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 (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; + } 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..861a0167662e 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,33 +34,42 @@ static pet_t *pet_create_internal( if (!pet_local_var) { return NULL; } + memset(pet_local_var, 0, sizeof(pet_t)); + 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; } __attribute__((deprecated)) pet_t *pet_create( - long id, + long *id, category_t *category, char *name, list_t *photo_urls, list_t *tags, openapi_petstore_pet_STATUS_e status ) { - return pet_create_internal ( - id, + long *id_copy = NULL; + if (id) { + id_copy = malloc(sizeof(long)); + if (id_copy) *id_copy = *id; + } + pet_t *result = pet_create_internal ( + id_copy, category, name, photo_urls, tags, status ); + if (!result) { + free(id_copy); + } + return result; } void pet_free(pet_t *pet) { @@ -72,6 +81,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 +115,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,9 +201,14 @@ 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; + char *name_local_str = NULL; + // define the local list for pet->photo_urls list_t *photo_urlsList = NULL; @@ -207,6 +225,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 @@ -297,21 +321,35 @@ pet_t *pet_parseFromJSON(cJSON *petJSON){ } + if (name && !cJSON_IsNull(name)) name_local_str = strdup(name->valuestring); + pet_local_var = pet_create_internal ( - id ? id->valuedouble : 0, + 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 ); + if (!pet_local_var) { + goto end; + } + 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; } + 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/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..ce7c30b8f49f 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/tag.c +++ b/samples/client/petstore/c-useJsonUnformatted/model/tag.c @@ -6,28 +6,37 @@ 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; } + memset(tag_local_var, 0, sizeof(tag_t)); + tag_local_var->_library_owned = 1; tag_local_var->id = id; tag_local_var->name = name; - - tag_local_var->_library_owned = 1; return tag_local_var; } __attribute__((deprecated)) tag_t *tag_create( - long id, + long *id, char *name ) { - return tag_create_internal ( - id, + long *id_copy = NULL; + if (id) { + id_copy = malloc(sizeof(long)); + if (id_copy) *id_copy = *id; + } + tag_t *result = tag_create_internal ( + id_copy, name ); + if (!result) { + free(id_copy); + } + return result; } void tag_free(tag_t *tag) { @@ -39,6 +48,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 +64,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 +89,11 @@ tag_t *tag_parseFromJSON(cJSON *tagJSON){ tag_t *tag_local_var = NULL; + // 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)) { @@ -86,6 +104,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 @@ -101,13 +125,27 @@ tag_t *tag_parseFromJSON(cJSON *tagJSON){ } + if (name && !cJSON_IsNull(name)) name_local_str = strdup(name->valuestring); + tag_local_var = tag_create_internal ( - id ? id->valuedouble : 0, - name && !cJSON_IsNull(name) ? strdup(name->valuestring) : NULL + id_local_var, + name_local_str ); + if (!tag_local_var) { + goto end; + } + return tag_local_var; end: + if (id_local_var) { + 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/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..9a560c7fea41 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,6 +21,8 @@ static user_t *user_create_internal( if (!user_local_var) { return NULL; } + memset(user_local_var, 0, sizeof(user_t)); + user_local_var->_library_owned = 1; user_local_var->id = id; user_local_var->username = username; user_local_var->first_name = first_name; @@ -31,35 +33,48 @@ static user_t *user_create_internal( 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; } __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 ) { - return user_create_internal ( - id, + 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; + } + user_t *result = user_create_internal ( + id_copy, username, first_name, last_name, email, password, phone, - user_status, + user_status_copy, extra, preference ); + if (!result) { + free(id_copy); + free(user_status_copy); + } + return result; } void user_free(user_t *user) { @@ -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,24 @@ user_t *user_parseFromJSON(cJSON *userJSON){ user_t *user_local_var = NULL; + // 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; + // define the local map for user->extra list_t *extraList = NULL; @@ -231,6 +272,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 +362,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 @@ -350,21 +403,64 @@ 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 ? id->valuedouble : 0, - 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, + id_local_var, + 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 ); + if (!user_local_var) { + goto end; + } + return user_local_var; end: + if (id_local_var) { + 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; + } 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 ); diff --git a/samples/client/petstore/c/model/api_response.c b/samples/client/petstore/c/model/api_response.c index d0169c84f5b2..e77d4101158e 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,24 +14,33 @@ static api_response_t *api_response_create_internal( if (!api_response_local_var) { return NULL; } + memset(api_response_local_var, 0, sizeof(api_response_t)); + 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; } __attribute__((deprecated)) api_response_t *api_response_create( - int code, + int *code, char *type, char *message ) { - return api_response_create_internal ( - code, + int *code_copy = NULL; + if (code) { + code_copy = malloc(sizeof(int)); + if (code_copy) *code_copy = *code; + } + 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) { @@ -43,6 +52,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 +72,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 +105,13 @@ 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; + + char *type_local_str = NULL; + + char *message_local_str = NULL; + // api_response->code cJSON *code = cJSON_GetObjectItemCaseSensitive(api_responseJSON, "code"); if (cJSON_IsNull(code)) { @@ -102,6 +122,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 @@ -129,14 +155,33 @@ 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 ? code->valuedouble : 0, - type && !cJSON_IsNull(type) ? strdup(type->valuestring) : NULL, - message && !cJSON_IsNull(message) ? strdup(message->valuestring) : NULL + code_local_var, + type_local_str, + message_local_str ); + if (!api_response_local_var) { + goto end; + } + return api_response_local_var; end: + if (code_local_var) { + 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/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..de6198a3a4bf 100644 --- a/samples/client/petstore/c/model/category.c +++ b/samples/client/petstore/c/model/category.c @@ -6,28 +6,37 @@ 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; } + memset(category_local_var, 0, sizeof(category_t)); + category_local_var->_library_owned = 1; category_local_var->id = id; category_local_var->name = name; - - category_local_var->_library_owned = 1; return category_local_var; } __attribute__((deprecated)) category_t *category_create( - long id, + long *id, char *name ) { - return category_create_internal ( - id, + long *id_copy = NULL; + if (id) { + id_copy = malloc(sizeof(long)); + if (id_copy) *id_copy = *id; + } + category_t *result = category_create_internal ( + id_copy, name ); + if (!result) { + free(id_copy); + } + return result; } void category_free(category_t *category) { @@ -39,6 +48,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 +64,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 +89,11 @@ category_t *category_parseFromJSON(cJSON *categoryJSON){ category_t *category_local_var = NULL; + // 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)) { @@ -86,6 +104,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 @@ -101,13 +125,27 @@ category_t *category_parseFromJSON(cJSON *categoryJSON){ } + if (name && !cJSON_IsNull(name)) name_local_str = strdup(name->valuestring); + category_local_var = category_create_internal ( - id ? id->valuedouble : 0, - name && !cJSON_IsNull(name) ? strdup(name->valuestring) : NULL + id_local_var, + name_local_str ); + if (!category_local_var) { + goto end; + } + return category_local_var; end: + if (id_local_var) { + 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/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..b4bb82e1889d 100644 --- a/samples/client/petstore/c/model/mapped_model.c +++ b/samples/client/petstore/c/model/mapped_model.c @@ -6,28 +6,37 @@ 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; } + memset(MappedModel_local_var, 0, sizeof(MappedModel_t)); + MappedModel_local_var->_library_owned = 1; MappedModel_local_var->another_property = another_property; MappedModel_local_var->uuid_property = uuid_property; - - MappedModel_local_var->_library_owned = 1; return MappedModel_local_var; } __attribute__((deprecated)) MappedModel_t *MappedModel_create( - int another_property, + int *another_property, char *uuid_property ) { - return MappedModel_create_internal ( - another_property, + int *another_property_copy = NULL; + if (another_property) { + another_property_copy = malloc(sizeof(int)); + if (another_property_copy) *another_property_copy = *another_property; + } + 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) { @@ -39,6 +48,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 +64,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 +89,11 @@ 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; + + char *uuid_property_local_str = NULL; + // MappedModel->another_property cJSON *another_property = cJSON_GetObjectItemCaseSensitive(MappedModelJSON, "another_property"); if (cJSON_IsNull(another_property)) { @@ -86,6 +104,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 @@ -101,13 +125,27 @@ 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 ? another_property->valuedouble : 0, - uuid_property && !cJSON_IsNull(uuid_property) ? strdup(uuid_property->valuestring) : NULL + another_property_local_var, + uuid_property_local_str ); + if (!MappedModel_local_var) { + goto end; + } + return MappedModel_local_var; end: + if (another_property_local_var) { + 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/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..5a9d85ab0fb6 100644 --- a/samples/client/petstore/c/model/model_with_set_propertes.c +++ b/samples/client/petstore/c/model/model_with_set_propertes.c @@ -13,10 +13,10 @@ 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->_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; } @@ -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) { @@ -160,11 +163,16 @@ 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 ); + 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 d67d1d47c714..dab67dd0dbec 100644 --- a/samples/client/petstore/c/model/order.c +++ b/samples/client/petstore/c/model/order.c @@ -23,44 +23,71 @@ 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; } + memset(order_local_var, 0, sizeof(order_t)); + 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; 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, - pet_id, - quantity, + 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; + } + order_t *result = order_create_internal ( + id_copy, + pet_id_copy, + quantity_copy, ship_date, status, - complete + 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) { @@ -72,10 +99,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 +127,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 +135,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 +143,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 +168,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 +185,20 @@ 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; + + char *ship_date_local_str = 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,52 @@ 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; } + if (ship_date && !cJSON_IsNull(ship_date)) ship_date_local_str = strdup(ship_date->valuestring); + order_local_var = order_create_internal ( - id ? id->valuedouble : 0, - pet_id ? pet_id->valuedouble : 0, - quantity ? quantity->valuedouble : 0, - ship_date && !cJSON_IsNull(ship_date) ? strdup(ship_date->valuestring) : NULL, + id_local_var, + pet_id_local_var, + quantity_local_var, + ship_date_local_str, status ? statusVariable : openapi_petstore_order_STATUS_NULL, - complete ? complete->valueint : 0 + complete_local_var ); + if (!order_local_var) { + goto end; + } + 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 (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; + } 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..861a0167662e 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,33 +34,42 @@ static pet_t *pet_create_internal( if (!pet_local_var) { return NULL; } + memset(pet_local_var, 0, sizeof(pet_t)); + 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; } __attribute__((deprecated)) pet_t *pet_create( - long id, + long *id, category_t *category, char *name, list_t *photo_urls, list_t *tags, openapi_petstore_pet_STATUS_e status ) { - return pet_create_internal ( - id, + long *id_copy = NULL; + if (id) { + id_copy = malloc(sizeof(long)); + if (id_copy) *id_copy = *id; + } + pet_t *result = pet_create_internal ( + id_copy, category, name, photo_urls, tags, status ); + if (!result) { + free(id_copy); + } + return result; } void pet_free(pet_t *pet) { @@ -72,6 +81,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 +115,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,9 +201,14 @@ 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; + char *name_local_str = NULL; + // define the local list for pet->photo_urls list_t *photo_urlsList = NULL; @@ -207,6 +225,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 @@ -297,21 +321,35 @@ pet_t *pet_parseFromJSON(cJSON *petJSON){ } + if (name && !cJSON_IsNull(name)) name_local_str = strdup(name->valuestring); + pet_local_var = pet_create_internal ( - id ? id->valuedouble : 0, + 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 ); + if (!pet_local_var) { + goto end; + } + 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; } + 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/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..ce7c30b8f49f 100644 --- a/samples/client/petstore/c/model/tag.c +++ b/samples/client/petstore/c/model/tag.c @@ -6,28 +6,37 @@ 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; } + memset(tag_local_var, 0, sizeof(tag_t)); + tag_local_var->_library_owned = 1; tag_local_var->id = id; tag_local_var->name = name; - - tag_local_var->_library_owned = 1; return tag_local_var; } __attribute__((deprecated)) tag_t *tag_create( - long id, + long *id, char *name ) { - return tag_create_internal ( - id, + long *id_copy = NULL; + if (id) { + id_copy = malloc(sizeof(long)); + if (id_copy) *id_copy = *id; + } + tag_t *result = tag_create_internal ( + id_copy, name ); + if (!result) { + free(id_copy); + } + return result; } void tag_free(tag_t *tag) { @@ -39,6 +48,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 +64,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 +89,11 @@ tag_t *tag_parseFromJSON(cJSON *tagJSON){ tag_t *tag_local_var = NULL; + // 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)) { @@ -86,6 +104,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 @@ -101,13 +125,27 @@ tag_t *tag_parseFromJSON(cJSON *tagJSON){ } + if (name && !cJSON_IsNull(name)) name_local_str = strdup(name->valuestring); + tag_local_var = tag_create_internal ( - id ? id->valuedouble : 0, - name && !cJSON_IsNull(name) ? strdup(name->valuestring) : NULL + id_local_var, + name_local_str ); + if (!tag_local_var) { + goto end; + } + return tag_local_var; end: + if (id_local_var) { + 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/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..9a560c7fea41 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,6 +21,8 @@ static user_t *user_create_internal( if (!user_local_var) { return NULL; } + memset(user_local_var, 0, sizeof(user_t)); + user_local_var->_library_owned = 1; user_local_var->id = id; user_local_var->username = username; user_local_var->first_name = first_name; @@ -31,35 +33,48 @@ static user_t *user_create_internal( 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; } __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 ) { - return user_create_internal ( - id, + 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; + } + user_t *result = user_create_internal ( + id_copy, username, first_name, last_name, email, password, phone, - user_status, + user_status_copy, extra, preference ); + if (!result) { + free(id_copy); + free(user_status_copy); + } + return result; } void user_free(user_t *user) { @@ -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,24 @@ user_t *user_parseFromJSON(cJSON *userJSON){ user_t *user_local_var = NULL; + // 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; + // define the local map for user->extra list_t *extraList = NULL; @@ -231,6 +272,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 +362,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 @@ -350,21 +403,64 @@ 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 ? id->valuedouble : 0, - 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, + id_local_var, + 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 ); + if (!user_local_var) { + goto end; + } + return user_local_var; end: + if (id_local_var) { + 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; + } 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);