From e568f73b3934109594a5f7e105efd4794516ef6f Mon Sep 17 00:00:00 2001 From: Michele Albano Date: Fri, 28 Feb 2020 06:08:34 -0800 Subject: [PATCH 1/5] PR to solve 2 open issues on enums: Issue 5091 needs to generate enums also when the enum is directly in a param to a API call, instead than in a model. I did that by copying and adapting enum code from *model* to *api* Issue 4293 needs to decorate enums, for when enum names or enum values are repeated over the yaml definition. --- .../resources/C-libcurl/api-body.mustache | 94 ++++++++++++++++++- .../resources/C-libcurl/api-header.mustache | 26 +++++ .../resources/C-libcurl/model-body.mustache | 66 +++++++------ .../resources/C-libcurl/model-header.mustache | 30 +++--- samples/client/petstore/c/api/PetAPI.c | 78 +++++++++++++++ samples/client/petstore/c/api/PetAPI.h | 40 ++++++++ samples/client/petstore/c/api/StoreAPI.c | 21 +++++ samples/client/petstore/c/api/StoreAPI.h | 8 ++ samples/client/petstore/c/api/UserAPI.c | 33 +++++++ samples/client/petstore/c/api/UserAPI.h | 20 ++++ samples/client/petstore/c/model/order.c | 8 +- samples/client/petstore/c/model/order.h | 10 +- samples/client/petstore/c/model/pet.c | 8 +- samples/client/petstore/c/model/pet.h | 10 +- 14 files changed, 384 insertions(+), 68 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/C-libcurl/api-body.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/api-body.mustache index 6180ad435d0f..9f496728a42e 100644 --- a/modules/openapi-generator/src/main/resources/C-libcurl/api-body.mustache +++ b/modules/openapi-generator/src/main/resources/C-libcurl/api-body.mustache @@ -11,6 +11,95 @@ snprintf(dst, 256, "%ld", (long int)(src));\ }while(0) +{{#operations}} +{{#operation}} +{{#allParams}} + +{{#isEnum}} +// Functions for enum {{enumName}} for {{{classname}}}_{{{operationId}}} + + +char* {{enumName}}_ToString({{baseName}}_e {{enumName}}){ +char *{{enumName}}Array[] = { {{#allowableValues}}{{#enumVars}}"{{{value}}}"{{^-last}},{{/-last}}{{/enumVars}}{{/allowableValues}} }; + return {{enumName}}Array[{{enumName}}]; +} + +{{baseName}}_e {{enumName}}_FromString(char* {{enumName}}){ + int stringToReturn = 0; + char *{{enumName}}Array[] = { {{#allowableValues}}{{#enumVars}}"{{{value}}}"{{^-last}},{{/-last}}{{/enumVars}}{{/allowableValues}} }; + size_t sizeofArray = sizeof({{enumName}}Array) / sizeof({{enumName}}Array[0]); + while(stringToReturn < sizeofArray) { + if(strcmp({{enumName}}, {{enumName}}Array[stringToReturn]) == 0) { + return stringToReturn; + } + stringToReturn++; + } + return 0; +} + +cJSON *{{enumName}}_convertToJSON({{baseName}}_e {{enumName}}) { +cJSON *item = cJSON_CreateObject(); +{{#isString}} + if(cJSON_AddStringToObject(item, "{{{paramName}}}", {{{enumName}}}_ToString({{{enumName}}})) == NULL) { + goto fail; + } +{{/isString}} +{{#isNumeric}} + if(cJSON_AddNumberToObject(item, "{{{paramName}}}", {{{enumName}}}) == NULL) { + goto fail; + } +{{/isNumeric}} + return item; + fail: + cJSON_Delete(item); + return NULL; +} + +{{baseName}}_e {{enumName}}_parseFromJSON(cJSON *{{enumName}}JSON){ + +{{baseName}}_e *{{enumName}} = NULL; + +{{#isEnum}} +{{#isNumeric}} +cJSON *{{{enumName}}}Var = cJSON_GetObjectItemCaseSensitive({{enumName}}JSON, "{{{paramName}}}"); +if(!cJSON_IsNumber({{{enumName}}}Var)) +{ + goto end; +} +{{/isNumeric}} +{{#isString}} +{{{baseName}}}_e {{enumName}}Variable; +cJSON *{{{enumName}}}Var = cJSON_GetObjectItemCaseSensitive({{enumName}}JSON, "{{{paramName}}}"); +if(!cJSON_IsString({{{enumName}}}Var) || ({{{enumName}}}Var->valuestring == NULL)){ + goto end; +} +{{enumName}}Variable = {{enumName}}_FromString({{{enumName}}}Var->valuestring); +{{/isString}} +{{/isEnum}} +return {{enumName}}Variable; +end: +return 0; +} +{{/isEnum}} + +{{/allParams}} +{{/operation}} +{{/operations}} + + + + + + + + + + + + + + + {{#operations}} {{#operation}} {{#summary}} @@ -143,7 +232,8 @@ {{^isListContainer}} keyQuery_{{{paramName}}} = strdup("{{{baseName}}}"); valueQuery_{{{paramName}}} = {{#isString}}{{^isEnum}}strdup({{/isEnum}}{{/isString}}({{{paramName}}}){{#isString}}{{^isEnum}}){{/isEnum}}{{/isString}}; - keyPairQuery_{{paramName}} = keyValuePair_create(keyQuery_{{{paramName}}}, {{#isEnum}}(void *){{/isEnum}}{{^isString}}&{{/isString}}valueQuery_{{{paramName}}}); + keyPairQuery_{{paramName}} = keyValuePair_create(keyQuery_{{{paramName}}}, {{#isEnum}}(void *)strdup({{enumName}}_ToString( + {{/isEnum}}{{^isString}}&{{/isString}}valueQuery_{{{paramName}}}{{#isEnum}})){{/isEnum}}); list_addElement(localVarQueryParameters,keyPairQuery_{{paramName}}); {{/isListContainer}} } @@ -334,10 +424,12 @@ free(keyQuery_{{{paramName}}}); keyQuery_{{{paramName}}} = NULL; } +{{^isEnum}} if(valueQuery_{{{paramName}}}){ free(valueQuery_{{{paramName}}}); valueQuery_{{{paramName}}} = NULL; } +{{/isEnum}} if(keyPairQuery_{{{paramName}}}){ keyValuePair_free(keyPairQuery_{{{paramName}}}); keyPairQuery_{{{paramName}}} = NULL; diff --git a/modules/openapi-generator/src/main/resources/C-libcurl/api-header.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/api-header.mustache index 4556d2d02940..c49a807fe0e4 100644 --- a/modules/openapi-generator/src/main/resources/C-libcurl/api-header.mustache +++ b/modules/openapi-generator/src/main/resources/C-libcurl/api-header.mustache @@ -8,6 +8,32 @@ {{/imports}} +{{#operations}} +{{#operation}} +{{#allParams}} + +{{#isEnum}} +// Enum {{enumName}} for {{{classname}}}_{{{operationId}}} + +{{#allowableValues}} +typedef enum { {{#enumVars}} {{enumName}}_{{{value}}}{{#first}} = 0{{/first}}{{^-last}},{{/-last}}{{/enumVars}} } {{baseName}}_e; +{{/allowableValues}} + +char* {{enumName}}_ToString({{baseName}}_e {{enumName}}); + +{{baseName}}_e {{enumName}}_FromString(char* {{enumName}}); + +cJSON *{{enumName}}_convertToJSON({{baseName}}_e {{enumName}}); + +{{baseName}}_e {{enumName}}_parseFromJSON(cJSON *{{enumName}}JSON); + +{{/isEnum}} + +{{/allParams}} +{{/operation}} +{{/operations}} + + {{#operations}} {{#operation}} {{#summary}} 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 c2cd285c9663..a48fecb80808 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 @@ -7,12 +7,12 @@ {{#isEnum}} -char* {{classname}}_ToString({{classname}}_e {{classname}}){ -char *{{classname}}Array[] = { {{#allowableValues}}{{#enumVars}}"{{{value}}}"{{^-last}},{{/-last}}{{/enumVars}}{{/allowableValues}} }; +char* {{classFilename}}_{{classname}}_ToString({{classFilename}}_{{classname}}_e {{classname}}) { + char *{{classname}}Array[] = { {{#allowableValues}}{{#enumVars}}"{{{value}}}"{{^-last}},{{/-last}}{{/enumVars}}{{/allowableValues}} }; return {{classname}}Array[{{classname}}]; } -{{classname}}_e {{classname}}_FromString(char* {{classname}}){ +{{classFilename}}_{{classname}}_e {{classFilename}}_{{classname}}_FromString(char* {{classname}}) { int stringToReturn = 0; char *{{classname}}Array[] = { {{#allowableValues}}{{#enumVars}}"{{{value}}}"{{^-last}},{{/-last}}{{/enumVars}}{{/allowableValues}} }; size_t sizeofArray = sizeof({{classname}}Array) / sizeof({{classname}}Array[0]); @@ -25,10 +25,10 @@ char *{{classname}}Array[] = { {{#allowableValues}}{{#enumVars}}"{{{value}}}"{{ return 0; } -cJSON *{{classname}}_convertToJSON({{classname}}_e {{classname}}) { -cJSON *item = cJSON_CreateObject(); +cJSON *{{classFilename}}_{{classname}}_convertToJSON({{classFilename}}_{{classname}}_e {{classname}}) { + cJSON *item = cJSON_CreateObject(); {{#isString}} - if(cJSON_AddStringToObject(item, "{{{classname}}}", {{{classname}}}_ToString({{{classname}}})) == NULL) { + if(cJSON_AddStringToObject(item, "{{{classname}}}", {{classFilename}}_{{{classname}}}_ToString({{{classname}}})) == NULL) { goto fail; } {{/isString}} @@ -38,35 +38,33 @@ cJSON *item = cJSON_CreateObject(); } {{/isNumeric}} return item; - fail: +fail: cJSON_Delete(item); return NULL; } -{{classname}}_e {{classname}}_parseFromJSON(cJSON *{{classname}}JSON){ - -{{classname}}_e *{{classname}} = NULL; - +{{classFilename}}_{{classname}}_e {{classFilename}}_{{classname}}_parseFromJSON(cJSON *{{classname}}JSON) { + {{classFilename}}_{{classname}}_e *{{classname}} = NULL; {{#isEnum}} {{#isNumeric}} -cJSON *{{{classname}}}Var = cJSON_GetObjectItemCaseSensitive({{classname}}JSON, "{{{classname}}}"); -if(!cJSON_IsNumber({{{classname}}}Var)) -{ - goto end; -} + cJSON *{{{classname}}}Var = cJSON_GetObjectItemCaseSensitive({{classname}}JSON, "{{{classname}}}"); + if(!cJSON_IsNumber({{{classname}}}Var)) + { + goto end; + } {{/isNumeric}} {{#isString}} -{{{classname}}}_e {{classname}}Variable; -cJSON *{{{classname}}}Var = cJSON_GetObjectItemCaseSensitive({{classname}}JSON, "{{{classname}}}"); -if(!cJSON_IsString({{{classname}}}Var) || ({{{classname}}}Var->valuestring == NULL)){ - goto end; -} -{{classname}}Variable = {{classname}}_FromString({{{classname}}}Var->valuestring); + {{classFilename}}_{{{classname}}}_e {{classname}}Variable; + cJSON *{{{classname}}}Var = cJSON_GetObjectItemCaseSensitive({{classname}}JSON, "{{{classname}}}"); + if(!cJSON_IsString({{{classname}}}Var) || ({{{classname}}}Var->valuestring == NULL)){ + goto end; + } + {{classname}}Variable = {{classFilename}}_{{classname}}_FromString({{{classname}}}Var->valuestring); {{/isString}} {{/isEnum}} -return {{classname}}Variable; + return {{classname}}Variable; end: -return 0; + return 0; } {{/isEnum}} {{^isEnum}} @@ -74,12 +72,12 @@ return 0; {{^isContainer}} {{^isModel}} {{#isEnum}} - char* {{name}}{{classname}}_ToString({{name}}_e {{name}}){ + char* {{name}}{{classname}}_ToString({{classFilename}}_{{name}}_e {{name}}){ char *{{name}}Array[] = { {{#allowableValues}}{{#enumVars}}"{{{value}}}"{{^-last}},{{/-last}}{{/enumVars}}{{/allowableValues}} }; return {{name}}Array[{{name}}]; } - {{name}}_e {{name}}{{classname}}_FromString(char* {{name}}){ + {{classFilename}}_{{name}}_e {{name}}{{classname}}_FromString(char* {{name}}){ int stringToReturn = 0; char *{{name}}Array[] = { {{#allowableValues}}{{#enumVars}}"{{{value}}}"{{^-last}},{{/-last}}{{/enumVars}}{{/allowableValues}} }; size_t sizeofArray = sizeof({{name}}Array) / sizeof({{name}}Array[0]); @@ -98,12 +96,12 @@ return 0; {{#items}} {{^isModel}} {{#isEnum}} - char* {{name}}{{classname}}_ToString({{name}}_e {{name}}){ + char* {{name}}{{classname}}_ToString({{classFilename}}_{{name}}_e {{name}}){ char *{{name}}Array[] = { {{#allowableValues}}{{#enumVars}}"{{{value}}}"{{^-last}},{{/-last}}{{/enumVars}}{{/allowableValues}} }; return {{name}}Array[{{name}} - 1]; } - {{name}}_e {{name}}{{classname}}_FromString(char* {{name}}){ + {{classFilename}}_{{name}}_e {{name}}{{classname}}_FromString(char* {{name}}){ int stringToReturn = 0; char *{{name}}Array[] = { {{#allowableValues}}{{#enumVars}}"{{{value}}}"{{^-last}},{{/-last}}{{/enumVars}}{{/allowableValues}} }; size_t sizeofArray = sizeof({{name}}Array) / sizeof({{name}}Array[0]); @@ -127,7 +125,7 @@ return 0; {{^isPrimitiveType}} {{#isModel}} {{#isEnum}} - {{datatype}}_e {{name}}{{#hasMore}},{{/hasMore}} + {{classFilename}}_{{datatype}}_e {{name}}{{#hasMore}},{{/hasMore}} {{/isEnum}} {{^isEnum}} {{datatype}}_t *{{name}}{{#hasMore}},{{/hasMore}} @@ -152,7 +150,7 @@ return 0; {{/isBoolean}} {{#isEnum}} {{#isString}} - {{name}}_e {{name}}{{#hasMore}},{{/hasMore}} + {{classFilename}}_{{name}}_e {{name}}{{#hasMore}},{{/hasMore}} {{/isString}} {{/isEnum}} {{^isEnum}} @@ -409,7 +407,7 @@ cJSON *{{classname}}_convertToJSON({{classname}}_t *{{classname}}) { listEntry_t *{{{name}}}ListEntry; if ({{{classname}}}->{{{name}}}) { list_ForEach({{{name}}}ListEntry, {{classname}}->{{{name}}}) { - cJSON *itemLocal = {{complexType}}_convertToJSON({{#isEnum}}{{#items}}({{datatypeWithEnum}}_e){{/items}}{{/isEnum}}{{{name}}}ListEntry->data); + cJSON *itemLocal = {{complexType}}_convertToJSON({{#isEnum}}{{#items}}({{classFilename}}_{{datatypeWithEnum}}_e){{/items}}{{/isEnum}}{{{name}}}ListEntry->data); if(itemLocal == NULL) { goto fail; } @@ -490,7 +488,7 @@ fail: {{/isBoolean}} {{#isEnum}} {{#isString}} - {{{name}}}_e {{name}}Variable; + {{classFilename}}_{{{name}}}_e {{name}}Variable; {{^required}}if ({{{name}}}) { {{/required}} if(!cJSON_IsString({{{name}}})) { @@ -548,7 +546,7 @@ fail: {{^isPrimitiveType}} {{#isModel}} {{#isEnum}} - {{datatypeWithEnum}}_e {{name}}_local_nonprim_enum; + {{classFilename}}_{{datatypeWithEnum}}_e {{name}}_local_nonprim_enum; {{^required}}if ({{{name}}}) { {{/required}} {{{name}}}_local_nonprim_enum = {{datatypeWithEnum}}_parseFromJSON({{{name}}}); //enum model {{/isEnum}} @@ -625,7 +623,7 @@ fail: if(!cJSON_IsObject({{{name}}}_local_nonprimitive)){ goto end; } - {{#isEnum}}{{#items}}{{datatypeWithEnum}}_e {{/items}}{{/isEnum}}{{^isEnum}}{{complexType}}_t *{{/isEnum}}{{{name}}}Item = {{complexType}}_parseFromJSON({{{name}}}_local_nonprimitive); + {{#isEnum}}{{#items}}{{classFilename}}_{{datatypeWithEnum}}_e {{/items}}{{/isEnum}}{{^isEnum}}{{complexType}}_t *{{/isEnum}}{{{name}}}Item = {{complexType}}_parseFromJSON({{{name}}}_local_nonprimitive); list_addElement({{{name}}}List, {{#isEnum}}{{#items}}(void *){{/items}}{{/isEnum}}{{{name}}}Item); } 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 28817ccd91e8..c5fb2528b092 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 @@ -18,16 +18,16 @@ {{#isEnum}} {{#allowableValues}} -typedef enum { {{#enumVars}} {{{value}}}{{#first}} = 0{{/first}}{{^-last}},{{/-last}}{{/enumVars}} } {{classname}}_e; +typedef enum { {{#enumVars}} {{classFilename}}_{{enumName}}_{{{value}}}{{#first}} = 0{{/first}}{{^-last}},{{/-last}}{{/enumVars}} } {{classFilename}}_{{classname}}_e; {{/allowableValues}} -char* {{classname}}_ToString({{classname}}_e {{classname}}); +char* {{classFilename}}_{{classname}}_ToString({{classFilename}}_{{classname}}_e {{classname}}); -{{classname}}_e {{classname}}_FromString(char* {{classname}}); +{{classFilename}}_{{classname}}_e {{classFilename}}_{{classname}}_FromString(char* {{classname}}); -cJSON *{{classname}}_convertToJSON({{classname}}_e {{classname}}); +cJSON *{{classFilename}}_{{classname}}_convertToJSON({{classFilename}}_{{classname}}_e {{classname}}); -{{classname}}_e {{classname}}_parseFromJSON(cJSON *{{classname}}JSON); +{{classFilename}}_{{classname}}_e {{classFilename}}_{{classname}}_parseFromJSON(cJSON *{{classname}}JSON); {{/isEnum}} {{^isEnum}} @@ -36,12 +36,12 @@ cJSON *{{classname}}_convertToJSON({{classname}}_e {{classname}}); {{^isModel}} {{#isEnum}} {{#allowableValues}} - typedef enum { {{#enumVars}} {{{value}}}{{#first}} = 0{{/first}}{{^-last}},{{/-last}}{{/enumVars}} } {{name}}_e; + typedef enum { {{#enumVars}} {{classFilename}}_{{enumName}}_{{{value}}}{{#first}} = 0{{/first}}{{^-last}},{{/-last}}{{/enumVars}} } {{classFilename}}_{{name}}_e; {{/allowableValues}} - char* {{name}}_ToString({{name}}_e {{name}}); + char* {{classFilename}}_{{name}}_ToString({{classFilename}}_{{name}}_e {{name}}); - {{name}}_e {{name}}_FromString(char* {{name}}); + {{classFilename}}_{{name}}_e {{classFilename}}_{{name}}_FromString(char* {{name}}); {{/isEnum}} {{/isModel}} {{/isContainer}} @@ -50,12 +50,12 @@ cJSON *{{classname}}_convertToJSON({{classname}}_e {{classname}}); {{^isModel}} {{#isEnum}} {{#allowableValues}} - typedef enum { {{#enumVars}} {{{value}}}{{^-last}},{{/-last}}{{/enumVars}} } {{name}}_e; + typedef enum { {{#enumVars}} {{classFilename}}_{{enumName}}_{{{value}}}{{^-last}},{{/-last}}{{/enumVars}} } {{classFilename}}_{{name}}_e; {{/allowableValues}} - char* {{name}}_ToString({{name}}_e {{name}}); + char* {{classFilename}}_{{name}}_ToString({{classFilename}}_{{name}}_e {{name}}); - {{name}}_e {{name}}_FromString(char* {{name}}); + {{classFilename}}_{{name}}_e {{classFilename}}_{{name}}_FromString(char* {{name}}); {{/isEnum}} {{/isModel}} {{/items}} @@ -69,7 +69,7 @@ typedef struct {{classname}}_t { {{^isPrimitiveType}} {{#isModel}} {{#isEnum}} - {{datatype}}_e {{name}}; //enum model + {{classFilename}}_{{datatype}}_e {{name}}; //enum model {{/isEnum}} {{^isEnum}} struct {{datatype}}_t *{{name}}; //model @@ -94,7 +94,7 @@ typedef struct {{classname}}_t { {{/isBoolean}} {{#isEnum}} {{#isString}} - {{name}}_e {{name}}; //enum + {{classFilename}}_{{name}}_e {{name}}; //enum {{/isString}} {{/isEnum}} {{^isEnum}} @@ -139,7 +139,7 @@ typedef struct {{classname}}_t { {{^isPrimitiveType}} {{#isModel}} {{#isEnum}} - {{datatype}}_e {{name}}{{#hasMore}},{{/hasMore}} + {{classFilename}}_{{datatype}}_e {{name}}{{#hasMore}},{{/hasMore}} {{/isEnum}} {{^isEnum}} {{datatype}}_t *{{name}}{{#hasMore}},{{/hasMore}} @@ -164,7 +164,7 @@ typedef struct {{classname}}_t { {{/isBoolean}} {{#isEnum}} {{#isString}} - {{name}}_e {{name}}{{#hasMore}},{{/hasMore}} + {{classFilename}}_{{name}}_e {{name}}{{#hasMore}},{{/hasMore}} {{/isString}} {{/isEnum}} {{^isEnum}} diff --git a/samples/client/petstore/c/api/PetAPI.c b/samples/client/petstore/c/api/PetAPI.c index 593839286420..f6c1bee15f77 100644 --- a/samples/client/petstore/c/api/PetAPI.c +++ b/samples/client/petstore/c/api/PetAPI.c @@ -10,6 +10,84 @@ char dst[256];\ snprintf(dst, 256, "%ld", (long int)(src));\ }while(0) + + + + + + + + +// Functions for enum STATUS for PetAPI_findPetsByStatus + + +char* STATUS_ToString(status_e STATUS){ +char *STATUSArray[] = { "available","pending","sold" }; + return STATUSArray[STATUS]; +} + +status_e STATUS_FromString(char* STATUS){ + int stringToReturn = 0; + char *STATUSArray[] = { "available","pending","sold" }; + size_t sizeofArray = sizeof(STATUSArray) / sizeof(STATUSArray[0]); + while(stringToReturn < sizeofArray) { + if(strcmp(STATUS, STATUSArray[stringToReturn]) == 0) { + return stringToReturn; + } + stringToReturn++; + } + return 0; +} + +cJSON *STATUS_convertToJSON(status_e STATUS) { +cJSON *item = cJSON_CreateObject(); + return item; + fail: + cJSON_Delete(item); + return NULL; +} + +status_e STATUS_parseFromJSON(cJSON *STATUSJSON){ + +status_e *STATUS = NULL; + +return STATUSVariable; +end: +return 0; +} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + // Add a new pet to the store // diff --git a/samples/client/petstore/c/api/PetAPI.h b/samples/client/petstore/c/api/PetAPI.h index 5cb6c9de2c65..d86a6e254235 100644 --- a/samples/client/petstore/c/api/PetAPI.h +++ b/samples/client/petstore/c/api/PetAPI.h @@ -6,6 +6,46 @@ #include "../include/keyValuePair.h" #include "../model/api_response.h" #include "../model/pet.h" + + + + + + + + + +// Enum STATUS for PetAPI_findPetsByStatus + +typedef enum { STATUS_available, STATUS_pending, STATUS_sold } status_e; + +char* STATUS_ToString(status_e STATUS); + +status_e STATUS_FromString(char* STATUS); + +cJSON *STATUS_convertToJSON(status_e STATUS); + +status_e STATUS_parseFromJSON(cJSON *STATUSJSON); + + + + + + + + + + + + + + + + + + + + // Add a new pet to the store diff --git a/samples/client/petstore/c/api/StoreAPI.c b/samples/client/petstore/c/api/StoreAPI.c index 0a4a3a37fedb..514dad1b8e2d 100644 --- a/samples/client/petstore/c/api/StoreAPI.c +++ b/samples/client/petstore/c/api/StoreAPI.c @@ -11,6 +11,27 @@ snprintf(dst, 256, "%ld", (long int)(src));\ }while(0) + + + + + + + + + + + + + + + + + + + + + // Delete purchase order by ID // // For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors diff --git a/samples/client/petstore/c/api/StoreAPI.h b/samples/client/petstore/c/api/StoreAPI.h index cba13a977d55..698cb8eaff64 100644 --- a/samples/client/petstore/c/api/StoreAPI.h +++ b/samples/client/petstore/c/api/StoreAPI.h @@ -7,6 +7,14 @@ #include "../model/order.h" + + + + + + + + // Delete purchase order by ID // // For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors diff --git a/samples/client/petstore/c/api/UserAPI.c b/samples/client/petstore/c/api/UserAPI.c index 28f8c8b8c935..c4aa3d1c4679 100644 --- a/samples/client/petstore/c/api/UserAPI.c +++ b/samples/client/petstore/c/api/UserAPI.c @@ -11,6 +11,39 @@ snprintf(dst, 256, "%ld", (long int)(src));\ }while(0) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + // Create user // // This can only be done by the logged in user. diff --git a/samples/client/petstore/c/api/UserAPI.h b/samples/client/petstore/c/api/UserAPI.h index 57f04a721940..e86aac7ec417 100644 --- a/samples/client/petstore/c/api/UserAPI.h +++ b/samples/client/petstore/c/api/UserAPI.h @@ -6,6 +6,26 @@ #include "../include/keyValuePair.h" #include "../model/user.h" + + + + + + + + + + + + + + + + + + + + // Create user // diff --git a/samples/client/petstore/c/model/order.c b/samples/client/petstore/c/model/order.c index c4b866541183..d6e230424c0f 100644 --- a/samples/client/petstore/c/model/order.c +++ b/samples/client/petstore/c/model/order.c @@ -4,12 +4,12 @@ #include "order.h" - char* statusorder_ToString(status_e status){ + char* statusorder_ToString(order_status_e status){ char *statusArray[] = { "placed","approved","delivered" }; return statusArray[status]; } - status_e statusorder_FromString(char* status){ + order_status_e statusorder_FromString(char* status){ int stringToReturn = 0; char *statusArray[] = { "placed","approved","delivered" }; size_t sizeofArray = sizeof(statusArray) / sizeof(statusArray[0]); @@ -27,7 +27,7 @@ order_t *order_create( long pet_id, int quantity, char *ship_date, - status_e status, + order_status_e status, int complete ) { order_t *order_local_var = malloc(sizeof(order_t)); @@ -152,7 +152,7 @@ order_t *order_parseFromJSON(cJSON *orderJSON){ // order->status cJSON *status = cJSON_GetObjectItemCaseSensitive(orderJSON, "status"); - status_e statusVariable; + order_status_e statusVariable; if (status) { if(!cJSON_IsString(status)) { diff --git a/samples/client/petstore/c/model/order.h b/samples/client/petstore/c/model/order.h index 35458e07df5f..9cbd26cb7fb5 100644 --- a/samples/client/petstore/c/model/order.h +++ b/samples/client/petstore/c/model/order.h @@ -12,11 +12,11 @@ #include "../include/list.h" #include "../include/keyValuePair.h" - typedef enum { placed, approved, delivered } status_e; + typedef enum { order_STATUS_placed, order_STATUS_approved, order_STATUS_delivered } order_status_e; - char* status_ToString(status_e status); + char* order_status_ToString(order_status_e status); - status_e status_FromString(char* status); + order_status_e order_status_FromString(char* status); typedef struct order_t { @@ -24,7 +24,7 @@ typedef struct order_t { long pet_id; //numeric int quantity; //numeric char *ship_date; //date time - status_e status; //enum + order_status_e status; //enum int complete; //boolean } order_t; @@ -34,7 +34,7 @@ order_t *order_create( long pet_id, int quantity, char *ship_date, - status_e status, + order_status_e status, int complete ); diff --git a/samples/client/petstore/c/model/pet.c b/samples/client/petstore/c/model/pet.c index af321141f7d7..d0b75139c2ee 100644 --- a/samples/client/petstore/c/model/pet.c +++ b/samples/client/petstore/c/model/pet.c @@ -4,12 +4,12 @@ #include "pet.h" - char* statuspet_ToString(status_e status){ + char* statuspet_ToString(pet_status_e status){ char *statusArray[] = { "available","pending","sold" }; return statusArray[status]; } - status_e statuspet_FromString(char* status){ + pet_status_e statuspet_FromString(char* status){ int stringToReturn = 0; char *statusArray[] = { "available","pending","sold" }; size_t sizeofArray = sizeof(statusArray) / sizeof(statusArray[0]); @@ -28,7 +28,7 @@ pet_t *pet_create( char *name, list_t *photo_urls, list_t *tags, - status_e status + pet_status_e status ) { pet_t *pet_local_var = malloc(sizeof(pet_t)); if (!pet_local_var) { @@ -228,7 +228,7 @@ pet_t *pet_parseFromJSON(cJSON *petJSON){ // pet->status cJSON *status = cJSON_GetObjectItemCaseSensitive(petJSON, "status"); - status_e statusVariable; + pet_status_e statusVariable; if (status) { if(!cJSON_IsString(status)) { diff --git a/samples/client/petstore/c/model/pet.h b/samples/client/petstore/c/model/pet.h index e8c0aac854d0..e4eb2893dceb 100644 --- a/samples/client/petstore/c/model/pet.h +++ b/samples/client/petstore/c/model/pet.h @@ -14,11 +14,11 @@ #include "category.h" #include "tag.h" - typedef enum { available, pending, sold } status_e; + typedef enum { pet_STATUS_available, pet_STATUS_pending, pet_STATUS_sold } pet_status_e; - char* status_ToString(status_e status); + char* pet_status_ToString(pet_status_e status); - status_e status_FromString(char* status); + pet_status_e pet_status_FromString(char* status); typedef struct pet_t { @@ -27,7 +27,7 @@ typedef struct pet_t { char *name; // string list_t *photo_urls; //primitive container list_t *tags; //nonprimitive container - status_e status; //enum + pet_status_e status; //enum } pet_t; @@ -37,7 +37,7 @@ pet_t *pet_create( char *name, list_t *photo_urls, list_t *tags, - status_e status + pet_status_e status ); void pet_free(pet_t *pet); From c3fa511358317fa391181852f6540c42db0057d4 Mon Sep 17 00:00:00 2001 From: Michele Albano Date: Fri, 28 Feb 2020 06:08:34 -0800 Subject: [PATCH 2/5] PR to solve 2 open issues on enums: Issue 5091 needs to generate enums also when the enum is directly in a param to a API call, instead than in a model. I did that by copying and adapting enum code from *model* to *api* Issue 4293 needs to decorate enums, for when enum names or enum values are repeated over the yaml definition. --- .../resources/C-libcurl/api-body.mustache | 19 +------------------ samples/client/petstore/c/api/PetAPI.c | 16 +--------------- samples/client/petstore/c/api/StoreAPI.c | 13 ------------- samples/client/petstore/c/api/UserAPI.c | 13 ------------- 4 files changed, 2 insertions(+), 59 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/C-libcurl/api-body.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/api-body.mustache index 9f496728a42e..8a6a0c20ab23 100644 --- a/modules/openapi-generator/src/main/resources/C-libcurl/api-body.mustache +++ b/modules/openapi-generator/src/main/resources/C-libcurl/api-body.mustache @@ -57,9 +57,7 @@ cJSON *item = cJSON_CreateObject(); {{baseName}}_e {{enumName}}_parseFromJSON(cJSON *{{enumName}}JSON){ -{{baseName}}_e *{{enumName}} = NULL; - -{{#isEnum}} +{{{baseName}}}_e {{enumName}}Variable = 0; {{#isNumeric}} cJSON *{{{enumName}}}Var = cJSON_GetObjectItemCaseSensitive({{enumName}}JSON, "{{{paramName}}}"); if(!cJSON_IsNumber({{{enumName}}}Var)) @@ -68,14 +66,12 @@ if(!cJSON_IsNumber({{{enumName}}}Var)) } {{/isNumeric}} {{#isString}} -{{{baseName}}}_e {{enumName}}Variable; cJSON *{{{enumName}}}Var = cJSON_GetObjectItemCaseSensitive({{enumName}}JSON, "{{{paramName}}}"); if(!cJSON_IsString({{{enumName}}}Var) || ({{{enumName}}}Var->valuestring == NULL)){ goto end; } {{enumName}}Variable = {{enumName}}_FromString({{{enumName}}}Var->valuestring); {{/isString}} -{{/isEnum}} return {{enumName}}Variable; end: return 0; @@ -87,19 +83,6 @@ return 0; {{/operations}} - - - - - - - - - - - - - {{#operations}} {{#operation}} {{#summary}} diff --git a/samples/client/petstore/c/api/PetAPI.c b/samples/client/petstore/c/api/PetAPI.c index f6c1bee15f77..70a932f37271 100644 --- a/samples/client/petstore/c/api/PetAPI.c +++ b/samples/client/petstore/c/api/PetAPI.c @@ -49,8 +49,7 @@ cJSON *item = cJSON_CreateObject(); status_e STATUS_parseFromJSON(cJSON *STATUSJSON){ -status_e *STATUS = NULL; - +status_e STATUSVariable = 0; return STATUSVariable; end: return 0; @@ -68,19 +67,6 @@ return 0; - - - - - - - - - - - - - diff --git a/samples/client/petstore/c/api/StoreAPI.c b/samples/client/petstore/c/api/StoreAPI.c index 514dad1b8e2d..6b01f95d7af9 100644 --- a/samples/client/petstore/c/api/StoreAPI.c +++ b/samples/client/petstore/c/api/StoreAPI.c @@ -17,19 +17,6 @@ - - - - - - - - - - - - - // Delete purchase order by ID diff --git a/samples/client/petstore/c/api/UserAPI.c b/samples/client/petstore/c/api/UserAPI.c index c4aa3d1c4679..2eace04e7f6f 100644 --- a/samples/client/petstore/c/api/UserAPI.c +++ b/samples/client/petstore/c/api/UserAPI.c @@ -23,19 +23,6 @@ - - - - - - - - - - - - - From 54a95d84890ae3fc60d4f0bddd307e3bc7f79a3d Mon Sep 17 00:00:00 2001 From: Michele Albano Date: Sun, 1 Mar 2020 11:56:45 -0800 Subject: [PATCH 3/5] Enums decorated: with {{projectName}}_{{classVarName}}_{{enumName}}_ in the models, with {{projectName}}_{{classVarName}}_{{enumName}}_ in the operations. --- .../resources/C-libcurl/api-body.mustache | 56 +++++++++---------- .../resources/C-libcurl/api-header.mustache | 14 +---- .../resources/C-libcurl/model-body.mustache | 34 +++++------ .../resources/C-libcurl/model-header.mustache | 30 +++++----- samples/client/petstore/c/api/PetAPI.c | 39 ++++++------- samples/client/petstore/c/api/PetAPI.h | 10 +--- samples/client/petstore/c/api/StoreAPI.c | 7 +-- samples/client/petstore/c/api/UserAPI.c | 15 +++-- samples/client/petstore/c/model/order.c | 12 ++-- samples/client/petstore/c/model/order.h | 10 ++-- samples/client/petstore/c/model/pet.c | 12 ++-- samples/client/petstore/c/model/pet.h | 10 ++-- 12 files changed, 113 insertions(+), 136 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/C-libcurl/api-body.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/api-body.mustache index 8a6a0c20ab23..a0f37ff321fa 100644 --- a/modules/openapi-generator/src/main/resources/C-libcurl/api-body.mustache +++ b/modules/openapi-generator/src/main/resources/C-libcurl/api-body.mustache @@ -18,15 +18,14 @@ {{#isEnum}} // Functions for enum {{enumName}} for {{{classname}}}_{{{operationId}}} - -char* {{enumName}}_ToString({{baseName}}_e {{enumName}}){ -char *{{enumName}}Array[] = { {{#allowableValues}}{{#enumVars}}"{{{value}}}"{{^-last}},{{/-last}}{{/enumVars}}{{/allowableValues}} }; +static char* {{{operationId}}}_{{enumName}}_ToString({{projectName}}_{{operationId}}_{{baseName}}_e {{enumName}}){ + char *{{enumName}}Array[] = { "NULL"{{#allowableValues}}{{#enumVars}}, "{{{value}}}"{{/enumVars}}{{/allowableValues}} }; return {{enumName}}Array[{{enumName}}]; } -{{baseName}}_e {{enumName}}_FromString(char* {{enumName}}){ +static {{projectName}}_{{operationId}}_{{baseName}}_e {{{operationId}}}_{{enumName}}_FromString(char* {{enumName}}){ int stringToReturn = 0; - char *{{enumName}}Array[] = { {{#allowableValues}}{{#enumVars}}"{{{value}}}"{{^-last}},{{/-last}}{{/enumVars}}{{/allowableValues}} }; + char *{{enumName}}Array[] = { "NULL"{{#allowableValues}}{{#enumVars}}, "{{{value}}}"{{/enumVars}}{{/allowableValues}} }; size_t sizeofArray = sizeof({{enumName}}Array) / sizeof({{enumName}}Array[0]); while(stringToReturn < sizeofArray) { if(strcmp({{enumName}}, {{enumName}}Array[stringToReturn]) == 0) { @@ -37,10 +36,10 @@ char *{{enumName}}Array[] = { {{#allowableValues}}{{#enumVars}}"{{{value}}}"{{^ return 0; } -cJSON *{{enumName}}_convertToJSON({{baseName}}_e {{enumName}}) { -cJSON *item = cJSON_CreateObject(); +static cJSON *{{{operationId}}}_{{enumName}}_convertToJSON({{projectName}}_{{operationId}}_{{baseName}}_e {{enumName}}) { + cJSON *item = cJSON_CreateObject(); {{#isString}} - if(cJSON_AddStringToObject(item, "{{{paramName}}}", {{{enumName}}}_ToString({{{enumName}}})) == NULL) { + if(cJSON_AddStringToObject(item, "{{{paramName}}}", {{{operationId}}}_{{{enumName}}}_ToString({{{enumName}}})) == NULL) { goto fail; } {{/isString}} @@ -55,26 +54,26 @@ cJSON *item = cJSON_CreateObject(); return NULL; } -{{baseName}}_e {{enumName}}_parseFromJSON(cJSON *{{enumName}}JSON){ - -{{{baseName}}}_e {{enumName}}Variable = 0; +static {{projectName}}_{{operationId}}_{{baseName}}_e {{{operationId}}}_{{enumName}}_parseFromJSON(cJSON* {{enumName}}JSON) { + {{projectName}}_{{operationId}}_{{baseName}}_e {{enumName}}Variable = 0; {{#isNumeric}} -cJSON *{{{enumName}}}Var = cJSON_GetObjectItemCaseSensitive({{enumName}}JSON, "{{{paramName}}}"); -if(!cJSON_IsNumber({{{enumName}}}Var)) -{ - goto end; -} + cJSON *{{{enumName}}}Var = cJSON_GetObjectItemCaseSensitive({{enumName}}JSON, "{{{paramName}}}"); + if(!cJSON_IsNumber({{{enumName}}}Var)) + { + goto end; + } {{/isNumeric}} {{#isString}} -cJSON *{{{enumName}}}Var = cJSON_GetObjectItemCaseSensitive({{enumName}}JSON, "{{{paramName}}}"); -if(!cJSON_IsString({{{enumName}}}Var) || ({{{enumName}}}Var->valuestring == NULL)){ - goto end; -} -{{enumName}}Variable = {{enumName}}_FromString({{{enumName}}}Var->valuestring); + cJSON *{{{enumName}}}Var = cJSON_GetObjectItemCaseSensitive({{enumName}}JSON, "{{{paramName}}}"); + if(!cJSON_IsString({{{enumName}}}Var) || ({{{enumName}}}Var->valuestring == NULL)) + { + goto end; + } + {{enumName}}Variable = {{{operationId}}}_{{enumName}}_FromString({{{enumName}}}Var->valuestring); {{/isString}} -return {{enumName}}Variable; + return {{enumName}}Variable; end: -return 0; + return 0; } {{/isEnum}} @@ -82,7 +81,6 @@ return 0; {{/operation}} {{/operations}} - {{#operations}} {{#operation}} {{#summary}} @@ -94,7 +92,7 @@ return 0; // {{/notes}} {{#returnType}}{{#returnTypeIsPrimitive}}{{#returnSimpleType}}{{{.}}}*{{/returnSimpleType}}{{^returnSimpleType}}{{#isListContainer}}{{{.}}}_t*{{/isListContainer}}{{#isMapContainer}}{{{.}}}{{/isMapContainer}}{{/returnSimpleType}}{{/returnTypeIsPrimitive}}{{^returnTypeIsPrimitive}}{{{.}}}_t*{{/returnTypeIsPrimitive}}{{/returnType}}{{^returnType}}void{{/returnType}} -{{{classname}}}_{{{operationId}}}(apiClient_t *apiClient{{#allParams}} ,{{#isPrimitiveType}}{{#isNumber}}{{{dataType}}}{{/isNumber}}{{#isLong}}{{{dataType}}}{{/isLong}}{{#isInteger}}{{{dataType}}}{{/isInteger}}{{#isDouble}}{{{dataType}}}{{/isDouble}}{{#isFloat}}{{{dataType}}}{{/isFloat}}{{#isBoolean}}{{dataType}}{{/isBoolean}}{{#isEnum}}{{#isString}}{{{baseName}}}_e{{/isString}}{{/isEnum}}{{^isEnum}}{{#isString}}{{{dataType}}} *{{/isString}}{{/isEnum}}{{#isByteArray}}{{{dataType}}}{{/isByteArray}}{{#isDate}}{{{dataType}}}{{/isDate}}{{#isDateTime}}{{{dataType}}}{{/isDateTime}}{{#isFile}}{{{dataType}}}{{/isFile}}{{#isFreeFormObject}}{{dataType}}_t *{{/isFreeFormObject}}{{/isPrimitiveType}}{{^isPrimitiveType}}{{#isModel}}{{#isEnum}}{{datatypeWithEnum}}_e{{/isEnum}}{{^isEnum}}{{{dataType}}}_t *{{/isEnum}}{{/isModel}}{{^isModel}}{{^isListContainer}}{{#isEnum}}{{datatypeWithEnum}}_e{{/isEnum}}{{/isListContainer}}{{/isModel}}{{#isUuid}}{{dataType}} *{{/isUuid}}{{#isEmail}}{{dataType}}{{/isEmail}}{{/isPrimitiveType}}{{#isContainer}}{{#isListContainer}}{{dataType}}_t *{{/isListContainer}}{{#isMapContainer}}{{dataType}}{{/isMapContainer}}{{/isContainer}} {{{paramName}}}{{/allParams}}) +{{{classname}}}_{{{operationId}}}(apiClient_t *apiClient{{#allParams}}, {{#isPrimitiveType}}{{#isNumber}}{{{dataType}}}{{/isNumber}}{{#isLong}}{{{dataType}}}{{/isLong}}{{#isInteger}}{{{dataType}}}{{/isInteger}}{{#isDouble}}{{{dataType}}}{{/isDouble}}{{#isFloat}}{{{dataType}}}{{/isFloat}}{{#isBoolean}}{{dataType}}{{/isBoolean}}{{#isEnum}}{{#isString}}{{projectName}}_{{operationId}}_{{baseName}}_e{{/isString}}{{/isEnum}}{{^isEnum}}{{#isString}}{{{dataType}}} *{{/isString}}{{/isEnum}}{{#isByteArray}}{{{dataType}}}{{/isByteArray}}{{#isDate}}{{{dataType}}}{{/isDate}}{{#isDateTime}}{{{dataType}}}{{/isDateTime}}{{#isFile}}{{{dataType}}}{{/isFile}}{{#isFreeFormObject}}{{dataType}}_t *{{/isFreeFormObject}}{{/isPrimitiveType}}{{^isPrimitiveType}}{{#isModel}}{{#isEnum}}{{datatypeWithEnum}}_e{{/isEnum}}{{^isEnum}}{{{dataType}}}_t *{{/isEnum}}{{/isModel}}{{^isModel}}{{^isListContainer}}{{#isEnum}}{{datatypeWithEnum}}_e{{/isEnum}}{{/isListContainer}}{{/isModel}}{{#isUuid}}{{dataType}} *{{/isUuid}}{{#isEmail}}{{dataType}}{{/isEmail}}{{/isPrimitiveType}}{{#isContainer}}{{#isListContainer}}{{dataType}}_t *{{/isListContainer}}{{#isMapContainer}}{{dataType}}{{/isMapContainer}}{{/isContainer}} {{{paramName}}}{{/allParams}}) { list_t *localVarQueryParameters = {{#hasQueryParams}}list_create();{{/hasQueryParams}}{{^hasQueryParams}}NULL;{{/hasQueryParams}} list_t *localVarHeaderParameters = {{#hasHeaderParams}}list_create();{{/hasHeaderParams}}{{^hasHeaderParams}}NULL;{{/hasHeaderParams}} @@ -189,7 +187,7 @@ return 0; // header parameters char *keyHeader_{{{paramName}}}; - {{#isPrimitiveType}}{{#isNumber}}{{{dataType}}}{{/isNumber}}{{#isLong}}{{{dataType}}}{{/isLong}}{{#isInteger}}{{{dataType}}}{{/isInteger}}{{#isDouble}}{{{dataType}}}{{/isDouble}}{{#isFloat}}{{{dataType}}}{{/isFloat}}{{#isBoolean}}{{dataType}}{{/isBoolean}}{{#isEnum}}{{#isString}}{{{baseName}}}_e{{/isString}}{{/isEnum}}{{^isEnum}}{{#isString}}{{{dataType}}} *{{/isString}}{{/isEnum}}{{#isByteArray}}{{{dataType}}}{{/isByteArray}}{{#isDate}}{{{dataType}}}{{/isDate}}{{#isDateTime}}{{{dataType}}}{{/isDateTime}}{{#isFile}}{{{dataType}}}{{/isFile}}{{/isPrimitiveType}}{{^isPrimitiveType}}{{#isModel}}{{#isEnum}}{{datatypeWithEnum}}_e{{/isEnum}}{{^isEnum}}{{{dataType}}}_t *{{/isEnum}}{{/isModel}}{{^isModel}}{{#isEnum}}{{datatypeWithEnum}}_e{{/isEnum}}{{/isModel}}{{#isUuid}}{{dataType}} *{{/isUuid}}{{#isEmail}}{{dataType}}{{/isEmail}}{{/isPrimitiveType}} valueHeader_{{{paramName}}}; + {{#isPrimitiveType}}{{#isNumber}}{{{dataType}}}{{/isNumber}}{{#isLong}}{{{dataType}}}{{/isLong}}{{#isInteger}}{{{dataType}}}{{/isInteger}}{{#isDouble}}{{{dataType}}}{{/isDouble}}{{#isFloat}}{{{dataType}}}{{/isFloat}}{{#isBoolean}}{{dataType}}{{/isBoolean}}{{#isEnum}}{{#isString}}{{projectName}}_{{operationId}}_{{baseName}}_e{{/isString}}{{/isEnum}}{{^isEnum}}{{#isString}}{{{dataType}}} *{{/isString}}{{/isEnum}}{{#isByteArray}}{{{dataType}}}{{/isByteArray}}{{#isDate}}{{{dataType}}}{{/isDate}}{{#isDateTime}}{{{dataType}}}{{/isDateTime}}{{#isFile}}{{{dataType}}}{{/isFile}}{{/isPrimitiveType}}{{^isPrimitiveType}}{{#isModel}}{{#isEnum}}{{datatypeWithEnum}}_e{{/isEnum}}{{^isEnum}}{{{dataType}}}_t *{{/isEnum}}{{/isModel}}{{^isModel}}{{#isEnum}}{{datatypeWithEnum}}_e{{/isEnum}}{{/isModel}}{{#isUuid}}{{dataType}} *{{/isUuid}}{{#isEmail}}{{dataType}}{{/isEmail}}{{/isPrimitiveType}} valueHeader_{{{paramName}}}; keyValuePair_t *keyPairHeader_{{paramName}} = 0; if ({{paramName}}) { keyHeader_{{{paramName}}} = strdup("{{{baseName}}}"); @@ -204,7 +202,7 @@ return 0; // query parameters {{^isListContainer}} char *keyQuery_{{{paramName}}} = NULL; - {{#isPrimitiveType}}{{#isNumber}}{{{dataType}}}{{/isNumber}}{{#isLong}}{{{dataType}}}{{/isLong}}{{#isInteger}}{{{dataType}}}{{/isInteger}}{{#isDouble}}{{{dataType}}}{{/isDouble}}{{#isFloat}}{{{dataType}}}{{/isFloat}}{{#isBoolean}}{{{dataType}}}{{/isBoolean}}{{#isEnum}}{{#isString}}{{{baseName}}}_e{{/isString}}{{/isEnum}}{{^isEnum}}{{#isString}}{{{dataType}}} *{{/isString}}{{/isEnum}}{{#isByteArray}}{{{dataType}}}{{/isByteArray}}{{#isDate}}{{{dataType}}}{{/isDate}}{{#isDateTime}}{{{dataType}}}{{/isDateTime}}{{#isFile}}{{{dataType}}}{{/isFile}}{{/isPrimitiveType}}{{^isPrimitiveType}}{{#isModel}}{{#isEnum}}{{datatypeWithEnum}}_e{{/isEnum}}{{^isEnum}}{{{dataType}}}_t *{{/isEnum}}{{/isModel}}{{^isModel}}{{#isEnum}}{{datatypeWithEnum}}_e{{/isEnum}}{{/isModel}}{{#isUuid}}{{dataType}} *{{/isUuid}}{{#isEmail}}{{dataType}}{{/isEmail}}{{/isPrimitiveType}} valueQuery_{{{paramName}}} {{#isString}}{{^isEnum}}= NULL{{/isEnum}}{{/isString}}; + {{#isPrimitiveType}}{{#isNumber}}{{{dataType}}}{{/isNumber}}{{#isLong}}{{{dataType}}}{{/isLong}}{{#isInteger}}{{{dataType}}}{{/isInteger}}{{#isDouble}}{{{dataType}}}{{/isDouble}}{{#isFloat}}{{{dataType}}}{{/isFloat}}{{#isBoolean}}{{{dataType}}}{{/isBoolean}}{{#isEnum}}{{#isString}}{{projectName}}_{{operationId}}_{{baseName}}_e{{/isString}}{{/isEnum}}{{^isEnum}}{{#isString}}{{{dataType}}} *{{/isString}}{{/isEnum}}{{#isByteArray}}{{{dataType}}}{{/isByteArray}}{{#isDate}}{{{dataType}}}{{/isDate}}{{#isDateTime}}{{{dataType}}}{{/isDateTime}}{{#isFile}}{{{dataType}}}{{/isFile}}{{/isPrimitiveType}}{{^isPrimitiveType}}{{#isModel}}{{#isEnum}}{{datatypeWithEnum}}_e{{/isEnum}}{{^isEnum}}{{{dataType}}}_t *{{/isEnum}}{{/isModel}}{{^isModel}}{{#isEnum}}{{datatypeWithEnum}}_e{{/isEnum}}{{/isModel}}{{#isUuid}}{{dataType}} *{{/isUuid}}{{#isEmail}}{{dataType}}{{/isEmail}}{{/isPrimitiveType}} valueQuery_{{{paramName}}} {{#isString}}{{^isEnum}}= NULL{{/isEnum}}{{/isString}}; keyValuePair_t *keyPairQuery_{{paramName}} = 0; {{/isListContainer}} if ({{paramName}}) @@ -215,7 +213,7 @@ return 0; {{^isListContainer}} keyQuery_{{{paramName}}} = strdup("{{{baseName}}}"); valueQuery_{{{paramName}}} = {{#isString}}{{^isEnum}}strdup({{/isEnum}}{{/isString}}({{{paramName}}}){{#isString}}{{^isEnum}}){{/isEnum}}{{/isString}}; - keyPairQuery_{{paramName}} = keyValuePair_create(keyQuery_{{{paramName}}}, {{#isEnum}}(void *)strdup({{enumName}}_ToString( + keyPairQuery_{{paramName}} = keyValuePair_create(keyQuery_{{{paramName}}}, {{#isEnum}}(void *)strdup({{{operationId}}}_{{enumName}}_ToString( {{/isEnum}}{{^isString}}&{{/isString}}valueQuery_{{{paramName}}}{{#isEnum}})){{/isEnum}}); list_addElement(localVarQueryParameters,keyPairQuery_{{paramName}}); {{/isListContainer}} @@ -231,7 +229,7 @@ return 0; {{/isFile}} {{^isFile}} char *keyForm_{{paramName}}; - {{#isPrimitiveType}}{{#isNumber}}{{{dataType}}}{{/isNumber}}{{#isLong}}{{{dataType}}}{{/isLong}}{{#isInteger}}{{{dataType}}}{{/isInteger}}{{#isDouble}}{{{dataType}}}{{/isDouble}}{{#isFloat}}{{{dataType}}}{{/isFloat}}{{#isBoolean}}{{dataType}}{{/isBoolean}}{{#isEnum}}{{#isString}}{{{baseName}}}_e{{/isString}}{{/isEnum}}{{^isEnum}}{{#isString}}{{{dataType}}} *{{/isString}}{{/isEnum}}{{#isByteArray}}{{{dataType}}}{{/isByteArray}}{{#isDate}}{{{dataType}}}{{/isDate}}{{#isDateTime}}{{{dataType}}}{{/isDateTime}}{{#isFile}}{{{dataType}}}{{/isFile}}{{/isPrimitiveType}}{{^isPrimitiveType}}{{#isModel}}{{#isEnum}}{{datatypeWithEnum}}_e{{/isEnum}}{{^isEnum}}{{{dataType}}}_t *{{/isEnum}}{{/isModel}}{{^isModel}}{{#isEnum}}{{datatypeWithEnum}}_e{{/isEnum}}{{/isModel}}{{#isUuid}}{{dataType}} *{{/isUuid}}{{#isEmail}}{{dataType}}{{/isEmail}}{{/isPrimitiveType}} valueForm_{{paramName}}; + {{#isPrimitiveType}}{{#isNumber}}{{{dataType}}}{{/isNumber}}{{#isLong}}{{{dataType}}}{{/isLong}}{{#isInteger}}{{{dataType}}}{{/isInteger}}{{#isDouble}}{{{dataType}}}{{/isDouble}}{{#isFloat}}{{{dataType}}}{{/isFloat}}{{#isBoolean}}{{dataType}}{{/isBoolean}}{{#isEnum}}{{#isString}}{{projectName}}_{{operationId}}_{{baseName}}_e{{/isString}}{{/isEnum}}{{^isEnum}}{{#isString}}{{{dataType}}} *{{/isString}}{{/isEnum}}{{#isByteArray}}{{{dataType}}}{{/isByteArray}}{{#isDate}}{{{dataType}}}{{/isDate}}{{#isDateTime}}{{{dataType}}}{{/isDateTime}}{{#isFile}}{{{dataType}}}{{/isFile}}{{/isPrimitiveType}}{{^isPrimitiveType}}{{#isModel}}{{#isEnum}}{{datatypeWithEnum}}_e{{/isEnum}}{{^isEnum}}{{{dataType}}}_t *{{/isEnum}}{{/isModel}}{{^isModel}}{{#isEnum}}{{datatypeWithEnum}}_e{{/isEnum}}{{/isModel}}{{#isUuid}}{{dataType}} *{{/isUuid}}{{#isEmail}}{{dataType}}{{/isEmail}}{{/isPrimitiveType}} valueForm_{{paramName}}; keyValuePair_t *keyPairForm_{{paramName}} = 0; {{/isFile}} if ({{paramName}} != NULL) diff --git a/modules/openapi-generator/src/main/resources/C-libcurl/api-header.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/api-header.mustache index c49a807fe0e4..cb6e1546f03c 100644 --- a/modules/openapi-generator/src/main/resources/C-libcurl/api-header.mustache +++ b/modules/openapi-generator/src/main/resources/C-libcurl/api-header.mustache @@ -16,17 +16,9 @@ // Enum {{enumName}} for {{{classname}}}_{{{operationId}}} {{#allowableValues}} -typedef enum { {{#enumVars}} {{enumName}}_{{{value}}}{{#first}} = 0{{/first}}{{^-last}},{{/-last}}{{/enumVars}} } {{baseName}}_e; -{{/allowableValues}} - -char* {{enumName}}_ToString({{baseName}}_e {{enumName}}); - -{{baseName}}_e {{enumName}}_FromString(char* {{enumName}}); - -cJSON *{{enumName}}_convertToJSON({{baseName}}_e {{enumName}}); - -{{baseName}}_e {{enumName}}_parseFromJSON(cJSON *{{enumName}}JSON); +typedef enum { {{projectName}}_{{operationId}}_{{enumName}}_NULL = 0{{#enumVars}}, {{projectName}}_{{operationId}}_{{enumName}}_{{{value}}}{{/enumVars}} } {{projectName}}_{{operationId}}_{{baseName}}_e; +{{/allowableValues}} {{/isEnum}} {{/allParams}} @@ -45,7 +37,7 @@ cJSON *{{enumName}}_convertToJSON({{baseName}}_e {{enumName}}); // {{/notes}} {{#returnType}}{{#returnTypeIsPrimitive}}{{#returnSimpleType}}{{{.}}}*{{/returnSimpleType}}{{^returnSimpleType}}{{#isListContainer}}{{{.}}}_t*{{/isListContainer}}{{#isMapContainer}}{{{.}}}{{/isMapContainer}}{{/returnSimpleType}}{{/returnTypeIsPrimitive}}{{^returnTypeIsPrimitive}}{{{.}}}_t*{{/returnTypeIsPrimitive}}{{/returnType}}{{^returnType}}void{{/returnType}} -{{{classname}}}_{{{operationId}}}(apiClient_t *apiClient{{#allParams}} ,{{#isPrimitiveType}}{{#isNumber}}{{{dataType}}}{{/isNumber}}{{#isLong}}{{{dataType}}}{{/isLong}}{{#isInteger}}{{{dataType}}}{{/isInteger}}{{#isDouble}}{{{dataType}}}{{/isDouble}}{{#isFloat}}{{{dataType}}}{{/isFloat}}{{#isBoolean}}{{dataType}}{{/isBoolean}}{{#isEnum}}{{#isString}}{{{baseName}}}_e{{/isString}}{{/isEnum}}{{^isEnum}}{{#isString}}{{{dataType}}} *{{/isString}}{{/isEnum}}{{#isByteArray}}{{{dataType}}}{{/isByteArray}}{{#isDate}}{{{dataType}}}{{/isDate}}{{#isDateTime}}{{{dataType}}}{{/isDateTime}}{{#isFile}}{{{dataType}}}{{/isFile}}{{#isFreeFormObject}}{{dataType}}_t *{{/isFreeFormObject}}{{/isPrimitiveType}}{{^isPrimitiveType}}{{#isModel}}{{#isEnum}}{{datatypeWithEnum}}_e{{/isEnum}}{{^isEnum}}{{{dataType}}}_t *{{/isEnum}}{{/isModel}}{{^isModel}}{{^isListContainer}}{{#isEnum}}{{datatypeWithEnum}}_e{{/isEnum}}{{/isListContainer}}{{/isModel}}{{#isUuid}}{{dataType}} *{{/isUuid}}{{#isEmail}}{{dataType}}{{/isEmail}}{{/isPrimitiveType}}{{#isContainer}}{{#isListContainer}}{{dataType}}_t *{{/isListContainer}}{{#isMapContainer}}{{dataType}}{{/isMapContainer}}{{/isContainer}} {{{paramName}}}{{/allParams}}); +{{{classname}}}_{{{operationId}}}(apiClient_t *apiClient{{#allParams}} ,{{#isPrimitiveType}}{{#isNumber}}{{{dataType}}}{{/isNumber}}{{#isLong}}{{{dataType}}}{{/isLong}}{{#isInteger}}{{{dataType}}}{{/isInteger}}{{#isDouble}}{{{dataType}}}{{/isDouble}}{{#isFloat}}{{{dataType}}}{{/isFloat}}{{#isBoolean}}{{dataType}}{{/isBoolean}}{{#isEnum}}{{#isString}}{{projectName}}_{{operationId}}_{{baseName}}_e{{/isString}}{{/isEnum}}{{^isEnum}}{{#isString}}{{{dataType}}} *{{/isString}}{{/isEnum}}{{#isByteArray}}{{{dataType}}}{{/isByteArray}}{{#isDate}}{{{dataType}}}{{/isDate}}{{#isDateTime}}{{{dataType}}}{{/isDateTime}}{{#isFile}}{{{dataType}}}{{/isFile}}{{#isFreeFormObject}}{{dataType}}_t *{{/isFreeFormObject}}{{/isPrimitiveType}}{{^isPrimitiveType}}{{#isModel}}{{#isEnum}}{{datatypeWithEnum}}_e{{/isEnum}}{{^isEnum}}{{{dataType}}}_t *{{/isEnum}}{{/isModel}}{{^isModel}}{{^isListContainer}}{{#isEnum}}{{datatypeWithEnum}}_e{{/isEnum}}{{/isListContainer}}{{/isModel}}{{#isUuid}}{{dataType}} *{{/isUuid}}{{#isEmail}}{{dataType}}{{/isEmail}}{{/isPrimitiveType}}{{#isContainer}}{{#isListContainer}}{{dataType}}_t *{{/isListContainer}}{{#isMapContainer}}{{dataType}}{{/isMapContainer}}{{/isContainer}} {{{paramName}}}{{/allParams}}); {{/operation}} 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 a48fecb80808..47562f2cc002 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 @@ -7,14 +7,14 @@ {{#isEnum}} -char* {{classFilename}}_{{classname}}_ToString({{classFilename}}_{{classname}}_e {{classname}}) { - char *{{classname}}Array[] = { {{#allowableValues}}{{#enumVars}}"{{{value}}}"{{^-last}},{{/-last}}{{/enumVars}}{{/allowableValues}} }; +char* {{classFilename}}_{{classname}}_ToString({{projectName}}_{{classVarName}}_{{enumName}}_e {{classname}}) { + char *{{classname}}Array[] = { "NULL"{{#allowableValues}}{{#enumVars}}, "{{{value}}}"{{/enumVars}}{{/allowableValues}} }; return {{classname}}Array[{{classname}}]; } -{{classFilename}}_{{classname}}_e {{classFilename}}_{{classname}}_FromString(char* {{classname}}) { +{{projectName}}_{{classVarName}}_{{enumName}}_e {{classFilename}}_{{classname}}_FromString(char* {{classname}}) { int stringToReturn = 0; - char *{{classname}}Array[] = { {{#allowableValues}}{{#enumVars}}"{{{value}}}"{{^-last}},{{/-last}}{{/enumVars}}{{/allowableValues}} }; + char *{{classname}}Array[] = { "NULL"{{#allowableValues}}{{#enumVars}}, "{{{value}}}"{{/enumVars}}{{/allowableValues}} }; size_t sizeofArray = sizeof({{classname}}Array) / sizeof({{classname}}Array[0]); while(stringToReturn < sizeofArray) { if(strcmp({{classname}}, {{classname}}Array[stringToReturn]) == 0) { @@ -25,7 +25,7 @@ char* {{classFilename}}_{{classname}}_ToString({{classFilename}}_{{classname}}_e return 0; } -cJSON *{{classFilename}}_{{classname}}_convertToJSON({{classFilename}}_{{classname}}_e {{classname}}) { +cJSON *{{classFilename}}_{{classname}}_convertToJSON({{projectName}}_{{classVarName}}_{{enumName}}_e {{classname}}) { cJSON *item = cJSON_CreateObject(); {{#isString}} if(cJSON_AddStringToObject(item, "{{{classname}}}", {{classFilename}}_{{{classname}}}_ToString({{{classname}}})) == NULL) { @@ -72,14 +72,14 @@ end: {{^isContainer}} {{^isModel}} {{#isEnum}} - char* {{name}}{{classname}}_ToString({{classFilename}}_{{name}}_e {{name}}){ - char *{{name}}Array[] = { {{#allowableValues}}{{#enumVars}}"{{{value}}}"{{^-last}},{{/-last}}{{/enumVars}}{{/allowableValues}} }; + char* {{name}}{{classname}}_ToString({{projectName}}_{{classVarName}}_{{enumName}}_e {{name}}) { + char* {{name}}Array[] = { "NULL"{{#allowableValues}}{{#enumVars}}, "{{{value}}}"{{/enumVars}}{{/allowableValues}} }; return {{name}}Array[{{name}}]; } - {{classFilename}}_{{name}}_e {{name}}{{classname}}_FromString(char* {{name}}){ + {{projectName}}_{{classVarName}}_{{enumName}}_e {{name}}{{classname}}_FromString(char* {{name}}){ int stringToReturn = 0; - char *{{name}}Array[] = { {{#allowableValues}}{{#enumVars}}"{{{value}}}"{{^-last}},{{/-last}}{{/enumVars}}{{/allowableValues}} }; + char *{{name}}Array[] = { "NULL"{{#allowableValues}}{{#enumVars}}, "{{{value}}}"{{/enumVars}}{{/allowableValues}} }; size_t sizeofArray = sizeof({{name}}Array) / sizeof({{name}}Array[0]); while(stringToReturn < sizeofArray) { if(strcmp({{name}}, {{name}}Array[stringToReturn]) == 0) { @@ -96,14 +96,14 @@ end: {{#items}} {{^isModel}} {{#isEnum}} - char* {{name}}{{classname}}_ToString({{classFilename}}_{{name}}_e {{name}}){ - char *{{name}}Array[] = { {{#allowableValues}}{{#enumVars}}"{{{value}}}"{{^-last}},{{/-last}}{{/enumVars}}{{/allowableValues}} }; + char* {{name}}{{classname}}_ToString({{projectName}}_{{classVarName}}_{{enumName}}_e {{name}}) { + char *{{name}}Array[] = { "NULL"{{#allowableValues}}{{#enumVars}}, "{{{value}}}"{{/enumVars}}{{/allowableValues}} }; return {{name}}Array[{{name}} - 1]; } - {{classFilename}}_{{name}}_e {{name}}{{classname}}_FromString(char* {{name}}){ + {{projectName}}_{{classVarName}}_{{enumName}}_e {{name}}{{classname}}_FromString(char* {{name}}){ int stringToReturn = 0; - char *{{name}}Array[] = { {{#allowableValues}}{{#enumVars}}"{{{value}}}"{{^-last}},{{/-last}}{{/enumVars}}{{/allowableValues}} }; + char *{{name}}Array[] = { "NULL"{{#allowableValues}}{{#enumVars}}, "{{{value}}}"{{/enumVars}}{{/allowableValues}} }; size_t sizeofArray = sizeof({{name}}Array) / sizeof({{name}}Array[0]); while(stringToReturn < sizeofArray) { if(strcmp({{name}}, {{name}}Array[stringToReturn]) == 0) { @@ -125,7 +125,7 @@ end: {{^isPrimitiveType}} {{#isModel}} {{#isEnum}} - {{classFilename}}_{{datatype}}_e {{name}}{{#hasMore}},{{/hasMore}} + {{projectName}}_{{classVarName}}_{{enumName}}_e {{name}}{{#hasMore}},{{/hasMore}} {{/isEnum}} {{^isEnum}} {{datatype}}_t *{{name}}{{#hasMore}},{{/hasMore}} @@ -150,7 +150,7 @@ end: {{/isBoolean}} {{#isEnum}} {{#isString}} - {{classFilename}}_{{name}}_e {{name}}{{#hasMore}},{{/hasMore}} + {{projectName}}_{{classVarName}}_{{enumName}}_e {{name}}{{#hasMore}},{{/hasMore}} {{/isString}} {{/isEnum}} {{^isEnum}} @@ -407,7 +407,7 @@ cJSON *{{classname}}_convertToJSON({{classname}}_t *{{classname}}) { listEntry_t *{{{name}}}ListEntry; if ({{{classname}}}->{{{name}}}) { list_ForEach({{{name}}}ListEntry, {{classname}}->{{{name}}}) { - cJSON *itemLocal = {{complexType}}_convertToJSON({{#isEnum}}{{#items}}({{classFilename}}_{{datatypeWithEnum}}_e){{/items}}{{/isEnum}}{{{name}}}ListEntry->data); + cJSON *itemLocal = {{complexType}}_convertToJSON({{#isEnum}}{{#items}}({{projectName}}_{{classVarName}}_{{enumName}}_e){{/items}}{{/isEnum}}{{{name}}}ListEntry->data); if(itemLocal == NULL) { goto fail; } @@ -488,7 +488,7 @@ fail: {{/isBoolean}} {{#isEnum}} {{#isString}} - {{classFilename}}_{{{name}}}_e {{name}}Variable; + {{projectName}}_{{classVarName}}_{{enumName}}_e {{name}}Variable; {{^required}}if ({{{name}}}) { {{/required}} if(!cJSON_IsString({{{name}}})) { 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 c5fb2528b092..910f956d53c3 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 @@ -18,16 +18,16 @@ {{#isEnum}} {{#allowableValues}} -typedef enum { {{#enumVars}} {{classFilename}}_{{enumName}}_{{{value}}}{{#first}} = 0{{/first}}{{^-last}},{{/-last}}{{/enumVars}} } {{classFilename}}_{{classname}}_e; +typedef enum { {{projectName}}_{{classVarName}}_{{enumName}}_NULL = 0{{#enumVars}}, {{projectName}}_{{classVarName}}_{{enumName}}_{{{value}}}{{/enumVars}} } {{projectName}}_{{classVarName}}_{{enumName}}_e; {{/allowableValues}} -char* {{classFilename}}_{{classname}}_ToString({{classFilename}}_{{classname}}_e {{classname}}); +char* {{classFilename}}_{{classname}}_ToString({{projectName}}_{{classVarName}}_{{enumName}}_e {{classname}}); -{{classFilename}}_{{classname}}_e {{classFilename}}_{{classname}}_FromString(char* {{classname}}); +{{projectName}}_{{classVarName}}_{{enumName}}_e {{classFilename}}_{{classname}}_FromString(char* {{classname}}); -cJSON *{{classFilename}}_{{classname}}_convertToJSON({{classFilename}}_{{classname}}_e {{classname}}); +cJSON *{{classFilename}}_{{classname}}_convertToJSON({{projectName}}_{{classVarName}}_{{enumName}}_e {{classname}}); -{{classFilename}}_{{classname}}_e {{classFilename}}_{{classname}}_parseFromJSON(cJSON *{{classname}}JSON); +{{projectName}}_{{classVarName}}_{{enumName}}_e {{classFilename}}_{{classname}}_parseFromJSON(cJSON *{{classname}}JSON); {{/isEnum}} {{^isEnum}} @@ -36,12 +36,12 @@ cJSON *{{classFilename}}_{{classname}}_convertToJSON({{classFilename}}_{{classna {{^isModel}} {{#isEnum}} {{#allowableValues}} - typedef enum { {{#enumVars}} {{classFilename}}_{{enumName}}_{{{value}}}{{#first}} = 0{{/first}}{{^-last}},{{/-last}}{{/enumVars}} } {{classFilename}}_{{name}}_e; + typedef enum { {{projectName}}_{{classVarName}}_{{enumName}}_NULL = 0{{#enumVars}}, {{projectName}}_{{classVarName}}_{{enumName}}_{{{value}}}{{/enumVars}} } {{projectName}}_{{classVarName}}_{{enumName}}_e; {{/allowableValues}} - char* {{classFilename}}_{{name}}_ToString({{classFilename}}_{{name}}_e {{name}}); + char* {{classFilename}}_{{name}}_ToString({{projectName}}_{{classVarName}}_{{enumName}}_e {{name}}); - {{classFilename}}_{{name}}_e {{classFilename}}_{{name}}_FromString(char* {{name}}); + {{projectName}}_{{classVarName}}_{{enumName}}_e {{classFilename}}_{{name}}_FromString(char* {{name}}); {{/isEnum}} {{/isModel}} {{/isContainer}} @@ -50,12 +50,12 @@ cJSON *{{classFilename}}_{{classname}}_convertToJSON({{classFilename}}_{{classna {{^isModel}} {{#isEnum}} {{#allowableValues}} - typedef enum { {{#enumVars}} {{classFilename}}_{{enumName}}_{{{value}}}{{^-last}},{{/-last}}{{/enumVars}} } {{classFilename}}_{{name}}_e; + typedef enum { {{projectName}}_{{classVarName}}_{{enumName}}_NULL = 0{{#enumVars}}, {{projectName}}_{{classVarName}}_{{enumName}}_{{{value}}}{{/enumVars}} } {{projectName}}_{{classVarName}}_{{enumName}}_e; {{/allowableValues}} - char* {{classFilename}}_{{name}}_ToString({{classFilename}}_{{name}}_e {{name}}); + char* {{classFilename}}_{{name}}_ToString({{projectName}}_{{classVarName}}_{{enumName}}_e {{name}}); - {{classFilename}}_{{name}}_e {{classFilename}}_{{name}}_FromString(char* {{name}}); + {{projectName}}_{{classVarName}}_{{enumName}}_e {{classFilename}}_{{name}}_FromString(char* {{name}}); {{/isEnum}} {{/isModel}} {{/items}} @@ -69,7 +69,7 @@ typedef struct {{classname}}_t { {{^isPrimitiveType}} {{#isModel}} {{#isEnum}} - {{classFilename}}_{{datatype}}_e {{name}}; //enum model + {{projectName}}_{{classVarName}}_{{enumName}}_e {{name}}; //enum model {{/isEnum}} {{^isEnum}} struct {{datatype}}_t *{{name}}; //model @@ -94,7 +94,7 @@ typedef struct {{classname}}_t { {{/isBoolean}} {{#isEnum}} {{#isString}} - {{classFilename}}_{{name}}_e {{name}}; //enum + {{projectName}}_{{classVarName}}_{{enumName}}_e {{name}}; //enum {{/isString}} {{/isEnum}} {{^isEnum}} @@ -139,7 +139,7 @@ typedef struct {{classname}}_t { {{^isPrimitiveType}} {{#isModel}} {{#isEnum}} - {{classFilename}}_{{datatype}}_e {{name}}{{#hasMore}},{{/hasMore}} + {{projectName}}_{{classVarName}}_{{enumName}}_e {{name}}{{#hasMore}},{{/hasMore}} {{/isEnum}} {{^isEnum}} {{datatype}}_t *{{name}}{{#hasMore}},{{/hasMore}} @@ -164,7 +164,7 @@ typedef struct {{classname}}_t { {{/isBoolean}} {{#isEnum}} {{#isString}} - {{classFilename}}_{{name}}_e {{name}}{{#hasMore}},{{/hasMore}} + {{projectName}}_{{classVarName}}_{{enumName}}_e {{name}}{{#hasMore}},{{/hasMore}} {{/isString}} {{/isEnum}} {{^isEnum}} diff --git a/samples/client/petstore/c/api/PetAPI.c b/samples/client/petstore/c/api/PetAPI.c index 70a932f37271..5ec19b142d58 100644 --- a/samples/client/petstore/c/api/PetAPI.c +++ b/samples/client/petstore/c/api/PetAPI.c @@ -20,15 +20,14 @@ // Functions for enum STATUS for PetAPI_findPetsByStatus - -char* STATUS_ToString(status_e STATUS){ -char *STATUSArray[] = { "available","pending","sold" }; +static char* findPetsByStatus_STATUS_ToString(openapi_petstore_findPetsByStatus_status_e STATUS){ + char *STATUSArray[] = { "NULL", "available", "pending", "sold" }; return STATUSArray[STATUS]; } -status_e STATUS_FromString(char* STATUS){ +static openapi_petstore_findPetsByStatus_status_e findPetsByStatus_STATUS_FromString(char* STATUS){ int stringToReturn = 0; - char *STATUSArray[] = { "available","pending","sold" }; + char *STATUSArray[] = { "NULL", "available", "pending", "sold" }; size_t sizeofArray = sizeof(STATUSArray) / sizeof(STATUSArray[0]); while(stringToReturn < sizeofArray) { if(strcmp(STATUS, STATUSArray[stringToReturn]) == 0) { @@ -39,20 +38,19 @@ status_e STATUS_FromString(char* STATUS){ return 0; } -cJSON *STATUS_convertToJSON(status_e STATUS) { -cJSON *item = cJSON_CreateObject(); +static cJSON *findPetsByStatus_STATUS_convertToJSON(openapi_petstore_findPetsByStatus_status_e STATUS) { + cJSON *item = cJSON_CreateObject(); return item; fail: cJSON_Delete(item); return NULL; } -status_e STATUS_parseFromJSON(cJSON *STATUSJSON){ - -status_e STATUSVariable = 0; -return STATUSVariable; +static openapi_petstore_findPetsByStatus_status_e findPetsByStatus_STATUS_parseFromJSON(cJSON* STATUSJSON) { + openapi_petstore_findPetsByStatus_status_e STATUSVariable = 0; + return STATUSVariable; end: -return 0; + return 0; } @@ -73,12 +71,11 @@ return 0; - // Add a new pet to the store // void -PetAPI_addPet(apiClient_t *apiClient ,pet_t * body) +PetAPI_addPet(apiClient_t *apiClient, pet_t * body) { list_t *localVarQueryParameters = NULL; list_t *localVarHeaderParameters = NULL; @@ -137,7 +134,7 @@ PetAPI_addPet(apiClient_t *apiClient ,pet_t * body) // Deletes a pet // void -PetAPI_deletePet(apiClient_t *apiClient ,long petId ,char * api_key) +PetAPI_deletePet(apiClient_t *apiClient, long petId, char * api_key) { list_t *localVarQueryParameters = NULL; list_t *localVarHeaderParameters = list_create(); @@ -215,7 +212,7 @@ PetAPI_deletePet(apiClient_t *apiClient ,long petId ,char * api_key) // Multiple status values can be provided with comma separated strings // list_t* -PetAPI_findPetsByStatus(apiClient_t *apiClient ,list_t * status) +PetAPI_findPetsByStatus(apiClient_t *apiClient, list_t * status) { list_t *localVarQueryParameters = list_create(); list_t *localVarHeaderParameters = NULL; @@ -294,7 +291,7 @@ PetAPI_findPetsByStatus(apiClient_t *apiClient ,list_t * status) // Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing. // list_t* -PetAPI_findPetsByTags(apiClient_t *apiClient ,list_t * tags) +PetAPI_findPetsByTags(apiClient_t *apiClient, list_t * tags) { list_t *localVarQueryParameters = list_create(); list_t *localVarHeaderParameters = NULL; @@ -373,7 +370,7 @@ PetAPI_findPetsByTags(apiClient_t *apiClient ,list_t * tags) // Returns a single pet // pet_t* -PetAPI_getPetById(apiClient_t *apiClient ,long petId) +PetAPI_getPetById(apiClient_t *apiClient, long petId) { list_t *localVarQueryParameters = NULL; list_t *localVarHeaderParameters = NULL; @@ -452,7 +449,7 @@ PetAPI_getPetById(apiClient_t *apiClient ,long petId) // Update an existing pet // void -PetAPI_updatePet(apiClient_t *apiClient ,pet_t * body) +PetAPI_updatePet(apiClient_t *apiClient, pet_t * body) { list_t *localVarQueryParameters = NULL; list_t *localVarHeaderParameters = NULL; @@ -517,7 +514,7 @@ PetAPI_updatePet(apiClient_t *apiClient ,pet_t * body) // Updates a pet in the store with form data // void -PetAPI_updatePetWithForm(apiClient_t *apiClient ,long petId ,char * name ,char * status) +PetAPI_updatePetWithForm(apiClient_t *apiClient, long petId, char * name, char * status) { list_t *localVarQueryParameters = NULL; list_t *localVarHeaderParameters = NULL; @@ -609,7 +606,7 @@ PetAPI_updatePetWithForm(apiClient_t *apiClient ,long petId ,char * name ,char * // uploads an image // api_response_t* -PetAPI_uploadFile(apiClient_t *apiClient ,long petId ,char * additionalMetadata ,binary_t* file) +PetAPI_uploadFile(apiClient_t *apiClient, long petId, char * additionalMetadata, binary_t* file) { list_t *localVarQueryParameters = NULL; list_t *localVarHeaderParameters = NULL; diff --git a/samples/client/petstore/c/api/PetAPI.h b/samples/client/petstore/c/api/PetAPI.h index d86a6e254235..c0501da49408 100644 --- a/samples/client/petstore/c/api/PetAPI.h +++ b/samples/client/petstore/c/api/PetAPI.h @@ -17,15 +17,7 @@ // Enum STATUS for PetAPI_findPetsByStatus -typedef enum { STATUS_available, STATUS_pending, STATUS_sold } status_e; - -char* STATUS_ToString(status_e STATUS); - -status_e STATUS_FromString(char* STATUS); - -cJSON *STATUS_convertToJSON(status_e STATUS); - -status_e STATUS_parseFromJSON(cJSON *STATUSJSON); +typedef enum { openapi_petstore_findPetsByStatus_STATUS_NULL = 0, openapi_petstore_findPetsByStatus_STATUS_available, openapi_petstore_findPetsByStatus_STATUS_pending, openapi_petstore_findPetsByStatus_STATUS_sold } openapi_petstore_findPetsByStatus_status_e; diff --git a/samples/client/petstore/c/api/StoreAPI.c b/samples/client/petstore/c/api/StoreAPI.c index 6b01f95d7af9..586c25a2dfc5 100644 --- a/samples/client/petstore/c/api/StoreAPI.c +++ b/samples/client/petstore/c/api/StoreAPI.c @@ -18,13 +18,12 @@ - // Delete purchase order by ID // // For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors // void -StoreAPI_deleteOrder(apiClient_t *apiClient ,char * orderId) +StoreAPI_deleteOrder(apiClient_t *apiClient, char * orderId) { list_t *localVarQueryParameters = NULL; list_t *localVarHeaderParameters = NULL; @@ -146,7 +145,7 @@ StoreAPI_getInventory(apiClient_t *apiClient) // For valid response try integer IDs with value <= 5 or > 10. Other values will generated exceptions // order_t* -StoreAPI_getOrderById(apiClient_t *apiClient ,long orderId) +StoreAPI_getOrderById(apiClient_t *apiClient, long orderId) { list_t *localVarQueryParameters = NULL; list_t *localVarHeaderParameters = NULL; @@ -225,7 +224,7 @@ StoreAPI_getOrderById(apiClient_t *apiClient ,long orderId) // Place an order for a pet // order_t* -StoreAPI_placeOrder(apiClient_t *apiClient ,order_t * body) +StoreAPI_placeOrder(apiClient_t *apiClient, order_t * body) { list_t *localVarQueryParameters = NULL; list_t *localVarHeaderParameters = NULL; diff --git a/samples/client/petstore/c/api/UserAPI.c b/samples/client/petstore/c/api/UserAPI.c index 2eace04e7f6f..c2c0f28f0ded 100644 --- a/samples/client/petstore/c/api/UserAPI.c +++ b/samples/client/petstore/c/api/UserAPI.c @@ -29,14 +29,13 @@ - // Create user // // This can only be done by the logged in user. // void -UserAPI_createUser(apiClient_t *apiClient ,user_t * body) +UserAPI_createUser(apiClient_t *apiClient, user_t * body) { list_t *localVarQueryParameters = NULL; list_t *localVarHeaderParameters = NULL; @@ -93,7 +92,7 @@ UserAPI_createUser(apiClient_t *apiClient ,user_t * body) // Creates list of users with given input array // void -UserAPI_createUsersWithArrayInput(apiClient_t *apiClient ,list_t * body) +UserAPI_createUsersWithArrayInput(apiClient_t *apiClient, list_t * body) { list_t *localVarQueryParameters = NULL; list_t *localVarHeaderParameters = NULL; @@ -172,7 +171,7 @@ UserAPI_createUsersWithArrayInput(apiClient_t *apiClient ,list_t * body) // Creates list of users with given input array // void -UserAPI_createUsersWithListInput(apiClient_t *apiClient ,list_t * body) +UserAPI_createUsersWithListInput(apiClient_t *apiClient, list_t * body) { list_t *localVarQueryParameters = NULL; list_t *localVarHeaderParameters = NULL; @@ -253,7 +252,7 @@ UserAPI_createUsersWithListInput(apiClient_t *apiClient ,list_t * body) // This can only be done by the logged in user. // void -UserAPI_deleteUser(apiClient_t *apiClient ,char * username) +UserAPI_deleteUser(apiClient_t *apiClient, char * username) { list_t *localVarQueryParameters = NULL; list_t *localVarHeaderParameters = NULL; @@ -313,7 +312,7 @@ UserAPI_deleteUser(apiClient_t *apiClient ,char * username) // Get user by user name // user_t* -UserAPI_getUserByName(apiClient_t *apiClient ,char * username) +UserAPI_getUserByName(apiClient_t *apiClient, char * username) { list_t *localVarQueryParameters = NULL; list_t *localVarHeaderParameters = NULL; @@ -388,7 +387,7 @@ UserAPI_getUserByName(apiClient_t *apiClient ,char * username) // Logs user into the system // char* -UserAPI_loginUser(apiClient_t *apiClient ,char * username ,char * password) +UserAPI_loginUser(apiClient_t *apiClient, char * username, char * password) { list_t *localVarQueryParameters = list_create(); list_t *localVarHeaderParameters = NULL; @@ -539,7 +538,7 @@ UserAPI_logoutUser(apiClient_t *apiClient) // This can only be done by the logged in user. // void -UserAPI_updateUser(apiClient_t *apiClient ,char * username ,user_t * body) +UserAPI_updateUser(apiClient_t *apiClient, char * username, user_t * body) { list_t *localVarQueryParameters = NULL; list_t *localVarHeaderParameters = NULL; diff --git a/samples/client/petstore/c/model/order.c b/samples/client/petstore/c/model/order.c index d6e230424c0f..cfd48a0c1bb3 100644 --- a/samples/client/petstore/c/model/order.c +++ b/samples/client/petstore/c/model/order.c @@ -4,14 +4,14 @@ #include "order.h" - char* statusorder_ToString(order_status_e status){ - char *statusArray[] = { "placed","approved","delivered" }; + char* statusorder_ToString(openapi_petstore_order_STATUS_e status) { + char* statusArray[] = { "NULL", "placed", "approved", "delivered" }; return statusArray[status]; } - order_status_e statusorder_FromString(char* status){ + openapi_petstore_order_STATUS_e statusorder_FromString(char* status){ int stringToReturn = 0; - char *statusArray[] = { "placed","approved","delivered" }; + char *statusArray[] = { "NULL", "placed", "approved", "delivered" }; size_t sizeofArray = sizeof(statusArray) / sizeof(statusArray[0]); while(stringToReturn < sizeofArray) { if(strcmp(status, statusArray[stringToReturn]) == 0) { @@ -27,7 +27,7 @@ order_t *order_create( long pet_id, int quantity, char *ship_date, - order_status_e status, + openapi_petstore_order_STATUS_e status, int complete ) { order_t *order_local_var = malloc(sizeof(order_t)); @@ -152,7 +152,7 @@ order_t *order_parseFromJSON(cJSON *orderJSON){ // order->status cJSON *status = cJSON_GetObjectItemCaseSensitive(orderJSON, "status"); - order_status_e statusVariable; + openapi_petstore_order_STATUS_e statusVariable; if (status) { if(!cJSON_IsString(status)) { diff --git a/samples/client/petstore/c/model/order.h b/samples/client/petstore/c/model/order.h index 9cbd26cb7fb5..c297cafa7528 100644 --- a/samples/client/petstore/c/model/order.h +++ b/samples/client/petstore/c/model/order.h @@ -12,11 +12,11 @@ #include "../include/list.h" #include "../include/keyValuePair.h" - typedef enum { order_STATUS_placed, order_STATUS_approved, order_STATUS_delivered } order_status_e; + typedef enum { openapi_petstore_order_STATUS_NULL = 0, openapi_petstore_order_STATUS_placed, openapi_petstore_order_STATUS_approved, openapi_petstore_order_STATUS_delivered } openapi_petstore_order_STATUS_e; - char* order_status_ToString(order_status_e status); + char* order_status_ToString(openapi_petstore_order_STATUS_e status); - order_status_e order_status_FromString(char* status); + openapi_petstore_order_STATUS_e order_status_FromString(char* status); typedef struct order_t { @@ -24,7 +24,7 @@ typedef struct order_t { long pet_id; //numeric int quantity; //numeric char *ship_date; //date time - order_status_e status; //enum + openapi_petstore_order_STATUS_e status; //enum int complete; //boolean } order_t; @@ -34,7 +34,7 @@ order_t *order_create( long pet_id, int quantity, char *ship_date, - order_status_e status, + openapi_petstore_order_STATUS_e status, int complete ); diff --git a/samples/client/petstore/c/model/pet.c b/samples/client/petstore/c/model/pet.c index d0b75139c2ee..8295d88b3e06 100644 --- a/samples/client/petstore/c/model/pet.c +++ b/samples/client/petstore/c/model/pet.c @@ -4,14 +4,14 @@ #include "pet.h" - char* statuspet_ToString(pet_status_e status){ - char *statusArray[] = { "available","pending","sold" }; + char* statuspet_ToString(openapi_petstore_pet_STATUS_e status) { + char* statusArray[] = { "NULL", "available", "pending", "sold" }; return statusArray[status]; } - pet_status_e statuspet_FromString(char* status){ + openapi_petstore_pet_STATUS_e statuspet_FromString(char* status){ int stringToReturn = 0; - char *statusArray[] = { "available","pending","sold" }; + char *statusArray[] = { "NULL", "available", "pending", "sold" }; size_t sizeofArray = sizeof(statusArray) / sizeof(statusArray[0]); while(stringToReturn < sizeofArray) { if(strcmp(status, statusArray[stringToReturn]) == 0) { @@ -28,7 +28,7 @@ pet_t *pet_create( char *name, list_t *photo_urls, list_t *tags, - pet_status_e status + openapi_petstore_pet_STATUS_e status ) { pet_t *pet_local_var = malloc(sizeof(pet_t)); if (!pet_local_var) { @@ -228,7 +228,7 @@ pet_t *pet_parseFromJSON(cJSON *petJSON){ // pet->status cJSON *status = cJSON_GetObjectItemCaseSensitive(petJSON, "status"); - pet_status_e statusVariable; + openapi_petstore_pet_STATUS_e statusVariable; if (status) { if(!cJSON_IsString(status)) { diff --git a/samples/client/petstore/c/model/pet.h b/samples/client/petstore/c/model/pet.h index e4eb2893dceb..65495632e6e1 100644 --- a/samples/client/petstore/c/model/pet.h +++ b/samples/client/petstore/c/model/pet.h @@ -14,11 +14,11 @@ #include "category.h" #include "tag.h" - typedef enum { pet_STATUS_available, pet_STATUS_pending, pet_STATUS_sold } pet_status_e; + typedef enum { openapi_petstore_pet_STATUS_NULL = 0, openapi_petstore_pet_STATUS_available, openapi_petstore_pet_STATUS_pending, openapi_petstore_pet_STATUS_sold } openapi_petstore_pet_STATUS_e; - char* pet_status_ToString(pet_status_e status); + char* pet_status_ToString(openapi_petstore_pet_STATUS_e status); - pet_status_e pet_status_FromString(char* status); + openapi_petstore_pet_STATUS_e pet_status_FromString(char* status); typedef struct pet_t { @@ -27,7 +27,7 @@ typedef struct pet_t { char *name; // string list_t *photo_urls; //primitive container list_t *tags; //nonprimitive container - pet_status_e status; //enum + openapi_petstore_pet_STATUS_e status; //enum } pet_t; @@ -37,7 +37,7 @@ pet_t *pet_create( char *name, list_t *photo_urls, list_t *tags, - pet_status_e status + openapi_petstore_pet_STATUS_e status ); void pet_free(pet_t *pet); From 1400a8e46c71ca639203fbcabb49c876b8dbc4f7 Mon Sep 17 00:00:00 2001 From: Michele Albano Date: Tue, 10 Mar 2020 06:46:57 -0700 Subject: [PATCH 4/5] Changes to the c client: - Removed white space. - Removed ToJSON and FromJSON function for the enums, since they are not used - Sanitized project name in the .java file. For example, this solves a problem with the issue #2338, where the yaml file had title: "Skycoin REST API." --- .../languages/CLibcurlClientCodegen.java | 8 ++++- .../resources/C-libcurl/api-body.mustache | 11 +++++-- .../resources/C-libcurl/api-header.mustache | 5 --- .../resources/C-libcurl/model-body.mustache | 22 ++++++------- .../resources/C-libcurl/model-header.mustache | 25 +++++++++----- samples/client/petstore/c/api/PetAPI.c | 33 +++++-------------- samples/client/petstore/c/api/PetAPI.h | 29 ---------------- samples/client/petstore/c/api/StoreAPI.c | 6 ---- samples/client/petstore/c/api/StoreAPI.h | 8 ----- samples/client/petstore/c/api/UserAPI.c | 18 ---------- samples/client/petstore/c/api/UserAPI.h | 20 ----------- samples/client/petstore/c/model/order.c | 10 +++--- samples/client/petstore/c/model/order.h | 9 +++-- samples/client/petstore/c/model/pet.c | 10 +++--- samples/client/petstore/c/model/pet.h | 9 +++-- 15 files changed, 73 insertions(+), 150 deletions(-) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CLibcurlClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CLibcurlClientCodegen.java index 2b5830ea890e..a411b302d7c2 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CLibcurlClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CLibcurlClientCodegen.java @@ -617,11 +617,17 @@ public void preprocessOpenAPI(OpenAPI openAPI) { } else { setProjectName(defaultProjectName); } + System.out.println("michele was here preprocessOpenAPI "+projectName); additionalProperties.put(PROJECT_NAME, projectName); + System.out.println("michele was here preprocessOpenAPI after "+projectName); } public void setProjectName(String projectName) { - this.projectName = underscore(projectName.toLowerCase(Locale.ROOT)); + System.out.println("michele was here setProjectName this1 "+this.projectName); + System.out.println("michele was here setProjectName no1 "+projectName); + this.projectName = underscore(sanitizeName(projectName.toLowerCase(Locale.ROOT))); + System.out.println("michele was here setProjectName this2 "+this.projectName); + System.out.println("michele was here setProjectName no2 "+projectName); } @Override diff --git a/modules/openapi-generator/src/main/resources/C-libcurl/api-body.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/api-body.mustache index a0f37ff321fa..c8daaf0e444b 100644 --- a/modules/openapi-generator/src/main/resources/C-libcurl/api-body.mustache +++ b/modules/openapi-generator/src/main/resources/C-libcurl/api-body.mustache @@ -14,7 +14,6 @@ {{#operations}} {{#operation}} {{#allParams}} - {{#isEnum}} // Functions for enum {{enumName}} for {{{classname}}}_{{{operationId}}} @@ -36,6 +35,10 @@ static {{projectName}}_{{operationId}}_{{baseName}}_e {{{operationId}}}_{{enumNa return 0; } +/* +// Function {{{operationId}}}_{{enumName}}_convertToJSON is not currently used, +// since conversion to JSON passes through the conversion of the model, and ToString. The function is kept for future reference. +// static cJSON *{{{operationId}}}_{{enumName}}_convertToJSON({{projectName}}_{{operationId}}_{{baseName}}_e {{enumName}}) { cJSON *item = cJSON_CreateObject(); {{#isString}} @@ -54,6 +57,9 @@ static cJSON *{{{operationId}}}_{{enumName}}_convertToJSON({{projectName}}_{{ope return NULL; } +// Function {{{operationId}}}_{{enumName}}_parseFromJSON is not currently used, +// since conversion from JSON passes through the conversion of the model, and FromString. The function is kept for future reference. +// static {{projectName}}_{{operationId}}_{{baseName}}_e {{{operationId}}}_{{enumName}}_parseFromJSON(cJSON* {{enumName}}JSON) { {{projectName}}_{{operationId}}_{{baseName}}_e {{enumName}}Variable = 0; {{#isNumeric}} @@ -75,8 +81,9 @@ static {{projectName}}_{{operationId}}_{{baseName}}_e {{{operationId}}}_{{enumNa end: return 0; } -{{/isEnum}} +*/ +{{/isEnum}} {{/allParams}} {{/operation}} {{/operations}} diff --git a/modules/openapi-generator/src/main/resources/C-libcurl/api-header.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/api-header.mustache index cb6e1546f03c..6b2d7773d431 100644 --- a/modules/openapi-generator/src/main/resources/C-libcurl/api-header.mustache +++ b/modules/openapi-generator/src/main/resources/C-libcurl/api-header.mustache @@ -7,25 +7,20 @@ {{#imports}}{{{import}}} {{/imports}} - {{#operations}} {{#operation}} {{#allParams}} - {{#isEnum}} // Enum {{enumName}} for {{{classname}}}_{{{operationId}}} - {{#allowableValues}} typedef enum { {{projectName}}_{{operationId}}_{{enumName}}_NULL = 0{{#enumVars}}, {{projectName}}_{{operationId}}_{{enumName}}_{{{value}}}{{/enumVars}} } {{projectName}}_{{operationId}}_{{baseName}}_e; {{/allowableValues}} {{/isEnum}} - {{/allParams}} {{/operation}} {{/operations}} - {{#operations}} {{#operation}} {{#summary}} 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 47562f2cc002..5e3e49c29e46 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 @@ -72,12 +72,12 @@ end: {{^isContainer}} {{^isModel}} {{#isEnum}} - char* {{name}}{{classname}}_ToString({{projectName}}_{{classVarName}}_{{enumName}}_e {{name}}) { +char* {{name}}{{classname}}_ToString({{projectName}}_{{classVarName}}_{{enumName}}_e {{name}}) { char* {{name}}Array[] = { "NULL"{{#allowableValues}}{{#enumVars}}, "{{{value}}}"{{/enumVars}}{{/allowableValues}} }; - return {{name}}Array[{{name}}]; - } + return {{name}}Array[{{name}}]; +} - {{projectName}}_{{classVarName}}_{{enumName}}_e {{name}}{{classname}}_FromString(char* {{name}}){ +{{projectName}}_{{classVarName}}_{{enumName}}_e {{name}}{{classname}}_FromString(char* {{name}}){ int stringToReturn = 0; char *{{name}}Array[] = { "NULL"{{#allowableValues}}{{#enumVars}}, "{{{value}}}"{{/enumVars}}{{/allowableValues}} }; size_t sizeofArray = sizeof({{name}}Array) / sizeof({{name}}Array[0]); @@ -88,7 +88,7 @@ end: stringToReturn++; } return 0; - } +} {{/isEnum}} {{/isModel}} {{/isContainer}} @@ -96,12 +96,12 @@ end: {{#items}} {{^isModel}} {{#isEnum}} - char* {{name}}{{classname}}_ToString({{projectName}}_{{classVarName}}_{{enumName}}_e {{name}}) { - char *{{name}}Array[] = { "NULL"{{#allowableValues}}{{#enumVars}}, "{{{value}}}"{{/enumVars}}{{/allowableValues}} }; - return {{name}}Array[{{name}} - 1]; - } +char* {{name}}{{classname}}_ToString({{projectName}}_{{classVarName}}_{{enumName}}_e {{name}}) { + char *{{name}}Array[] = { "NULL"{{#allowableValues}}{{#enumVars}}, "{{{value}}}"{{/enumVars}}{{/allowableValues}} }; + return {{name}}Array[{{name}} - 1]; +} - {{projectName}}_{{classVarName}}_{{enumName}}_e {{name}}{{classname}}_FromString(char* {{name}}){ +{{projectName}}_{{classVarName}}_{{enumName}}_e {{name}}{{classname}}_FromString(char* {{name}}) { int stringToReturn = 0; char *{{name}}Array[] = { "NULL"{{#allowableValues}}{{#enumVars}}, "{{{value}}}"{{/enumVars}}{{/allowableValues}} }; size_t sizeofArray = sizeof({{name}}Array) / sizeof({{name}}Array[0]); @@ -112,7 +112,7 @@ end: stringToReturn++; } return 0; - } +} {{/isEnum}} {{/isModel}} {{/items}} 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 910f956d53c3..03a03276f66e 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 @@ -16,8 +16,9 @@ {{/imports}} {{#isEnum}} - {{#allowableValues}} +// Enum {{enumName}} for {{classVarName}} + typedef enum { {{projectName}}_{{classVarName}}_{{enumName}}_NULL = 0{{#enumVars}}, {{projectName}}_{{classVarName}}_{{enumName}}_{{{value}}}{{/enumVars}} } {{projectName}}_{{classVarName}}_{{enumName}}_e; {{/allowableValues}} @@ -25,9 +26,9 @@ char* {{classFilename}}_{{classname}}_ToString({{projectName}}_{{classVarName}}_ {{projectName}}_{{classVarName}}_{{enumName}}_e {{classFilename}}_{{classname}}_FromString(char* {{classname}}); -cJSON *{{classFilename}}_{{classname}}_convertToJSON({{projectName}}_{{classVarName}}_{{enumName}}_e {{classname}}); +//cJSON *{{classFilename}}_{{classname}}_convertToJSON({{projectName}}_{{classVarName}}_{{enumName}}_e {{classname}}); -{{projectName}}_{{classVarName}}_{{enumName}}_e {{classFilename}}_{{classname}}_parseFromJSON(cJSON *{{classname}}JSON); +//{{projectName}}_{{classVarName}}_{{enumName}}_e {{classFilename}}_{{classname}}_parseFromJSON(cJSON *{{classname}}JSON); {{/isEnum}} {{^isEnum}} @@ -35,13 +36,16 @@ cJSON *{{classFilename}}_{{classname}}_convertToJSON({{projectName}}_{{classVarN {{^isContainer}} {{^isModel}} {{#isEnum}} +// Enum {{enumName}} for {{classVarName}} + {{#allowableValues}} - typedef enum { {{projectName}}_{{classVarName}}_{{enumName}}_NULL = 0{{#enumVars}}, {{projectName}}_{{classVarName}}_{{enumName}}_{{{value}}}{{/enumVars}} } {{projectName}}_{{classVarName}}_{{enumName}}_e; +typedef enum { {{projectName}}_{{classVarName}}_{{enumName}}_NULL = 0{{#enumVars}}, {{projectName}}_{{classVarName}}_{{enumName}}_{{{value}}}{{/enumVars}} } {{projectName}}_{{classVarName}}_{{enumName}}_e; {{/allowableValues}} - char* {{classFilename}}_{{name}}_ToString({{projectName}}_{{classVarName}}_{{enumName}}_e {{name}}); +char* {{classFilename}}_{{name}}_ToString({{projectName}}_{{classVarName}}_{{enumName}}_e {{name}}); + +{{projectName}}_{{classVarName}}_{{enumName}}_e {{classFilename}}_{{name}}_FromString(char* {{name}}); - {{projectName}}_{{classVarName}}_{{enumName}}_e {{classFilename}}_{{name}}_FromString(char* {{name}}); {{/isEnum}} {{/isModel}} {{/isContainer}} @@ -49,13 +53,16 @@ cJSON *{{classFilename}}_{{classname}}_convertToJSON({{projectName}}_{{classVarN {{#items}} {{^isModel}} {{#isEnum}} +// Enum {{enumName}} for {{classVarName}} + {{#allowableValues}} - typedef enum { {{projectName}}_{{classVarName}}_{{enumName}}_NULL = 0{{#enumVars}}, {{projectName}}_{{classVarName}}_{{enumName}}_{{{value}}}{{/enumVars}} } {{projectName}}_{{classVarName}}_{{enumName}}_e; +typedef enum { {{projectName}}_{{classVarName}}_{{enumName}}_NULL = 0{{#enumVars}}, {{projectName}}_{{classVarName}}_{{enumName}}_{{{value}}}{{/enumVars}} } {{projectName}}_{{classVarName}}_{{enumName}}_e; {{/allowableValues}} - char* {{classFilename}}_{{name}}_ToString({{projectName}}_{{classVarName}}_{{enumName}}_e {{name}}); +char* {{classFilename}}_{{name}}_ToString({{projectName}}_{{classVarName}}_{{enumName}}_e {{name}}); + +{{projectName}}_{{classVarName}}_{{enumName}}_e {{classFilename}}_{{name}}_FromString(char* {{name}}); - {{projectName}}_{{classVarName}}_{{enumName}}_e {{classFilename}}_{{name}}_FromString(char* {{name}}); {{/isEnum}} {{/isModel}} {{/items}} diff --git a/samples/client/petstore/c/api/PetAPI.c b/samples/client/petstore/c/api/PetAPI.c index 5ec19b142d58..5dc27c6bbf59 100644 --- a/samples/client/petstore/c/api/PetAPI.c +++ b/samples/client/petstore/c/api/PetAPI.c @@ -11,13 +11,6 @@ snprintf(dst, 256, "%ld", (long int)(src));\ }while(0) - - - - - - - // Functions for enum STATUS for PetAPI_findPetsByStatus static char* findPetsByStatus_STATUS_ToString(openapi_petstore_findPetsByStatus_status_e STATUS){ @@ -38,6 +31,10 @@ static openapi_petstore_findPetsByStatus_status_e findPetsByStatus_STATUS_FromSt return 0; } +/* +// Function findPetsByStatus_STATUS_convertToJSON is not currently used, +// since conversion to JSON passes through the conversion of the model, and ToString. The function is kept for future reference. +// static cJSON *findPetsByStatus_STATUS_convertToJSON(openapi_petstore_findPetsByStatus_status_e STATUS) { cJSON *item = cJSON_CreateObject(); return item; @@ -46,30 +43,16 @@ static cJSON *findPetsByStatus_STATUS_convertToJSON(openapi_petstore_findPetsByS return NULL; } +// Function findPetsByStatus_STATUS_parseFromJSON is not currently used, +// since conversion from JSON passes through the conversion of the model, and FromString. The function is kept for future reference. +// static openapi_petstore_findPetsByStatus_status_e findPetsByStatus_STATUS_parseFromJSON(cJSON* STATUSJSON) { openapi_petstore_findPetsByStatus_status_e STATUSVariable = 0; return STATUSVariable; end: return 0; } - - - - - - - - - - - - - - - - - - +*/ // Add a new pet to the store diff --git a/samples/client/petstore/c/api/PetAPI.h b/samples/client/petstore/c/api/PetAPI.h index c0501da49408..b4bfc6cdf9cd 100644 --- a/samples/client/petstore/c/api/PetAPI.h +++ b/samples/client/petstore/c/api/PetAPI.h @@ -7,38 +7,9 @@ #include "../model/api_response.h" #include "../model/pet.h" - - - - - - - - // Enum STATUS for PetAPI_findPetsByStatus - typedef enum { openapi_petstore_findPetsByStatus_STATUS_NULL = 0, openapi_petstore_findPetsByStatus_STATUS_available, openapi_petstore_findPetsByStatus_STATUS_pending, openapi_petstore_findPetsByStatus_STATUS_sold } openapi_petstore_findPetsByStatus_status_e; - - - - - - - - - - - - - - - - - - - - // Add a new pet to the store // diff --git a/samples/client/petstore/c/api/StoreAPI.c b/samples/client/petstore/c/api/StoreAPI.c index 586c25a2dfc5..d8d4778e7bea 100644 --- a/samples/client/petstore/c/api/StoreAPI.c +++ b/samples/client/petstore/c/api/StoreAPI.c @@ -12,12 +12,6 @@ }while(0) - - - - - - // Delete purchase order by ID // // For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors diff --git a/samples/client/petstore/c/api/StoreAPI.h b/samples/client/petstore/c/api/StoreAPI.h index 698cb8eaff64..cba13a977d55 100644 --- a/samples/client/petstore/c/api/StoreAPI.h +++ b/samples/client/petstore/c/api/StoreAPI.h @@ -7,14 +7,6 @@ #include "../model/order.h" - - - - - - - - // Delete purchase order by ID // // For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors diff --git a/samples/client/petstore/c/api/UserAPI.c b/samples/client/petstore/c/api/UserAPI.c index c2c0f28f0ded..2bb91981179e 100644 --- a/samples/client/petstore/c/api/UserAPI.c +++ b/samples/client/petstore/c/api/UserAPI.c @@ -12,24 +12,6 @@ }while(0) - - - - - - - - - - - - - - - - - - // Create user // // This can only be done by the logged in user. diff --git a/samples/client/petstore/c/api/UserAPI.h b/samples/client/petstore/c/api/UserAPI.h index e86aac7ec417..57f04a721940 100644 --- a/samples/client/petstore/c/api/UserAPI.h +++ b/samples/client/petstore/c/api/UserAPI.h @@ -6,26 +6,6 @@ #include "../include/keyValuePair.h" #include "../model/user.h" - - - - - - - - - - - - - - - - - - - - // Create user // diff --git a/samples/client/petstore/c/model/order.c b/samples/client/petstore/c/model/order.c index cfd48a0c1bb3..a9216f78348d 100644 --- a/samples/client/petstore/c/model/order.c +++ b/samples/client/petstore/c/model/order.c @@ -4,12 +4,12 @@ #include "order.h" - char* statusorder_ToString(openapi_petstore_order_STATUS_e status) { +char* statusorder_ToString(openapi_petstore_order_STATUS_e status) { char* statusArray[] = { "NULL", "placed", "approved", "delivered" }; - return statusArray[status]; - } + return statusArray[status]; +} - openapi_petstore_order_STATUS_e statusorder_FromString(char* status){ +openapi_petstore_order_STATUS_e statusorder_FromString(char* status){ int stringToReturn = 0; char *statusArray[] = { "NULL", "placed", "approved", "delivered" }; size_t sizeofArray = sizeof(statusArray) / sizeof(statusArray[0]); @@ -20,7 +20,7 @@ stringToReturn++; } return 0; - } +} order_t *order_create( long id, diff --git a/samples/client/petstore/c/model/order.h b/samples/client/petstore/c/model/order.h index c297cafa7528..d09836034210 100644 --- a/samples/client/petstore/c/model/order.h +++ b/samples/client/petstore/c/model/order.h @@ -12,11 +12,14 @@ #include "../include/list.h" #include "../include/keyValuePair.h" - typedef enum { openapi_petstore_order_STATUS_NULL = 0, openapi_petstore_order_STATUS_placed, openapi_petstore_order_STATUS_approved, openapi_petstore_order_STATUS_delivered } openapi_petstore_order_STATUS_e; +// Enum STATUS for order - char* order_status_ToString(openapi_petstore_order_STATUS_e status); +typedef enum { openapi_petstore_order_STATUS_NULL = 0, openapi_petstore_order_STATUS_placed, openapi_petstore_order_STATUS_approved, openapi_petstore_order_STATUS_delivered } openapi_petstore_order_STATUS_e; + +char* order_status_ToString(openapi_petstore_order_STATUS_e status); + +openapi_petstore_order_STATUS_e order_status_FromString(char* status); - openapi_petstore_order_STATUS_e order_status_FromString(char* status); typedef struct order_t { diff --git a/samples/client/petstore/c/model/pet.c b/samples/client/petstore/c/model/pet.c index 8295d88b3e06..1765e45c1763 100644 --- a/samples/client/petstore/c/model/pet.c +++ b/samples/client/petstore/c/model/pet.c @@ -4,12 +4,12 @@ #include "pet.h" - char* statuspet_ToString(openapi_petstore_pet_STATUS_e status) { +char* statuspet_ToString(openapi_petstore_pet_STATUS_e status) { char* statusArray[] = { "NULL", "available", "pending", "sold" }; - return statusArray[status]; - } + return statusArray[status]; +} - openapi_petstore_pet_STATUS_e statuspet_FromString(char* status){ +openapi_petstore_pet_STATUS_e statuspet_FromString(char* status){ int stringToReturn = 0; char *statusArray[] = { "NULL", "available", "pending", "sold" }; size_t sizeofArray = sizeof(statusArray) / sizeof(statusArray[0]); @@ -20,7 +20,7 @@ stringToReturn++; } return 0; - } +} pet_t *pet_create( long id, diff --git a/samples/client/petstore/c/model/pet.h b/samples/client/petstore/c/model/pet.h index 65495632e6e1..bced36bcdf2a 100644 --- a/samples/client/petstore/c/model/pet.h +++ b/samples/client/petstore/c/model/pet.h @@ -14,11 +14,14 @@ #include "category.h" #include "tag.h" - typedef enum { openapi_petstore_pet_STATUS_NULL = 0, openapi_petstore_pet_STATUS_available, openapi_petstore_pet_STATUS_pending, openapi_petstore_pet_STATUS_sold } openapi_petstore_pet_STATUS_e; +// Enum STATUS for pet - char* pet_status_ToString(openapi_petstore_pet_STATUS_e status); +typedef enum { openapi_petstore_pet_STATUS_NULL = 0, openapi_petstore_pet_STATUS_available, openapi_petstore_pet_STATUS_pending, openapi_petstore_pet_STATUS_sold } openapi_petstore_pet_STATUS_e; + +char* pet_status_ToString(openapi_petstore_pet_STATUS_e status); + +openapi_petstore_pet_STATUS_e pet_status_FromString(char* status); - openapi_petstore_pet_STATUS_e pet_status_FromString(char* status); typedef struct pet_t { From bfdd7cff4949c00252b50b97841b81d7873a2c7b Mon Sep 17 00:00:00 2001 From: Michele Albano Date: Tue, 10 Mar 2020 06:46:57 -0700 Subject: [PATCH 5/5] Changes to the c client: - Removed white space. - Removed ToJSON and FromJSON function for the enums, since they are not used - Sanitized project name in the .java file. For example, this solves a problem with the issue #2338, where the yaml file had title: "Skycoin REST API." --- .../openapitools/codegen/languages/CLibcurlClientCodegen.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CLibcurlClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CLibcurlClientCodegen.java index a411b302d7c2..0be8ee130b2b 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CLibcurlClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CLibcurlClientCodegen.java @@ -623,11 +623,7 @@ public void preprocessOpenAPI(OpenAPI openAPI) { } public void setProjectName(String projectName) { - System.out.println("michele was here setProjectName this1 "+this.projectName); - System.out.println("michele was here setProjectName no1 "+projectName); this.projectName = underscore(sanitizeName(projectName.toLowerCase(Locale.ROOT))); - System.out.println("michele was here setProjectName this2 "+this.projectName); - System.out.println("michele was here setProjectName no2 "+projectName); } @Override