From e1549037438b698f5cf654b92f52c3b25065a63c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ernesto=20Fern=C3=A1ndez?= Date: Sun, 5 Jan 2025 23:53:36 -0300 Subject: [PATCH] Fix a few issues with the C generator (part 8) (#20378) * [C] Deprecate *_create() to avoid *_free() confusion The behaviour of *_free() doesn't match *_create(), so the user should avoid using them together. But they still need *_free() to clean up library-allocated objects, so add a _library_owned flag to each struct as an attempt to tell them apart. This isn't perfect though, because the user may neglect to zero the field, but they would still see a warning once in a while so it serves its purpose. To prevent the new deprecation warnings (intended for the user) from showing up during the library build itself, define a new family of *_create_internal() functions, and turn *_create() into simple wrappers. * Update samples * add eafer to c technical committee --------- Co-authored-by: William Cheng --- README.md | 2 +- .../resources/C-libcurl/model-body.mustache | 93 ++++++++++++++++++- .../resources/C-libcurl/model-header.mustache | 3 +- .../c-useJsonUnformatted/model/api_response.c | 20 +++- .../c-useJsonUnformatted/model/api_response.h | 3 +- .../c-useJsonUnformatted/model/category.c | 18 +++- .../c-useJsonUnformatted/model/category.h | 3 +- .../c-useJsonUnformatted/model/mapped_model.c | 18 +++- .../c-useJsonUnformatted/model/mapped_model.h | 3 +- .../model/model_with_set_propertes.c | 18 +++- .../model/model_with_set_propertes.h | 3 +- .../c-useJsonUnformatted/model/order.c | 26 +++++- .../c-useJsonUnformatted/model/order.h | 3 +- .../petstore/c-useJsonUnformatted/model/pet.c | 26 +++++- .../petstore/c-useJsonUnformatted/model/pet.h | 3 +- .../petstore/c-useJsonUnformatted/model/tag.c | 18 +++- .../petstore/c-useJsonUnformatted/model/tag.h | 3 +- .../c-useJsonUnformatted/model/user.c | 34 ++++++- .../c-useJsonUnformatted/model/user.h | 3 +- .../client/petstore/c/model/api_response.c | 20 +++- .../client/petstore/c/model/api_response.h | 3 +- samples/client/petstore/c/model/category.c | 18 +++- samples/client/petstore/c/model/category.h | 3 +- .../client/petstore/c/model/mapped_model.c | 18 +++- .../client/petstore/c/model/mapped_model.h | 3 +- .../c/model/model_with_set_propertes.c | 18 +++- .../c/model/model_with_set_propertes.h | 3 +- samples/client/petstore/c/model/order.c | 26 +++++- samples/client/petstore/c/model/order.h | 3 +- samples/client/petstore/c/model/pet.c | 26 +++++- samples/client/petstore/c/model/pet.h | 3 +- samples/client/petstore/c/model/tag.c | 18 +++- samples/client/petstore/c/model/tag.h | 3 +- samples/client/petstore/c/model/user.c | 34 ++++++- samples/client/petstore/c/model/user.h | 3 +- 35 files changed, 450 insertions(+), 52 deletions(-) diff --git a/README.md b/README.md index b31c58d649a9..1cf42af4539e 100644 --- a/README.md +++ b/README.md @@ -1210,7 +1210,7 @@ If you want to join the committee, please kindly apply by sending an email to te | Android | @jaz-ah (2017/09) | | Apex | | | Bash | @frol (2017/07) @bkryza (2017/08) @kenjones-cisco (2017/09) | -| C | @zhemant (2018/11) @ityuhui (2019/12) @michelealbano (2020/03) | +| C | @zhemant (2018/11) @ityuhui (2019/12) @michelealbano (2020/03) @eafer (2024/12) | | C++ | @ravinikam (2017/07) @stkrwork (2017/07) @etherealjoy (2018/02) @martindelille (2018/03) @muttleyxd (2019/08) | | C# | @mandrean (2017/08) @shibayan (2020/02) @Blackclaws (2021/03) @lucamazzanti (2021/05) @iBicha (2023/07) | | Clojure | | 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 a835a9b37080..0cfe0541be17 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 @@ -119,7 +119,7 @@ char* {{classname}}_{{name}}_ToString({{projectName}}_{{classVarName}}_{{enumNam {{/isContainer}} {{/vars}} -{{classname}}_t *{{classname}}_create( +static {{classname}}_t *{{classname}}_create_internal( {{#vars}} {{^isContainer}} {{^isPrimitiveType}} @@ -205,14 +205,103 @@ char* {{classname}}_{{name}}_ToString({{projectName}}_{{classVarName}}_{{enumNam {{classname}}_local_var->{{{name}}} = {{{name}}}; {{/vars}} + {{classname}}_local_var->_library_owned = 1; return {{classname}}_local_var; } +__attribute__((deprecated)) {{classname}}_t *{{classname}}_create( + {{#vars}} + {{^isContainer}} + {{^isPrimitiveType}} + {{#isModel}} + {{#isEnum}} + {{projectName}}_{{classVarName}}_{{enumName}}_e {{name}}{{^-last}},{{/-last}} + {{/isEnum}} + {{^isEnum}} + {{datatype}}_t *{{name}}{{^-last}},{{/-last}} + {{/isEnum}} + {{/isModel}} + {{^isModel}} + {{^isFreeFormObject}} + {{^isEnum}} + {{datatype}}_t *{{name}}{{^-last}},{{/-last}} + {{/isEnum}} + {{#isEnum}} + {{projectName}}_{{dataType}}_{{enumName}}_e {{name}}{{^-last}},{{/-last}} + {{/isEnum}} + {{/isFreeFormObject}} + {{/isModel}} + {{#isUuid}} + {{datatype}} *{{name}}{{^-last}},{{/-last}} + {{/isUuid}} + {{#isEmail}} + {{datatype}} *{{name}}{{^-last}},{{/-last}} + {{/isEmail}} + {{#isFreeFormObject}} + {{datatype}}_t *{{name}}{{^-last}},{{/-last}} + {{/isFreeFormObject}} + {{/isPrimitiveType}} + {{#isPrimitiveType}} + {{#isNumeric}} + {{datatype}} {{name}}{{^-last}},{{/-last}} + {{/isNumeric}} + {{#isBoolean}} + {{datatype}} {{name}}{{^-last}},{{/-last}} + {{/isBoolean}} + {{#isEnum}} + {{#isString}} + {{projectName}}_{{classVarName}}_{{enumName}}_e {{name}}{{^-last}},{{/-last}} + {{/isString}} + {{/isEnum}} + {{^isEnum}} + {{#isString}} + {{datatype}} *{{name}}{{^-last}},{{/-last}} + {{/isString}} + {{/isEnum}} + {{#isByteArray}} + {{datatype}} *{{name}}{{^-last}},{{/-last}} + {{/isByteArray}} + {{#isBinary}} + {{datatype}} {{name}}{{^-last}},{{/-last}} + {{/isBinary}} + {{#isDate}} + {{datatype}} *{{name}}{{^-last}},{{/-last}} + {{/isDate}} + {{#isDateTime}} + {{datatype}} *{{name}}{{^-last}},{{/-last}} + {{/isDateTime}} + {{/isPrimitiveType}} + {{/isContainer}} + {{#isContainer}} + {{#isArray}} + {{#isPrimitiveType}} + {{datatype}}_t *{{name}}{{^-last}},{{/-last}} + {{/isPrimitiveType}} + {{^isPrimitiveType}} + {{datatype}}_t *{{name}}{{^-last}},{{/-last}} + {{/isPrimitiveType}} + {{/isArray}} + {{#isMap}} + {{datatype}} {{name}}{{^-last}},{{/-last}} + {{/isMap}} + {{/isContainer}} + {{/vars}} + ) { + return {{classname}}_create_internal ( + {{#vars}} + {{name}}{{^-last}},{{/-last}} + {{/vars}} + ); +} void {{classname}}_free({{classname}}_t *{{classname}}) { if(NULL == {{classname}}){ return ; } + if({{classname}}->_library_owned != 1){ + fprintf(stderr, "WARNING: %s() does NOT free objects allocated by the user\n", "{{classname}}_free"); + return ; + } listEntry_t *listEntry; {{#vars}} {{^isContainer}} @@ -859,7 +948,7 @@ fail: {{/vars}} - {{classname}}_local_var = {{classname}}_create ( + {{classname}}_local_var = {{classname}}_create_internal ( {{#vars}} {{^isContainer}} {{^isPrimitiveType}} 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 d69dc8d5cd34..236d9e8c3cba 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 @@ -154,9 +154,10 @@ typedef struct {{classname}}_t { {{/isContainer}} {{/vars}} + int _library_owned; // Is the library responsible for freeing this object? } {{classname}}_t; -{{classname}}_t *{{classname}}_create( +__attribute__((deprecated)) {{classname}}_t *{{classname}}_create( {{#vars}} {{^isContainer}} {{^isPrimitiveType}} diff --git a/samples/client/petstore/c-useJsonUnformatted/model/api_response.c b/samples/client/petstore/c-useJsonUnformatted/model/api_response.c index ccae1f010102..d0169c84f5b2 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/api_response.c +++ b/samples/client/petstore/c-useJsonUnformatted/model/api_response.c @@ -5,7 +5,7 @@ -api_response_t *api_response_create( +static api_response_t *api_response_create_internal( int code, char *type, char *message @@ -18,14 +18,30 @@ api_response_t *api_response_create( 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, + char *type, + char *message + ) { + return api_response_create_internal ( + code, + type, + message + ); +} void api_response_free(api_response_t *api_response) { if(NULL == api_response){ return ; } + if(api_response->_library_owned != 1){ + fprintf(stderr, "WARNING: %s() does NOT free objects allocated by the user\n", "api_response_free"); + return ; + } listEntry_t *listEntry; if (api_response->type) { free(api_response->type); @@ -113,7 +129,7 @@ api_response_t *api_response_parseFromJSON(cJSON *api_responseJSON){ } - api_response_local_var = api_response_create ( + 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 diff --git a/samples/client/petstore/c-useJsonUnformatted/model/api_response.h b/samples/client/petstore/c-useJsonUnformatted/model/api_response.h index d64dcbacedd9..3d9eb71ff5d5 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/api_response.h +++ b/samples/client/petstore/c-useJsonUnformatted/model/api_response.h @@ -23,9 +23,10 @@ typedef struct api_response_t { char *type; // string char *message; // string + int _library_owned; // Is the library responsible for freeing this object? } api_response_t; -api_response_t *api_response_create( +__attribute__((deprecated)) api_response_t *api_response_create( 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 a1ea1a5d5cee..2b060a568015 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/category.c +++ b/samples/client/petstore/c-useJsonUnformatted/model/category.c @@ -5,7 +5,7 @@ -category_t *category_create( +static category_t *category_create_internal( long id, char *name ) { @@ -16,14 +16,28 @@ category_t *category_create( 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, + char *name + ) { + return category_create_internal ( + id, + name + ); +} void category_free(category_t *category) { if(NULL == category){ return ; } + if(category->_library_owned != 1){ + fprintf(stderr, "WARNING: %s() does NOT free objects allocated by the user\n", "category_free"); + return ; + } listEntry_t *listEntry; if (category->name) { free(category->name); @@ -87,7 +101,7 @@ category_t *category_parseFromJSON(cJSON *categoryJSON){ } - category_local_var = category_create ( + category_local_var = category_create_internal ( id ? id->valuedouble : 0, name && !cJSON_IsNull(name) ? strdup(name->valuestring) : NULL ); diff --git a/samples/client/petstore/c-useJsonUnformatted/model/category.h b/samples/client/petstore/c-useJsonUnformatted/model/category.h index ec9efd6ccf60..bd27e27e35a3 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/category.h +++ b/samples/client/petstore/c-useJsonUnformatted/model/category.h @@ -22,9 +22,10 @@ typedef struct category_t { long id; //numeric char *name; // string + int _library_owned; // Is the library responsible for freeing this object? } category_t; -category_t *category_create( +__attribute__((deprecated)) category_t *category_create( 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 7423e32eb338..3ab1e861c326 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/mapped_model.c +++ b/samples/client/petstore/c-useJsonUnformatted/model/mapped_model.c @@ -5,7 +5,7 @@ -MappedModel_t *MappedModel_create( +static MappedModel_t *MappedModel_create_internal( int another_property, char *uuid_property ) { @@ -16,14 +16,28 @@ MappedModel_t *MappedModel_create( 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, + char *uuid_property + ) { + return MappedModel_create_internal ( + another_property, + uuid_property + ); +} void MappedModel_free(MappedModel_t *MappedModel) { if(NULL == MappedModel){ return ; } + if(MappedModel->_library_owned != 1){ + fprintf(stderr, "WARNING: %s() does NOT free objects allocated by the user\n", "MappedModel_free"); + return ; + } listEntry_t *listEntry; if (MappedModel->uuid_property) { free(MappedModel->uuid_property); @@ -87,7 +101,7 @@ MappedModel_t *MappedModel_parseFromJSON(cJSON *MappedModelJSON){ } - MappedModel_local_var = MappedModel_create ( + MappedModel_local_var = MappedModel_create_internal ( another_property ? another_property->valuedouble : 0, uuid_property && !cJSON_IsNull(uuid_property) ? strdup(uuid_property->valuestring) : NULL ); diff --git a/samples/client/petstore/c-useJsonUnformatted/model/mapped_model.h b/samples/client/petstore/c-useJsonUnformatted/model/mapped_model.h index 83b1d4581ba6..b962632d647d 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/mapped_model.h +++ b/samples/client/petstore/c-useJsonUnformatted/model/mapped_model.h @@ -22,9 +22,10 @@ typedef struct MappedModel_t { int another_property; //numeric char *uuid_property; // string + int _library_owned; // Is the library responsible for freeing this object? } MappedModel_t; -MappedModel_t *MappedModel_create( +__attribute__((deprecated)) MappedModel_t *MappedModel_create( 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 d29660fccfcd..0791d68f48a5 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 @@ -5,7 +5,7 @@ -model_with_set_propertes_t *model_with_set_propertes_create( +static model_with_set_propertes_t *model_with_set_propertes_create_internal( list_t *tag_set, list_t *string_set ) { @@ -16,14 +16,28 @@ model_with_set_propertes_t *model_with_set_propertes_create( 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; } +__attribute__((deprecated)) model_with_set_propertes_t *model_with_set_propertes_create( + list_t *tag_set, + list_t *string_set + ) { + return model_with_set_propertes_create_internal ( + tag_set, + string_set + ); +} void model_with_set_propertes_free(model_with_set_propertes_t *model_with_set_propertes) { if(NULL == model_with_set_propertes){ return ; } + if(model_with_set_propertes->_library_owned != 1){ + fprintf(stderr, "WARNING: %s() does NOT free objects allocated by the user\n", "model_with_set_propertes_free"); + return ; + } listEntry_t *listEntry; if (model_with_set_propertes->tag_set) { list_ForEach(listEntry, model_with_set_propertes->tag_set) { @@ -146,7 +160,7 @@ model_with_set_propertes_t *model_with_set_propertes_parseFromJSON(cJSON *model_ } - model_with_set_propertes_local_var = model_with_set_propertes_create ( + model_with_set_propertes_local_var = model_with_set_propertes_create_internal ( tag_set ? tag_setList : NULL, string_set ? string_setList : NULL ); diff --git a/samples/client/petstore/c-useJsonUnformatted/model/model_with_set_propertes.h b/samples/client/petstore/c-useJsonUnformatted/model/model_with_set_propertes.h index 537271638b99..aa82893b94bb 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/model_with_set_propertes.h +++ b/samples/client/petstore/c-useJsonUnformatted/model/model_with_set_propertes.h @@ -23,9 +23,10 @@ typedef struct model_with_set_propertes_t { list_t *tag_set; //nonprimitive container list_t *string_set; //primitive container + int _library_owned; // Is the library responsible for freeing this object? } model_with_set_propertes_t; -model_with_set_propertes_t *model_with_set_propertes_create( +__attribute__((deprecated)) model_with_set_propertes_t *model_with_set_propertes_create( list_t *tag_set, list_t *string_set ); diff --git a/samples/client/petstore/c-useJsonUnformatted/model/order.c b/samples/client/petstore/c-useJsonUnformatted/model/order.c index 055dd08a3896..d67d1d47c714 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/order.c +++ b/samples/client/petstore/c-useJsonUnformatted/model/order.c @@ -22,7 +22,7 @@ openapi_petstore_order_STATUS_e order_status_FromString(char* status){ return 0; } -order_t *order_create( +static order_t *order_create_internal( long id, long pet_id, int quantity, @@ -41,14 +41,36 @@ order_t *order_create( 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, + char *ship_date, + openapi_petstore_order_STATUS_e status, + int complete + ) { + return order_create_internal ( + id, + pet_id, + quantity, + ship_date, + status, + complete + ); +} void order_free(order_t *order) { if(NULL == order){ return ; } + if(order->_library_owned != 1){ + fprintf(stderr, "WARNING: %s() does NOT free objects allocated by the user\n", "order_free"); + return ; + } listEntry_t *listEntry; if (order->ship_date) { free(order->ship_date); @@ -195,7 +217,7 @@ order_t *order_parseFromJSON(cJSON *orderJSON){ } - order_local_var = order_create ( + order_local_var = order_create_internal ( id ? id->valuedouble : 0, pet_id ? pet_id->valuedouble : 0, quantity ? quantity->valuedouble : 0, diff --git a/samples/client/petstore/c-useJsonUnformatted/model/order.h b/samples/client/petstore/c-useJsonUnformatted/model/order.h index 32914a227499..1b0a47b3028e 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/order.h +++ b/samples/client/petstore/c-useJsonUnformatted/model/order.h @@ -34,9 +34,10 @@ typedef struct order_t { openapi_petstore_order_STATUS_e status; //enum int complete; //boolean + int _library_owned; // Is the library responsible for freeing this object? } order_t; -order_t *order_create( +__attribute__((deprecated)) order_t *order_create( long id, long pet_id, int quantity, diff --git a/samples/client/petstore/c-useJsonUnformatted/model/pet.c b/samples/client/petstore/c-useJsonUnformatted/model/pet.c index 2472e769095d..1393cc4275a6 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/pet.c +++ b/samples/client/petstore/c-useJsonUnformatted/model/pet.c @@ -22,7 +22,7 @@ openapi_petstore_pet_STATUS_e pet_status_FromString(char* status){ return 0; } -pet_t *pet_create( +static pet_t *pet_create_internal( long id, category_t *category, char *name, @@ -41,14 +41,36 @@ pet_t *pet_create( 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, + category_t *category, + char *name, + list_t *photo_urls, + list_t *tags, + openapi_petstore_pet_STATUS_e status + ) { + return pet_create_internal ( + id, + category, + name, + photo_urls, + tags, + status + ); +} void pet_free(pet_t *pet) { if(NULL == pet){ return ; } + if(pet->_library_owned != 1){ + fprintf(stderr, "WARNING: %s() does NOT free objects allocated by the user\n", "pet_free"); + return ; + } listEntry_t *listEntry; if (pet->category) { category_free(pet->category); @@ -275,7 +297,7 @@ pet_t *pet_parseFromJSON(cJSON *petJSON){ } - pet_local_var = pet_create ( + pet_local_var = pet_create_internal ( id ? id->valuedouble : 0, category ? category_local_nonprim : NULL, strdup(name->valuestring), diff --git a/samples/client/petstore/c-useJsonUnformatted/model/pet.h b/samples/client/petstore/c-useJsonUnformatted/model/pet.h index d74025510143..860197e63a53 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/pet.h +++ b/samples/client/petstore/c-useJsonUnformatted/model/pet.h @@ -36,9 +36,10 @@ typedef struct pet_t { list_t *tags; //nonprimitive container openapi_petstore_pet_STATUS_e status; //enum + int _library_owned; // Is the library responsible for freeing this object? } pet_t; -pet_t *pet_create( +__attribute__((deprecated)) pet_t *pet_create( long id, category_t *category, char *name, diff --git a/samples/client/petstore/c-useJsonUnformatted/model/tag.c b/samples/client/petstore/c-useJsonUnformatted/model/tag.c index f79d34ef9acd..e4b4f94af53d 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/tag.c +++ b/samples/client/petstore/c-useJsonUnformatted/model/tag.c @@ -5,7 +5,7 @@ -tag_t *tag_create( +static tag_t *tag_create_internal( long id, char *name ) { @@ -16,14 +16,28 @@ tag_t *tag_create( 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, + char *name + ) { + return tag_create_internal ( + id, + name + ); +} void tag_free(tag_t *tag) { if(NULL == tag){ return ; } + if(tag->_library_owned != 1){ + fprintf(stderr, "WARNING: %s() does NOT free objects allocated by the user\n", "tag_free"); + return ; + } listEntry_t *listEntry; if (tag->name) { free(tag->name); @@ -87,7 +101,7 @@ tag_t *tag_parseFromJSON(cJSON *tagJSON){ } - tag_local_var = tag_create ( + tag_local_var = tag_create_internal ( id ? id->valuedouble : 0, name && !cJSON_IsNull(name) ? strdup(name->valuestring) : NULL ); diff --git a/samples/client/petstore/c-useJsonUnformatted/model/tag.h b/samples/client/petstore/c-useJsonUnformatted/model/tag.h index 9e7b5d053a9d..d4b29e4d2e04 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/tag.h +++ b/samples/client/petstore/c-useJsonUnformatted/model/tag.h @@ -22,9 +22,10 @@ typedef struct tag_t { long id; //numeric char *name; // string + int _library_owned; // Is the library responsible for freeing this object? } tag_t; -tag_t *tag_create( +__attribute__((deprecated)) tag_t *tag_create( 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 111bafdc02ad..ce31e6ad17df 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/user.c +++ b/samples/client/petstore/c-useJsonUnformatted/model/user.c @@ -5,7 +5,7 @@ -user_t *user_create( +static user_t *user_create_internal( long id, char *username, char *first_name, @@ -32,14 +32,44 @@ user_t *user_create( 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, + char *username, + char *first_name, + char *last_name, + char *email, + char *password, + char *phone, + int user_status, + list_t* extra, + openapi_petstore_preference__e preference + ) { + return user_create_internal ( + id, + username, + first_name, + last_name, + email, + password, + phone, + user_status, + extra, + preference + ); +} void user_free(user_t *user) { if(NULL == user){ return ; } + if(user->_library_owned != 1){ + fprintf(stderr, "WARNING: %s() does NOT free objects allocated by the user\n", "user_free"); + return ; + } listEntry_t *listEntry; if (user->username) { free(user->username); @@ -320,7 +350,7 @@ user_t *user_parseFromJSON(cJSON *userJSON){ } - user_local_var = user_create ( + 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, diff --git a/samples/client/petstore/c-useJsonUnformatted/model/user.h b/samples/client/petstore/c-useJsonUnformatted/model/user.h index badbc747b308..4643e020a5fe 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/user.h +++ b/samples/client/petstore/c-useJsonUnformatted/model/user.h @@ -32,9 +32,10 @@ typedef struct user_t { list_t* extra; //map openapi_petstore_preference__e preference; //referenced enum + int _library_owned; // Is the library responsible for freeing this object? } user_t; -user_t *user_create( +__attribute__((deprecated)) user_t *user_create( long id, char *username, char *first_name, diff --git a/samples/client/petstore/c/model/api_response.c b/samples/client/petstore/c/model/api_response.c index ccae1f010102..d0169c84f5b2 100644 --- a/samples/client/petstore/c/model/api_response.c +++ b/samples/client/petstore/c/model/api_response.c @@ -5,7 +5,7 @@ -api_response_t *api_response_create( +static api_response_t *api_response_create_internal( int code, char *type, char *message @@ -18,14 +18,30 @@ api_response_t *api_response_create( 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, + char *type, + char *message + ) { + return api_response_create_internal ( + code, + type, + message + ); +} void api_response_free(api_response_t *api_response) { if(NULL == api_response){ return ; } + if(api_response->_library_owned != 1){ + fprintf(stderr, "WARNING: %s() does NOT free objects allocated by the user\n", "api_response_free"); + return ; + } listEntry_t *listEntry; if (api_response->type) { free(api_response->type); @@ -113,7 +129,7 @@ api_response_t *api_response_parseFromJSON(cJSON *api_responseJSON){ } - api_response_local_var = api_response_create ( + 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 diff --git a/samples/client/petstore/c/model/api_response.h b/samples/client/petstore/c/model/api_response.h index d64dcbacedd9..3d9eb71ff5d5 100644 --- a/samples/client/petstore/c/model/api_response.h +++ b/samples/client/petstore/c/model/api_response.h @@ -23,9 +23,10 @@ typedef struct api_response_t { char *type; // string char *message; // string + int _library_owned; // Is the library responsible for freeing this object? } api_response_t; -api_response_t *api_response_create( +__attribute__((deprecated)) api_response_t *api_response_create( 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 a1ea1a5d5cee..2b060a568015 100644 --- a/samples/client/petstore/c/model/category.c +++ b/samples/client/petstore/c/model/category.c @@ -5,7 +5,7 @@ -category_t *category_create( +static category_t *category_create_internal( long id, char *name ) { @@ -16,14 +16,28 @@ category_t *category_create( 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, + char *name + ) { + return category_create_internal ( + id, + name + ); +} void category_free(category_t *category) { if(NULL == category){ return ; } + if(category->_library_owned != 1){ + fprintf(stderr, "WARNING: %s() does NOT free objects allocated by the user\n", "category_free"); + return ; + } listEntry_t *listEntry; if (category->name) { free(category->name); @@ -87,7 +101,7 @@ category_t *category_parseFromJSON(cJSON *categoryJSON){ } - category_local_var = category_create ( + category_local_var = category_create_internal ( id ? id->valuedouble : 0, name && !cJSON_IsNull(name) ? strdup(name->valuestring) : NULL ); diff --git a/samples/client/petstore/c/model/category.h b/samples/client/petstore/c/model/category.h index ec9efd6ccf60..bd27e27e35a3 100644 --- a/samples/client/petstore/c/model/category.h +++ b/samples/client/petstore/c/model/category.h @@ -22,9 +22,10 @@ typedef struct category_t { long id; //numeric char *name; // string + int _library_owned; // Is the library responsible for freeing this object? } category_t; -category_t *category_create( +__attribute__((deprecated)) category_t *category_create( 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 7423e32eb338..3ab1e861c326 100644 --- a/samples/client/petstore/c/model/mapped_model.c +++ b/samples/client/petstore/c/model/mapped_model.c @@ -5,7 +5,7 @@ -MappedModel_t *MappedModel_create( +static MappedModel_t *MappedModel_create_internal( int another_property, char *uuid_property ) { @@ -16,14 +16,28 @@ MappedModel_t *MappedModel_create( 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, + char *uuid_property + ) { + return MappedModel_create_internal ( + another_property, + uuid_property + ); +} void MappedModel_free(MappedModel_t *MappedModel) { if(NULL == MappedModel){ return ; } + if(MappedModel->_library_owned != 1){ + fprintf(stderr, "WARNING: %s() does NOT free objects allocated by the user\n", "MappedModel_free"); + return ; + } listEntry_t *listEntry; if (MappedModel->uuid_property) { free(MappedModel->uuid_property); @@ -87,7 +101,7 @@ MappedModel_t *MappedModel_parseFromJSON(cJSON *MappedModelJSON){ } - MappedModel_local_var = MappedModel_create ( + MappedModel_local_var = MappedModel_create_internal ( another_property ? another_property->valuedouble : 0, uuid_property && !cJSON_IsNull(uuid_property) ? strdup(uuid_property->valuestring) : NULL ); diff --git a/samples/client/petstore/c/model/mapped_model.h b/samples/client/petstore/c/model/mapped_model.h index 83b1d4581ba6..b962632d647d 100644 --- a/samples/client/petstore/c/model/mapped_model.h +++ b/samples/client/petstore/c/model/mapped_model.h @@ -22,9 +22,10 @@ typedef struct MappedModel_t { int another_property; //numeric char *uuid_property; // string + int _library_owned; // Is the library responsible for freeing this object? } MappedModel_t; -MappedModel_t *MappedModel_create( +__attribute__((deprecated)) MappedModel_t *MappedModel_create( 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 d29660fccfcd..0791d68f48a5 100644 --- a/samples/client/petstore/c/model/model_with_set_propertes.c +++ b/samples/client/petstore/c/model/model_with_set_propertes.c @@ -5,7 +5,7 @@ -model_with_set_propertes_t *model_with_set_propertes_create( +static model_with_set_propertes_t *model_with_set_propertes_create_internal( list_t *tag_set, list_t *string_set ) { @@ -16,14 +16,28 @@ model_with_set_propertes_t *model_with_set_propertes_create( 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; } +__attribute__((deprecated)) model_with_set_propertes_t *model_with_set_propertes_create( + list_t *tag_set, + list_t *string_set + ) { + return model_with_set_propertes_create_internal ( + tag_set, + string_set + ); +} void model_with_set_propertes_free(model_with_set_propertes_t *model_with_set_propertes) { if(NULL == model_with_set_propertes){ return ; } + if(model_with_set_propertes->_library_owned != 1){ + fprintf(stderr, "WARNING: %s() does NOT free objects allocated by the user\n", "model_with_set_propertes_free"); + return ; + } listEntry_t *listEntry; if (model_with_set_propertes->tag_set) { list_ForEach(listEntry, model_with_set_propertes->tag_set) { @@ -146,7 +160,7 @@ model_with_set_propertes_t *model_with_set_propertes_parseFromJSON(cJSON *model_ } - model_with_set_propertes_local_var = model_with_set_propertes_create ( + model_with_set_propertes_local_var = model_with_set_propertes_create_internal ( tag_set ? tag_setList : NULL, string_set ? string_setList : NULL ); diff --git a/samples/client/petstore/c/model/model_with_set_propertes.h b/samples/client/petstore/c/model/model_with_set_propertes.h index 537271638b99..aa82893b94bb 100644 --- a/samples/client/petstore/c/model/model_with_set_propertes.h +++ b/samples/client/petstore/c/model/model_with_set_propertes.h @@ -23,9 +23,10 @@ typedef struct model_with_set_propertes_t { list_t *tag_set; //nonprimitive container list_t *string_set; //primitive container + int _library_owned; // Is the library responsible for freeing this object? } model_with_set_propertes_t; -model_with_set_propertes_t *model_with_set_propertes_create( +__attribute__((deprecated)) model_with_set_propertes_t *model_with_set_propertes_create( list_t *tag_set, list_t *string_set ); diff --git a/samples/client/petstore/c/model/order.c b/samples/client/petstore/c/model/order.c index 055dd08a3896..d67d1d47c714 100644 --- a/samples/client/petstore/c/model/order.c +++ b/samples/client/petstore/c/model/order.c @@ -22,7 +22,7 @@ openapi_petstore_order_STATUS_e order_status_FromString(char* status){ return 0; } -order_t *order_create( +static order_t *order_create_internal( long id, long pet_id, int quantity, @@ -41,14 +41,36 @@ order_t *order_create( 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, + char *ship_date, + openapi_petstore_order_STATUS_e status, + int complete + ) { + return order_create_internal ( + id, + pet_id, + quantity, + ship_date, + status, + complete + ); +} void order_free(order_t *order) { if(NULL == order){ return ; } + if(order->_library_owned != 1){ + fprintf(stderr, "WARNING: %s() does NOT free objects allocated by the user\n", "order_free"); + return ; + } listEntry_t *listEntry; if (order->ship_date) { free(order->ship_date); @@ -195,7 +217,7 @@ order_t *order_parseFromJSON(cJSON *orderJSON){ } - order_local_var = order_create ( + order_local_var = order_create_internal ( id ? id->valuedouble : 0, pet_id ? pet_id->valuedouble : 0, quantity ? quantity->valuedouble : 0, diff --git a/samples/client/petstore/c/model/order.h b/samples/client/petstore/c/model/order.h index 32914a227499..1b0a47b3028e 100644 --- a/samples/client/petstore/c/model/order.h +++ b/samples/client/petstore/c/model/order.h @@ -34,9 +34,10 @@ typedef struct order_t { openapi_petstore_order_STATUS_e status; //enum int complete; //boolean + int _library_owned; // Is the library responsible for freeing this object? } order_t; -order_t *order_create( +__attribute__((deprecated)) order_t *order_create( long id, long pet_id, int quantity, diff --git a/samples/client/petstore/c/model/pet.c b/samples/client/petstore/c/model/pet.c index 2472e769095d..1393cc4275a6 100644 --- a/samples/client/petstore/c/model/pet.c +++ b/samples/client/petstore/c/model/pet.c @@ -22,7 +22,7 @@ openapi_petstore_pet_STATUS_e pet_status_FromString(char* status){ return 0; } -pet_t *pet_create( +static pet_t *pet_create_internal( long id, category_t *category, char *name, @@ -41,14 +41,36 @@ pet_t *pet_create( 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, + category_t *category, + char *name, + list_t *photo_urls, + list_t *tags, + openapi_petstore_pet_STATUS_e status + ) { + return pet_create_internal ( + id, + category, + name, + photo_urls, + tags, + status + ); +} void pet_free(pet_t *pet) { if(NULL == pet){ return ; } + if(pet->_library_owned != 1){ + fprintf(stderr, "WARNING: %s() does NOT free objects allocated by the user\n", "pet_free"); + return ; + } listEntry_t *listEntry; if (pet->category) { category_free(pet->category); @@ -275,7 +297,7 @@ pet_t *pet_parseFromJSON(cJSON *petJSON){ } - pet_local_var = pet_create ( + pet_local_var = pet_create_internal ( id ? id->valuedouble : 0, category ? category_local_nonprim : NULL, strdup(name->valuestring), diff --git a/samples/client/petstore/c/model/pet.h b/samples/client/petstore/c/model/pet.h index d74025510143..860197e63a53 100644 --- a/samples/client/petstore/c/model/pet.h +++ b/samples/client/petstore/c/model/pet.h @@ -36,9 +36,10 @@ typedef struct pet_t { list_t *tags; //nonprimitive container openapi_petstore_pet_STATUS_e status; //enum + int _library_owned; // Is the library responsible for freeing this object? } pet_t; -pet_t *pet_create( +__attribute__((deprecated)) pet_t *pet_create( long id, category_t *category, char *name, diff --git a/samples/client/petstore/c/model/tag.c b/samples/client/petstore/c/model/tag.c index f79d34ef9acd..e4b4f94af53d 100644 --- a/samples/client/petstore/c/model/tag.c +++ b/samples/client/petstore/c/model/tag.c @@ -5,7 +5,7 @@ -tag_t *tag_create( +static tag_t *tag_create_internal( long id, char *name ) { @@ -16,14 +16,28 @@ tag_t *tag_create( 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, + char *name + ) { + return tag_create_internal ( + id, + name + ); +} void tag_free(tag_t *tag) { if(NULL == tag){ return ; } + if(tag->_library_owned != 1){ + fprintf(stderr, "WARNING: %s() does NOT free objects allocated by the user\n", "tag_free"); + return ; + } listEntry_t *listEntry; if (tag->name) { free(tag->name); @@ -87,7 +101,7 @@ tag_t *tag_parseFromJSON(cJSON *tagJSON){ } - tag_local_var = tag_create ( + tag_local_var = tag_create_internal ( id ? id->valuedouble : 0, name && !cJSON_IsNull(name) ? strdup(name->valuestring) : NULL ); diff --git a/samples/client/petstore/c/model/tag.h b/samples/client/petstore/c/model/tag.h index 9e7b5d053a9d..d4b29e4d2e04 100644 --- a/samples/client/petstore/c/model/tag.h +++ b/samples/client/petstore/c/model/tag.h @@ -22,9 +22,10 @@ typedef struct tag_t { long id; //numeric char *name; // string + int _library_owned; // Is the library responsible for freeing this object? } tag_t; -tag_t *tag_create( +__attribute__((deprecated)) tag_t *tag_create( long id, char *name ); diff --git a/samples/client/petstore/c/model/user.c b/samples/client/petstore/c/model/user.c index 111bafdc02ad..ce31e6ad17df 100644 --- a/samples/client/petstore/c/model/user.c +++ b/samples/client/petstore/c/model/user.c @@ -5,7 +5,7 @@ -user_t *user_create( +static user_t *user_create_internal( long id, char *username, char *first_name, @@ -32,14 +32,44 @@ user_t *user_create( 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, + char *username, + char *first_name, + char *last_name, + char *email, + char *password, + char *phone, + int user_status, + list_t* extra, + openapi_petstore_preference__e preference + ) { + return user_create_internal ( + id, + username, + first_name, + last_name, + email, + password, + phone, + user_status, + extra, + preference + ); +} void user_free(user_t *user) { if(NULL == user){ return ; } + if(user->_library_owned != 1){ + fprintf(stderr, "WARNING: %s() does NOT free objects allocated by the user\n", "user_free"); + return ; + } listEntry_t *listEntry; if (user->username) { free(user->username); @@ -320,7 +350,7 @@ user_t *user_parseFromJSON(cJSON *userJSON){ } - user_local_var = user_create ( + 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, diff --git a/samples/client/petstore/c/model/user.h b/samples/client/petstore/c/model/user.h index badbc747b308..4643e020a5fe 100644 --- a/samples/client/petstore/c/model/user.h +++ b/samples/client/petstore/c/model/user.h @@ -32,9 +32,10 @@ typedef struct user_t { list_t* extra; //map openapi_petstore_preference__e preference; //referenced enum + int _library_owned; // Is the library responsible for freeing this object? } user_t; -user_t *user_create( +__attribute__((deprecated)) user_t *user_create( long id, char *username, char *first_name,