From 1cc783b94a7a511aed640c00c06c0768be50b19c Mon Sep 17 00:00:00 2001 From: William Cheng Date: Thu, 11 Oct 2018 16:11:48 +0800 Subject: [PATCH 1/2] better format in the c template --- .../languages/CLibcurlClientCodegen.java | 2 +- .../resources/C-libcurl/api-body.mustache | 70 +- .../resources/C-libcurl/apiClient.c.mustache | 651 +++++++++--------- .../resources/C-libcurl/apiKey.c.mustache | 12 +- .../resources/C-libcurl/api_pet_test.mustache | 195 +++--- .../resources/C-libcurl/api_test.mustache | 22 +- .../main/resources/C-libcurl/list.c.mustache | 226 +++--- .../resources/C-libcurl/model-body.mustache | 35 +- .../resources/C-libcurl/model-header.mustache | 4 +- samples/client/petstore/c/api/PetAPI.c | 25 +- samples/client/petstore/c/api/StoreAPI.c | 11 - samples/client/petstore/c/api/UserAPI.c | 14 - .../client/petstore/c/model/api_response.h | 2 - samples/client/petstore/c/model/category.h | 2 - samples/client/petstore/c/model/order.c | 1 - samples/client/petstore/c/model/order.h | 2 - samples/client/petstore/c/model/pet.c | 1 - samples/client/petstore/c/model/pet.h | 2 - samples/client/petstore/c/model/tag.h | 2 - samples/client/petstore/c/model/user.h | 2 - samples/client/petstore/c/src/apiClient.c | 8 +- samples/client/petstore/c/src/apiKey.c | 2 +- samples/client/petstore/c/src/list.c | 2 +- 23 files changed, 610 insertions(+), 683 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 1049f12eed95..7a0a5953ff82 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 @@ -173,7 +173,7 @@ public void processOpts() { super.processOpts(); if (StringUtils.isEmpty(System.getenv("C_POST_PROCESS_FILE"))) { - LOGGER.info("Environment variable C_POST_PROCESS_FILE not defined so the C code may not be properly formatted by uncrustify (0.66 or later) or other code formatter. To define it, try `export C_POST_PROCESS_FILE=\"/usr/local/bin/uncrustify --no-backup\"` (Linux/Mac)"); + LOGGER.info("Environment variable C_POST_PROCESS_FILE not defined so the C code may not be properly formatted by uncrustify (0.66 or later) or other code formatter. To define it, try `export C_POST_PROCESS_FILE=\"/usr/local/bin/uncrustify --no-backup\" && export UNCRUSTIFY_CONFIG=/path/to/uncrustify-rules.cfg` (Linux/Mac). Note: replace /path/to with the location of uncrustify-rules.cfg"); } // make api and model doc path available in mustache template 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 940be1bcdfd9..cd8593a13fc1 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,8 +14,6 @@ snprintf(dst, 256, "%ld", (long int)(src));\ }while(0) - - {{#operations}} {{#operation}} {{#summary}} @@ -36,7 +34,7 @@ char *localVarBodyParameters = NULL; // create the path - long sizeOfPath = strlen("{{{path}}}")+1; + long sizeOfPath = strlen("{{{path}}}")+1; char *localVarPath = malloc(sizeOfPath); snprintf(localVarPath, sizeOfPath, "{{{path}}}"); @@ -47,7 +45,7 @@ {{#isLong}} if({{baseName}} == 0){ - goto end; + goto end; } char* localVarToReplace = malloc(sizeOfPathParams); snprintf(localVarToReplace, sizeOfPathParams, "%s%s%s", "{", "{{baseName}}", "}"); @@ -74,8 +72,7 @@ char *keyHeader_{{{baseName}}}; char *valueHeader_{{{baseName}}}; keyValuePair_t *keyPairHeader_{{baseName}} = 0; - if ({{baseName}}) - { + if ({{baseName}}) { keyHeader_{{{baseName}}} = strdup("{{{baseName}}}"); valueHeader_{{{baseName}}} = strdup({{{baseName}}}); keyPairHeader_{{baseName}} = keyValuePair_create(keyHeader_{{{baseName}}}, valueHeader_{{{baseName}}}); @@ -86,7 +83,7 @@ // query parameters {{#isListContainer}} - if ({{baseName}} !=NULL ) + if ({{baseName}} !=NULL ) {{/isListContainer}} {{^isListContainer}} char *keyQuery_{{{baseName}}}; @@ -157,13 +154,13 @@ // Body Param {{#isListContainer}} //notstring - cJSON *localVar_{{baseName}}; - cJSON *localVarItemJSON_{{baseName}}; - cJSON *localVarSingleItemJSON_{{baseName}}; - if({{baseName}} != NULL){ + cJSON *localVar_{{baseName}}; + cJSON *localVarItemJSON_{{baseName}}; + cJSON *localVarSingleItemJSON_{{baseName}}; + if ({{baseName}} != NULL) { localVarItemJSON_{{baseName}} = cJSON_CreateObject(); localVarSingleItemJSON_{{baseName}} = cJSON_AddArrayToObject(localVarItemJSON_{{baseName}}, "{{baseName}}"); - if(localVarSingleItemJSON_{{baseName}} == NULL) { + if (localVarSingleItemJSON_{{baseName}} == NULL) { // nonprimitive container const char *error_ptr = cJSON_GetErrorPtr(); if(error_ptr != NULL) { @@ -176,12 +173,12 @@ list_ForEach({{baseName}}BodyListEntry, {{baseName}}) { localVar_{{baseName}} = user_convertToJSON({{baseName}}BodyListEntry->data); if(localVar_{{baseName}} == NULL) { - const char *error_ptr = cJSON_GetErrorPtr(); - if(error_ptr != NULL) { - fprintf(stderr, "Error Before: %s\n", error_ptr); - goto end; + const char *error_ptr = cJSON_GetErrorPtr(); + if(error_ptr != NULL) { + fprintf(stderr, "Error Before: %s\n", error_ptr); + goto end; + } } - } cJSON_AddItemToArray(localVarSingleItemJSON_{{baseName}}, localVar_{{baseName}}); } @@ -190,22 +187,19 @@ {{/isListContainer}} {{^isListContainer}} cJSON *localVarSingleItemJSON_{{baseName}}; - if({{baseName}} != NULL){ - //string + if ({{baseName}} != NULL) { + //string localVarSingleItemJSON_{{baseName}} = {{baseName}}_convertToJSON({{baseName}}); localVarBodyParameters = cJSON_Print(localVarSingleItemJSON_{{baseName}}); } {{/isListContainer}} {{/bodyParam}} {{#produces}} - - list_addElement(localVarHeaderType,"{{{mediaType}}}"); //produces + list_addElement(localVarHeaderType,"{{{mediaType}}}"); //produces {{/produces}} {{#consumes}} - - list_addElement(localVarContentType,"{{{mediaType}}}"); //consumes + list_addElement(localVarContentType,"{{{mediaType}}}"); //consumes {{/consumes}} - apiClient_invoke(apiClient, localVarPath, localVarQueryParameters, @@ -221,8 +215,8 @@ printf("%s\n","{{message}}"); } {{/responses}} - {{#returnType}} - {{#returnTypeIsPrimitive}} + {{#returnType}} + {{#returnTypeIsPrimitive}} {{#returnSimpleType}} //primitive reutrn type simple {{returnType}}* elementToReturn = strdup(({{returnType}}*)apiClient->dataReceived); @@ -234,9 +228,9 @@ cJSON *{{{baseName}}}VarJSON; list_t *elementToReturn = list_create(); cJSON_ArrayForEach({{{baseName}}}VarJSON, {{baseName}}localVarJSON){ - keyValuePair_t *keyPair = keyValuePair_create({{{baseName}}}VarJSON->string, cJSON_Print({{{baseName}}}VarJSON)); - list_addElement(elementToReturn, keyPair); - free(keyPair->value); + keyValuePair_t *keyPair = keyValuePair_create({{{baseName}}}VarJSON->string, cJSON_Print({{{baseName}}}VarJSON)); + list_addElement(elementToReturn, keyPair); + free(keyPair->value); } cJSON_Delete({{baseName}}localVarJSON); @@ -252,12 +246,12 @@ cJSON *{{{baseName}}}VarJSON; cJSON_ArrayForEach({{{baseName}}}VarJSON, {{classname}}localVarJSON) { - if(!cJSON_IsObject({{{baseName}}}VarJSON)) - { - // return 0; - } - char *localVarJSONToChar = cJSON_Print({{{baseName}}}VarJSON); - list_addElement(elementToReturn , localVarJSONToChar); + if(!cJSON_IsObject({{{baseName}}}VarJSON)) + { + // return 0; + } + char *localVarJSONToChar = cJSON_Print({{{baseName}}}VarJSON); + list_addElement(elementToReturn , localVarJSONToChar); } cJSON_Delete( {{classname}}localVarJSON); @@ -267,7 +261,7 @@ //nonprimitive not container {{{returnBaseType}}}_t *elementToReturn = {{{returnBaseType}}}_parseFromJSON(apiClient->dataReceived); if(elementToReturn == NULL) { - // return 0; + // return 0; } {{/returnContainer}} @@ -328,7 +322,7 @@ {{/formParams}} return elementToReturn; end: - return NULL; + return NULL; {{/returnType}} {{^returnType}} //No return type @@ -338,7 +332,7 @@ end: apiClient_free(apiClient); {{#hasFormParams}}list_free(localVarFormParameters);{{/hasFormParams}} {{#hasProduces}}list_free(localVarHeaderType);{{/hasProduces}} {{#hasConsumes}}list_free(localVarContentType);{{/hasConsumes}} - {{#pathParams}} + {{#pathParams}} free(localVarToReplace); {{/pathParams}} {{#headerParams}} diff --git a/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.c.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.c.mustache index dc1f14c26715..d7cd515ffcea 100644 --- a/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.c.mustache +++ b/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.c.mustache @@ -8,287 +8,287 @@ size_t writeDataCallback(void *buffer, size_t size, size_t nmemb, void *userp); apiClient_t *apiClient_create() { - curl_global_init(CURL_GLOBAL_ALL); - apiClient_t *apiClient = malloc(sizeof(apiClient_t)); - apiClient->basePath = "http://petstore.swagger.io:80/v2"; - apiClient->dataReceived = NULL; - apiClient->response_code = 0; - #ifdef BASIC_AUTH - apiClient->username = NULL; - apiClient->password = NULL; - #endif // BASIC_AUTH - #ifdef OAUTH2 - apiClient->accessToken = NULL; - #endif // OAUTH2 - return apiClient; + curl_global_init(CURL_GLOBAL_ALL); + apiClient_t *apiClient = malloc(sizeof(apiClient_t)); + apiClient->basePath = "http://petstore.swagger.io:80/v2"; + apiClient->dataReceived = NULL; + apiClient->response_code = 0; + #ifdef BASIC_AUTH + apiClient->username = NULL; + apiClient->password = NULL; + #endif // BASIC_AUTH + #ifdef OAUTH2 + apiClient->accessToken = NULL; + #endif // OAUTH2 + return apiClient; } void apiClient_free(apiClient_t *apiClient) { - if(apiClient->dataReceived) { - free(apiClient->dataReceived); - } - free(apiClient); - curl_global_cleanup(); + if(apiClient->dataReceived) { + free(apiClient->dataReceived); + } + free(apiClient); + curl_global_cleanup(); } void replaceSpaceWithPlus(char *stringToProcess) { - for(int i = 0; i < strlen(stringToProcess); i++) { - if(stringToProcess[i] == ' ') { - stringToProcess[i] = '+'; - } - } + for(int i = 0; i < strlen(stringToProcess); i++) { + if(stringToProcess[i] == ' ') { + stringToProcess[i] = '+'; + } + } } -char *assembleTargetUrl(char *basePath, - char *operationParameter, - list_t *queryParameters) { - int neededBufferSizeForQueryParameters = 0; - listEntry_t *listEntry; - - if(queryParameters != NULL) { - list_ForEach(listEntry, queryParameters) { - keyValuePair_t *pair = listEntry->data; - neededBufferSizeForQueryParameters += - strlen(pair->key) + strlen(pair->value); - } - - neededBufferSizeForQueryParameters += - (queryParameters->count * 2); // each keyValuePair is separated by a = and a & except the last, but this makes up for the ? at the beginning - } - - int operationParameterLength = 0; - int basePathLength = strlen(basePath); - bool slashNeedsToBeAppendedToBasePath = false; - - if(operationParameter != NULL) { - operationParameterLength = (1 + strlen(operationParameter)); - } - if(basePath[strlen(basePath) - 1] != '/') { - slashNeedsToBeAppendedToBasePath = true; - basePathLength++; - } - - char *targetUrl = - malloc( - neededBufferSizeForQueryParameters + basePathLength + operationParameterLength + - 1); - - strcpy(targetUrl, basePath); - - if(operationParameter != NULL) { - strcat(targetUrl, operationParameter); - } - - if(queryParameters != NULL) { - strcat(targetUrl, "?"); - list_ForEach(listEntry, queryParameters) { - keyValuePair_t *pair = listEntry->data; - replaceSpaceWithPlus(pair->key); - strcat(targetUrl, pair->key); - strcat(targetUrl, "="); - replaceSpaceWithPlus(pair->value); - strcat(targetUrl, pair->value); - if(listEntry->nextListEntry != NULL) { - strcat(targetUrl, "&"); - } - } - } - - return targetUrl; +char *assembleTargetUrl(char *basePath, + char *operationParameter, + list_t *queryParameters) { + int neededBufferSizeForQueryParameters = 0; + listEntry_t *listEntry; + + if(queryParameters != NULL) { + list_ForEach(listEntry, queryParameters) { + keyValuePair_t *pair = listEntry->data; + neededBufferSizeForQueryParameters += + strlen(pair->key) + strlen(pair->value); + } + + neededBufferSizeForQueryParameters += + (queryParameters->count * 2); // each keyValuePair is separated by a = and a & except the last, but this makes up for the ? at the beginning + } + + int operationParameterLength = 0; + int basePathLength = strlen(basePath); + bool slashNeedsToBeAppendedToBasePath = false; + + if(operationParameter != NULL) { + operationParameterLength = (1 + strlen(operationParameter)); + } + if(basePath[strlen(basePath) - 1] != '/') { + slashNeedsToBeAppendedToBasePath = true; + basePathLength++; + } + + char *targetUrl = + malloc( + neededBufferSizeForQueryParameters + basePathLength + operationParameterLength + + 1); + + strcpy(targetUrl, basePath); + + if(operationParameter != NULL) { + strcat(targetUrl, operationParameter); + } + + if(queryParameters != NULL) { + strcat(targetUrl, "?"); + list_ForEach(listEntry, queryParameters) { + keyValuePair_t *pair = listEntry->data; + replaceSpaceWithPlus(pair->key); + strcat(targetUrl, pair->key); + strcat(targetUrl, "="); + replaceSpaceWithPlus(pair->value); + strcat(targetUrl, pair->value); + if(listEntry->nextListEntry != NULL) { + strcat(targetUrl, "&"); + } + } + } + + return targetUrl; } char *assembleHeaderField(char *key, char *value) { - char *header = malloc(strlen(key) + strlen(value) + 3); + char *header = malloc(strlen(key) + strlen(value) + 3); - strcpy(header, key), - strcat(header, ": "); - strcat(header, value); + strcpy(header, key), + strcat(header, ": "); + strcat(header, value); - return header; + return header; } void postData(CURL *handle, char *bodyParameters) { - curl_easy_setopt(handle, CURLOPT_POSTFIELDS, bodyParameters); - curl_easy_setopt(handle, CURLOPT_POSTFIELDSIZE_LARGE, - strlen(bodyParameters)); + curl_easy_setopt(handle, CURLOPT_POSTFIELDS, bodyParameters); + curl_easy_setopt(handle, CURLOPT_POSTFIELDSIZE_LARGE, + strlen(bodyParameters)); } int lengthOfKeyPair(keyValuePair_t *keyPair) { - long length = 0; - if((keyPair->key != NULL) && - (keyPair->value != NULL) ) - { - length = strlen(keyPair->key) + strlen(keyPair->value); - return length; - } - return 0; + long length = 0; + if((keyPair->key != NULL) && + (keyPair->value != NULL) ) + { + length = strlen(keyPair->key) + strlen(keyPair->value); + return length; + } + return 0; } -void apiClient_invoke(apiClient_t *apiClient, - char *operationParameter, - list_t *queryParameters, - list_t *headerParameters, - list_t *formParameters, - list_t *headerType, - list_t *contentType, - char *bodyParameters, - char *requestType) { - CURL *handle = curl_easy_init(); - CURLcode res; - - if(handle) { - listEntry_t *listEntry; - curl_mime *mime = NULL; - struct curl_slist *headers = NULL; - char *buffContent = NULL; - char *buffHeader = NULL; - FileStruct *fileVar = NULL; - char *formString = NULL; - - if(headerType != NULL) { - list_ForEach(listEntry, headerType) { - if(strstr((char *) listEntry->data, - "xml") == NULL) - { - buffHeader = malloc(strlen( - "Accept: ") + - strlen((char *) - listEntry-> - data) + 1); - sprintf(buffHeader, "%s%s", "Accept: ", - (char *) listEntry->data); - headers = curl_slist_append(headers, - buffHeader); - free(buffHeader); - } - } - } - if(contentType != NULL) { - list_ForEach(listEntry, contentType) { - if(strstr((char *) listEntry->data, - "xml") == NULL) - { - buffContent = - malloc(strlen( - "Content-Type: ") + strlen( - (char *) - listEntry->data) + - 1); - sprintf(buffContent, "%s%s", - "Content-Type: ", - (char *) listEntry->data); - headers = curl_slist_append(headers, - buffContent); - } - } - } else { - headers = curl_slist_append(headers, - "Content-Type: application/json"); - } - - if(requestType != NULL) { - curl_easy_setopt(handle, CURLOPT_CUSTOMREQUEST, - requestType); - } - - if(formParameters != NULL) { - if(strstr(buffContent, - "application/x-www-form-urlencoded") != NULL) - { - long parameterLength = 0; - long keyPairLength = 0; - list_ForEach(listEntry, formParameters) { - keyValuePair_t *keyPair = - listEntry->data; - - keyPairLength = - lengthOfKeyPair(keyPair) + 1; - - if(listEntry->nextListEntry != NULL) { - parameterLength++; - } - parameterLength = parameterLength + - keyPairLength; - } - - formString = malloc(parameterLength + 1); - memset(formString, 0, parameterLength + 1); - - list_ForEach(listEntry, formParameters) { - keyValuePair_t *keyPair = - listEntry->data; - if((keyPair->key != NULL) && - (keyPair->value != NULL) ) - { - strcat(formString, - keyPair->key); - strcat(formString, "="); - strcat(formString, - keyPair->value); - if(listEntry->nextListEntry != - NULL) - { - strcat(formString, "&"); - } - } - } - curl_easy_setopt(handle, CURLOPT_POSTFIELDS, - formString); - } - if(strstr(buffContent, "multipart/form-data") != NULL) { - mime = curl_mime_init(handle); - list_ForEach(listEntry, formParameters) { - keyValuePair_t *keyValuePair = - listEntry->data; - - if((keyValuePair->key != NULL) && - (keyValuePair->value != NULL) ) - { - curl_mimepart *part = - curl_mime_addpart(mime); - - curl_mime_name(part, - keyValuePair->key); - - - if(strcmp(keyValuePair->key, - "file") == 0) - { - printf("Size of fileVar - %p\n",fileVar); - memcpy(&fileVar, - keyValuePair->value, - sizeof(fileVar)); +void apiClient_invoke(apiClient_t *apiClient, + char *operationParameter, + list_t *queryParameters, + list_t *headerParameters, + list_t *formParameters, + list_t *headerType, + list_t *contentType, + char *bodyParameters, + char *requestType) { + CURL *handle = curl_easy_init(); + CURLcode res; + + if(handle) { + listEntry_t *listEntry; + curl_mime *mime = NULL; + struct curl_slist *headers = NULL; + char *buffContent = NULL; + char *buffHeader = NULL; + FileStruct *fileVar = NULL; + char *formString = NULL; + + if(headerType != NULL) { + list_ForEach(listEntry, headerType) { + if(strstr((char *) listEntry->data, + "xml") == NULL) + { + buffHeader = malloc(strlen( + "Accept: ") + + strlen((char *) + listEntry-> + data) + 1); + sprintf(buffHeader, "%s%s", "Accept: ", + (char *) listEntry->data); + headers = curl_slist_append(headers, + buffHeader); + free(buffHeader); + } + } + } + if(contentType != NULL) { + list_ForEach(listEntry, contentType) { + if(strstr((char *) listEntry->data, + "xml") == NULL) + { + buffContent = + malloc(strlen( + "Content-Type: ") + strlen( + (char *) + listEntry->data) + + 1); + sprintf(buffContent, "%s%s", + "Content-Type: ", + (char *) listEntry->data); + headers = curl_slist_append(headers, + buffContent); + } + } + } else { + headers = curl_slist_append(headers, + "Content-Type: application/json"); + } + + if(requestType != NULL) { + curl_easy_setopt(handle, CURLOPT_CUSTOMREQUEST, + requestType); + } + + if(formParameters != NULL) { + if(strstr(buffContent, + "application/x-www-form-urlencoded") != NULL) + { + long parameterLength = 0; + long keyPairLength = 0; + list_ForEach(listEntry, formParameters) { + keyValuePair_t *keyPair = + listEntry->data; + + keyPairLength = + lengthOfKeyPair(keyPair) + 1; + + if(listEntry->nextListEntry != NULL) { + parameterLength++; + } + parameterLength = parameterLength + + keyPairLength; + } + + formString = malloc(parameterLength + 1); + memset(formString, 0, parameterLength + 1); + + list_ForEach(listEntry, formParameters) { + keyValuePair_t *keyPair = + listEntry->data; + if((keyPair->key != NULL) && + (keyPair->value != NULL) ) + { + strcat(formString, + keyPair->key); + strcat(formString, "="); + strcat(formString, + keyPair->value); + if(listEntry->nextListEntry != + NULL) + { + strcat(formString, "&"); + } + } + } + curl_easy_setopt(handle, CURLOPT_POSTFIELDS, + formString); + } + if(strstr(buffContent, "multipart/form-data") != NULL) { + mime = curl_mime_init(handle); + list_ForEach(listEntry, formParameters) { + keyValuePair_t *keyValuePair = + listEntry->data; + + if((keyValuePair->key != NULL) && + (keyValuePair->value != NULL) ) + { + curl_mimepart *part = + curl_mime_addpart(mime); + + curl_mime_name(part, + keyValuePair->key); + + + if(strcmp(keyValuePair->key, + "file") == 0) + { + printf("Size of fileVar - %p\n",fileVar); + memcpy(&fileVar, + keyValuePair->value, + sizeof(fileVar)); printf("Size of fileVar1 - %p\n",fileVar); - curl_mime_data(part, - fileVar->fileData, - fileVar->fileSize); - curl_mime_filename(part, - "image.png"); - } else { - curl_mime_data(part, - keyValuePair->value, - CURL_ZERO_TERMINATED); - } - } - } - curl_easy_setopt(handle, CURLOPT_MIMEPOST, - mime); - } - } - - list_ForEach(listEntry, headerParameters) { - keyValuePair_t *keyValuePair = listEntry->data; - if((keyValuePair->key != NULL) && - (keyValuePair->value != NULL) ) - { - char *headerValueToWrite = assembleHeaderField( - keyValuePair->key, keyValuePair->value); - curl_slist_append(headers, headerValueToWrite); - free(headerValueToWrite); - } - } - // this would only be generated for apiKey authentication + curl_mime_data(part, + fileVar->fileData, + fileVar->fileSize); + curl_mime_filename(part, + "image.png"); + } else { + curl_mime_data(part, + keyValuePair->value, + CURL_ZERO_TERMINATED); + } + } + } + curl_easy_setopt(handle, CURLOPT_MIMEPOST, + mime); + } + } + + list_ForEach(listEntry, headerParameters) { + keyValuePair_t *keyValuePair = listEntry->data; + if((keyValuePair->key != NULL) && + (keyValuePair->value != NULL) ) + { + char *headerValueToWrite = assembleHeaderField( + keyValuePair->key, keyValuePair->value); + curl_slist_append(headers, headerValueToWrite); + free(headerValueToWrite); + } + } + // this would only be generated for apiKey authentication #ifdef API_KEY list_ForEach(listEntry, apiClient->apiKeys) { keyValuePair_t *apiKey = listEntry->data; @@ -303,21 +303,21 @@ void apiClient_invoke(apiClient_t *apiClient, } #endif // API_KEY - char *targetUrl = - assembleTargetUrl(apiClient->basePath, - operationParameter, - queryParameters); - - curl_easy_setopt(handle, CURLOPT_URL, targetUrl); - curl_easy_setopt(handle, - CURLOPT_WRITEFUNCTION, - writeDataCallback); - curl_easy_setopt(handle, - CURLOPT_WRITEDATA, - &apiClient->dataReceived); - curl_easy_setopt(handle, CURLOPT_HTTPHEADER, headers); - curl_easy_setopt(handle, CURLOPT_VERBOSE, 0); // to get curl debug msg 0: to disable, 1L:to enable - // this would only be generated for OAuth2 authentication + char *targetUrl = + assembleTargetUrl(apiClient->basePath, + operationParameter, + queryParameters); + + curl_easy_setopt(handle, CURLOPT_URL, targetUrl); + curl_easy_setopt(handle, + CURLOPT_WRITEFUNCTION, + writeDataCallback); + curl_easy_setopt(handle, + CURLOPT_WRITEDATA, + &apiClient->dataReceived); + curl_easy_setopt(handle, CURLOPT_HTTPHEADER, headers); + curl_easy_setopt(handle, CURLOPT_VERBOSE, 0); // to get curl debug msg 0: to disable, 1L:to enable + // this would only be generated for OAuth2 authentication #ifdef OAUTH2 if(apiClient->accessToken != NULL) { // curl_easy_setopt(handle, CURLOPT_HTTPAUTH, CURLAUTH_BEARER); @@ -387,64 +387,63 @@ void apiClient_invoke(apiClient_t *apiClient, free(formString); curl_mime_free(mime); } - } + } } size_t writeDataCallback(void *buffer, size_t size, size_t nmemb, void *userp) { - *(char **) userp = strdup(buffer); + *(char **) userp = strdup(buffer); - return size * nmemb; + return size * nmemb; } char *strReplace(char *orig, char *rep, char *with) { - char *result; // the return string - char *ins; // the next insert point - char *tmp; // varies - int lenRep; // length of rep (the string to remove) - int lenWith; // length of with (the string to replace rep with) - int lenFront; // distance between rep and end of last rep - int count; // number of replacements - - // sanity checks and initialization - if(!orig || - !rep) - { - return NULL; - } - lenRep = strlen(rep); - if(lenRep == 0) { - return NULL; // empty rep causes infinite loop during count - } - if(!with) { - with = ""; - } - lenWith = strlen(with); - - // count the number of replacements needed - ins = orig; - for(count = 0; tmp = strstr(ins, rep); ++count) { - ins = tmp + lenRep; - } - - tmp = result = malloc(strlen(orig) + (lenWith - lenRep) * count + 1); - - if(!result) { - return NULL; - } - char *originalPointer = orig; // copying original pointer to free the memory - // first time through the loop, all the variable are set correctly - // from here on, - // tmp points to the end of the result string - // ins points to the next occurrence of rep in orig - // orig points to the remainder of orig after "end of rep" - while(count--) { - ins = strstr(orig, rep); - lenFront = ins - orig; - tmp = strncpy(tmp, orig, lenFront) + lenFront; - tmp = strcpy(tmp, with) + lenWith; - orig += lenFront + lenRep; // move to next "end of rep" - } - strcpy(tmp, orig); - free(originalPointer); - return result; + char *result; // the return string + char *ins; // the next insert point + char *tmp; // varies + int lenRep; // length of rep (the string to remove) + int lenWith; // length of with (the string to replace rep with) + int lenFront; // distance between rep and end of last rep + int count; // number of replacements + + // sanity checks and initialization + if(!orig || !rep) + { + return NULL; + } + lenRep = strlen(rep); + if(lenRep == 0) { + return NULL; // empty rep causes infinite loop during count + } + if(!with) { + with = ""; + } + lenWith = strlen(with); + + // count the number of replacements needed + ins = orig; + for(count = 0; tmp = strstr(ins, rep); ++count) { + ins = tmp + lenRep; + } + + tmp = result = malloc(strlen(orig) + (lenWith - lenRep) * count + 1); + + if(!result) { + return NULL; + } + char *originalPointer = orig; // copying original pointer to free the memory + // first time through the loop, all the variable are set correctly + // from here on, + // tmp points to the end of the result string + // ins points to the next occurrence of rep in orig + // orig points to the remainder of orig after "end of rep" + while(count--) { + ins = strstr(orig, rep); + lenFront = ins - orig; + tmp = strncpy(tmp, orig, lenFront) + lenFront; + tmp = strcpy(tmp, with) + lenWith; + orig += lenFront + lenRep; // move to next "end of rep" + } + strcpy(tmp, orig); + free(originalPointer); + return result; } diff --git a/modules/openapi-generator/src/main/resources/C-libcurl/apiKey.c.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/apiKey.c.mustache index aa05f3dfa818..3ea47781ee29 100644 --- a/modules/openapi-generator/src/main/resources/C-libcurl/apiKey.c.mustache +++ b/modules/openapi-generator/src/main/resources/C-libcurl/apiKey.c.mustache @@ -3,12 +3,12 @@ #include "keyValuePair.h" keyValuePair_t *keyValuePair_create(char *key, char *value) { - keyValuePair_t *keyValuePair = malloc(sizeof(keyValuePair_t)); - keyValuePair->key = key; - keyValuePair->value = value; - return keyValuePair; + keyValuePair_t *keyValuePair = malloc(sizeof(keyValuePair_t)); + keyValuePair->key = key; + keyValuePair->value = value; + return keyValuePair; } void keyValuePair_free(keyValuePair_t *keyValuePair) { - free(keyValuePair); -} \ No newline at end of file + free(keyValuePair); +} diff --git a/modules/openapi-generator/src/main/resources/C-libcurl/api_pet_test.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/api_pet_test.mustache index 026b2fb8d617..13014042f80b 100644 --- a/modules/openapi-generator/src/main/resources/C-libcurl/api_pet_test.mustache +++ b/modules/openapi-generator/src/main/resources/C-libcurl/api_pet_test.mustache @@ -1,129 +1,128 @@ #include - #include - #include - #include - #include "apiClient.h" - #include "cJSON.h" - #include "pet.h" - #include "PetAPI.h" - #include "category.h" - #include "tag.h" - #include "keyValuePair.h" - - #define EXAMPLE_CATEGORY_NAME "Example Category" - #define EXAMPLE_CATEGORY_ID 5 - #define EXAMPLE_PET_NAME "Example Pet" - #define EXAMPLE_URL_1 "http://www.github.com" - #define EXAMPLE_URL_2 "http://www.gitter.im" - #define EXAMPLE_TAG_1_NAME "beautiful code" - #define EXAMPLE_TAG_2_NAME "at least I tried" - #define EXAMPLE_TAG_1_ID 1 - #define EXAMPLE_TAG_2_ID 542353 - #define EXAMPLE_PET_ID 1234 // Set to 0 to generate a new pet +#include +#include +#include +#include "apiClient.h" +#include "cJSON.h" +#include "pet.h" +#include "PetAPI.h" +#include "category.h" +#include "tag.h" +#include "keyValuePair.h" + +#define EXAMPLE_CATEGORY_NAME "Example Category" +#define EXAMPLE_CATEGORY_ID 5 +#define EXAMPLE_PET_NAME "Example Pet" +#define EXAMPLE_URL_1 "http://www.github.com" +#define EXAMPLE_URL_2 "http://www.gitter.im" +#define EXAMPLE_TAG_1_NAME "beautiful code" +#define EXAMPLE_TAG_2_NAME "at least I tried" +#define EXAMPLE_TAG_1_ID 1 +#define EXAMPLE_TAG_2_ID 542353 +#define EXAMPLE_PET_ID 1234 // Set to 0 to generate a new pet int main() { -// Add pet test - apiClient_t *apiClient = apiClient_create(); + // Add pet test + apiClient_t *apiClient = apiClient_create(); - char *categoryName = malloc(strlen(EXAMPLE_CATEGORY_NAME) + 1); - strcpy(categoryName, EXAMPLE_CATEGORY_NAME); + char *categoryName = malloc(strlen(EXAMPLE_CATEGORY_NAME) + 1); + strcpy(categoryName, EXAMPLE_CATEGORY_NAME); - category_t *category = - category_create(EXAMPLE_CATEGORY_ID, categoryName); + category_t *category = + category_create(EXAMPLE_CATEGORY_ID, categoryName); - char *petName = malloc(strlen(EXAMPLE_PET_NAME) + 1); - strcpy(petName, EXAMPLE_PET_NAME); + char *petName = malloc(strlen(EXAMPLE_PET_NAME) + 1); + strcpy(petName, EXAMPLE_PET_NAME); - char *exampleUrl1 = malloc(strlen(EXAMPLE_URL_1) + 1); - strcpy(exampleUrl1, EXAMPLE_URL_1); + char *exampleUrl1 = malloc(strlen(EXAMPLE_URL_1) + 1); + strcpy(exampleUrl1, EXAMPLE_URL_1); - char *exampleUrl2 = malloc(strlen(EXAMPLE_URL_2) + 1); - strcpy(exampleUrl2, EXAMPLE_URL_2); + char *exampleUrl2 = malloc(strlen(EXAMPLE_URL_2) + 1); + strcpy(exampleUrl2, EXAMPLE_URL_2); - list_t *photoUrls = list_create(); + list_t *photoUrls = list_create(); - list_addElement(photoUrls, exampleUrl1); - list_addElement(photoUrls, exampleUrl2); + list_addElement(photoUrls, exampleUrl1); + list_addElement(photoUrls, exampleUrl2); - char *exampleTag1Name = malloc(strlen(EXAMPLE_TAG_1_NAME) + 1); - strcpy(exampleTag1Name, EXAMPLE_TAG_1_NAME); - tag_t *exampleTag1 = tag_create(EXAMPLE_TAG_1_ID, exampleTag1Name); + char *exampleTag1Name = malloc(strlen(EXAMPLE_TAG_1_NAME) + 1); + strcpy(exampleTag1Name, EXAMPLE_TAG_1_NAME); + tag_t *exampleTag1 = tag_create(EXAMPLE_TAG_1_ID, exampleTag1Name); - char *exampleTag2Name = malloc(strlen(EXAMPLE_TAG_2_NAME) + 1); - strcpy(exampleTag2Name, EXAMPLE_TAG_2_NAME); - tag_t *exampleTag2 = tag_create(EXAMPLE_TAG_2_ID, exampleTag2Name); + char *exampleTag2Name = malloc(strlen(EXAMPLE_TAG_2_NAME) + 1); + strcpy(exampleTag2Name, EXAMPLE_TAG_2_NAME); + tag_t *exampleTag2 = tag_create(EXAMPLE_TAG_2_ID, exampleTag2Name); - list_t *tags = list_create(); + list_t *tags = list_create(); - list_addElement(tags, exampleTag1); - list_addElement(tags, exampleTag2); + list_addElement(tags, exampleTag1); + list_addElement(tags, exampleTag2); - status_e status = available; - pet_t *pet = - pet_create(EXAMPLE_PET_ID, - category, - petName, - photoUrls, - tags, - status); + status_e status = available; + pet_t *pet = + pet_create(EXAMPLE_PET_ID, + category, + petName, + photoUrls, + tags, + status); - PetAPI_addPet(apiClient, pet); - pet_free(pet); + PetAPI_addPet(apiClient, pet); + pet_free(pet); + //Pet update with form test + char *petName1 = "Rocky Handsome"; -//Pet update with form test - char *petName1 = "Rocky Handsome"; + char *petName2 = "sold"; - char *petName2 = "sold"; + apiClient_t *apiClient1 = apiClient_create(); + PetAPI_updatePetWithForm(apiClient1, EXAMPLE_PET_ID, petName1, + petName2); - apiClient_t *apiClient1 = apiClient_create(); - PetAPI_updatePetWithForm(apiClient1, EXAMPLE_PET_ID, petName1, - petName2); + //Get pet by id test + apiClient_t *apiClient2 = apiClient_create(); + pet_t *mypet = PetAPI_getPetById(apiClient2, EXAMPLE_PET_ID); -//Get pet by id test - apiClient_t *apiClient2 = apiClient_create(); - pet_t *mypet = PetAPI_getPetById(apiClient2, EXAMPLE_PET_ID); + cJSON *JSONR = pet_convertToJSON(mypet); + char *petJson = cJSON_Print(JSONR); + printf("Data is:%s\n", petJson); - cJSON *JSONR = pet_convertToJSON(mypet); - char *petJson = cJSON_Print(JSONR); - printf("Data is:%s\n", petJson); + assert(strcmp(mypet->name, "Rocky Handsome") == 0); + assert(mypet->id == EXAMPLE_PET_ID); + assert(strcmp(mypet->category->name, EXAMPLE_CATEGORY_NAME) == 0); + assert(mypet->category->id == EXAMPLE_CATEGORY_ID); + assert(strcmp(list_getElementAt(mypet->photoUrls, + 0)->data, EXAMPLE_URL_1) == 0); + assert(strcmp(list_getElementAt(mypet->photoUrls, + 1)->data, EXAMPLE_URL_2) == 0); + assert(((tag_t *) list_getElementAt(mypet->tags, + 0)->data)->id == EXAMPLE_TAG_1_ID); + assert(((tag_t *) list_getElementAt(mypet->tags, + 1)->data)->id == EXAMPLE_TAG_2_ID); + assert(strcmp(((tag_t *) list_getElementAt(mypet->tags, 0)->data)->name, + EXAMPLE_TAG_1_NAME) == 0); + assert(strcmp(((tag_t *) list_getElementAt(mypet->tags, 1)->data)->name, + EXAMPLE_TAG_2_NAME) == 0); - assert(strcmp(mypet->name, "Rocky Handsome") == 0); - assert(mypet->id == EXAMPLE_PET_ID); - assert(strcmp(mypet->category->name, EXAMPLE_CATEGORY_NAME) == 0); - assert(mypet->category->id == EXAMPLE_CATEGORY_ID); - assert(strcmp(list_getElementAt(mypet->photoUrls, - 0)->data, EXAMPLE_URL_1) == 0); - assert(strcmp(list_getElementAt(mypet->photoUrls, - 1)->data, EXAMPLE_URL_2) == 0); - assert(((tag_t *) list_getElementAt(mypet->tags, - 0)->data)->id == EXAMPLE_TAG_1_ID); - assert(((tag_t *) list_getElementAt(mypet->tags, - 1)->data)->id == EXAMPLE_TAG_2_ID); - assert(strcmp(((tag_t *) list_getElementAt(mypet->tags, 0)->data)->name, - EXAMPLE_TAG_1_NAME) == 0); - assert(strcmp(((tag_t *) list_getElementAt(mypet->tags, 1)->data)->name, - EXAMPLE_TAG_2_NAME) == 0); + free(petJson); + cJSON_Delete(JSONR); + pet_free(mypet); - free(petJson); - cJSON_Delete(JSONR); - pet_free(mypet); + //Pet upload file Test + apiClient_t *apiClient3 = apiClient_create(); + FILE *file = fopen("/opt/image.png", "r"); + if(file != NULL){ + api_response_t *respo = PetAPI_uploadFile(apiClient3, + EXAMPLE_PET_ID, + "dec", + file); -//Pet upload file Test - apiClient_t *apiClient3 = apiClient_create(); - FILE *file = fopen("/opt/image.png", "r"); - if(file != NULL){ - api_response_t *respo = PetAPI_uploadFile(apiClient3, - EXAMPLE_PET_ID, - "dec", - file); - - api_response_free(respo); - fclose(file); - } + api_response_free(respo); + fclose(file); + } } diff --git a/modules/openapi-generator/src/main/resources/C-libcurl/api_test.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/api_test.mustache index 3b8522636ff0..e228feac2862 100644 --- a/modules/openapi-generator/src/main/resources/C-libcurl/api_test.mustache +++ b/modules/openapi-generator/src/main/resources/C-libcurl/api_test.mustache @@ -1,19 +1,19 @@ #include - #define MAX_BUFFER_LENGTH 9 +#define MAX_BUFFER_LENGTH 9 - #include - #include - #include +#include +#include +#include - #include "apiClient.h" - #include "cJSON.h" - #include "pet.h" - #include "PetAPI.h" - #include "category.h" - #include "tag.h" +#include "apiClient.h" +#include "cJSON.h" +#include "pet.h" +#include "PetAPI.h" +#include "category.h" +#include "tag.h" - #include "keyValuePair.h" +#include "keyValuePair.h" int main() { printf("Hello world1\n"); diff --git a/modules/openapi-generator/src/main/resources/C-libcurl/list.c.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/list.c.mustache index 0d680d48ee4d..8a5e4de342ad 100644 --- a/modules/openapi-generator/src/main/resources/C-libcurl/list.c.mustache +++ b/modules/openapi-generator/src/main/resources/C-libcurl/list.c.mustache @@ -7,163 +7,163 @@ #include "tag.h" static listEntry_t *listEntry_create(void *data) { - listEntry_t *createdListEntry = malloc(sizeof(listEntry_t)); - if(createdListEntry == NULL) { - // TODO Malloc Failure - return NULL; - } - createdListEntry->data = data; - - return createdListEntry; + listEntry_t *createdListEntry = malloc(sizeof(listEntry_t)); + if(createdListEntry == NULL) { + // TODO Malloc Failure + return NULL; + } + createdListEntry->data = data; + + return createdListEntry; } void listEntry_free(listEntry_t *listEntry, void *additionalData) { - free(listEntry); + free(listEntry); } void listEntry_printAsInt(listEntry_t *listEntry, void *additionalData) { - printf("%i\n", *((int *) (listEntry->data))); + printf("%i\n", *((int *) (listEntry->data))); } list_t *list_create() { - list_t *createdList = malloc(sizeof(list_t)); - if(createdList == NULL) { - // TODO Malloc Failure - return NULL; - } - createdList->firstEntry = NULL; - createdList->lastEntry = NULL; - createdList->count = 0; - - return createdList; + list_t *createdList = malloc(sizeof(list_t)); + if(createdList == NULL) { + // TODO Malloc Failure + return NULL; + } + createdList->firstEntry = NULL; + createdList->lastEntry = NULL; + createdList->count = 0; + + return createdList; } void list_iterateThroughListForward(list_t *list, void (*operationToPerform)( - listEntry_t *, - void *callbackFunctionUsedData), + listEntry_t *, + void *callbackFunctionUsedData), void *additionalDataNeededForCallbackFunction) { - listEntry_t *currentListEntry = list->firstEntry; - listEntry_t *nextListEntry; + listEntry_t *currentListEntry = list->firstEntry; + listEntry_t *nextListEntry; - if(currentListEntry == NULL) { - return; - } + if(currentListEntry == NULL) { + return; + } - nextListEntry = currentListEntry->nextListEntry; + nextListEntry = currentListEntry->nextListEntry; - operationToPerform(currentListEntry, - additionalDataNeededForCallbackFunction); - currentListEntry = nextListEntry; + operationToPerform(currentListEntry, + additionalDataNeededForCallbackFunction); + currentListEntry = nextListEntry; - while(currentListEntry != NULL) { - nextListEntry = currentListEntry->nextListEntry; - operationToPerform(currentListEntry, - additionalDataNeededForCallbackFunction); - currentListEntry = nextListEntry; - } + while(currentListEntry != NULL) { + nextListEntry = currentListEntry->nextListEntry; + operationToPerform(currentListEntry, + additionalDataNeededForCallbackFunction); + currentListEntry = nextListEntry; + } } void list_iterateThroughListBackward(list_t *list, void (*operationToPerform)( - listEntry_t *, - void *callbackFunctionUsedData), + listEntry_t *, + void *callbackFunctionUsedData), void *additionalDataNeededForCallbackFunction) { - listEntry_t *currentListEntry = list->lastEntry; - listEntry_t *nextListEntry = currentListEntry->prevListEntry; - - if(currentListEntry == NULL) { - return; - } - - operationToPerform(currentListEntry, - additionalDataNeededForCallbackFunction); - currentListEntry = nextListEntry; - - while(currentListEntry != NULL) { - nextListEntry = currentListEntry->prevListEntry; - operationToPerform(currentListEntry, - additionalDataNeededForCallbackFunction); - currentListEntry = nextListEntry; - } + listEntry_t *currentListEntry = list->lastEntry; + listEntry_t *nextListEntry = currentListEntry->prevListEntry; + + if(currentListEntry == NULL) { + return; + } + + operationToPerform(currentListEntry, + additionalDataNeededForCallbackFunction); + currentListEntry = nextListEntry; + + while(currentListEntry != NULL) { + nextListEntry = currentListEntry->prevListEntry; + operationToPerform(currentListEntry, + additionalDataNeededForCallbackFunction); + currentListEntry = nextListEntry; + } } void list_free(list_t *list) { - list_iterateThroughListForward(list, listEntry_free, NULL); - free(list); + list_iterateThroughListForward(list, listEntry_free, NULL); + free(list); } void list_addElement(list_t *list, void *dataToAddInList) { - listEntry_t *newListEntry = listEntry_create(dataToAddInList); - if(newListEntry == NULL) { - // TODO Malloc Failure - return; - } - if(list->firstEntry == NULL) { - list->firstEntry = newListEntry; - list->lastEntry = newListEntry; + listEntry_t *newListEntry = listEntry_create(dataToAddInList); + if(newListEntry == NULL) { + // TODO Malloc Failure + return; + } + if(list->firstEntry == NULL) { + list->firstEntry = newListEntry; + list->lastEntry = newListEntry; - newListEntry->prevListEntry = NULL; - newListEntry->nextListEntry = NULL; + newListEntry->prevListEntry = NULL; + newListEntry->nextListEntry = NULL; - list->count++; + list->count++; - return; - } + return; + } - list->lastEntry->nextListEntry = newListEntry; - newListEntry->prevListEntry = list->lastEntry; - newListEntry->nextListEntry = NULL; - list->lastEntry = newListEntry; + list->lastEntry->nextListEntry = newListEntry; + newListEntry->prevListEntry = list->lastEntry; + newListEntry->nextListEntry = NULL; + list->lastEntry = newListEntry; - list->count++; + list->count++; } void list_removeElement(list_t *list, listEntry_t *elementToRemove) { - listEntry_t *elementBeforeElementToRemove = - elementToRemove->prevListEntry; - listEntry_t *elementAfterElementToRemove = - elementToRemove->nextListEntry; - - if(elementBeforeElementToRemove != NULL) { - elementBeforeElementToRemove->nextListEntry = - elementAfterElementToRemove; - } else { - list->firstEntry = elementAfterElementToRemove; - } - - if(elementAfterElementToRemove != NULL) { - elementAfterElementToRemove->prevListEntry = - elementBeforeElementToRemove; - } else { - list->lastEntry = elementBeforeElementToRemove; - } - - listEntry_free(elementToRemove, NULL); - - list->count--; + listEntry_t *elementBeforeElementToRemove = + elementToRemove->prevListEntry; + listEntry_t *elementAfterElementToRemove = + elementToRemove->nextListEntry; + + if(elementBeforeElementToRemove != NULL) { + elementBeforeElementToRemove->nextListEntry = + elementAfterElementToRemove; + } else { + list->firstEntry = elementAfterElementToRemove; + } + + if(elementAfterElementToRemove != NULL) { + elementAfterElementToRemove->prevListEntry = + elementBeforeElementToRemove; + } else { + list->lastEntry = elementBeforeElementToRemove; + } + + listEntry_free(elementToRemove, NULL); + + list->count--; } listEntry_t *list_getElementAt(list_t *list, long indexOfElement) { - listEntry_t *currentListEntry; + listEntry_t *currentListEntry; - if((list->count / 2) > indexOfElement) { - currentListEntry = list->firstEntry; + if((list->count / 2) > indexOfElement) { + currentListEntry = list->firstEntry; - for(int i = 0; i < indexOfElement; i++) { - currentListEntry = currentListEntry->nextListEntry; - } + for(int i = 0; i < indexOfElement; i++) { + currentListEntry = currentListEntry->nextListEntry; + } - return currentListEntry; - } else { - currentListEntry = list->lastEntry; + return currentListEntry; + } else { + currentListEntry = list->lastEntry; - for(int i = 1; i < (list->count - indexOfElement); i++) { - currentListEntry = currentListEntry->prevListEntry; - } + for(int i = 1; i < (list->count - indexOfElement); i++) { + currentListEntry = currentListEntry->prevListEntry; + } - return currentListEntry; - } -} \ No newline at end of file + return currentListEntry; + } +} 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 aeda56afee52..53a9cd629bbd 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 @@ -11,26 +11,25 @@ {{/imports}} {{#vars}} - {{#isEnum}} - char* {{baseName}}{{classname}}_ToString({{baseName}}_e {{baseName}}){ - char *{{baseName}}Array[] = { {{#allowableValues}}{{#enumVars}}"{{{value}}}"{{^-last}},{{/-last}}{{/enumVars}}{{/allowableValues}} }; - return {{baseName}}Array[{{baseName}}]; - - } +{{#isEnum}} +char* {{baseName}}{{classname}}_ToString({{baseName}}_e {{baseName}}){ + char *{{baseName}}Array[] = { {{#allowableValues}}{{#enumVars}}"{{{value}}}"{{^-last}},{{/-last}}{{/enumVars}}{{/allowableValues}} }; + return {{baseName}}Array[{{baseName}}]; +} - {{baseName}}_e {{baseName}}{{classname}}_FromString(char* {{baseName}}){ - int stringToReturn = 0; - char *{{baseName}}Array[] = { {{#allowableValues}}{{#enumVars}}"{{{value}}}"{{^-last}},{{/-last}}{{/enumVars}}{{/allowableValues}} }; - size_t sizeofArray = sizeof({{baseName}}Array) / sizeof({{baseName}}Array[0]); - while(stringToReturn < sizeofArray) { - if(strcmp({{baseName}}, {{baseName}}Array[stringToReturn]) == 0) { - return stringToReturn; - } - stringToReturn++; - } +{{baseName}}_e {{baseName}}{{classname}}_FromString(char* {{baseName}}){ + int stringToReturn = 0; + char *{{baseName}}Array[] = { {{#allowableValues}}{{#enumVars}}"{{{value}}}"{{^-last}},{{/-last}}{{/enumVars}}{{/allowableValues}} }; + size_t sizeofArray = sizeof({{baseName}}Array) / sizeof({{baseName}}Array[0]); + while(stringToReturn < sizeofArray) { + if(strcmp({{baseName}}, {{baseName}}Array[stringToReturn]) == 0) { + return stringToReturn; } - {{/isEnum}} + stringToReturn++; + } +} +{{/isEnum}} {{/vars}} {{classname}}_t *{{classname}}_create( @@ -222,7 +221,7 @@ fail: {{classname}}_t *{{classname}}_parseFromJSON(char *jsonString){ {{classname}}_t *{{classname}} = NULL; - cJSON *{{classname}}JSON = cJSON_Parse(jsonString); + cJSON *{{classname}}JSON = cJSON_Parse(jsonString); if({{classname}}JSON == NULL){ const char *error_ptr = cJSON_GetErrorPtr(); if (error_ptr != NULL) { 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 d514f24c064a..b94106ca8ef8 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 @@ -76,7 +76,7 @@ typedef struct {{classname}}_t { {{/isListContainer}} {{#isMapContainer}} {{datatype}} *{{baseName}}; -// TODO map/hash not supported at the moment + // TODO map/hash not supported at the moment {{/isMapContainer}} {{/isContainer}} {{/vars}} @@ -134,7 +134,5 @@ void {{classname}}_free({{classname}}_t *{{classname}}); cJSON *{{classname}}_convertToJSON({{classname}}_t *{{classname}}); - - #endif /* _{{classname}}_H_ */ {{/model}}{{/models}} diff --git a/samples/client/petstore/c/api/PetAPI.c b/samples/client/petstore/c/api/PetAPI.c index 0f7da44c321f..8aa0d79dadf2 100644 --- a/samples/client/petstore/c/api/PetAPI.c +++ b/samples/client/petstore/c/api/PetAPI.c @@ -14,8 +14,6 @@ snprintf(dst, 256, "%ld", (long int) (src)); \ } while(0) - - // Add a new pet to the store // void PetAPI_addPet(apiClient_t *apiClient, pet_t *pet) { @@ -40,11 +38,8 @@ void PetAPI_addPet(apiClient_t *apiClient, pet_t *pet) { localVarBodyParameters = cJSON_Print(localVarSingleItemJSON_pet); } - list_addElement(localVarContentType, "application/json"); // consumes - list_addElement(localVarContentType, "application/xml"); // consumes - apiClient_invoke(apiClient, localVarPath, localVarQueryParameters, @@ -115,7 +110,6 @@ void PetAPI_deletePet(apiClient_t *apiClient, long petId, char *api_key) { list_addElement(localVarHeaderParameters, keyPairHeader_api_key); } - apiClient_invoke(apiClient, localVarPath, localVarQueryParameters, @@ -166,11 +160,8 @@ list_t *PetAPI_findPetsByStatus(apiClient_t *apiClient, list_t *status) { // listContainer localVarQueryParameters = status; } - list_addElement(localVarHeaderType, "application/xml"); // produces - list_addElement(localVarHeaderType, "application/json"); // produces - apiClient_invoke(apiClient, localVarPath, localVarQueryParameters, @@ -240,11 +231,8 @@ list_t *PetAPI_findPetsByTags(apiClient_t *apiClient, list_t *tags) { // listContainer localVarQueryParameters = tags; } - list_addElement(localVarHeaderType, "application/xml"); // produces - list_addElement(localVarHeaderType, "application/json"); // produces - apiClient_invoke(apiClient, localVarPath, localVarQueryParameters, @@ -325,11 +313,8 @@ pet_t *PetAPI_getPetById(apiClient_t *apiClient, long petId) { localVarPath = strReplace(localVarPath, localVarToReplace, localVarBuff); - list_addElement(localVarHeaderType, "application/xml"); // produces - list_addElement(localVarHeaderType, "application/json"); // produces - apiClient_invoke(apiClient, localVarPath, localVarQueryParameters, @@ -393,11 +378,8 @@ void PetAPI_updatePet(apiClient_t *apiClient, pet_t *pet) { localVarBodyParameters = cJSON_Print(localVarSingleItemJSON_pet); } - list_addElement(localVarContentType, "application/json"); // consumes - list_addElement(localVarContentType, "application/xml"); // consumes - apiClient_invoke(apiClient, localVarPath, localVarQueryParameters, @@ -486,10 +468,8 @@ void PetAPI_updatePetWithForm(apiClient_t *apiClient, long petId, char *name, valueForm_status); list_addElement(localVarFormParameters, keyPairForm_status); // String } - list_addElement(localVarContentType, - "application/x-www-form-urlencoded"); // consumes - + "application/x-www-form-urlencoded"); // consumes apiClient_invoke(apiClient, localVarPath, localVarQueryParameters, @@ -592,11 +572,8 @@ api_response_t *PetAPI_uploadFile(apiClient_t *apiClient, long petId, keyPairForm_file = keyValuePair_create(keyForm_file, valueFile); list_addElement(localVarFormParameters, keyPairForm_file); // file adding } - list_addElement(localVarHeaderType, "application/json"); // produces - list_addElement(localVarContentType, "multipart/form-data"); // consumes - apiClient_invoke(apiClient, localVarPath, localVarQueryParameters, diff --git a/samples/client/petstore/c/api/StoreAPI.c b/samples/client/petstore/c/api/StoreAPI.c index 791b8f6e9531..be9101a4b2b8 100644 --- a/samples/client/petstore/c/api/StoreAPI.c +++ b/samples/client/petstore/c/api/StoreAPI.c @@ -13,8 +13,6 @@ 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 @@ -43,7 +41,6 @@ void StoreAPI_deleteOrder(apiClient_t *apiClient, char *orderId) { sprintf(localVarToReplace, "%s%s%s", "{", "orderId", "}"); localVarPath = strReplace(localVarPath, localVarToReplace, orderId); - apiClient_invoke(apiClient, localVarPath, localVarQueryParameters, @@ -88,9 +85,7 @@ list_t *StoreAPI_getInventory(apiClient_t *apiClient) { char *localVarPath = malloc(sizeOfPath); snprintf(localVarPath, sizeOfPath, "/store/inventory"); - list_addElement(localVarHeaderType, "application/json"); // produces - apiClient_invoke(apiClient, localVarPath, localVarQueryParameters, @@ -162,11 +157,8 @@ order_t *StoreAPI_getOrderById(apiClient_t *apiClient, long orderId) { localVarPath = strReplace(localVarPath, localVarToReplace, localVarBuff); - list_addElement(localVarHeaderType, "application/xml"); // produces - list_addElement(localVarHeaderType, "application/json"); // produces - apiClient_invoke(apiClient, localVarPath, localVarQueryParameters, @@ -230,11 +222,8 @@ order_t *StoreAPI_placeOrder(apiClient_t *apiClient, order_t *order) { localVarBodyParameters = cJSON_Print( localVarSingleItemJSON_order); } - list_addElement(localVarHeaderType, "application/xml"); // produces - list_addElement(localVarHeaderType, "application/json"); // produces - apiClient_invoke(apiClient, localVarPath, localVarQueryParameters, diff --git a/samples/client/petstore/c/api/UserAPI.c b/samples/client/petstore/c/api/UserAPI.c index c5fc3f72769e..ec76ce6ff9a6 100644 --- a/samples/client/petstore/c/api/UserAPI.c +++ b/samples/client/petstore/c/api/UserAPI.c @@ -14,8 +14,6 @@ snprintf(dst, 256, "%ld", (long int) (src)); \ } while(0) - - // Create user // // This can only be done by the logged in user. @@ -42,7 +40,6 @@ void UserAPI_createUser(apiClient_t *apiClient, user_t *user) { localVarBodyParameters = cJSON_Print(localVarSingleItemJSON_user); } - apiClient_invoke(apiClient, localVarPath, localVarQueryParameters, @@ -121,7 +118,6 @@ void UserAPI_createUsersWithArrayInput(apiClient_t *apiClient, list_t *user) { localVarBodyParameters = cJSON_Print(localVarItemJSON_user); } - apiClient_invoke(apiClient, localVarPath, localVarQueryParameters, @@ -202,7 +198,6 @@ void UserAPI_createUsersWithListInput(apiClient_t *apiClient, list_t *user) { localVarBodyParameters = cJSON_Print(localVarItemJSON_user); } - apiClient_invoke(apiClient, localVarPath, localVarQueryParameters, @@ -258,7 +253,6 @@ void UserAPI_deleteUser(apiClient_t *apiClient, char *username) { sprintf(localVarToReplace, "%s%s%s", "{", "username", "}"); localVarPath = strReplace(localVarPath, localVarToReplace, username); - apiClient_invoke(apiClient, localVarPath, localVarQueryParameters, @@ -312,11 +306,8 @@ user_t *UserAPI_getUserByName(apiClient_t *apiClient, char *username) { sprintf(localVarToReplace, "%s%s%s", "{", "username", "}"); localVarPath = strReplace(localVarPath, localVarToReplace, username); - list_addElement(localVarHeaderType, "application/xml"); // produces - list_addElement(localVarHeaderType, "application/json"); // produces - apiClient_invoke(apiClient, localVarPath, localVarQueryParameters, @@ -398,11 +389,8 @@ char *UserAPI_loginUser(apiClient_t *apiClient, char *username, valueQuery_password); list_addElement(localVarQueryParameters, keyPairQuery_password); } - list_addElement(localVarHeaderType, "application/xml"); // produces - list_addElement(localVarHeaderType, "application/json"); // produces - apiClient_invoke(apiClient, localVarPath, localVarQueryParameters, @@ -455,7 +443,6 @@ void UserAPI_logoutUser(apiClient_t *apiClient) { char *localVarPath = malloc(sizeOfPath); snprintf(localVarPath, sizeOfPath, "/user/logout"); - apiClient_invoke(apiClient, localVarPath, localVarQueryParameters, @@ -516,7 +503,6 @@ void UserAPI_updateUser(apiClient_t *apiClient, char *username, user_t *user) { localVarBodyParameters = cJSON_Print(localVarSingleItemJSON_user); } - apiClient_invoke(apiClient, localVarPath, localVarQueryParameters, diff --git a/samples/client/petstore/c/model/api_response.h b/samples/client/petstore/c/model/api_response.h index fca00fa5f3cb..96eddce14304 100644 --- a/samples/client/petstore/c/model/api_response.h +++ b/samples/client/petstore/c/model/api_response.h @@ -27,6 +27,4 @@ api_response_t *api_response_parseFromJSON(char *jsonString); cJSON *api_response_convertToJSON(api_response_t *api_response); - - #endif /* _api_response_H_ */ diff --git a/samples/client/petstore/c/model/category.h b/samples/client/petstore/c/model/category.h index 3c5de5f6743e..46dce4ac2b30 100644 --- a/samples/client/petstore/c/model/category.h +++ b/samples/client/petstore/c/model/category.h @@ -26,6 +26,4 @@ category_t *category_parseFromJSON(char *jsonString); cJSON *category_convertToJSON(category_t *category); - - #endif /* _category_H_ */ diff --git a/samples/client/petstore/c/model/order.c b/samples/client/petstore/c/model/order.c index 2687d485a1d7..3e5c053b6487 100644 --- a/samples/client/petstore/c/model/order.c +++ b/samples/client/petstore/c/model/order.c @@ -10,7 +10,6 @@ char *statusorder_ToString(status_e status) { return statusArray[status]; } - status_e statusorder_FromString(char *status) { int stringToReturn = 0; char *statusArray[] = { "placed", "approved", "delivered" }; diff --git a/samples/client/petstore/c/model/order.h b/samples/client/petstore/c/model/order.h index e49144e920fd..14008cc0eb42 100644 --- a/samples/client/petstore/c/model/order.h +++ b/samples/client/petstore/c/model/order.h @@ -38,6 +38,4 @@ order_t *order_parseFromJSON(char *jsonString); cJSON *order_convertToJSON(order_t *order); - - #endif /* _order_H_ */ diff --git a/samples/client/petstore/c/model/pet.c b/samples/client/petstore/c/model/pet.c index d5fd2bbc2779..4ffd61348148 100644 --- a/samples/client/petstore/c/model/pet.c +++ b/samples/client/petstore/c/model/pet.c @@ -13,7 +13,6 @@ char *statuspet_ToString(status_e status) { return statusArray[status]; } - status_e statuspet_FromString(char *status) { int stringToReturn = 0; char *statusArray[] = { "available", "pending", "sold" }; diff --git a/samples/client/petstore/c/model/pet.h b/samples/client/petstore/c/model/pet.h index 28b3c8956232..275a8415f7c1 100644 --- a/samples/client/petstore/c/model/pet.h +++ b/samples/client/petstore/c/model/pet.h @@ -38,6 +38,4 @@ pet_t *pet_parseFromJSON(char *jsonString); cJSON *pet_convertToJSON(pet_t *pet); - - #endif /* _pet_H_ */ diff --git a/samples/client/petstore/c/model/tag.h b/samples/client/petstore/c/model/tag.h index 48fa09f473fa..403c2dd0af4a 100644 --- a/samples/client/petstore/c/model/tag.h +++ b/samples/client/petstore/c/model/tag.h @@ -26,6 +26,4 @@ tag_t *tag_parseFromJSON(char *jsonString); cJSON *tag_convertToJSON(tag_t *tag); - - #endif /* _tag_H_ */ diff --git a/samples/client/petstore/c/model/user.h b/samples/client/petstore/c/model/user.h index 6784cc86f1b7..e6dd8e1914e2 100644 --- a/samples/client/petstore/c/model/user.h +++ b/samples/client/petstore/c/model/user.h @@ -33,6 +33,4 @@ user_t *user_parseFromJSON(char *jsonString); cJSON *user_convertToJSON(user_t *user); - - #endif /* _user_H_ */ diff --git a/samples/client/petstore/c/src/apiClient.c b/samples/client/petstore/c/src/apiClient.c index f2b5bc0b649f..28dce049de9b 100644 --- a/samples/client/petstore/c/src/apiClient.c +++ b/samples/client/petstore/c/src/apiClient.c @@ -13,13 +13,13 @@ apiClient_t *apiClient_create() { apiClient->basePath = "http://petstore.swagger.io:80/v2"; apiClient->dataReceived = NULL; apiClient->response_code = 0; - #ifdef BASIC_AUTH + #ifdef BASIC_AUTH apiClient->username = NULL; apiClient->password = NULL; - #endif // BASIC_AUTH - #ifdef OAUTH2 + #endif // BASIC_AUTH + #ifdef OAUTH2 apiClient->accessToken = NULL; - #endif // OAUTH2 + #endif // OAUTH2 return apiClient; } diff --git a/samples/client/petstore/c/src/apiKey.c b/samples/client/petstore/c/src/apiKey.c index 1cb45bcb85bc..97fd37f0951f 100644 --- a/samples/client/petstore/c/src/apiKey.c +++ b/samples/client/petstore/c/src/apiKey.c @@ -11,4 +11,4 @@ keyValuePair_t *keyValuePair_create(char *key, char *value) { void keyValuePair_free(keyValuePair_t *keyValuePair) { free(keyValuePair); -} \ No newline at end of file +} diff --git a/samples/client/petstore/c/src/list.c b/samples/client/petstore/c/src/list.c index 710c720b21bc..feea1dc3e97f 100644 --- a/samples/client/petstore/c/src/list.c +++ b/samples/client/petstore/c/src/list.c @@ -164,4 +164,4 @@ listEntry_t *list_getElementAt(list_t *list, long indexOfElement) { return currentListEntry; } -} \ No newline at end of file +} From 389477c16244e839212e52745b1f496ec46dc201 Mon Sep 17 00:00:00 2001 From: William Cheng Date: Thu, 11 Oct 2018 17:19:46 +0800 Subject: [PATCH 2/2] minor format fix --- .../codegen/languages/CLibcurlClientCodegen.java | 13 +++---------- 1 file changed, 3 insertions(+), 10 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 7a0a5953ff82..4db8ae65e17d 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 @@ -152,7 +152,7 @@ public CLibcurlClientCodegen() { typeMapping.put("array", "list"); typeMapping.put("map", "list_t*"); typeMapping.put("date-time", "char"); - + // remove modelPackage and apiPackage added by default Iterator itr = cliOptions.iterator(); while (itr.hasNext()) { @@ -202,13 +202,6 @@ public void processOpts() { supportingFiles.add(new SupportingFile("cJSON.c.mustache", "external" + File.separator + "src", "cJSON.c")); supportingFiles.add(new SupportingFile("cJSON.h.mustache", "external" + File.separator + "include", "cJSON.h")); - // test files should not be overwritten - writeOptional(outputFolder, new SupportingFile("api_store_test.mustache", "unit-tests", "manual-StoreAPI.c")); - writeOptional(outputFolder, new SupportingFile("api_user_test.mustache", "unit-tests", "manual-UserAPI.c")); - writeOptional(outputFolder, new SupportingFile("api_pet_test.mustache", "unit-tests", "manual-PetAPI.c")); - writeOptional(outputFolder, new SupportingFile("model_order_test.mustache", "unit-tests", "manual-order.c")); - writeOptional(outputFolder, new SupportingFile("model_user_test.mustache", "unit-tests", "manual-user.c")); - //writeOptional(outputFolder, new SupportingFile("apiKey.c.mustache", "unit-tests", "apiKey.c")); } @Override @@ -418,7 +411,7 @@ public String toEnumValue(String value, String datatype) { if ("Integer".equals(datatype) || "Float".equals(datatype)) { return value; } else { - return escapeText(value) ; + return escapeText(value); } } @@ -498,7 +491,7 @@ public String toOperationId(String operationId) { public String toApiImport(String name) { return apiPackage() + "/" + toApiFilename(name); } - + @Override public String toModelImport(String name) { return "#include \"" + name + ".h\"";