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 18968d2c7c00..103c33e0c4e7 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 @@ -553,6 +553,18 @@ fail: {{classname}}_t *{{classname}}_local_var = NULL; {{#vars}} + {{#isContainer}} + {{#isArray}} + // define the local list for {{{classname}}}->{{{name}}} + list_t *{{{name}}}List = NULL; + + {{/isArray}} + {{#isMap}} + // define the local map for {{{classname}}}->{{{name}}} + list_t *{{{name}}}List = NULL; + + {{/isMap}} + {{/isContainer}} {{^isContainer}} {{^isPrimitiveType}} {{#isModel}} @@ -693,9 +705,8 @@ fail: {{#isContainer}} {{#isArray}} {{#isPrimitiveType}} - list_t *{{{name}}}List; {{^required}}if ({{{name}}}) { {{/required}} - cJSON *{{{name}}}_local; + cJSON *{{{name}}}_local = NULL; if(!cJSON_IsArray({{{name}}})) { goto end;//primitive container } @@ -735,9 +746,8 @@ fail: } {{/isPrimitiveType}} {{^isPrimitiveType}} - list_t *{{{name}}}List; {{^required}}if ({{{name}}}) { {{/required}} - cJSON *{{{name}}}_local_nonprimitive; + cJSON *{{{name}}}_local_nonprimitive = NULL; if(!cJSON_IsArray({{{name}}})){ goto end; //nonprimitive container } @@ -756,9 +766,9 @@ fail: {{/isPrimitiveType}} {{/isArray}} {{#isMap}} - list_t *{{{name}}}List; {{^required}}if ({{{name}}}) { {{/required}} - cJSON *{{{name}}}_local_map; + {{#isPrimitiveType}} + cJSON *{{{name}}}_local_map = NULL; if(!cJSON_IsObject({{{name}}})) { goto end;//primitive map container } @@ -799,6 +809,12 @@ fail: {{/items}} list_addElement({{{name}}}List , localMapKeyPair); } + {{/isPrimitiveType}} + {{^isPrimitiveType}} + + // The data type of the elements in {{{classname}}}->{{{name}}} is currently not supported. + + {{/isPrimitiveType}} {{/isMap}} {{/isContainer}} {{^required}} @@ -904,6 +920,74 @@ end: {{/isModel}} {{/isPrimitiveType}} {{/isContainer}} + {{#isContainer}} + {{#isArray}} + {{#isPrimitiveType}} + if ({{{name}}}List) { + {{#items}} + {{#isString}} + listEntry_t *listEntry = NULL; + list_ForEach(listEntry, {{{name}}}List) { + free(listEntry->data); + listEntry->data = NULL; + } + {{/isString}} + {{#isNumeric}} + listEntry_t *listEntry = NULL; + list_ForEach(listEntry, {{{name}}}List) { + free(listEntry->data); + listEntry->data = NULL; + } + {{/isNumeric}} + {{/items}} + list_freeList({{{name}}}List); + {{{name}}}List = NULL; + } + {{/isPrimitiveType}} + {{^isPrimitiveType}} + if ({{{name}}}List) { + listEntry_t *listEntry = NULL; + list_ForEach(listEntry, {{{name}}}List) { + {{complexType}}_free(listEntry->data); + listEntry->data = NULL; + } + list_freeList({{{name}}}List); + {{{name}}}List = NULL; + } + {{/isPrimitiveType}} + {{/isArray}} + {{#isMap}} + {{#isPrimitiveType}} + if ({{{name}}}List) { + listEntry_t *listEntry = NULL; + list_ForEach(listEntry, {{{name}}}List) { + keyValuePair_t *localKeyValue = (keyValuePair_t*) listEntry->data; + free(localKeyValue->key); + localKeyValue->key = NULL; + {{#items}} + {{#isString}} + free(localKeyValue->value); + localKeyValue->value = NULL; + {{/isString}} + {{#isByteArray}} + free(localKeyValue->value); + localKeyValue->value = NULL; + {{/isByteArray}} + {{/items}} + keyValuePair_free(localKeyValue); + localKeyValue = NULL; + } + list_freeList({{{name}}}List); + {{{name}}}List = NULL; + } + {{/isPrimitiveType}} + {{^isPrimitiveType}} + + // The data type of the elements in {{{classname}}}->{{{name}}} is currently not supported. + + {{/isPrimitiveType}} + {{/isMap}} + {{/isContainer}} {{/vars}} return NULL; diff --git a/samples/client/petstore/c/model/pet.c b/samples/client/petstore/c/model/pet.c index a9f9c27ee472..278854ce519d 100644 --- a/samples/client/petstore/c/model/pet.c +++ b/samples/client/petstore/c/model/pet.c @@ -171,6 +171,12 @@ pet_t *pet_parseFromJSON(cJSON *petJSON){ // define the local variable for pet->category category_t *category_local_nonprim = NULL; + // define the local list for pet->photo_urls + list_t *photo_urlsList = NULL; + + // define the local list for pet->tags + list_t *tagsList = NULL; + // pet->id cJSON *id = cJSON_GetObjectItemCaseSensitive(petJSON, "id"); if (id) { @@ -204,9 +210,8 @@ pet_t *pet_parseFromJSON(cJSON *petJSON){ goto end; } - list_t *photo_urlsList; - cJSON *photo_urls_local; + cJSON *photo_urls_local = NULL; if(!cJSON_IsArray(photo_urls)) { goto end;//primitive container } @@ -223,9 +228,8 @@ pet_t *pet_parseFromJSON(cJSON *petJSON){ // pet->tags cJSON *tags = cJSON_GetObjectItemCaseSensitive(petJSON, "tags"); - list_t *tagsList; if (tags) { - cJSON *tags_local_nonprimitive; + cJSON *tags_local_nonprimitive = NULL; if(!cJSON_IsArray(tags)){ goto end; //nonprimitive container } @@ -270,6 +274,24 @@ pet_t *pet_parseFromJSON(cJSON *petJSON){ category_free(category_local_nonprim); category_local_nonprim = NULL; } + if (photo_urlsList) { + listEntry_t *listEntry = NULL; + list_ForEach(listEntry, photo_urlsList) { + free(listEntry->data); + listEntry->data = NULL; + } + list_freeList(photo_urlsList); + photo_urlsList = NULL; + } + if (tagsList) { + listEntry_t *listEntry = NULL; + list_ForEach(listEntry, tagsList) { + tag_free(listEntry->data); + listEntry->data = NULL; + } + list_freeList(tagsList); + tagsList = NULL; + } return NULL; }