diff --git a/modules/openapi-generator/src/test/resources/2_0/c/petstore.yaml b/modules/openapi-generator/src/test/resources/2_0/c/petstore.yaml index c5125f976e7..082c039eadb 100644 --- a/modules/openapi-generator/src/test/resources/2_0/c/petstore.yaml +++ b/modules/openapi-generator/src/test/resources/2_0/c/petstore.yaml @@ -75,6 +75,24 @@ paths: - petstore_auth: - 'write:pets' - 'read:pets' + /pet/specialty: + get: + tags: + - pet + summary: Specialty of the shop + description: Returns the kind of pet the store specializes in + operationId: specialtyPet + produces: + - application/xml + - application/json + parameters: [] + responses: + '200': + description: successful operation + schema: + $ref: '#/definitions/Preference' + security: + - api_key: [] /pet/findByStatus: get: tags: @@ -626,6 +644,17 @@ definitions: type: string xml: name: Category + Preference: + title: Pet preference + description: A user's preference in pets + type: string + enum: + - cats + - dogs + - birds + - fish + - snakes + - other User: title: a User description: A User who is purchasing from the pet store @@ -650,6 +679,12 @@ definitions: type: integer format: int32 description: User Status + extra: + type: object + nullable: true + additionalProperties: true + preference: + $ref: '#/definitions/Preference' xml: name: User Tag: diff --git a/samples/client/petstore/c-useJsonUnformatted/.openapi-generator/FILES b/samples/client/petstore/c-useJsonUnformatted/.openapi-generator/FILES index 56afd08ac99..043461c7a2e 100644 --- a/samples/client/petstore/c-useJsonUnformatted/.openapi-generator/FILES +++ b/samples/client/petstore/c-useJsonUnformatted/.openapi-generator/FILES @@ -16,6 +16,7 @@ docs/category.md docs/model_with_set_propertes.md docs/order.md docs/pet.md +docs/preference.md docs/tag.md docs/user.md external/cJSON.c @@ -41,6 +42,8 @@ model/order.c model/order.h model/pet.c model/pet.h +model/preference.c +model/preference.h model/tag.c model/tag.h model/user.c diff --git a/samples/client/petstore/c-useJsonUnformatted/CMakeLists.txt b/samples/client/petstore/c-useJsonUnformatted/CMakeLists.txt index e41b31d33ab..7eab107fda2 100644 --- a/samples/client/petstore/c-useJsonUnformatted/CMakeLists.txt +++ b/samples/client/petstore/c-useJsonUnformatted/CMakeLists.txt @@ -60,6 +60,7 @@ set(SRCS model/model_with_set_propertes.c model/order.c model/pet.c + model/preference.c model/tag.c model/user.c api/PetAPI.c @@ -82,6 +83,7 @@ set(HDRS model/model_with_set_propertes.h model/order.h model/pet.h + model/preference.h model/tag.h model/user.h api/PetAPI.h diff --git a/samples/client/petstore/c-useJsonUnformatted/README.md b/samples/client/petstore/c-useJsonUnformatted/README.md index a2724768f15..707999204fb 100644 --- a/samples/client/petstore/c-useJsonUnformatted/README.md +++ b/samples/client/petstore/c-useJsonUnformatted/README.md @@ -70,6 +70,7 @@ Category | Method | HTTP request | Description *PetAPI* | [**PetAPI_findPetsByStatus**](docs/PetAPI.md#PetAPI_findPetsByStatus) | **GET** /pet/findByStatus | Finds Pets by status *PetAPI* | [**PetAPI_findPetsByTags**](docs/PetAPI.md#PetAPI_findPetsByTags) | **GET** /pet/findByTags | Finds Pets by tags *PetAPI* | [**PetAPI_getPetById**](docs/PetAPI.md#PetAPI_getPetById) | **GET** /pet/{petId} | Find pet by ID +*PetAPI* | [**PetAPI_specialtyPet**](docs/PetAPI.md#PetAPI_specialtyPet) | **GET** /pet/specialty | Specialty of the shop *PetAPI* | [**PetAPI_updatePet**](docs/PetAPI.md#PetAPI_updatePet) | **PUT** /pet | Update an existing pet *PetAPI* | [**PetAPI_updatePetWithForm**](docs/PetAPI.md#PetAPI_updatePetWithForm) | **POST** /pet/{petId} | Updates a pet in the store with form data *PetAPI* | [**PetAPI_uploadFile**](docs/PetAPI.md#PetAPI_uploadFile) | **POST** /pet/{petId}/uploadImage | uploads an image @@ -96,6 +97,7 @@ Category | Method | HTTP request | Description - [model_with_set_propertes_t](docs/model_with_set_propertes.md) - [order_t](docs/order.md) - [pet_t](docs/pet.md) + - [preference_t](docs/preference.md) - [tag_t](docs/tag.md) - [user_t](docs/user.md) diff --git a/samples/client/petstore/c-useJsonUnformatted/api/PetAPI.c b/samples/client/petstore/c-useJsonUnformatted/api/PetAPI.c index f17d4fd0c26..6cb279c58bb 100644 --- a/samples/client/petstore/c-useJsonUnformatted/api/PetAPI.c +++ b/samples/client/petstore/c-useJsonUnformatted/api/PetAPI.c @@ -460,6 +460,70 @@ end: } +// Specialty of the shop +// +// Returns the kind of pet the store specializes in +// +openapi_petstore_preference__e +PetAPI_specialtyPet(apiClient_t *apiClient) +{ + list_t *localVarQueryParameters = NULL; + list_t *localVarHeaderParameters = NULL; + list_t *localVarFormParameters = NULL; + list_t *localVarHeaderType = list_createList(); + list_t *localVarContentType = NULL; + char *localVarBodyParameters = NULL; + + // create the path + long sizeOfPath = strlen("/pet/specialty")+1; + char *localVarPath = malloc(sizeOfPath); + snprintf(localVarPath, sizeOfPath, "/pet/specialty"); + + + + list_addElement(localVarHeaderType,"application/xml"); //produces + list_addElement(localVarHeaderType,"application/json"); //produces + apiClient_invoke(apiClient, + localVarPath, + localVarQueryParameters, + localVarHeaderParameters, + localVarFormParameters, + localVarHeaderType, + localVarContentType, + localVarBodyParameters, + "GET"); + + // uncomment below to debug the error response + //if (apiClient->response_code == 200) { + // printf("%s\n","successful operation"); + //} + //nonprimitive not container + cJSON *PetAPIlocalVarJSON = cJSON_Parse(apiClient->dataReceived); + openapi_petstore_preference__e elementToReturn = preference_parseFromJSON(PetAPIlocalVarJSON); + cJSON_Delete(PetAPIlocalVarJSON); + if(elementToReturn == 0) { + // return 0; + } + + //return type + if (apiClient->dataReceived) { + free(apiClient->dataReceived); + apiClient->dataReceived = NULL; + apiClient->dataReceivedLen = 0; + } + + + + list_freeList(localVarHeaderType); + + free(localVarPath); + return elementToReturn; +end: + free(localVarPath); + return 0; + +} + // Update an existing pet // void diff --git a/samples/client/petstore/c-useJsonUnformatted/api/PetAPI.h b/samples/client/petstore/c-useJsonUnformatted/api/PetAPI.h index 9b5c586fda7..d33d0394b2d 100644 --- a/samples/client/petstore/c-useJsonUnformatted/api/PetAPI.h +++ b/samples/client/petstore/c-useJsonUnformatted/api/PetAPI.h @@ -7,6 +7,7 @@ #include "../include/binary.h" #include "../model/api_response.h" #include "../model/pet.h" +#include "../model/preference.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; @@ -48,6 +49,14 @@ pet_t* PetAPI_getPetById(apiClient_t *apiClient, long petId); +// Specialty of the shop +// +// Returns the kind of pet the store specializes in +// +openapi_petstore_preference__e +PetAPI_specialtyPet(apiClient_t *apiClient); + + // Update an existing pet // void diff --git a/samples/client/petstore/c-useJsonUnformatted/docs/PetAPI.md b/samples/client/petstore/c-useJsonUnformatted/docs/PetAPI.md index 392ef4d08a7..bcdc9e8e1c3 100644 --- a/samples/client/petstore/c-useJsonUnformatted/docs/PetAPI.md +++ b/samples/client/petstore/c-useJsonUnformatted/docs/PetAPI.md @@ -9,6 +9,7 @@ Method | HTTP request | Description [**PetAPI_findPetsByStatus**](PetAPI.md#PetAPI_findPetsByStatus) | **GET** /pet/findByStatus | Finds Pets by status [**PetAPI_findPetsByTags**](PetAPI.md#PetAPI_findPetsByTags) | **GET** /pet/findByTags | Finds Pets by tags [**PetAPI_getPetById**](PetAPI.md#PetAPI_getPetById) | **GET** /pet/{petId} | Find pet by ID +[**PetAPI_specialtyPet**](PetAPI.md#PetAPI_specialtyPet) | **GET** /pet/specialty | Specialty of the shop [**PetAPI_updatePet**](PetAPI.md#PetAPI_updatePet) | **PUT** /pet | Update an existing pet [**PetAPI_updatePetWithForm**](PetAPI.md#PetAPI_updatePetWithForm) | **POST** /pet/{petId} | Updates a pet in the store with form data [**PetAPI_uploadFile**](PetAPI.md#PetAPI_uploadFile) | **POST** /pet/{petId}/uploadImage | uploads an image @@ -153,6 +154,36 @@ Name | Type | Description | Notes [pet_t](pet.md) * +### Authorization + +[api_key](../README.md#api_key) + +### HTTP request headers + + - **Content-Type**: Not defined + - **Accept**: application/xml, application/json + +[[Back to top]](#) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to Model list]](../README.md#documentation-for-models) [[Back to README]](../README.md) + +# **PetAPI_specialtyPet** +```c +// Specialty of the shop +// +// Returns the kind of pet the store specializes in +// +preference_t* PetAPI_specialtyPet(apiClient_t *apiClient); +``` + +### Parameters +Name | Type | Description | Notes +------------- | ------------- | ------------- | ------------- +**apiClient** | **apiClient_t \*** | context containing the client configuration | + +### Return type + +[preference_t](preference.md) * + + ### Authorization [api_key](../README.md#api_key) diff --git a/samples/client/petstore/c-useJsonUnformatted/docs/preference.md b/samples/client/petstore/c-useJsonUnformatted/docs/preference.md new file mode 100644 index 00000000000..ab63d47cb7e --- /dev/null +++ b/samples/client/petstore/c-useJsonUnformatted/docs/preference.md @@ -0,0 +1,9 @@ +# preference_t + +## Properties +Name | Type | Description | Notes +------------ | ------------- | ------------- | ------------- + +[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md) + + diff --git a/samples/client/petstore/c-useJsonUnformatted/docs/user.md b/samples/client/petstore/c-useJsonUnformatted/docs/user.md index 3b9219c158b..22b807f47ef 100644 --- a/samples/client/petstore/c-useJsonUnformatted/docs/user.md +++ b/samples/client/petstore/c-useJsonUnformatted/docs/user.md @@ -11,6 +11,8 @@ Name | Type | Description | Notes **password** | **char \*** | | [optional] **phone** | **char \*** | | [optional] **user_status** | **int** | User Status | [optional] +**extra** | **list_t*** | | [optional] +**preference** | **preference_t \*** | | [optional] [[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md) diff --git a/samples/client/petstore/c-useJsonUnformatted/model/preference.c b/samples/client/petstore/c-useJsonUnformatted/model/preference.c new file mode 100644 index 00000000000..ac7e600fed0 --- /dev/null +++ b/samples/client/petstore/c-useJsonUnformatted/model/preference.c @@ -0,0 +1,47 @@ +#include +#include +#include +#include "preference.h" + + +char* preference_preference_ToString(openapi_petstore_preference__e preference) { + char *preferenceArray[] = { "NULL", "cats", "dogs", "birds", "fish", "snakes", "other" }; + return preferenceArray[preference]; +} + +openapi_petstore_preference__e preference_preference_FromString(char* preference) { + int stringToReturn = 0; + char *preferenceArray[] = { "NULL", "cats", "dogs", "birds", "fish", "snakes", "other" }; + size_t sizeofArray = sizeof(preferenceArray) / sizeof(preferenceArray[0]); + while(stringToReturn < sizeofArray) { + if(strcmp(preference, preferenceArray[stringToReturn]) == 0) { + return stringToReturn; + } + stringToReturn++; + } + return 0; +} + +cJSON *preference_convertToJSON(openapi_petstore_preference__e preference) { + cJSON *item = cJSON_CreateObject(); + if(cJSON_AddStringToObject(item, "preference", preference_preference_ToString(preference)) == NULL) { + goto fail; + } + return item; +fail: + cJSON_Delete(item); + return NULL; +} + +openapi_petstore_preference__e preference_parseFromJSON(cJSON *preferenceJSON) { + openapi_petstore_preference__e *preference = NULL; + openapi_petstore_preference__e preferenceVariable; + cJSON *preferenceVar = cJSON_GetObjectItemCaseSensitive(preferenceJSON, "preference"); + if(!cJSON_IsString(preferenceVar) || (preferenceVar->valuestring == NULL)){ + goto end; + } + preferenceVariable = preference_preference_FromString(preferenceVar->valuestring); + return preferenceVariable; +end: + return 0; +} diff --git a/samples/client/petstore/c-useJsonUnformatted/model/preference.h b/samples/client/petstore/c-useJsonUnformatted/model/preference.h new file mode 100644 index 00000000000..1f6644f0836 --- /dev/null +++ b/samples/client/petstore/c-useJsonUnformatted/model/preference.h @@ -0,0 +1,32 @@ +/* + * preference.h + * + * A user's preference in pets + */ + +#ifndef _preference_H_ +#define _preference_H_ + +#include +#include "../external/cJSON.h" +#include "../include/list.h" +#include "../include/keyValuePair.h" +#include "../include/binary.h" + +typedef struct preference_t preference_t; + + +// Enum for preference + +typedef enum { openapi_petstore_preference__NULL = 0, openapi_petstore_preference__cats, openapi_petstore_preference__dogs, openapi_petstore_preference__birds, openapi_petstore_preference__fish, openapi_petstore_preference__snakes, openapi_petstore_preference__other } openapi_petstore_preference__e; + +char* preference_preference_ToString(openapi_petstore_preference__e preference); + +openapi_petstore_preference__e preference_preference_FromString(char* preference); + +cJSON *preference_convertToJSON(openapi_petstore_preference__e preference); + +openapi_petstore_preference__e preference_parseFromJSON(cJSON *preferenceJSON); + +#endif /* _preference_H_ */ + diff --git a/samples/client/petstore/c-useJsonUnformatted/model/user.c b/samples/client/petstore/c-useJsonUnformatted/model/user.c index eb188153ccf..b43d428e1ff 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/user.c +++ b/samples/client/petstore/c-useJsonUnformatted/model/user.c @@ -13,7 +13,9 @@ user_t *user_create( char *email, char *password, char *phone, - int user_status + int user_status, + list_t* extra, + openapi_petstore_preference__e preference ) { user_t *user_local_var = malloc(sizeof(user_t)); if (!user_local_var) { @@ -27,6 +29,8 @@ user_t *user_create( user_local_var->password = password; user_local_var->phone = phone; user_local_var->user_status = user_status; + user_local_var->extra = extra; + user_local_var->preference = preference; return user_local_var; } @@ -61,6 +65,16 @@ void user_free(user_t *user) { free(user->phone); user->phone = NULL; } + if (user->extra) { + list_ForEach(listEntry, user->extra) { + keyValuePair_t *localKeyValue = (keyValuePair_t*) listEntry->data; + free (localKeyValue->key); + free (localKeyValue->value); + keyValuePair_free(localKeyValue); + } + list_freeList(user->extra); + user->extra = NULL; + } free(user); } @@ -130,6 +144,35 @@ cJSON *user_convertToJSON(user_t *user) { } } + + // user->extra + if(user->extra) { + cJSON *extra = cJSON_AddObjectToObject(item, "extra"); + if(extra == NULL) { + goto fail; //primitive map container + } + cJSON *localMapObject = extra; + listEntry_t *extraListEntry; + if (user->extra) { + list_ForEach(extraListEntry, user->extra) { + keyValuePair_t *localKeyValue = (keyValuePair_t*)extraListEntry->data; + } + } + } + + + // user->preference + if(user->preference != openapi_petstore_preference__NULL) { + cJSON *preference_local_JSON = preference_convertToJSON(user->preference); + if(preference_local_JSON == NULL) { + goto fail; // custom + } + cJSON_AddItemToObject(item, "preference", preference_local_JSON); + if(item->child == NULL) { + goto fail; + } + } + return item; fail: if (item) { @@ -142,6 +185,12 @@ user_t *user_parseFromJSON(cJSON *userJSON){ user_t *user_local_var = NULL; + // define the local map for user->extra + list_t *extraList = NULL; + + // define the local variable for user->preference + openapi_petstore_preference__e preference_local_nonprim = 0; + // user->id cJSON *id = cJSON_GetObjectItemCaseSensitive(userJSON, "id"); if (id) { @@ -214,6 +263,32 @@ user_t *user_parseFromJSON(cJSON *userJSON){ } } + // user->extra + cJSON *extra = cJSON_GetObjectItemCaseSensitive(userJSON, "extra"); + if (extra) { + cJSON *extra_local_map = NULL; + if(!cJSON_IsObject(extra) && !cJSON_IsNull(extra)) + { + goto end;//primitive map container + } + if(cJSON_IsObject(extra)) + { + extraList = list_createList(); + keyValuePair_t *localMapKeyPair; + cJSON_ArrayForEach(extra_local_map, extra) + { + cJSON *localMapObject = extra_local_map; + list_addElement(extraList , localMapKeyPair); + } + } + } + + // user->preference + cJSON *preference = cJSON_GetObjectItemCaseSensitive(userJSON, "preference"); + if (preference) { + preference_local_nonprim = preference_parseFromJSON(preference); //custom + } + user_local_var = user_create ( id ? id->valuedouble : 0, @@ -223,11 +298,28 @@ user_t *user_parseFromJSON(cJSON *userJSON){ email && !cJSON_IsNull(email) ? strdup(email->valuestring) : NULL, password && !cJSON_IsNull(password) ? strdup(password->valuestring) : NULL, phone && !cJSON_IsNull(phone) ? strdup(phone->valuestring) : NULL, - user_status ? user_status->valuedouble : 0 + user_status ? user_status->valuedouble : 0, + extra ? extraList : NULL, + preference ? preference_local_nonprim : 0 ); return user_local_var; end: + if (extraList) { + listEntry_t *listEntry = NULL; + list_ForEach(listEntry, extraList) { + keyValuePair_t *localKeyValue = (keyValuePair_t*) listEntry->data; + free(localKeyValue->key); + localKeyValue->key = NULL; + keyValuePair_free(localKeyValue); + localKeyValue = NULL; + } + list_freeList(extraList); + extraList = NULL; + } + if (preference_local_nonprim) { + preference_local_nonprim = 0; + } return NULL; } diff --git a/samples/client/petstore/c-useJsonUnformatted/model/user.h b/samples/client/petstore/c-useJsonUnformatted/model/user.h index 45fa42c3f91..badbc747b30 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/user.h +++ b/samples/client/petstore/c-useJsonUnformatted/model/user.h @@ -15,6 +15,8 @@ typedef struct user_t user_t; +#include "any_type.h" +#include "preference.h" @@ -27,6 +29,8 @@ typedef struct user_t { char *password; // string char *phone; // string int user_status; //numeric + list_t* extra; //map + openapi_petstore_preference__e preference; //referenced enum } user_t; @@ -38,7 +42,9 @@ user_t *user_create( char *email, char *password, char *phone, - int user_status + int user_status, + list_t* extra, + openapi_petstore_preference__e preference ); void user_free(user_t *user); diff --git a/samples/client/petstore/c-useJsonUnformatted/unit-test/test_preference.c b/samples/client/petstore/c-useJsonUnformatted/unit-test/test_preference.c new file mode 100644 index 00000000000..96468361f58 --- /dev/null +++ b/samples/client/petstore/c-useJsonUnformatted/unit-test/test_preference.c @@ -0,0 +1,56 @@ +#ifndef preference_TEST +#define preference_TEST + +// the following is to include only the main from the first c file +#ifndef TEST_MAIN +#define TEST_MAIN +#define preference_MAIN +#endif // TEST_MAIN + +#include +#include +#include +#include +#include "../external/cJSON.h" + +#include "../model/preference.h" +preference_t* instantiate_preference(int include_optional); + + + +preference_t* instantiate_preference(int include_optional) { + preference_t* preference = NULL; + if (include_optional) { + preference = preference_create( + ); + } else { + preference = preference_create( + ); + } + + return preference; +} + + +#ifdef preference_MAIN + +void test_preference(int include_optional) { + preference_t* preference_1 = instantiate_preference(include_optional); + + cJSON* jsonpreference_1 = preference_convertToJSON(preference_1); + printf("preference :\n%s\n", cJSON_PrintUnformatted(jsonpreference_1)); + preference_t* preference_2 = preference_parseFromJSON(jsonpreference_1); + cJSON* jsonpreference_2 = preference_convertToJSON(preference_2); + printf("repeating preference:\n%s\n", cJSON_PrintUnformatted(jsonpreference_2)); +} + +int main() { + test_preference(1); + test_preference(0); + + printf("Hello world \n"); + return 0; +} + +#endif // preference_MAIN +#endif // preference_TEST diff --git a/samples/client/petstore/c/.openapi-generator/FILES b/samples/client/petstore/c/.openapi-generator/FILES index 4f6e6397e71..eff7ae71434 100644 --- a/samples/client/petstore/c/.openapi-generator/FILES +++ b/samples/client/petstore/c/.openapi-generator/FILES @@ -15,6 +15,7 @@ docs/category.md docs/model_with_set_propertes.md docs/order.md docs/pet.md +docs/preference.md docs/tag.md docs/user.md external/cJSON.c @@ -40,6 +41,8 @@ model/order.c model/order.h model/pet.c model/pet.h +model/preference.c +model/preference.h model/tag.c model/tag.h model/user.c diff --git a/samples/client/petstore/c/README.md b/samples/client/petstore/c/README.md index a2724768f15..707999204fb 100644 --- a/samples/client/petstore/c/README.md +++ b/samples/client/petstore/c/README.md @@ -70,6 +70,7 @@ Category | Method | HTTP request | Description *PetAPI* | [**PetAPI_findPetsByStatus**](docs/PetAPI.md#PetAPI_findPetsByStatus) | **GET** /pet/findByStatus | Finds Pets by status *PetAPI* | [**PetAPI_findPetsByTags**](docs/PetAPI.md#PetAPI_findPetsByTags) | **GET** /pet/findByTags | Finds Pets by tags *PetAPI* | [**PetAPI_getPetById**](docs/PetAPI.md#PetAPI_getPetById) | **GET** /pet/{petId} | Find pet by ID +*PetAPI* | [**PetAPI_specialtyPet**](docs/PetAPI.md#PetAPI_specialtyPet) | **GET** /pet/specialty | Specialty of the shop *PetAPI* | [**PetAPI_updatePet**](docs/PetAPI.md#PetAPI_updatePet) | **PUT** /pet | Update an existing pet *PetAPI* | [**PetAPI_updatePetWithForm**](docs/PetAPI.md#PetAPI_updatePetWithForm) | **POST** /pet/{petId} | Updates a pet in the store with form data *PetAPI* | [**PetAPI_uploadFile**](docs/PetAPI.md#PetAPI_uploadFile) | **POST** /pet/{petId}/uploadImage | uploads an image @@ -96,6 +97,7 @@ Category | Method | HTTP request | Description - [model_with_set_propertes_t](docs/model_with_set_propertes.md) - [order_t](docs/order.md) - [pet_t](docs/pet.md) + - [preference_t](docs/preference.md) - [tag_t](docs/tag.md) - [user_t](docs/user.md) diff --git a/samples/client/petstore/c/api/PetAPI.c b/samples/client/petstore/c/api/PetAPI.c index 36303a0ef5e..9cf09a2e4ad 100644 --- a/samples/client/petstore/c/api/PetAPI.c +++ b/samples/client/petstore/c/api/PetAPI.c @@ -460,6 +460,70 @@ end: } +// Specialty of the shop +// +// Returns the kind of pet the store specializes in +// +openapi_petstore_preference__e +PetAPI_specialtyPet(apiClient_t *apiClient) +{ + list_t *localVarQueryParameters = NULL; + list_t *localVarHeaderParameters = NULL; + list_t *localVarFormParameters = NULL; + list_t *localVarHeaderType = list_createList(); + list_t *localVarContentType = NULL; + char *localVarBodyParameters = NULL; + + // create the path + long sizeOfPath = strlen("/pet/specialty")+1; + char *localVarPath = malloc(sizeOfPath); + snprintf(localVarPath, sizeOfPath, "/pet/specialty"); + + + + list_addElement(localVarHeaderType,"application/xml"); //produces + list_addElement(localVarHeaderType,"application/json"); //produces + apiClient_invoke(apiClient, + localVarPath, + localVarQueryParameters, + localVarHeaderParameters, + localVarFormParameters, + localVarHeaderType, + localVarContentType, + localVarBodyParameters, + "GET"); + + // uncomment below to debug the error response + //if (apiClient->response_code == 200) { + // printf("%s\n","successful operation"); + //} + //nonprimitive not container + cJSON *PetAPIlocalVarJSON = cJSON_Parse(apiClient->dataReceived); + openapi_petstore_preference__e elementToReturn = preference_parseFromJSON(PetAPIlocalVarJSON); + cJSON_Delete(PetAPIlocalVarJSON); + if(elementToReturn == 0) { + // return 0; + } + + //return type + if (apiClient->dataReceived) { + free(apiClient->dataReceived); + apiClient->dataReceived = NULL; + apiClient->dataReceivedLen = 0; + } + + + + list_freeList(localVarHeaderType); + + free(localVarPath); + return elementToReturn; +end: + free(localVarPath); + return 0; + +} + // Update an existing pet // void diff --git a/samples/client/petstore/c/api/PetAPI.h b/samples/client/petstore/c/api/PetAPI.h index 9b5c586fda7..d33d0394b2d 100644 --- a/samples/client/petstore/c/api/PetAPI.h +++ b/samples/client/petstore/c/api/PetAPI.h @@ -7,6 +7,7 @@ #include "../include/binary.h" #include "../model/api_response.h" #include "../model/pet.h" +#include "../model/preference.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; @@ -48,6 +49,14 @@ pet_t* PetAPI_getPetById(apiClient_t *apiClient, long petId); +// Specialty of the shop +// +// Returns the kind of pet the store specializes in +// +openapi_petstore_preference__e +PetAPI_specialtyPet(apiClient_t *apiClient); + + // Update an existing pet // void diff --git a/samples/client/petstore/c/docs/PetAPI.md b/samples/client/petstore/c/docs/PetAPI.md index 392ef4d08a7..bcdc9e8e1c3 100644 --- a/samples/client/petstore/c/docs/PetAPI.md +++ b/samples/client/petstore/c/docs/PetAPI.md @@ -9,6 +9,7 @@ Method | HTTP request | Description [**PetAPI_findPetsByStatus**](PetAPI.md#PetAPI_findPetsByStatus) | **GET** /pet/findByStatus | Finds Pets by status [**PetAPI_findPetsByTags**](PetAPI.md#PetAPI_findPetsByTags) | **GET** /pet/findByTags | Finds Pets by tags [**PetAPI_getPetById**](PetAPI.md#PetAPI_getPetById) | **GET** /pet/{petId} | Find pet by ID +[**PetAPI_specialtyPet**](PetAPI.md#PetAPI_specialtyPet) | **GET** /pet/specialty | Specialty of the shop [**PetAPI_updatePet**](PetAPI.md#PetAPI_updatePet) | **PUT** /pet | Update an existing pet [**PetAPI_updatePetWithForm**](PetAPI.md#PetAPI_updatePetWithForm) | **POST** /pet/{petId} | Updates a pet in the store with form data [**PetAPI_uploadFile**](PetAPI.md#PetAPI_uploadFile) | **POST** /pet/{petId}/uploadImage | uploads an image @@ -153,6 +154,36 @@ Name | Type | Description | Notes [pet_t](pet.md) * +### Authorization + +[api_key](../README.md#api_key) + +### HTTP request headers + + - **Content-Type**: Not defined + - **Accept**: application/xml, application/json + +[[Back to top]](#) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to Model list]](../README.md#documentation-for-models) [[Back to README]](../README.md) + +# **PetAPI_specialtyPet** +```c +// Specialty of the shop +// +// Returns the kind of pet the store specializes in +// +preference_t* PetAPI_specialtyPet(apiClient_t *apiClient); +``` + +### Parameters +Name | Type | Description | Notes +------------- | ------------- | ------------- | ------------- +**apiClient** | **apiClient_t \*** | context containing the client configuration | + +### Return type + +[preference_t](preference.md) * + + ### Authorization [api_key](../README.md#api_key) diff --git a/samples/client/petstore/c/docs/preference.md b/samples/client/petstore/c/docs/preference.md new file mode 100644 index 00000000000..ab63d47cb7e --- /dev/null +++ b/samples/client/petstore/c/docs/preference.md @@ -0,0 +1,9 @@ +# preference_t + +## Properties +Name | Type | Description | Notes +------------ | ------------- | ------------- | ------------- + +[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md) + + diff --git a/samples/client/petstore/c/docs/user.md b/samples/client/petstore/c/docs/user.md index 3b9219c158b..22b807f47ef 100644 --- a/samples/client/petstore/c/docs/user.md +++ b/samples/client/petstore/c/docs/user.md @@ -11,6 +11,8 @@ Name | Type | Description | Notes **password** | **char \*** | | [optional] **phone** | **char \*** | | [optional] **user_status** | **int** | User Status | [optional] +**extra** | **list_t*** | | [optional] +**preference** | **preference_t \*** | | [optional] [[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md) diff --git a/samples/client/petstore/c/model/preference.c b/samples/client/petstore/c/model/preference.c new file mode 100644 index 00000000000..ac7e600fed0 --- /dev/null +++ b/samples/client/petstore/c/model/preference.c @@ -0,0 +1,47 @@ +#include +#include +#include +#include "preference.h" + + +char* preference_preference_ToString(openapi_petstore_preference__e preference) { + char *preferenceArray[] = { "NULL", "cats", "dogs", "birds", "fish", "snakes", "other" }; + return preferenceArray[preference]; +} + +openapi_petstore_preference__e preference_preference_FromString(char* preference) { + int stringToReturn = 0; + char *preferenceArray[] = { "NULL", "cats", "dogs", "birds", "fish", "snakes", "other" }; + size_t sizeofArray = sizeof(preferenceArray) / sizeof(preferenceArray[0]); + while(stringToReturn < sizeofArray) { + if(strcmp(preference, preferenceArray[stringToReturn]) == 0) { + return stringToReturn; + } + stringToReturn++; + } + return 0; +} + +cJSON *preference_convertToJSON(openapi_petstore_preference__e preference) { + cJSON *item = cJSON_CreateObject(); + if(cJSON_AddStringToObject(item, "preference", preference_preference_ToString(preference)) == NULL) { + goto fail; + } + return item; +fail: + cJSON_Delete(item); + return NULL; +} + +openapi_petstore_preference__e preference_parseFromJSON(cJSON *preferenceJSON) { + openapi_petstore_preference__e *preference = NULL; + openapi_petstore_preference__e preferenceVariable; + cJSON *preferenceVar = cJSON_GetObjectItemCaseSensitive(preferenceJSON, "preference"); + if(!cJSON_IsString(preferenceVar) || (preferenceVar->valuestring == NULL)){ + goto end; + } + preferenceVariable = preference_preference_FromString(preferenceVar->valuestring); + return preferenceVariable; +end: + return 0; +} diff --git a/samples/client/petstore/c/model/preference.h b/samples/client/petstore/c/model/preference.h new file mode 100644 index 00000000000..1f6644f0836 --- /dev/null +++ b/samples/client/petstore/c/model/preference.h @@ -0,0 +1,32 @@ +/* + * preference.h + * + * A user's preference in pets + */ + +#ifndef _preference_H_ +#define _preference_H_ + +#include +#include "../external/cJSON.h" +#include "../include/list.h" +#include "../include/keyValuePair.h" +#include "../include/binary.h" + +typedef struct preference_t preference_t; + + +// Enum for preference + +typedef enum { openapi_petstore_preference__NULL = 0, openapi_petstore_preference__cats, openapi_petstore_preference__dogs, openapi_petstore_preference__birds, openapi_petstore_preference__fish, openapi_petstore_preference__snakes, openapi_petstore_preference__other } openapi_petstore_preference__e; + +char* preference_preference_ToString(openapi_petstore_preference__e preference); + +openapi_petstore_preference__e preference_preference_FromString(char* preference); + +cJSON *preference_convertToJSON(openapi_petstore_preference__e preference); + +openapi_petstore_preference__e preference_parseFromJSON(cJSON *preferenceJSON); + +#endif /* _preference_H_ */ + diff --git a/samples/client/petstore/c/model/user.c b/samples/client/petstore/c/model/user.c index eb188153ccf..b43d428e1ff 100644 --- a/samples/client/petstore/c/model/user.c +++ b/samples/client/petstore/c/model/user.c @@ -13,7 +13,9 @@ user_t *user_create( char *email, char *password, char *phone, - int user_status + int user_status, + list_t* extra, + openapi_petstore_preference__e preference ) { user_t *user_local_var = malloc(sizeof(user_t)); if (!user_local_var) { @@ -27,6 +29,8 @@ user_t *user_create( user_local_var->password = password; user_local_var->phone = phone; user_local_var->user_status = user_status; + user_local_var->extra = extra; + user_local_var->preference = preference; return user_local_var; } @@ -61,6 +65,16 @@ void user_free(user_t *user) { free(user->phone); user->phone = NULL; } + if (user->extra) { + list_ForEach(listEntry, user->extra) { + keyValuePair_t *localKeyValue = (keyValuePair_t*) listEntry->data; + free (localKeyValue->key); + free (localKeyValue->value); + keyValuePair_free(localKeyValue); + } + list_freeList(user->extra); + user->extra = NULL; + } free(user); } @@ -130,6 +144,35 @@ cJSON *user_convertToJSON(user_t *user) { } } + + // user->extra + if(user->extra) { + cJSON *extra = cJSON_AddObjectToObject(item, "extra"); + if(extra == NULL) { + goto fail; //primitive map container + } + cJSON *localMapObject = extra; + listEntry_t *extraListEntry; + if (user->extra) { + list_ForEach(extraListEntry, user->extra) { + keyValuePair_t *localKeyValue = (keyValuePair_t*)extraListEntry->data; + } + } + } + + + // user->preference + if(user->preference != openapi_petstore_preference__NULL) { + cJSON *preference_local_JSON = preference_convertToJSON(user->preference); + if(preference_local_JSON == NULL) { + goto fail; // custom + } + cJSON_AddItemToObject(item, "preference", preference_local_JSON); + if(item->child == NULL) { + goto fail; + } + } + return item; fail: if (item) { @@ -142,6 +185,12 @@ user_t *user_parseFromJSON(cJSON *userJSON){ user_t *user_local_var = NULL; + // define the local map for user->extra + list_t *extraList = NULL; + + // define the local variable for user->preference + openapi_petstore_preference__e preference_local_nonprim = 0; + // user->id cJSON *id = cJSON_GetObjectItemCaseSensitive(userJSON, "id"); if (id) { @@ -214,6 +263,32 @@ user_t *user_parseFromJSON(cJSON *userJSON){ } } + // user->extra + cJSON *extra = cJSON_GetObjectItemCaseSensitive(userJSON, "extra"); + if (extra) { + cJSON *extra_local_map = NULL; + if(!cJSON_IsObject(extra) && !cJSON_IsNull(extra)) + { + goto end;//primitive map container + } + if(cJSON_IsObject(extra)) + { + extraList = list_createList(); + keyValuePair_t *localMapKeyPair; + cJSON_ArrayForEach(extra_local_map, extra) + { + cJSON *localMapObject = extra_local_map; + list_addElement(extraList , localMapKeyPair); + } + } + } + + // user->preference + cJSON *preference = cJSON_GetObjectItemCaseSensitive(userJSON, "preference"); + if (preference) { + preference_local_nonprim = preference_parseFromJSON(preference); //custom + } + user_local_var = user_create ( id ? id->valuedouble : 0, @@ -223,11 +298,28 @@ user_t *user_parseFromJSON(cJSON *userJSON){ email && !cJSON_IsNull(email) ? strdup(email->valuestring) : NULL, password && !cJSON_IsNull(password) ? strdup(password->valuestring) : NULL, phone && !cJSON_IsNull(phone) ? strdup(phone->valuestring) : NULL, - user_status ? user_status->valuedouble : 0 + user_status ? user_status->valuedouble : 0, + extra ? extraList : NULL, + preference ? preference_local_nonprim : 0 ); return user_local_var; end: + if (extraList) { + listEntry_t *listEntry = NULL; + list_ForEach(listEntry, extraList) { + keyValuePair_t *localKeyValue = (keyValuePair_t*) listEntry->data; + free(localKeyValue->key); + localKeyValue->key = NULL; + keyValuePair_free(localKeyValue); + localKeyValue = NULL; + } + list_freeList(extraList); + extraList = NULL; + } + if (preference_local_nonprim) { + preference_local_nonprim = 0; + } return NULL; } diff --git a/samples/client/petstore/c/model/user.h b/samples/client/petstore/c/model/user.h index 45fa42c3f91..badbc747b30 100644 --- a/samples/client/petstore/c/model/user.h +++ b/samples/client/petstore/c/model/user.h @@ -15,6 +15,8 @@ typedef struct user_t user_t; +#include "any_type.h" +#include "preference.h" @@ -27,6 +29,8 @@ typedef struct user_t { char *password; // string char *phone; // string int user_status; //numeric + list_t* extra; //map + openapi_petstore_preference__e preference; //referenced enum } user_t; @@ -38,7 +42,9 @@ user_t *user_create( char *email, char *password, char *phone, - int user_status + int user_status, + list_t* extra, + openapi_petstore_preference__e preference ); void user_free(user_t *user); diff --git a/samples/client/petstore/c/unit-test/test_preference.c b/samples/client/petstore/c/unit-test/test_preference.c new file mode 100644 index 00000000000..8b1e2163fb3 --- /dev/null +++ b/samples/client/petstore/c/unit-test/test_preference.c @@ -0,0 +1,56 @@ +#ifndef preference_TEST +#define preference_TEST + +// the following is to include only the main from the first c file +#ifndef TEST_MAIN +#define TEST_MAIN +#define preference_MAIN +#endif // TEST_MAIN + +#include +#include +#include +#include +#include "../external/cJSON.h" + +#include "../model/preference.h" +preference_t* instantiate_preference(int include_optional); + + + +preference_t* instantiate_preference(int include_optional) { + preference_t* preference = NULL; + if (include_optional) { + preference = preference_create( + ); + } else { + preference = preference_create( + ); + } + + return preference; +} + + +#ifdef preference_MAIN + +void test_preference(int include_optional) { + preference_t* preference_1 = instantiate_preference(include_optional); + + cJSON* jsonpreference_1 = preference_convertToJSON(preference_1); + printf("preference :\n%s\n", cJSON_Print(jsonpreference_1)); + preference_t* preference_2 = preference_parseFromJSON(jsonpreference_1); + cJSON* jsonpreference_2 = preference_convertToJSON(preference_2); + printf("repeating preference:\n%s\n", cJSON_Print(jsonpreference_2)); +} + +int main() { + test_preference(1); + test_preference(0); + + printf("Hello world \n"); + return 0; +} + +#endif // preference_MAIN +#endif // preference_TEST