From fa1e003a3be72da3f2b1788acd21924816d4c5e8 Mon Sep 17 00:00:00 2001 From: William Cheng Date: Wed, 13 Nov 2019 13:34:31 +0800 Subject: [PATCH] minor enhancement to c client generaotr (#4457) --- .../resources/C-libcurl/CMakeLists.txt.mustache | 13 ++++++++----- .../src/main/resources/C-libcurl/README.md.mustache | 12 ++++++------ samples/client/petstore/c/.openapi-generator-ignore | 1 + .../client/petstore/c/.openapi-generator/VERSION | 2 +- samples/client/petstore/c/CMakeLists.txt | 4 ++++ samples/client/petstore/c/README.md | 12 ++++++------ samples/client/petstore/c/api/PetAPI.c | 4 ++-- samples/client/petstore/c/api/StoreAPI.c | 7 ++++--- samples/client/petstore/c/api/UserAPI.c | 2 +- samples/client/petstore/c/src/apiClient.c | 2 +- 10 files changed, 34 insertions(+), 25 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/C-libcurl/CMakeLists.txt.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/CMakeLists.txt.mustache index 16b7102fb09..060f8f75eb3 100644 --- a/modules/openapi-generator/src/main/resources/C-libcurl/CMakeLists.txt.mustache +++ b/modules/openapi-generator/src/main/resources/C-libcurl/CMakeLists.txt.mustache @@ -73,11 +73,14 @@ set(HDRS "") ## This section shows how to use the above compiled libary to compile the source files ## set source files #set(SRCS -# unit-tests/manual-PetAPI.c -# unit-tests/manual-StoreAPI.c -# unit-tests/manual-UserAPI.c -# unit-tests/manual-order.c -# unit-tests/manual-user.c) +{{#apiInfo}} +{{#apis}} +{{#operations}} +# unit-tests/manual-{{classname}}.c +{{/operations}} +{{/apis}} +{{/apiInfo}} +#) ##set header files #set(HDRS diff --git a/modules/openapi-generator/src/main/resources/C-libcurl/README.md.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/README.md.mustache index b656b40bb95..08f5bf17e00 100644 --- a/modules/openapi-generator/src/main/resources/C-libcurl/README.md.mustache +++ b/modules/openapi-generator/src/main/resources/C-libcurl/README.md.mustache @@ -14,7 +14,7 @@ For more information, please visit [{{{infoUrl}}}]({{{infoUrl}}}) {{/infoUrl}} ## Installation -You'll need the `curl 7.58.0` package in order to build the API. To have code formatted nicely you also need to have uncrustify version 0.67. +You'll need the `curl 7.58.0` package in order to build the API. To have code formatted nicely, you also need to have uncrustify version 0.67 or later. # Prerequisites @@ -40,11 +40,11 @@ sudo make install ``` ## Compile the sample: -This will compile the generated code and create a library in build folder which has to be linked to the codes where API will be used. +This will compile the generated code and create a library in the build folder which has to be linked to the codes where API will be used. ```bash mkdir build cd build -// To install library to specific location use following command +// To install library to specific location, use following commands cmake -DCMAKE_INSTALL_PREFIX=/pathtolocaiton .. // for normal install use following command cmake .. @@ -58,11 +58,11 @@ To compile main.c(considering the file is present in build folder) use following -L - locaiton of the library(not required if cmake with normal installation is performed) -l library name ```bash -gcc main.c -L. -lpetstore -o main +gcc main.c -L. -l{{projectName}} -o main ``` -once compile, you can run it with ``` ./main ``` +Once compiled, you can run it with ``` ./main ``` -Note: You dont need to specify includes for models and include folder seperately as they are path linked. You just have to import the api.h file in your code, the include linking will work. +Note: You don't need to specify includes for models and include folder seperately as they are path linked. You just have to import the api.h file in your code, the include linking will work. ## Author diff --git a/samples/client/petstore/c/.openapi-generator-ignore b/samples/client/petstore/c/.openapi-generator-ignore index 7484ee590a3..f574e0daf8f 100644 --- a/samples/client/petstore/c/.openapi-generator-ignore +++ b/samples/client/petstore/c/.openapi-generator-ignore @@ -21,3 +21,4 @@ #docs/*.md # Then explicitly reverse the ignore rule for a single file: #!docs/README.md +CMakeLists.txt diff --git a/samples/client/petstore/c/.openapi-generator/VERSION b/samples/client/petstore/c/.openapi-generator/VERSION index afa63656064..d168f1d8bda 100644 --- a/samples/client/petstore/c/.openapi-generator/VERSION +++ b/samples/client/petstore/c/.openapi-generator/VERSION @@ -1 +1 @@ -4.0.0-SNAPSHOT \ No newline at end of file +4.2.1-SNAPSHOT \ No newline at end of file diff --git a/samples/client/petstore/c/CMakeLists.txt b/samples/client/petstore/c/CMakeLists.txt index 404cf6f17f9..1de992031da 100644 --- a/samples/client/petstore/c/CMakeLists.txt +++ b/samples/client/petstore/c/CMakeLists.txt @@ -52,10 +52,14 @@ set(HDRS ) +# Add library with project file with projectname as library name add_library(${pkgName} SHARED ${SRCS} ${HDRS}) +# Link dependent libraries target_link_libraries(${pkgName} ${CURL_LIBRARIES} ) +#install library to destination install(TARGETS ${pkgName} DESTINATION ${CMAKE_INSTALL_PREFIX}) +# Setting file variables to null set(SRCS "") set(HDRS "") diff --git a/samples/client/petstore/c/README.md b/samples/client/petstore/c/README.md index 56b9a34abed..0059cc47da8 100644 --- a/samples/client/petstore/c/README.md +++ b/samples/client/petstore/c/README.md @@ -8,7 +8,7 @@ This API client was generated by the [OpenAPI Generator](https://openapi-generat - Build package: org.openapitools.codegen.languages.CLibcurlClientCodegen ## Installation -You'll need the `curl 7.58.0` package in order to build the API. To have code formatted nicely you also need to have uncrustify version 0.67. +You'll need the `curl 7.58.0` package in order to build the API. To have code formatted nicely, you also need to have uncrustify version 0.67 or later. # Prerequisites @@ -34,11 +34,11 @@ sudo make install ``` ## Compile the sample: -This will compile the generated code and create a library in build folder which has to be linked to the codes where API will be used. +This will compile the generated code and create a library in the build folder which has to be linked to the codes where API will be used. ```bash mkdir build cd build -// To install library to specific location use following command +// To install library to specific location, use following commands cmake -DCMAKE_INSTALL_PREFIX=/pathtolocaiton .. // for normal install use following command cmake .. @@ -52,11 +52,11 @@ To compile main.c(considering the file is present in build folder) use following -L - locaiton of the library(not required if cmake with normal installation is performed) -l library name ```bash -gcc main.c -L. -lpetstore -o main +gcc main.c -L. -lopenapi_petstore -o main ``` -once compile, you can run it with ``` ./main ``` +Once compiled, you can run it with ``` ./main ``` -Note: You dont need to specify includes for models and include folder seperately as they are path linked. You just have to import the api.h file in your code, the include linking will work. +Note: You don't need to specify includes for models and include folder seperately as they are path linked. You just have to import the api.h file in your code, the include linking will work. ## Author diff --git a/samples/client/petstore/c/api/PetAPI.c b/samples/client/petstore/c/api/PetAPI.c index ad31d72f11e..cc7bc440c84 100644 --- a/samples/client/petstore/c/api/PetAPI.c +++ b/samples/client/petstore/c/api/PetAPI.c @@ -351,7 +351,7 @@ pet_t *PetAPI_getPetById(apiClient_t *apiClient, long petId) { // nonprimitive not container cJSON *PetAPIlocalVarJSON = cJSON_Parse(apiClient->dataReceived); pet_t *elementToReturn = pet_parseFromJSON(PetAPIlocalVarJSON); - cJSON_Delete(PetAPIlocalVarJSON); + cJSON_Delete(PetAPIlocalVarJSON); if(elementToReturn == NULL) { // return 0; } @@ -606,7 +606,7 @@ api_response_t *PetAPI_uploadFile(apiClient_t *apiClient, long petId, cJSON *PetAPIlocalVarJSON = cJSON_Parse(apiClient->dataReceived); api_response_t *elementToReturn = api_response_parseFromJSON( PetAPIlocalVarJSON); - cJSON_Delete(PetAPIlocalVarJSON); + cJSON_Delete(PetAPIlocalVarJSON); if(elementToReturn == NULL) { // return 0; } diff --git a/samples/client/petstore/c/api/StoreAPI.c b/samples/client/petstore/c/api/StoreAPI.c index d1269274a84..11dabde964f 100644 --- a/samples/client/petstore/c/api/StoreAPI.c +++ b/samples/client/petstore/c/api/StoreAPI.c @@ -111,7 +111,8 @@ list_t *StoreAPI_getInventory(apiClient_t *apiClient) { list_t *elementToReturn = list_create(); cJSON_ArrayForEach(VarJSON, localVarJSON) { keyValuePair_t *keyPair = - keyValuePair_create(strdup(VarJSON->string), cJSON_Print( + keyValuePair_create(strdup( + VarJSON->string), cJSON_Print( VarJSON)); list_addElement(elementToReturn, keyPair); } @@ -191,7 +192,7 @@ order_t *StoreAPI_getOrderById(apiClient_t *apiClient, long orderId) { // nonprimitive not container cJSON *StoreAPIlocalVarJSON = cJSON_Parse(apiClient->dataReceived); order_t *elementToReturn = order_parseFromJSON(StoreAPIlocalVarJSON); - cJSON_Delete(StoreAPIlocalVarJSON); + cJSON_Delete(StoreAPIlocalVarJSON); if(elementToReturn == NULL) { // return 0; } @@ -259,7 +260,7 @@ order_t *StoreAPI_placeOrder(apiClient_t *apiClient, order_t *body) { // nonprimitive not container cJSON *StoreAPIlocalVarJSON = cJSON_Parse(apiClient->dataReceived); order_t *elementToReturn = order_parseFromJSON(StoreAPIlocalVarJSON); - cJSON_Delete(StoreAPIlocalVarJSON); + cJSON_Delete(StoreAPIlocalVarJSON); if(elementToReturn == NULL) { // return 0; } diff --git a/samples/client/petstore/c/api/UserAPI.c b/samples/client/petstore/c/api/UserAPI.c index ae618b88696..9170293951e 100644 --- a/samples/client/petstore/c/api/UserAPI.c +++ b/samples/client/petstore/c/api/UserAPI.c @@ -331,7 +331,7 @@ user_t *UserAPI_getUserByName(apiClient_t *apiClient, char *username) { // nonprimitive not container cJSON *UserAPIlocalVarJSON = cJSON_Parse(apiClient->dataReceived); user_t *elementToReturn = user_parseFromJSON(UserAPIlocalVarJSON); - cJSON_Delete(UserAPIlocalVarJSON); + cJSON_Delete(UserAPIlocalVarJSON); if(elementToReturn == NULL) { // return 0; } diff --git a/samples/client/petstore/c/src/apiClient.c b/samples/client/petstore/c/src/apiClient.c index afb0e096cf1..eb569bd27a3 100644 --- a/samples/client/petstore/c/src/apiClient.c +++ b/samples/client/petstore/c/src/apiClient.c @@ -22,7 +22,7 @@ apiClient_t *apiClient_create() { } void apiClient_free(apiClient_t *apiClient) { - if(apiClient->apiKeys) { + if(apiClient->accessToken) { list_free(apiClient->apiKeys); } if(apiClient->accessToken) {