Skip to content

Commit 7edb387

Browse files
eafertimon-sbr
authored andcommitted
Fix a few issues with the C generator (part 7) (OpenAPITools#20366)
* [C] Test nullable booleans in form data * [C] Ban implicit int to pointer conversions * [C] Declare valueForm for bool as char * * Update samples
1 parent 6df02c7 commit 7edb387

File tree

13 files changed

+264
-5
lines changed

13 files changed

+264
-5
lines changed

modules/openapi-generator/src/main/resources/C-libcurl/CMakeLists.txt.mustache

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ cmake_policy(SET CMP0063 NEW)
66
set(CMAKE_C_VISIBILITY_PRESET default)
77
set(CMAKE_VISIBILITY_INLINES_HIDDEN OFF)
88
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
9-
set(CMAKE_C_FLAGS "-Werror=implicit-function-declaration -Werror=missing-declarations")
9+
set(CMAKE_C_FLAGS "-Werror=implicit-function-declaration -Werror=missing-declarations -Werror=int-conversion")
1010

1111
option(BUILD_SHARED_LIBS "Build using shared libraries" ON)
1212

modules/openapi-generator/src/main/resources/C-libcurl/api-body.mustache

+2-2
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ end:
275275
{{/isFile}}
276276
{{^isFile}}
277277
char *keyForm_{{paramName}} = NULL;
278-
{{#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;
278+
{{#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;
279279
keyValuePair_t *keyPairForm_{{paramName}} = 0;
280280
{{/isFile}}
281281
if ({{paramName}} != {{^isEnum}}NULL{{/isEnum}}{{#isEnum}}0{{/isEnum}})
@@ -301,7 +301,7 @@ end:
301301
valueForm_{{paramName}} = {{#isString}}{{^isEnum}}strdup({{/isEnum}}{{/isString}}({{{paramName}}}){{#isString}}{{^isEnum}}){{/isEnum}}{{/isString}};
302302
{{/isBoolean}}
303303
{{/isInteger}}
304-
keyPairForm_{{paramName}} = keyValuePair_create(keyForm_{{paramName}},{{#isString}}{{#isEnum}}(void *){{/isEnum}}{{/isString}}{{^isString}}&{{/isString}}valueForm_{{paramName}});
304+
keyPairForm_{{paramName}} = keyValuePair_create(keyForm_{{paramName}},{{#isString}}{{#isEnum}}(void *){{/isEnum}}{{/isString}}{{^isString}}{{^isBoolean}}&{{/isBoolean}}{{/isString}}valueForm_{{paramName}});
305305
list_addElement(localVarFormParameters,keyPairForm_{{paramName}});
306306
{{/isFile}}
307307
}

modules/openapi-generator/src/test/resources/2_0/c/petstore.yaml

+19
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,25 @@ paths:
395395
schema:
396396
type: string
397397
description: Thank you message
398+
'/store/recommend':
399+
post:
400+
tags:
401+
- store
402+
summary: Would you recommend our service to a friend?
403+
description: ''
404+
operationId: sendRecommend
405+
parameters:
406+
- in: formData
407+
name: recommend
408+
description: Would you recommend us or not?
409+
required: no
410+
type: boolean
411+
responses:
412+
'200':
413+
description: successful operation
414+
schema:
415+
type: string
416+
description: Thank you message
398417
/store/daysWithoutIncident:
399418
get:
400419
tags:

samples/client/others/c/bearerAuth/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ cmake_policy(SET CMP0063 NEW)
66
set(CMAKE_C_VISIBILITY_PRESET default)
77
set(CMAKE_VISIBILITY_INLINES_HIDDEN OFF)
88
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
9-
set(CMAKE_C_FLAGS "-Werror=implicit-function-declaration -Werror=missing-declarations")
9+
set(CMAKE_C_FLAGS "-Werror=implicit-function-declaration -Werror=missing-declarations -Werror=int-conversion")
1010

1111
option(BUILD_SHARED_LIBS "Build using shared libraries" ON)
1212

samples/client/petstore/c-useJsonUnformatted/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ cmake_policy(SET CMP0063 NEW)
66
set(CMAKE_C_VISIBILITY_PRESET default)
77
set(CMAKE_VISIBILITY_INLINES_HIDDEN OFF)
88
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
9-
set(CMAKE_C_FLAGS "-Werror=implicit-function-declaration -Werror=missing-declarations")
9+
set(CMAKE_C_FLAGS "-Werror=implicit-function-declaration -Werror=missing-declarations -Werror=int-conversion")
1010

1111
option(BUILD_SHARED_LIBS "Build using shared libraries" ON)
1212

samples/client/petstore/c-useJsonUnformatted/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ Category | Method | HTTP request | Description
8484
*StoreAPI* | [**StoreAPI_placeOrder**](docs/StoreAPI.md#StoreAPI_placeOrder) | **POST** /store/order | Place an order for a pet
8585
*StoreAPI* | [**StoreAPI_sendFeedback**](docs/StoreAPI.md#StoreAPI_sendFeedback) | **POST** /store/feedback | Send us a feedback message
8686
*StoreAPI* | [**StoreAPI_sendRating**](docs/StoreAPI.md#StoreAPI_sendRating) | **POST** /store/rating/{rating} | How would you rate our service?
87+
*StoreAPI* | [**StoreAPI_sendRecommend**](docs/StoreAPI.md#StoreAPI_sendRecommend) | **POST** /store/recommend | Would you recommend our service to a friend?
8788
*UserAPI* | [**UserAPI_createUser**](docs/UserAPI.md#UserAPI_createUser) | **POST** /user | Create user
8889
*UserAPI* | [**UserAPI_createUsersWithArrayInput**](docs/UserAPI.md#UserAPI_createUsersWithArrayInput) | **POST** /user/createWithArray | Creates list of users with given input array
8990
*UserAPI* | [**UserAPI_createUsersWithListInput**](docs/UserAPI.md#UserAPI_createUsersWithListInput) | **POST** /user/createWithList | Creates list of users with given input array

samples/client/petstore/c-useJsonUnformatted/api/StoreAPI.c

+82
Original file line numberDiff line numberDiff line change
@@ -541,3 +541,85 @@ StoreAPI_sendRating(apiClient_t *apiClient, openapi_petstore_sendRating_rating_e
541541

542542
}
543543

544+
// Would you recommend our service to a friend?
545+
//
546+
char*
547+
StoreAPI_sendRecommend(apiClient_t *apiClient, int *recommend)
548+
{
549+
list_t *localVarQueryParameters = NULL;
550+
list_t *localVarHeaderParameters = NULL;
551+
list_t *localVarFormParameters = list_createList();
552+
list_t *localVarHeaderType = list_createList();
553+
list_t *localVarContentType = list_createList();
554+
char *localVarBodyParameters = NULL;
555+
size_t localVarBodyLength = 0;
556+
557+
// clear the error code from the previous api call
558+
apiClient->response_code = 0;
559+
560+
// create the path
561+
long sizeOfPath = strlen("/store/recommend")+1;
562+
char *localVarPath = malloc(sizeOfPath);
563+
snprintf(localVarPath, sizeOfPath, "/store/recommend");
564+
565+
566+
567+
568+
569+
// form parameters
570+
char *keyForm_recommend = NULL;
571+
char * valueForm_recommend = 0;
572+
keyValuePair_t *keyPairForm_recommend = 0;
573+
if (recommend != NULL)
574+
{
575+
keyForm_recommend = strdup("recommend");
576+
valueForm_recommend = calloc(1,MAX_NUMBER_LENGTH);
577+
snprintf(valueForm_recommend, MAX_NUMBER_LENGTH, "%d", *recommend);
578+
keyPairForm_recommend = keyValuePair_create(keyForm_recommend,valueForm_recommend);
579+
list_addElement(localVarFormParameters,keyPairForm_recommend);
580+
}
581+
list_addElement(localVarHeaderType,"*/*"); //produces
582+
list_addElement(localVarContentType,"multipart/form-data"); //consumes
583+
apiClient_invoke(apiClient,
584+
localVarPath,
585+
localVarQueryParameters,
586+
localVarHeaderParameters,
587+
localVarFormParameters,
588+
localVarHeaderType,
589+
localVarContentType,
590+
localVarBodyParameters,
591+
localVarBodyLength,
592+
"POST");
593+
594+
// uncomment below to debug the error response
595+
//if (apiClient->response_code == 200) {
596+
// printf("%s\n","successful operation");
597+
//}
598+
//primitive return type simple string
599+
char* elementToReturn = NULL;
600+
if(apiClient->response_code >= 200 && apiClient->response_code < 300)
601+
elementToReturn = strdup((char*)apiClient->dataReceived);
602+
603+
if (apiClient->dataReceived) {
604+
free(apiClient->dataReceived);
605+
apiClient->dataReceived = NULL;
606+
apiClient->dataReceivedLen = 0;
607+
}
608+
609+
610+
list_freeList(localVarFormParameters);
611+
list_freeList(localVarHeaderType);
612+
list_freeList(localVarContentType);
613+
free(localVarPath);
614+
if (keyForm_recommend) {
615+
free(keyForm_recommend);
616+
keyForm_recommend = NULL;
617+
}
618+
free(keyPairForm_recommend);
619+
return elementToReturn;
620+
end:
621+
free(localVarPath);
622+
return NULL;
623+
624+
}
625+

samples/client/petstore/c-useJsonUnformatted/api/StoreAPI.h

+6
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,9 @@ char*
5353
StoreAPI_sendRating(apiClient_t *apiClient, openapi_petstore_sendRating_rating_e rating);
5454

5555

56+
// Would you recommend our service to a friend?
57+
//
58+
char*
59+
StoreAPI_sendRecommend(apiClient_t *apiClient, int *recommend);
60+
61+

samples/client/petstore/c-useJsonUnformatted/docs/StoreAPI.md

+31
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Method | HTTP request | Description
1010
[**StoreAPI_placeOrder**](StoreAPI.md#StoreAPI_placeOrder) | **POST** /store/order | Place an order for a pet
1111
[**StoreAPI_sendFeedback**](StoreAPI.md#StoreAPI_sendFeedback) | **POST** /store/feedback | Send us a feedback message
1212
[**StoreAPI_sendRating**](StoreAPI.md#StoreAPI_sendRating) | **POST** /store/rating/{rating} | How would you rate our service?
13+
[**StoreAPI_sendRecommend**](StoreAPI.md#StoreAPI_sendRecommend) | **POST** /store/recommend | Would you recommend our service to a friend?
1314

1415

1516
# **StoreAPI_deleteOrder**
@@ -195,3 +196,33 @@ No authorization required
195196

196197
[[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)
197198

199+
# **StoreAPI_sendRecommend**
200+
```c
201+
// Would you recommend our service to a friend?
202+
//
203+
char* StoreAPI_sendRecommend(apiClient_t *apiClient, int *recommend);
204+
```
205+
206+
### Parameters
207+
Name | Type | Description | Notes
208+
------------- | ------------- | ------------- | -------------
209+
**apiClient** | **apiClient_t \*** | context containing the client configuration |
210+
**recommend** | **int \*** | Would you recommend us or not? | [optional]
211+
212+
### Return type
213+
214+
char*
215+
216+
217+
218+
### Authorization
219+
220+
No authorization required
221+
222+
### HTTP request headers
223+
224+
- **Content-Type**: multipart/form-data
225+
- **Accept**: */*
226+
227+
[[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)
228+

samples/client/petstore/c/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ Category | Method | HTTP request | Description
8484
*StoreAPI* | [**StoreAPI_placeOrder**](docs/StoreAPI.md#StoreAPI_placeOrder) | **POST** /store/order | Place an order for a pet
8585
*StoreAPI* | [**StoreAPI_sendFeedback**](docs/StoreAPI.md#StoreAPI_sendFeedback) | **POST** /store/feedback | Send us a feedback message
8686
*StoreAPI* | [**StoreAPI_sendRating**](docs/StoreAPI.md#StoreAPI_sendRating) | **POST** /store/rating/{rating} | How would you rate our service?
87+
*StoreAPI* | [**StoreAPI_sendRecommend**](docs/StoreAPI.md#StoreAPI_sendRecommend) | **POST** /store/recommend | Would you recommend our service to a friend?
8788
*UserAPI* | [**UserAPI_createUser**](docs/UserAPI.md#UserAPI_createUser) | **POST** /user | Create user
8889
*UserAPI* | [**UserAPI_createUsersWithArrayInput**](docs/UserAPI.md#UserAPI_createUsersWithArrayInput) | **POST** /user/createWithArray | Creates list of users with given input array
8990
*UserAPI* | [**UserAPI_createUsersWithListInput**](docs/UserAPI.md#UserAPI_createUsersWithListInput) | **POST** /user/createWithList | Creates list of users with given input array

samples/client/petstore/c/api/StoreAPI.c

+82
Original file line numberDiff line numberDiff line change
@@ -541,3 +541,85 @@ StoreAPI_sendRating(apiClient_t *apiClient, openapi_petstore_sendRating_rating_e
541541

542542
}
543543

544+
// Would you recommend our service to a friend?
545+
//
546+
char*
547+
StoreAPI_sendRecommend(apiClient_t *apiClient, int *recommend)
548+
{
549+
list_t *localVarQueryParameters = NULL;
550+
list_t *localVarHeaderParameters = NULL;
551+
list_t *localVarFormParameters = list_createList();
552+
list_t *localVarHeaderType = list_createList();
553+
list_t *localVarContentType = list_createList();
554+
char *localVarBodyParameters = NULL;
555+
size_t localVarBodyLength = 0;
556+
557+
// clear the error code from the previous api call
558+
apiClient->response_code = 0;
559+
560+
// create the path
561+
long sizeOfPath = strlen("/store/recommend")+1;
562+
char *localVarPath = malloc(sizeOfPath);
563+
snprintf(localVarPath, sizeOfPath, "/store/recommend");
564+
565+
566+
567+
568+
569+
// form parameters
570+
char *keyForm_recommend = NULL;
571+
char * valueForm_recommend = 0;
572+
keyValuePair_t *keyPairForm_recommend = 0;
573+
if (recommend != NULL)
574+
{
575+
keyForm_recommend = strdup("recommend");
576+
valueForm_recommend = calloc(1,MAX_NUMBER_LENGTH);
577+
snprintf(valueForm_recommend, MAX_NUMBER_LENGTH, "%d", *recommend);
578+
keyPairForm_recommend = keyValuePair_create(keyForm_recommend,valueForm_recommend);
579+
list_addElement(localVarFormParameters,keyPairForm_recommend);
580+
}
581+
list_addElement(localVarHeaderType,"*/*"); //produces
582+
list_addElement(localVarContentType,"multipart/form-data"); //consumes
583+
apiClient_invoke(apiClient,
584+
localVarPath,
585+
localVarQueryParameters,
586+
localVarHeaderParameters,
587+
localVarFormParameters,
588+
localVarHeaderType,
589+
localVarContentType,
590+
localVarBodyParameters,
591+
localVarBodyLength,
592+
"POST");
593+
594+
// uncomment below to debug the error response
595+
//if (apiClient->response_code == 200) {
596+
// printf("%s\n","successful operation");
597+
//}
598+
//primitive return type simple string
599+
char* elementToReturn = NULL;
600+
if(apiClient->response_code >= 200 && apiClient->response_code < 300)
601+
elementToReturn = strdup((char*)apiClient->dataReceived);
602+
603+
if (apiClient->dataReceived) {
604+
free(apiClient->dataReceived);
605+
apiClient->dataReceived = NULL;
606+
apiClient->dataReceivedLen = 0;
607+
}
608+
609+
610+
list_freeList(localVarFormParameters);
611+
list_freeList(localVarHeaderType);
612+
list_freeList(localVarContentType);
613+
free(localVarPath);
614+
if (keyForm_recommend) {
615+
free(keyForm_recommend);
616+
keyForm_recommend = NULL;
617+
}
618+
free(keyPairForm_recommend);
619+
return elementToReturn;
620+
end:
621+
free(localVarPath);
622+
return NULL;
623+
624+
}
625+

samples/client/petstore/c/api/StoreAPI.h

+6
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,9 @@ char*
5353
StoreAPI_sendRating(apiClient_t *apiClient, openapi_petstore_sendRating_rating_e rating);
5454

5555

56+
// Would you recommend our service to a friend?
57+
//
58+
char*
59+
StoreAPI_sendRecommend(apiClient_t *apiClient, int *recommend);
60+
61+

samples/client/petstore/c/docs/StoreAPI.md

+31
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Method | HTTP request | Description
1010
[**StoreAPI_placeOrder**](StoreAPI.md#StoreAPI_placeOrder) | **POST** /store/order | Place an order for a pet
1111
[**StoreAPI_sendFeedback**](StoreAPI.md#StoreAPI_sendFeedback) | **POST** /store/feedback | Send us a feedback message
1212
[**StoreAPI_sendRating**](StoreAPI.md#StoreAPI_sendRating) | **POST** /store/rating/{rating} | How would you rate our service?
13+
[**StoreAPI_sendRecommend**](StoreAPI.md#StoreAPI_sendRecommend) | **POST** /store/recommend | Would you recommend our service to a friend?
1314

1415

1516
# **StoreAPI_deleteOrder**
@@ -195,3 +196,33 @@ No authorization required
195196

196197
[[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)
197198

199+
# **StoreAPI_sendRecommend**
200+
```c
201+
// Would you recommend our service to a friend?
202+
//
203+
char* StoreAPI_sendRecommend(apiClient_t *apiClient, int *recommend);
204+
```
205+
206+
### Parameters
207+
Name | Type | Description | Notes
208+
------------- | ------------- | ------------- | -------------
209+
**apiClient** | **apiClient_t \*** | context containing the client configuration |
210+
**recommend** | **int \*** | Would you recommend us or not? | [optional]
211+
212+
### Return type
213+
214+
char*
215+
216+
217+
218+
### Authorization
219+
220+
No authorization required
221+
222+
### HTTP request headers
223+
224+
- **Content-Type**: multipart/form-data
225+
- **Accept**: */*
226+
227+
[[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)
228+

0 commit comments

Comments
 (0)