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 2bf8fc3e9d06..eae9b75f3b47 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 @@ -9,6 +9,12 @@ keyValuePair_t *keyValuePair_create(char *key, void *value) { return keyValuePair; } +keyValuePair_t* keyValuePair_create_allocate(char* key, double value) { + double* boolpointer = malloc(sizeof(value)); + memcpy(boolpointer, &value, sizeof(value)); + return keyValuePair_create(key, boolpointer); +} + void keyValuePair_free(keyValuePair_t *keyValuePair) { free(keyValuePair); } diff --git a/modules/openapi-generator/src/main/resources/C-libcurl/keyValuePair.h.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/keyValuePair.h.mustache index 90f92e71f66b..cb839f29cdcf 100644 --- a/modules/openapi-generator/src/main/resources/C-libcurl/keyValuePair.h.mustache +++ b/modules/openapi-generator/src/main/resources/C-libcurl/keyValuePair.h.mustache @@ -10,6 +10,8 @@ typedef struct keyValuePair_t { keyValuePair_t *keyValuePair_create(char *key, void *value); +keyValuePair_t* keyValuePair_create_allocate(char* key, double value); + void keyValuePair_free(keyValuePair_t *keyValuePair); #endif /* _keyValuePair_H_ */ \ No newline at end of file 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 fb4f691615e8..c2cd285c9663 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 @@ -423,7 +423,7 @@ cJSON *{{classname}}_convertToJSON({{classname}}_t *{{classname}}) { if({{{name}}} == NULL) { goto fail; //primitive map container } - cJSON *localMapObject = cJSON_CreateObject(); //Memory free to be implemented in user code + cJSON *localMapObject = {{{name}}}; listEntry_t *{{{name}}}ListEntry; if ({{{classname}}}->{{{name}}}) { list_ForEach({{{name}}}ListEntry, {{{classname}}}->{{{name}}}) { @@ -442,7 +442,6 @@ cJSON *{{classname}}_convertToJSON({{classname}}_t *{{classname}}) { } {{/isString}} {{/items}} - cJSON_AddItemToObject({{{name}}},"", localMapObject); } } {{/isMapContainer}} @@ -643,22 +642,24 @@ fail: keyValuePair_t *localMapKeyPair; cJSON_ArrayForEach({{{name}}}_local_map, {{{name}}}) { + cJSON *localMapObject = {{{name}}}_local_map; + {{#items}} {{#isString}} - if(!cJSON_IsString({{{name}}}_local_map)) + if(!cJSON_IsString(localMapObject)) { goto end; } - localMapKeyPair = keyValuePair_create(strdup({{{name}}}_local_map->string),strdup({{{name}}}_local_map->valuestring)) - list_addElement({{{name}}}List , localMapKeyPair); + localMapKeyPair = keyValuePair_create(strdup(localMapObject->string),strdup(localMapObject->valuestring)); {{/isString}} {{^isString}} - if(!cJSON_IsNumber({{{name}}}_local_map)) + if(!cJSON_IsNumber(localMapObject)) { goto end; } - localMapKeyPair = keyValuePair_create(strdup({{{name}}}_local_map->string),&{{{name}}}_local_map->valuedouble ); - list_addElement({{{name}}}List , localMapKeyPair); + localMapKeyPair = keyValuePair_create(strdup(localMapObject->string),&localMapObject->valuedouble ); {{/isString}} + {{/items}} + list_addElement({{{name}}}List , localMapKeyPair); } {{/isMapContainer}} {{/isContainer}} diff --git a/samples/client/petstore/c/.openapi-generator/VERSION b/samples/client/petstore/c/.openapi-generator/VERSION index e4955748d3e7..bfbf77eb7fad 100644 --- a/samples/client/petstore/c/.openapi-generator/VERSION +++ b/samples/client/petstore/c/.openapi-generator/VERSION @@ -1 +1 @@ -4.2.2-SNAPSHOT \ No newline at end of file +4.3.0-SNAPSHOT \ No newline at end of file diff --git a/samples/client/petstore/c/api/UserAPI.c b/samples/client/petstore/c/api/UserAPI.c index 1656b4472bcd..28f8c8b8c935 100644 --- a/samples/client/petstore/c/api/UserAPI.c +++ b/samples/client/petstore/c/api/UserAPI.c @@ -386,8 +386,8 @@ UserAPI_loginUser(apiClient_t *apiClient ,char * username ,char * password) // query parameters - char *keyQuery_username; - char * valueQuery_username; + char *keyQuery_username = NULL; + char * valueQuery_username = NULL; keyValuePair_t *keyPairQuery_username = 0; if (username) { @@ -398,8 +398,8 @@ UserAPI_loginUser(apiClient_t *apiClient ,char * username ,char * password) } // query parameters - char *keyQuery_password; - char * valueQuery_password; + char *keyQuery_password = NULL; + char * valueQuery_password = NULL; keyValuePair_t *keyPairQuery_password = 0; if (password) { @@ -438,12 +438,30 @@ UserAPI_loginUser(apiClient_t *apiClient ,char * username ,char * password) list_free(localVarHeaderType); free(localVarPath); - free(keyQuery_username); - free(valueQuery_username); - keyValuePair_free(keyPairQuery_username); - free(keyQuery_password); - free(valueQuery_password); - keyValuePair_free(keyPairQuery_password); + if(keyQuery_username){ + free(keyQuery_username); + keyQuery_username = NULL; + } + if(valueQuery_username){ + free(valueQuery_username); + valueQuery_username = NULL; + } + if(keyPairQuery_username){ + keyValuePair_free(keyPairQuery_username); + keyPairQuery_username = NULL; + } + if(keyQuery_password){ + free(keyQuery_password); + keyQuery_password = NULL; + } + if(valueQuery_password){ + free(valueQuery_password); + valueQuery_password = NULL; + } + if(keyPairQuery_password){ + keyValuePair_free(keyPairQuery_password); + keyPairQuery_password = NULL; + } return elementToReturn; end: return NULL; diff --git a/samples/client/petstore/c/include/apiClient.h b/samples/client/petstore/c/include/apiClient.h index aea3ec93738c..cf6687f6c271 100644 --- a/samples/client/petstore/c/include/apiClient.h +++ b/samples/client/petstore/c/include/apiClient.h @@ -11,6 +11,7 @@ typedef struct apiClient_t { char *basePath; + char *caPath; void *dataReceived; long response_code; list_t *apiKeys; @@ -25,6 +26,11 @@ typedef struct binary_t apiClient_t* apiClient_create(); +apiClient_t* apiClient_create_with_base_path(const char *basePath +, const char *caPath +, list_t *apiKeys +); + void apiClient_free(apiClient_t *apiClient); 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); diff --git a/samples/client/petstore/c/include/keyValuePair.h b/samples/client/petstore/c/include/keyValuePair.h index 90f92e71f66b..cb839f29cdcf 100644 --- a/samples/client/petstore/c/include/keyValuePair.h +++ b/samples/client/petstore/c/include/keyValuePair.h @@ -10,6 +10,8 @@ typedef struct keyValuePair_t { keyValuePair_t *keyValuePair_create(char *key, void *value); +keyValuePair_t* keyValuePair_create_allocate(char* key, double value); + void keyValuePair_free(keyValuePair_t *keyValuePair); #endif /* _keyValuePair_H_ */ \ No newline at end of file diff --git a/samples/client/petstore/c/model/order.c b/samples/client/petstore/c/model/order.c index 5aafbecba359..c4b866541183 100644 --- a/samples/client/petstore/c/model/order.c +++ b/samples/client/petstore/c/model/order.c @@ -24,9 +24,9 @@ order_t *order_create( long id, - long petId, + long pet_id, int quantity, - char *shipDate, + char *ship_date, status_e status, int complete ) { @@ -35,9 +35,9 @@ order_t *order_create( return NULL; } order_local_var->id = id; - order_local_var->petId = petId; + order_local_var->pet_id = pet_id; order_local_var->quantity = quantity; - order_local_var->shipDate = shipDate; + order_local_var->ship_date = ship_date; order_local_var->status = status; order_local_var->complete = complete; @@ -47,7 +47,7 @@ order_t *order_create( void order_free(order_t *order) { listEntry_t *listEntry; - free(order->shipDate); + free(order->ship_date); free(order); } @@ -62,9 +62,9 @@ cJSON *order_convertToJSON(order_t *order) { } - // order->petId - if(order->petId) { - if(cJSON_AddNumberToObject(item, "petId", order->petId) == NULL) { + // order->pet_id + if(order->pet_id) { + if(cJSON_AddNumberToObject(item, "petId", order->pet_id) == NULL) { goto fail; //Numeric } } @@ -78,9 +78,9 @@ cJSON *order_convertToJSON(order_t *order) { } - // order->shipDate - if(order->shipDate) { - if(cJSON_AddStringToObject(item, "shipDate", order->shipDate) == NULL) { + // order->ship_date + if(order->ship_date) { + if(cJSON_AddStringToObject(item, "shipDate", order->ship_date) == NULL) { goto fail; //Date-Time } } @@ -123,10 +123,10 @@ order_t *order_parseFromJSON(cJSON *orderJSON){ } } - // order->petId - cJSON *petId = cJSON_GetObjectItemCaseSensitive(orderJSON, "petId"); - if (petId) { - if(!cJSON_IsNumber(petId)) + // order->pet_id + cJSON *pet_id = cJSON_GetObjectItemCaseSensitive(orderJSON, "petId"); + if (pet_id) { + if(!cJSON_IsNumber(pet_id)) { goto end; //Numeric } @@ -141,10 +141,10 @@ order_t *order_parseFromJSON(cJSON *orderJSON){ } } - // order->shipDate - cJSON *shipDate = cJSON_GetObjectItemCaseSensitive(orderJSON, "shipDate"); - if (shipDate) { - if(!cJSON_IsString(shipDate)) + // order->ship_date + cJSON *ship_date = cJSON_GetObjectItemCaseSensitive(orderJSON, "shipDate"); + if (ship_date) { + if(!cJSON_IsString(ship_date)) { goto end; //DateTime } @@ -173,9 +173,9 @@ order_t *order_parseFromJSON(cJSON *orderJSON){ order_local_var = order_create ( id ? id->valuedouble : 0, - petId ? petId->valuedouble : 0, + pet_id ? pet_id->valuedouble : 0, quantity ? quantity->valuedouble : 0, - shipDate ? strdup(shipDate->valuestring) : NULL, + ship_date ? strdup(ship_date->valuestring) : NULL, status ? statusVariable : -1, complete ? complete->valueint : 0 ); diff --git a/samples/client/petstore/c/model/order.h b/samples/client/petstore/c/model/order.h index e7f814e1fa13..35458e07df5f 100644 --- a/samples/client/petstore/c/model/order.h +++ b/samples/client/petstore/c/model/order.h @@ -21,9 +21,9 @@ typedef struct order_t { long id; //numeric - long petId; //numeric + long pet_id; //numeric int quantity; //numeric - char *shipDate; //date time + char *ship_date; //date time status_e status; //enum int complete; //boolean @@ -31,9 +31,9 @@ typedef struct order_t { order_t *order_create( long id, - long petId, + long pet_id, int quantity, - char *shipDate, + char *ship_date, status_e status, int complete ); diff --git a/samples/client/petstore/c/model/pet.c b/samples/client/petstore/c/model/pet.c index b25b785cc6be..af321141f7d7 100644 --- a/samples/client/petstore/c/model/pet.c +++ b/samples/client/petstore/c/model/pet.c @@ -26,7 +26,7 @@ pet_t *pet_create( long id, category_t *category, char *name, - list_t *photoUrls, + list_t *photo_urls, list_t *tags, status_e status ) { @@ -37,7 +37,7 @@ pet_t *pet_create( pet_local_var->id = id; pet_local_var->category = category; pet_local_var->name = name; - pet_local_var->photoUrls = photoUrls; + pet_local_var->photo_urls = photo_urls; pet_local_var->tags = tags; pet_local_var->status = status; @@ -49,10 +49,10 @@ void pet_free(pet_t *pet) { listEntry_t *listEntry; category_free(pet->category); free(pet->name); - list_ForEach(listEntry, pet->photoUrls) { + list_ForEach(listEntry, pet->photo_urls) { free(listEntry->data); } - list_free(pet->photoUrls); + list_free(pet->photo_urls); list_ForEach(listEntry, pet->tags) { tag_free(listEntry->data); } @@ -94,8 +94,8 @@ cJSON *pet_convertToJSON(pet_t *pet) { } - // pet->photoUrls - if (!pet->photoUrls) { + // pet->photo_urls + if (!pet->photo_urls) { goto fail; } @@ -105,7 +105,7 @@ cJSON *pet_convertToJSON(pet_t *pet) { } listEntry_t *photo_urlsListEntry; - list_ForEach(photo_urlsListEntry, pet->photoUrls) { + list_ForEach(photo_urlsListEntry, pet->photo_urls) { if(cJSON_AddStringToObject(photo_urls, "", (char*)photo_urlsListEntry->data) == NULL) { goto fail; @@ -181,21 +181,21 @@ pet_t *pet_parseFromJSON(cJSON *petJSON){ goto end; //String } - // pet->photoUrls - cJSON *photoUrls = cJSON_GetObjectItemCaseSensitive(petJSON, "photoUrls"); - if (!photoUrls) { + // pet->photo_urls + cJSON *photo_urls = cJSON_GetObjectItemCaseSensitive(petJSON, "photoUrls"); + if (!photo_urls) { goto end; } list_t *photo_urlsList; cJSON *photo_urls_local; - if(!cJSON_IsArray(photoUrls)) { + if(!cJSON_IsArray(photo_urls)) { goto end;//primitive container } photo_urlsList = list_create(); - cJSON_ArrayForEach(photo_urls_local, photoUrls) + cJSON_ArrayForEach(photo_urls_local, photo_urls) { if(!cJSON_IsString(photo_urls_local)) { diff --git a/samples/client/petstore/c/model/pet.h b/samples/client/petstore/c/model/pet.h index a29280648d88..e8c0aac854d0 100644 --- a/samples/client/petstore/c/model/pet.h +++ b/samples/client/petstore/c/model/pet.h @@ -23,9 +23,9 @@ typedef struct pet_t { long id; //numeric - category_t *category; //model + struct category_t *category; //model char *name; // string - list_t *photoUrls; //primitive container + list_t *photo_urls; //primitive container list_t *tags; //nonprimitive container status_e status; //enum @@ -35,7 +35,7 @@ pet_t *pet_create( long id, category_t *category, char *name, - list_t *photoUrls, + list_t *photo_urls, list_t *tags, status_e status ); diff --git a/samples/client/petstore/c/model/user.c b/samples/client/petstore/c/model/user.c index b480c68203f8..be2ca8b297cf 100644 --- a/samples/client/petstore/c/model/user.c +++ b/samples/client/petstore/c/model/user.c @@ -8,12 +8,12 @@ user_t *user_create( long id, char *username, - char *firstName, - char *lastName, + char *first_name, + char *last_name, char *email, char *password, char *phone, - int userStatus + int user_status ) { user_t *user_local_var = malloc(sizeof(user_t)); if (!user_local_var) { @@ -21,12 +21,12 @@ user_t *user_create( } user_local_var->id = id; user_local_var->username = username; - user_local_var->firstName = firstName; - user_local_var->lastName = lastName; + user_local_var->first_name = first_name; + user_local_var->last_name = last_name; user_local_var->email = email; user_local_var->password = password; user_local_var->phone = phone; - user_local_var->userStatus = userStatus; + user_local_var->user_status = user_status; return user_local_var; } @@ -35,8 +35,8 @@ user_t *user_create( void user_free(user_t *user) { listEntry_t *listEntry; free(user->username); - free(user->firstName); - free(user->lastName); + free(user->first_name); + free(user->last_name); free(user->email); free(user->password); free(user->phone); @@ -62,17 +62,17 @@ cJSON *user_convertToJSON(user_t *user) { } - // user->firstName - if(user->firstName) { - if(cJSON_AddStringToObject(item, "firstName", user->firstName) == NULL) { + // user->first_name + if(user->first_name) { + if(cJSON_AddStringToObject(item, "firstName", user->first_name) == NULL) { goto fail; //String } } - // user->lastName - if(user->lastName) { - if(cJSON_AddStringToObject(item, "lastName", user->lastName) == NULL) { + // user->last_name + if(user->last_name) { + if(cJSON_AddStringToObject(item, "lastName", user->last_name) == NULL) { goto fail; //String } } @@ -102,9 +102,9 @@ cJSON *user_convertToJSON(user_t *user) { } - // user->userStatus - if(user->userStatus) { - if(cJSON_AddNumberToObject(item, "userStatus", user->userStatus) == NULL) { + // user->user_status + if(user->user_status) { + if(cJSON_AddNumberToObject(item, "userStatus", user->user_status) == NULL) { goto fail; //Numeric } } @@ -139,19 +139,19 @@ user_t *user_parseFromJSON(cJSON *userJSON){ } } - // user->firstName - cJSON *firstName = cJSON_GetObjectItemCaseSensitive(userJSON, "firstName"); - if (firstName) { - if(!cJSON_IsString(firstName)) + // user->first_name + cJSON *first_name = cJSON_GetObjectItemCaseSensitive(userJSON, "firstName"); + if (first_name) { + if(!cJSON_IsString(first_name)) { goto end; //String } } - // user->lastName - cJSON *lastName = cJSON_GetObjectItemCaseSensitive(userJSON, "lastName"); - if (lastName) { - if(!cJSON_IsString(lastName)) + // user->last_name + cJSON *last_name = cJSON_GetObjectItemCaseSensitive(userJSON, "lastName"); + if (last_name) { + if(!cJSON_IsString(last_name)) { goto end; //String } @@ -184,10 +184,10 @@ user_t *user_parseFromJSON(cJSON *userJSON){ } } - // user->userStatus - cJSON *userStatus = cJSON_GetObjectItemCaseSensitive(userJSON, "userStatus"); - if (userStatus) { - if(!cJSON_IsNumber(userStatus)) + // user->user_status + cJSON *user_status = cJSON_GetObjectItemCaseSensitive(userJSON, "userStatus"); + if (user_status) { + if(!cJSON_IsNumber(user_status)) { goto end; //Numeric } @@ -197,12 +197,12 @@ user_t *user_parseFromJSON(cJSON *userJSON){ user_local_var = user_create ( id ? id->valuedouble : 0, username ? strdup(username->valuestring) : NULL, - firstName ? strdup(firstName->valuestring) : NULL, - lastName ? strdup(lastName->valuestring) : NULL, + first_name ? strdup(first_name->valuestring) : NULL, + last_name ? strdup(last_name->valuestring) : NULL, email ? strdup(email->valuestring) : NULL, password ? strdup(password->valuestring) : NULL, phone ? strdup(phone->valuestring) : NULL, - userStatus ? userStatus->valuedouble : 0 + user_status ? user_status->valuedouble : 0 ); return user_local_var; diff --git a/samples/client/petstore/c/model/user.h b/samples/client/petstore/c/model/user.h index 954bc64a1282..28ad882941bc 100644 --- a/samples/client/petstore/c/model/user.h +++ b/samples/client/petstore/c/model/user.h @@ -17,24 +17,24 @@ typedef struct user_t { long id; //numeric char *username; // string - char *firstName; // string - char *lastName; // string + char *first_name; // string + char *last_name; // string char *email; // string char *password; // string char *phone; // string - int userStatus; //numeric + int user_status; //numeric } user_t; user_t *user_create( long id, char *username, - char *firstName, - char *lastName, + char *first_name, + char *last_name, char *email, char *password, char *phone, - int userStatus + int user_status ); void user_free(user_t *user); diff --git a/samples/client/petstore/c/src/apiClient.c b/samples/client/petstore/c/src/apiClient.c index 492b05632032..e941b37afa91 100644 --- a/samples/client/petstore/c/src/apiClient.c +++ b/samples/client/petstore/c/src/apiClient.c @@ -12,7 +12,8 @@ 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/v2"; + apiClient->basePath = strdup("http://petstore.swagger.io/v2"); + apiClient->caPath = NULL; apiClient->dataReceived = NULL; apiClient->response_code = 0; apiClient->apiKeys = NULL; @@ -21,8 +22,61 @@ apiClient_t *apiClient_create() { return apiClient; } +apiClient_t *apiClient_create_with_base_path(const char *basePath +, const char *caPath +, list_t *apiKeys +) { + curl_global_init(CURL_GLOBAL_ALL); + apiClient_t *apiClient = malloc(sizeof(apiClient_t)); + if(basePath){ + apiClient->basePath = strdup(basePath); + }else{ + apiClient->basePath = strdup("http://petstore.swagger.io/v2"); + } + + if(caPath){ + apiClient->caPath = strdup(caPath); + }else{ + apiClient->caPath = NULL; + } + + apiClient->dataReceived = NULL; + apiClient->response_code = 0; + if(apiKeys!= NULL) { + apiClient->apiKeys = list_create(); + listEntry_t *listEntry = NULL; + list_ForEach(listEntry, apiKeys) { + keyValuePair_t *pair = listEntry->data; + keyValuePair_t *pairDup = keyValuePair_create(strdup(pair->key), strdup(pair->value)); + list_addElement(apiClient->apiKeys, pairDup); + } + }else{ + apiClient->apiKeys = NULL; + } + apiClient->accessToken = NULL; + + return apiClient; +} + void apiClient_free(apiClient_t *apiClient) { - if(apiClient->accessToken) { + if(apiClient->basePath) { + free(apiClient->basePath); + } + if(apiClient->caPath) { + free(apiClient->caPath); + } + if(apiClient->apiKeys) { + listEntry_t *listEntry = NULL; + list_ForEach(listEntry, apiClient->apiKeys) { + keyValuePair_t *pair = listEntry->data; + if(pair->key){ + free(pair->key); + } + if(pair->value){ + free(pair->value); + } + keyValuePair_free(pair); + } list_free(apiClient->apiKeys); } if(apiClient->accessToken) { @@ -287,6 +341,17 @@ void apiClient_invoke(apiClient_t *apiClient, free(headerValueToWrite); } } + + if( strstr(apiClient->basePath, "https") != NULL ){ + if (apiClient->caPath) { + curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, true); + curl_easy_setopt(handle, CURLOPT_CAINFO, apiClient->caPath); + } else { + curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, false); + curl_easy_setopt(handle, CURLOPT_SSL_VERIFYHOST, false); + } + } + // this would only be generated for apiKey authentication if (apiClient->apiKeys != NULL) { diff --git a/samples/client/petstore/c/src/apiKey.c b/samples/client/petstore/c/src/apiKey.c index 2bf8fc3e9d06..eae9b75f3b47 100644 --- a/samples/client/petstore/c/src/apiKey.c +++ b/samples/client/petstore/c/src/apiKey.c @@ -9,6 +9,12 @@ keyValuePair_t *keyValuePair_create(char *key, void *value) { return keyValuePair; } +keyValuePair_t* keyValuePair_create_allocate(char* key, double value) { + double* boolpointer = malloc(sizeof(value)); + memcpy(boolpointer, &value, sizeof(value)); + return keyValuePair_create(key, boolpointer); +} + void keyValuePair_free(keyValuePair_t *keyValuePair) { free(keyValuePair); }