From 812d89cbe589391350d01aa6120182049243d563 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ernesto=20Fern=C3=A1ndez?= Date: Thu, 26 Dec 2024 13:27:38 -0300 Subject: [PATCH] Fix a few issues with the C generator (part 7) (#20366) * [C] Test nullable booleans in form data * [C] Ban implicit int to pointer conversions * [C] Declare valueForm for bool as char * * Update samples --- .../C-libcurl/CMakeLists.txt.mustache | 2 +- .../resources/C-libcurl/api-body.mustache | 4 +- .../src/test/resources/2_0/c/petstore.yaml | 19 +++++ .../client/others/c/bearerAuth/CMakeLists.txt | 2 +- .../c-useJsonUnformatted/CMakeLists.txt | 2 +- .../petstore/c-useJsonUnformatted/README.md | 1 + .../c-useJsonUnformatted/api/StoreAPI.c | 82 +++++++++++++++++++ .../c-useJsonUnformatted/api/StoreAPI.h | 6 ++ .../c-useJsonUnformatted/docs/StoreAPI.md | 31 +++++++ samples/client/petstore/c/README.md | 1 + samples/client/petstore/c/api/StoreAPI.c | 82 +++++++++++++++++++ samples/client/petstore/c/api/StoreAPI.h | 6 ++ samples/client/petstore/c/docs/StoreAPI.md | 31 +++++++ 13 files changed, 264 insertions(+), 5 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 dfbd9ee55986..fa59c65e07ba 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 @@ -6,7 +6,7 @@ cmake_policy(SET CMP0063 NEW) set(CMAKE_C_VISIBILITY_PRESET default) set(CMAKE_VISIBILITY_INLINES_HIDDEN OFF) set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON) -set(CMAKE_C_FLAGS "-Werror=implicit-function-declaration -Werror=missing-declarations") +set(CMAKE_C_FLAGS "-Werror=implicit-function-declaration -Werror=missing-declarations -Werror=int-conversion") option(BUILD_SHARED_LIBS "Build using shared libraries" ON) 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 185e8c845a0a..0563a5f70c4d 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 @@ -275,7 +275,7 @@ end: {{/isFile}} {{^isFile}} char *keyForm_{{paramName}} = NULL; - {{#isPrimitiveType}}{{#isNumber}}{{{dataType}}}{{/isNumber}}{{#isLong}}{{{dataType}}}{{/isLong}}{{#isInteger}}{{{dataType}}}{{/isInteger}}{{#isDouble}}{{{dataType}}}{{/isDouble}}{{#isFloat}}{{{dataType}}}{{/isFloat}}{{#isBoolean}}{{dataType}}{{/isBoolean}}{{#isEnum}}{{#isString}}{{projectName}}_{{operationId}}_{{baseName}}_e{{/isString}}{{/isEnum}}{{^isEnum}}{{#isString}}{{{dataType}}} *{{/isString}}{{/isEnum}}{{#isByteArray}}{{{dataType}}} *{{/isByteArray}}{{#isDate}}{{{dataType}}}{{/isDate}}{{#isDateTime}}{{{dataType}}}{{/isDateTime}}{{#isFile}}{{{dataType}}}{{/isFile}}{{/isPrimitiveType}}{{^isPrimitiveType}}{{#isModel}}{{#isEnum}}{{datatypeWithEnum}}_e{{/isEnum}}{{^isEnum}}{{{dataType}}}_t *{{/isEnum}}{{/isModel}}{{^isModel}}{{#isEnum}}{{datatypeWithEnum}}_e{{/isEnum}}{{/isModel}}{{#isUuid}}{{dataType}} *{{/isUuid}}{{#isEmail}}{{dataType}}{{/isEmail}}{{/isPrimitiveType}} valueForm_{{paramName}} = 0; + {{#isPrimitiveType}}{{#isNumber}}{{{dataType}}}{{/isNumber}}{{#isLong}}{{{dataType}}}{{/isLong}}{{#isInteger}}{{{dataType}}}{{/isInteger}}{{#isDouble}}{{{dataType}}}{{/isDouble}}{{#isFloat}}{{{dataType}}}{{/isFloat}}{{#isBoolean}}char *{{/isBoolean}}{{#isEnum}}{{#isString}}{{projectName}}_{{operationId}}_{{baseName}}_e{{/isString}}{{/isEnum}}{{^isEnum}}{{#isString}}{{{dataType}}} *{{/isString}}{{/isEnum}}{{#isByteArray}}{{{dataType}}} *{{/isByteArray}}{{#isDate}}{{{dataType}}}{{/isDate}}{{#isDateTime}}{{{dataType}}}{{/isDateTime}}{{#isFile}}{{{dataType}}}{{/isFile}}{{/isPrimitiveType}}{{^isPrimitiveType}}{{#isModel}}{{#isEnum}}{{datatypeWithEnum}}_e{{/isEnum}}{{^isEnum}}{{{dataType}}}_t *{{/isEnum}}{{/isModel}}{{^isModel}}{{#isEnum}}{{datatypeWithEnum}}_e{{/isEnum}}{{/isModel}}{{#isUuid}}{{dataType}} *{{/isUuid}}{{#isEmail}}{{dataType}}{{/isEmail}}{{/isPrimitiveType}} valueForm_{{paramName}} = 0; keyValuePair_t *keyPairForm_{{paramName}} = 0; {{/isFile}} if ({{paramName}} != {{^isEnum}}NULL{{/isEnum}}{{#isEnum}}0{{/isEnum}}) @@ -301,7 +301,7 @@ end: valueForm_{{paramName}} = {{#isString}}{{^isEnum}}strdup({{/isEnum}}{{/isString}}({{{paramName}}}){{#isString}}{{^isEnum}}){{/isEnum}}{{/isString}}; {{/isBoolean}} {{/isInteger}} - keyPairForm_{{paramName}} = keyValuePair_create(keyForm_{{paramName}},{{#isString}}{{#isEnum}}(void *){{/isEnum}}{{/isString}}{{^isString}}&{{/isString}}valueForm_{{paramName}}); + keyPairForm_{{paramName}} = keyValuePair_create(keyForm_{{paramName}},{{#isString}}{{#isEnum}}(void *){{/isEnum}}{{/isString}}{{^isString}}{{^isBoolean}}&{{/isBoolean}}{{/isString}}valueForm_{{paramName}}); list_addElement(localVarFormParameters,keyPairForm_{{paramName}}); {{/isFile}} } 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 f20f1569b6db..d8d01947081c 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 @@ -395,6 +395,25 @@ paths: schema: type: string description: Thank you message + '/store/recommend': + post: + tags: + - store + summary: Would you recommend our service to a friend? + description: '' + operationId: sendRecommend + parameters: + - in: formData + name: recommend + description: Would you recommend us or not? + required: no + type: boolean + responses: + '200': + description: successful operation + schema: + type: string + description: Thank you message /store/daysWithoutIncident: get: tags: diff --git a/samples/client/others/c/bearerAuth/CMakeLists.txt b/samples/client/others/c/bearerAuth/CMakeLists.txt index 2dc8b7b11ace..e359d41c7789 100644 --- a/samples/client/others/c/bearerAuth/CMakeLists.txt +++ b/samples/client/others/c/bearerAuth/CMakeLists.txt @@ -6,7 +6,7 @@ cmake_policy(SET CMP0063 NEW) set(CMAKE_C_VISIBILITY_PRESET default) set(CMAKE_VISIBILITY_INLINES_HIDDEN OFF) set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON) -set(CMAKE_C_FLAGS "-Werror=implicit-function-declaration -Werror=missing-declarations") +set(CMAKE_C_FLAGS "-Werror=implicit-function-declaration -Werror=missing-declarations -Werror=int-conversion") option(BUILD_SHARED_LIBS "Build using shared libraries" ON) diff --git a/samples/client/petstore/c-useJsonUnformatted/CMakeLists.txt b/samples/client/petstore/c-useJsonUnformatted/CMakeLists.txt index c5a0b31ca181..cd72ab391660 100644 --- a/samples/client/petstore/c-useJsonUnformatted/CMakeLists.txt +++ b/samples/client/petstore/c-useJsonUnformatted/CMakeLists.txt @@ -6,7 +6,7 @@ cmake_policy(SET CMP0063 NEW) set(CMAKE_C_VISIBILITY_PRESET default) set(CMAKE_VISIBILITY_INLINES_HIDDEN OFF) set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON) -set(CMAKE_C_FLAGS "-Werror=implicit-function-declaration -Werror=missing-declarations") +set(CMAKE_C_FLAGS "-Werror=implicit-function-declaration -Werror=missing-declarations -Werror=int-conversion") option(BUILD_SHARED_LIBS "Build using shared libraries" ON) diff --git a/samples/client/petstore/c-useJsonUnformatted/README.md b/samples/client/petstore/c-useJsonUnformatted/README.md index 239e0f4c8ff4..a7cf2d3100af 100644 --- a/samples/client/petstore/c-useJsonUnformatted/README.md +++ b/samples/client/petstore/c-useJsonUnformatted/README.md @@ -84,6 +84,7 @@ Category | Method | HTTP request | Description *StoreAPI* | [**StoreAPI_placeOrder**](docs/StoreAPI.md#StoreAPI_placeOrder) | **POST** /store/order | Place an order for a pet *StoreAPI* | [**StoreAPI_sendFeedback**](docs/StoreAPI.md#StoreAPI_sendFeedback) | **POST** /store/feedback | Send us a feedback message *StoreAPI* | [**StoreAPI_sendRating**](docs/StoreAPI.md#StoreAPI_sendRating) | **POST** /store/rating/{rating} | How would you rate our service? +*StoreAPI* | [**StoreAPI_sendRecommend**](docs/StoreAPI.md#StoreAPI_sendRecommend) | **POST** /store/recommend | Would you recommend our service to a friend? *UserAPI* | [**UserAPI_createUser**](docs/UserAPI.md#UserAPI_createUser) | **POST** /user | Create user *UserAPI* | [**UserAPI_createUsersWithArrayInput**](docs/UserAPI.md#UserAPI_createUsersWithArrayInput) | **POST** /user/createWithArray | Creates list of users with given input array *UserAPI* | [**UserAPI_createUsersWithListInput**](docs/UserAPI.md#UserAPI_createUsersWithListInput) | **POST** /user/createWithList | Creates list of users with given input array diff --git a/samples/client/petstore/c-useJsonUnformatted/api/StoreAPI.c b/samples/client/petstore/c-useJsonUnformatted/api/StoreAPI.c index 11b7d9b4224a..f3e0009272f4 100644 --- a/samples/client/petstore/c-useJsonUnformatted/api/StoreAPI.c +++ b/samples/client/petstore/c-useJsonUnformatted/api/StoreAPI.c @@ -541,3 +541,85 @@ StoreAPI_sendRating(apiClient_t *apiClient, openapi_petstore_sendRating_rating_e } +// Would you recommend our service to a friend? +// +char* +StoreAPI_sendRecommend(apiClient_t *apiClient, int *recommend) +{ + list_t *localVarQueryParameters = NULL; + list_t *localVarHeaderParameters = NULL; + list_t *localVarFormParameters = list_createList(); + list_t *localVarHeaderType = list_createList(); + list_t *localVarContentType = list_createList(); + char *localVarBodyParameters = NULL; + size_t localVarBodyLength = 0; + + // clear the error code from the previous api call + apiClient->response_code = 0; + + // create the path + long sizeOfPath = strlen("/store/recommend")+1; + char *localVarPath = malloc(sizeOfPath); + snprintf(localVarPath, sizeOfPath, "/store/recommend"); + + + + + + // form parameters + char *keyForm_recommend = NULL; + char * valueForm_recommend = 0; + keyValuePair_t *keyPairForm_recommend = 0; + if (recommend != NULL) + { + keyForm_recommend = strdup("recommend"); + valueForm_recommend = calloc(1,MAX_NUMBER_LENGTH); + snprintf(valueForm_recommend, MAX_NUMBER_LENGTH, "%d", *recommend); + keyPairForm_recommend = keyValuePair_create(keyForm_recommend,valueForm_recommend); + list_addElement(localVarFormParameters,keyPairForm_recommend); + } + list_addElement(localVarHeaderType,"*/*"); //produces + list_addElement(localVarContentType,"multipart/form-data"); //consumes + apiClient_invoke(apiClient, + localVarPath, + localVarQueryParameters, + localVarHeaderParameters, + localVarFormParameters, + localVarHeaderType, + localVarContentType, + localVarBodyParameters, + localVarBodyLength, + "POST"); + + // uncomment below to debug the error response + //if (apiClient->response_code == 200) { + // printf("%s\n","successful operation"); + //} + //primitive return type simple string + char* elementToReturn = NULL; + if(apiClient->response_code >= 200 && apiClient->response_code < 300) + elementToReturn = strdup((char*)apiClient->dataReceived); + + if (apiClient->dataReceived) { + free(apiClient->dataReceived); + apiClient->dataReceived = NULL; + apiClient->dataReceivedLen = 0; + } + + + list_freeList(localVarFormParameters); + list_freeList(localVarHeaderType); + list_freeList(localVarContentType); + free(localVarPath); + if (keyForm_recommend) { + free(keyForm_recommend); + keyForm_recommend = NULL; + } + free(keyPairForm_recommend); + return elementToReturn; +end: + free(localVarPath); + return NULL; + +} + diff --git a/samples/client/petstore/c-useJsonUnformatted/api/StoreAPI.h b/samples/client/petstore/c-useJsonUnformatted/api/StoreAPI.h index 1608e2b27d66..693d21b7dd64 100644 --- a/samples/client/petstore/c-useJsonUnformatted/api/StoreAPI.h +++ b/samples/client/petstore/c-useJsonUnformatted/api/StoreAPI.h @@ -53,3 +53,9 @@ char* StoreAPI_sendRating(apiClient_t *apiClient, openapi_petstore_sendRating_rating_e rating); +// Would you recommend our service to a friend? +// +char* +StoreAPI_sendRecommend(apiClient_t *apiClient, int *recommend); + + diff --git a/samples/client/petstore/c-useJsonUnformatted/docs/StoreAPI.md b/samples/client/petstore/c-useJsonUnformatted/docs/StoreAPI.md index 11fab3fe58ad..8705a994803a 100644 --- a/samples/client/petstore/c-useJsonUnformatted/docs/StoreAPI.md +++ b/samples/client/petstore/c-useJsonUnformatted/docs/StoreAPI.md @@ -10,6 +10,7 @@ Method | HTTP request | Description [**StoreAPI_placeOrder**](StoreAPI.md#StoreAPI_placeOrder) | **POST** /store/order | Place an order for a pet [**StoreAPI_sendFeedback**](StoreAPI.md#StoreAPI_sendFeedback) | **POST** /store/feedback | Send us a feedback message [**StoreAPI_sendRating**](StoreAPI.md#StoreAPI_sendRating) | **POST** /store/rating/{rating} | How would you rate our service? +[**StoreAPI_sendRecommend**](StoreAPI.md#StoreAPI_sendRecommend) | **POST** /store/recommend | Would you recommend our service to a friend? # **StoreAPI_deleteOrder** @@ -195,3 +196,33 @@ No authorization required [[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) +# **StoreAPI_sendRecommend** +```c +// Would you recommend our service to a friend? +// +char* StoreAPI_sendRecommend(apiClient_t *apiClient, int *recommend); +``` + +### Parameters +Name | Type | Description | Notes +------------- | ------------- | ------------- | ------------- +**apiClient** | **apiClient_t \*** | context containing the client configuration | +**recommend** | **int \*** | Would you recommend us or not? | [optional] + +### Return type + +char* + + + +### Authorization + +No authorization required + +### HTTP request headers + + - **Content-Type**: multipart/form-data + - **Accept**: */* + +[[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) + diff --git a/samples/client/petstore/c/README.md b/samples/client/petstore/c/README.md index 239e0f4c8ff4..a7cf2d3100af 100644 --- a/samples/client/petstore/c/README.md +++ b/samples/client/petstore/c/README.md @@ -84,6 +84,7 @@ Category | Method | HTTP request | Description *StoreAPI* | [**StoreAPI_placeOrder**](docs/StoreAPI.md#StoreAPI_placeOrder) | **POST** /store/order | Place an order for a pet *StoreAPI* | [**StoreAPI_sendFeedback**](docs/StoreAPI.md#StoreAPI_sendFeedback) | **POST** /store/feedback | Send us a feedback message *StoreAPI* | [**StoreAPI_sendRating**](docs/StoreAPI.md#StoreAPI_sendRating) | **POST** /store/rating/{rating} | How would you rate our service? +*StoreAPI* | [**StoreAPI_sendRecommend**](docs/StoreAPI.md#StoreAPI_sendRecommend) | **POST** /store/recommend | Would you recommend our service to a friend? *UserAPI* | [**UserAPI_createUser**](docs/UserAPI.md#UserAPI_createUser) | **POST** /user | Create user *UserAPI* | [**UserAPI_createUsersWithArrayInput**](docs/UserAPI.md#UserAPI_createUsersWithArrayInput) | **POST** /user/createWithArray | Creates list of users with given input array *UserAPI* | [**UserAPI_createUsersWithListInput**](docs/UserAPI.md#UserAPI_createUsersWithListInput) | **POST** /user/createWithList | Creates list of users with given input array diff --git a/samples/client/petstore/c/api/StoreAPI.c b/samples/client/petstore/c/api/StoreAPI.c index 6da10d3e0807..4d039fdf19ab 100644 --- a/samples/client/petstore/c/api/StoreAPI.c +++ b/samples/client/petstore/c/api/StoreAPI.c @@ -541,3 +541,85 @@ StoreAPI_sendRating(apiClient_t *apiClient, openapi_petstore_sendRating_rating_e } +// Would you recommend our service to a friend? +// +char* +StoreAPI_sendRecommend(apiClient_t *apiClient, int *recommend) +{ + list_t *localVarQueryParameters = NULL; + list_t *localVarHeaderParameters = NULL; + list_t *localVarFormParameters = list_createList(); + list_t *localVarHeaderType = list_createList(); + list_t *localVarContentType = list_createList(); + char *localVarBodyParameters = NULL; + size_t localVarBodyLength = 0; + + // clear the error code from the previous api call + apiClient->response_code = 0; + + // create the path + long sizeOfPath = strlen("/store/recommend")+1; + char *localVarPath = malloc(sizeOfPath); + snprintf(localVarPath, sizeOfPath, "/store/recommend"); + + + + + + // form parameters + char *keyForm_recommend = NULL; + char * valueForm_recommend = 0; + keyValuePair_t *keyPairForm_recommend = 0; + if (recommend != NULL) + { + keyForm_recommend = strdup("recommend"); + valueForm_recommend = calloc(1,MAX_NUMBER_LENGTH); + snprintf(valueForm_recommend, MAX_NUMBER_LENGTH, "%d", *recommend); + keyPairForm_recommend = keyValuePair_create(keyForm_recommend,valueForm_recommend); + list_addElement(localVarFormParameters,keyPairForm_recommend); + } + list_addElement(localVarHeaderType,"*/*"); //produces + list_addElement(localVarContentType,"multipart/form-data"); //consumes + apiClient_invoke(apiClient, + localVarPath, + localVarQueryParameters, + localVarHeaderParameters, + localVarFormParameters, + localVarHeaderType, + localVarContentType, + localVarBodyParameters, + localVarBodyLength, + "POST"); + + // uncomment below to debug the error response + //if (apiClient->response_code == 200) { + // printf("%s\n","successful operation"); + //} + //primitive return type simple string + char* elementToReturn = NULL; + if(apiClient->response_code >= 200 && apiClient->response_code < 300) + elementToReturn = strdup((char*)apiClient->dataReceived); + + if (apiClient->dataReceived) { + free(apiClient->dataReceived); + apiClient->dataReceived = NULL; + apiClient->dataReceivedLen = 0; + } + + + list_freeList(localVarFormParameters); + list_freeList(localVarHeaderType); + list_freeList(localVarContentType); + free(localVarPath); + if (keyForm_recommend) { + free(keyForm_recommend); + keyForm_recommend = NULL; + } + free(keyPairForm_recommend); + return elementToReturn; +end: + free(localVarPath); + return NULL; + +} + diff --git a/samples/client/petstore/c/api/StoreAPI.h b/samples/client/petstore/c/api/StoreAPI.h index 1608e2b27d66..693d21b7dd64 100644 --- a/samples/client/petstore/c/api/StoreAPI.h +++ b/samples/client/petstore/c/api/StoreAPI.h @@ -53,3 +53,9 @@ char* StoreAPI_sendRating(apiClient_t *apiClient, openapi_petstore_sendRating_rating_e rating); +// Would you recommend our service to a friend? +// +char* +StoreAPI_sendRecommend(apiClient_t *apiClient, int *recommend); + + diff --git a/samples/client/petstore/c/docs/StoreAPI.md b/samples/client/petstore/c/docs/StoreAPI.md index 11fab3fe58ad..8705a994803a 100644 --- a/samples/client/petstore/c/docs/StoreAPI.md +++ b/samples/client/petstore/c/docs/StoreAPI.md @@ -10,6 +10,7 @@ Method | HTTP request | Description [**StoreAPI_placeOrder**](StoreAPI.md#StoreAPI_placeOrder) | **POST** /store/order | Place an order for a pet [**StoreAPI_sendFeedback**](StoreAPI.md#StoreAPI_sendFeedback) | **POST** /store/feedback | Send us a feedback message [**StoreAPI_sendRating**](StoreAPI.md#StoreAPI_sendRating) | **POST** /store/rating/{rating} | How would you rate our service? +[**StoreAPI_sendRecommend**](StoreAPI.md#StoreAPI_sendRecommend) | **POST** /store/recommend | Would you recommend our service to a friend? # **StoreAPI_deleteOrder** @@ -195,3 +196,33 @@ No authorization required [[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) +# **StoreAPI_sendRecommend** +```c +// Would you recommend our service to a friend? +// +char* StoreAPI_sendRecommend(apiClient_t *apiClient, int *recommend); +``` + +### Parameters +Name | Type | Description | Notes +------------- | ------------- | ------------- | ------------- +**apiClient** | **apiClient_t \*** | context containing the client configuration | +**recommend** | **int \*** | Would you recommend us or not? | [optional] + +### Return type + +char* + + + +### Authorization + +No authorization required + +### HTTP request headers + + - **Content-Type**: multipart/form-data + - **Accept**: */* + +[[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) +