diff --git a/modules/openapi-generator/src/main/resources/C-libcurl/object-body.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/object-body.mustache index d106693842f6..9606d41d980c 100644 --- a/modules/openapi-generator/src/main/resources/C-libcurl/object-body.mustache +++ b/modules/openapi-generator/src/main/resources/C-libcurl/object-body.mustache @@ -4,28 +4,48 @@ #include "object.h" object_t *object_create() { - object_t *object = malloc(sizeof(object_t)); + object_t *object = calloc(1, sizeof(object_t)); return object; } void object_free(object_t *object) { + if (!object) { + return ; + } + + if (object->temporary) { + free(object->temporary); + object->temporary = NULL; + } + free (object); } cJSON *object_convertToJSON(object_t *object) { - cJSON *item = cJSON_CreateObject(); + if (!object) { + return NULL; + } - return item; -fail: - cJSON_Delete(item); - return NULL; + if (!object->temporary) { + return cJSON_Parse("{}"); + } + + return cJSON_Parse(object->temporary); } -object_t *object_parseFromJSON(char *jsonString){ - object_t *object = NULL; +object_t *object_parseFromJSON(cJSON *json){ + if (!json) { + goto end; + } + object_t *object = object_create(); + if (!object) { + goto end; + } + object->temporary = cJSON_Print(json); return object; + end: return NULL; } diff --git a/modules/openapi-generator/src/main/resources/C-libcurl/object-header.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/object-header.mustache index 18503e0c276f..35bfeb60fd13 100644 --- a/modules/openapi-generator/src/main/resources/C-libcurl/object-header.mustache +++ b/modules/openapi-generator/src/main/resources/C-libcurl/object-header.mustache @@ -20,7 +20,7 @@ object_t *object_create(); void object_free(object_t *object); -object_t *object_parseFromJSON(char *jsonString); +object_t *object_parseFromJSON(cJSON *json); cJSON *object_convertToJSON(object_t *object); diff --git a/samples/client/petstore/c/model/object.c b/samples/client/petstore/c/model/object.c index d106693842f6..9606d41d980c 100644 --- a/samples/client/petstore/c/model/object.c +++ b/samples/client/petstore/c/model/object.c @@ -4,28 +4,48 @@ #include "object.h" object_t *object_create() { - object_t *object = malloc(sizeof(object_t)); + object_t *object = calloc(1, sizeof(object_t)); return object; } void object_free(object_t *object) { + if (!object) { + return ; + } + + if (object->temporary) { + free(object->temporary); + object->temporary = NULL; + } + free (object); } cJSON *object_convertToJSON(object_t *object) { - cJSON *item = cJSON_CreateObject(); + if (!object) { + return NULL; + } - return item; -fail: - cJSON_Delete(item); - return NULL; + if (!object->temporary) { + return cJSON_Parse("{}"); + } + + return cJSON_Parse(object->temporary); } -object_t *object_parseFromJSON(char *jsonString){ - object_t *object = NULL; +object_t *object_parseFromJSON(cJSON *json){ + if (!json) { + goto end; + } + object_t *object = object_create(); + if (!object) { + goto end; + } + object->temporary = cJSON_Print(json); return object; + end: return NULL; } diff --git a/samples/client/petstore/c/model/object.h b/samples/client/petstore/c/model/object.h index 18503e0c276f..35bfeb60fd13 100644 --- a/samples/client/petstore/c/model/object.h +++ b/samples/client/petstore/c/model/object.h @@ -20,7 +20,7 @@ object_t *object_create(); void object_free(object_t *object); -object_t *object_parseFromJSON(char *jsonString); +object_t *object_parseFromJSON(cJSON *json); cJSON *object_convertToJSON(object_t *object);