Skip to content

Commit 6b1b5cc

Browse files
authored
Update c curl generator to support float (#21103)
* [C-Curl] Client generator does not handle float properly (#21092) - Change function signature to float* - Change generator to convert float to string - Similar change for double and long * [C-Curl] Client generator does not handle float properly (#21092) - Generate samples - Fix generator for headers and path params * Revert "[C-Curl] Client generator does not handle float properly (#21092)" This reverts commit ba044a6. * Revert "[C-Curl] Client generator does not handle float properly (#21092)" This reverts commit f99c5b0. * [C-Curl] Client generator does not handle float properly (#21092) - Convert float, double and long to string - Generate samples * [C-Curl] Client generator does not handle float properly (#21092) - Add missing yaml example file * [C-Curl] Client generator does not handle float properly (#21092) - Transfer float and double in scientific notation with resp. 7 and 16 decimals - Adapt string size to number of required characters * [C-Curl] Client generator does not handle float properly (#21092) - Fix unused variable. - Fix snprintf string * * [C-Curl] Client generator does not handle float properly (#21092) - Generating samples * * [C-Curl] Client generator does not handle float properly (#21092) - Always allocate the exact string size
1 parent fd84c8f commit 6b1b5cc

File tree

15 files changed

+394
-2
lines changed

15 files changed

+394
-2
lines changed

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

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
#include <stdlib.h>
22
#include <stdio.h>
33
#include <ctype.h>
4+
45
#include "{{classname}}.h"
56

67
#define MAX_NUMBER_LENGTH 16
78
#define MAX_BUFFER_LENGTH 4096
9+
#define MAX_NUMBER_LENGTH_LONG 21
810

911
{{#operations}}
1012
{{#operation}}
@@ -229,10 +231,16 @@ end:
229231
// query parameters
230232
{{^isArray}}
231233
char *keyQuery_{{{paramName}}} = NULL;
232-
{{#isPrimitiveType}}{{#isNumber}}{{{dataType}}}{{/isNumber}}{{#isLong}}{{{dataType}}}{{/isLong}}{{#isInteger}}char *{{/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}} valueQuery_{{{paramName}}} {{#isString}}{{^isEnum}}= NULL{{/isEnum}}{{/isString}}{{#isInteger}}= NULL{{/isInteger}}{{#isBoolean}}= NULL{{/isBoolean}};
234+
{{#isPrimitiveType}}{{#isNumber}}{{{dataType}}}{{/isNumber}}{{#isLong}}char *{{/isLong}}{{#isInteger}}char *{{/isInteger}}{{#isDouble}}char *{{/isDouble}}{{#isFloat}}char *{{/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}} valueQuery_{{{paramName}}} {{#isString}}{{^isEnum}}= NULL{{/isEnum}}{{/isString}}{{#isInteger}}= NULL{{/isInteger}}{{#isBoolean}}= NULL{{/isBoolean}}{{#isDouble}}= NULL{{/isDouble}}{{#isFloat}}= NULL{{/isFloat}};
233235
keyValuePair_t *keyPairQuery_{{paramName}} = 0;
234236
{{/isArray}}
237+
{{^isLong}}
238+
{{^isFloat}}
239+
{{^isDouble}}
235240
if ({{paramName}})
241+
{{/isDouble}}
242+
{{/isFloat}}
243+
{{/isLong}}
236244
{
237245
{{#isArray}}
238246
list_addElement(localVarQueryParameters,{{paramName}});
@@ -243,17 +251,43 @@ end:
243251
valueQuery_{{{paramName}}} = calloc(1,MAX_NUMBER_LENGTH);
244252
snprintf(valueQuery_{{{paramName}}}, MAX_NUMBER_LENGTH, "%d", *{{{paramName}}});
245253
{{/isInteger}}
254+
{{#isFloat}}
255+
int s = snprintf(NULL, 0, "%.7e", {{{paramName}}});
256+
if (s >= 0)
257+
{
258+
valueQuery_{{{paramName}}} = calloc(1,s+1);
259+
snprintf(valueQuery_{{{paramName}}}, s+1, "%.7e", {{{paramName}}});
260+
}
261+
{{/isFloat}}
262+
{{#isDouble}}
263+
int s = snprintf(NULL, 0, "%.16e", {{{paramName}}});
264+
if (s >= 0)
265+
{
266+
valueQuery_{{{paramName}}} = calloc(1,s+1);
267+
snprintf(valueQuery_{{{paramName}}}, s+1, "%.16e", {{{paramName}}});
268+
}
269+
{{/isDouble}}
270+
{{#isLong}}
271+
valueQuery_{{{paramName}}} = calloc(1,MAX_NUMBER_LENGTH_LONG);
272+
snprintf(valueQuery_{{{paramName}}}, MAX_NUMBER_LENGTH_LONG, "%d", {{{paramName}}});
273+
{{/isLong}}
246274
{{#isBoolean}}
247275
valueQuery_{{{paramName}}} = calloc(1,MAX_NUMBER_LENGTH);
248276
snprintf(valueQuery_{{{paramName}}}, MAX_NUMBER_LENGTH, "%d", *{{{paramName}}});
249277
{{/isBoolean}}
250278
{{^isInteger}}
251279
{{^isBoolean}}
280+
{{^isFloat}}
281+
{{^isDouble}}
282+
{{^isLong}}
252283
valueQuery_{{{paramName}}} = {{#isString}}{{^isEnum}}strdup({{/isEnum}}{{/isString}}({{{paramName}}}){{#isString}}{{^isEnum}}){{/isEnum}}{{/isString}};
284+
{{/isLong}}
285+
{{/isDouble}}
286+
{{/isFloat}}
253287
{{/isBoolean}}
254288
{{/isInteger}}
255289
keyPairQuery_{{paramName}} = keyValuePair_create(keyQuery_{{{paramName}}}, {{#isEnum}}strdup({{{operationId}}}_{{enumName}}_ToString(
256-
{{/isEnum}}{{^isString}}{{^isInteger}}{{^isBoolean}}&{{/isBoolean}}{{/isInteger}}{{/isString}}valueQuery_{{{paramName}}}{{#isEnum}})){{/isEnum}});
290+
{{/isEnum}}{{^isString}}{{^isInteger}}{{^isFloat}}{{^isDouble}}{{^isLong}}{{^isBoolean}}&{{/isBoolean}}{{/isLong}}{{/isDouble}}{{/isFloat}}{{/isInteger}}{{/isString}}valueQuery_{{{paramName}}}{{#isEnum}})){{/isEnum}});
257291
list_addElement(localVarQueryParameters,keyPairQuery_{{paramName}});
258292
{{/isArray}}
259293
}

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,40 @@ paths:
727727
responses:
728728
'200':
729729
description: successful operation
730+
'/user/test_int32_int64_float_double':
731+
get:
732+
tags:
733+
- user
734+
summary: test int32, int64 float and double query parameters in API
735+
description: This can test int32, int64 float and double query parameters in API.
736+
operationId: test_int32_int64_float_double
737+
produces:
738+
- application/xml
739+
- application/json
740+
parameters:
741+
- name: floatnum
742+
in: query
743+
description: A float number
744+
type: number
745+
format: float
746+
- name: doublenum
747+
in: query
748+
description: A double number
749+
type: number
750+
format: double
751+
- name: int32num
752+
in: query
753+
description: An int32 number
754+
type: integer
755+
format: int32
756+
- name: int64num
757+
in: query
758+
description: An int64 number
759+
type: integer
760+
format: int64
761+
responses:
762+
'200':
763+
description: successful operation
730764
securityDefinitions:
731765
petstore_auth:
732766
type: oauth2

samples/client/others/c/bearerAuth/api/DefaultAPI.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
#include <stdlib.h>
22
#include <stdio.h>
33
#include <ctype.h>
4+
45
#include "DefaultAPI.h"
56

67
#define MAX_NUMBER_LENGTH 16
78
#define MAX_BUFFER_LENGTH 4096
9+
#define MAX_NUMBER_LENGTH_LONG 21
810

911

1012
// Returns private information.

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ Category | Method | HTTP request | Description
9292
*UserAPI* | [**UserAPI_getUserByName**](docs/UserAPI.md#UserAPI_getUserByName) | **GET** /user/{username} | Get user by user name
9393
*UserAPI* | [**UserAPI_loginUser**](docs/UserAPI.md#UserAPI_loginUser) | **GET** /user/login | Logs user into the system
9494
*UserAPI* | [**UserAPI_logoutUser**](docs/UserAPI.md#UserAPI_logoutUser) | **GET** /user/logout | Logs out current logged in user session
95+
*UserAPI* | [**UserAPI_testInt32Int64FloatDouble**](docs/UserAPI.md#UserAPI_testInt32Int64FloatDouble) | **GET** /user/test_int32_int64_float_double | test int32, int64 float and double query parameters in API
9596
*UserAPI* | [**UserAPI_testIntAndBool**](docs/UserAPI.md#UserAPI_testIntAndBool) | **GET** /user/testIntAndBool | test integer and boolean query parameters in API
9697
*UserAPI* | [**UserAPI_updateUser**](docs/UserAPI.md#UserAPI_updateUser) | **PUT** /user/{username} | Updated user
9798

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
#include <stdlib.h>
22
#include <stdio.h>
33
#include <ctype.h>
4+
45
#include "PetAPI.h"
56

67
#define MAX_NUMBER_LENGTH 16
78
#define MAX_BUFFER_LENGTH 4096
9+
#define MAX_NUMBER_LENGTH_LONG 21
810

911
// Functions for enum STATUS for PetAPI_findPetsByStatus
1012

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
#include <stdlib.h>
22
#include <stdio.h>
33
#include <ctype.h>
4+
45
#include "StoreAPI.h"
56

67
#define MAX_NUMBER_LENGTH 16
78
#define MAX_BUFFER_LENGTH 4096
9+
#define MAX_NUMBER_LENGTH_LONG 21
810

911
// Functions for enum RATING for StoreAPI_sendRating
1012

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

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
#include <stdlib.h>
22
#include <stdio.h>
33
#include <ctype.h>
4+
45
#include "UserAPI.h"
56

67
#define MAX_NUMBER_LENGTH 16
78
#define MAX_BUFFER_LENGTH 4096
9+
#define MAX_NUMBER_LENGTH_LONG 21
810

911

1012
// Create user
@@ -594,6 +596,118 @@ UserAPI_logoutUser(apiClient_t *apiClient)
594596

595597

596598

599+
free(localVarPath);
600+
601+
}
602+
603+
// test int32, int64 float and double query parameters in API
604+
//
605+
// This can test int32, int64 float and double query parameters in API.
606+
//
607+
void
608+
UserAPI_testInt32Int64FloatDouble(apiClient_t *apiClient, float floatnum, double doublenum, int *int32num, long int64num)
609+
{
610+
list_t *localVarQueryParameters = list_createList();
611+
list_t *localVarHeaderParameters = NULL;
612+
list_t *localVarFormParameters = NULL;
613+
list_t *localVarHeaderType = NULL;
614+
list_t *localVarContentType = NULL;
615+
char *localVarBodyParameters = NULL;
616+
size_t localVarBodyLength = 0;
617+
618+
// clear the error code from the previous api call
619+
apiClient->response_code = 0;
620+
621+
// create the path
622+
char *localVarPath = strdup("/user/test_int32_int64_float_double");
623+
624+
625+
626+
627+
628+
// query parameters
629+
char *keyQuery_floatnum = NULL;
630+
char * valueQuery_floatnum = NULL;
631+
keyValuePair_t *keyPairQuery_floatnum = 0;
632+
{
633+
keyQuery_floatnum = strdup("floatnum");
634+
int s = snprintf(NULL, 0, "%.7e", floatnum);
635+
if (s >= 0)
636+
{
637+
valueQuery_floatnum = calloc(1,s+1);
638+
snprintf(valueQuery_floatnum, s+1, "%.7e", floatnum);
639+
}
640+
keyPairQuery_floatnum = keyValuePair_create(keyQuery_floatnum, valueQuery_floatnum);
641+
list_addElement(localVarQueryParameters,keyPairQuery_floatnum);
642+
}
643+
644+
// query parameters
645+
char *keyQuery_doublenum = NULL;
646+
char * valueQuery_doublenum = NULL;
647+
keyValuePair_t *keyPairQuery_doublenum = 0;
648+
{
649+
keyQuery_doublenum = strdup("doublenum");
650+
int s = snprintf(NULL, 0, "%.16e", doublenum);
651+
if (s >= 0)
652+
{
653+
valueQuery_doublenum = calloc(1,s+1);
654+
snprintf(valueQuery_doublenum, s+1, "%.16e", doublenum);
655+
}
656+
keyPairQuery_doublenum = keyValuePair_create(keyQuery_doublenum, valueQuery_doublenum);
657+
list_addElement(localVarQueryParameters,keyPairQuery_doublenum);
658+
}
659+
660+
// query parameters
661+
char *keyQuery_int32num = NULL;
662+
char * valueQuery_int32num = NULL;
663+
keyValuePair_t *keyPairQuery_int32num = 0;
664+
if (int32num)
665+
{
666+
keyQuery_int32num = strdup("int32num");
667+
valueQuery_int32num = calloc(1,MAX_NUMBER_LENGTH);
668+
snprintf(valueQuery_int32num, MAX_NUMBER_LENGTH, "%d", *int32num);
669+
keyPairQuery_int32num = keyValuePair_create(keyQuery_int32num, valueQuery_int32num);
670+
list_addElement(localVarQueryParameters,keyPairQuery_int32num);
671+
}
672+
673+
// query parameters
674+
char *keyQuery_int64num = NULL;
675+
char * valueQuery_int64num ;
676+
keyValuePair_t *keyPairQuery_int64num = 0;
677+
{
678+
keyQuery_int64num = strdup("int64num");
679+
valueQuery_int64num = calloc(1,MAX_NUMBER_LENGTH_LONG);
680+
snprintf(valueQuery_int64num, MAX_NUMBER_LENGTH_LONG, "%d", int64num);
681+
keyPairQuery_int64num = keyValuePair_create(keyQuery_int64num, valueQuery_int64num);
682+
list_addElement(localVarQueryParameters,keyPairQuery_int64num);
683+
}
684+
apiClient_invoke(apiClient,
685+
localVarPath,
686+
localVarQueryParameters,
687+
localVarHeaderParameters,
688+
localVarFormParameters,
689+
localVarHeaderType,
690+
localVarContentType,
691+
localVarBodyParameters,
692+
localVarBodyLength,
693+
"GET");
694+
695+
// uncomment below to debug the error response
696+
//if (apiClient->response_code == 200) {
697+
// printf("%s\n","successful operation");
698+
//}
699+
//No return type
700+
end:
701+
if (apiClient->dataReceived) {
702+
free(apiClient->dataReceived);
703+
apiClient->dataReceived = NULL;
704+
apiClient->dataReceivedLen = 0;
705+
}
706+
list_freeList(localVarQueryParameters);
707+
708+
709+
710+
597711
free(localVarPath);
598712

599713
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ void
5454
UserAPI_logoutUser(apiClient_t *apiClient);
5555

5656

57+
// test int32, int64 float and double query parameters in API
58+
//
59+
// This can test int32, int64 float and double query parameters in API.
60+
//
61+
void
62+
UserAPI_testInt32Int64FloatDouble(apiClient_t *apiClient, float floatnum, double doublenum, int *int32num, long int64num);
63+
64+
5765
// test integer and boolean query parameters in API
5866
//
5967
// This can test integer and boolean query parameters in API.

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Method | HTTP request | Description
1111
[**UserAPI_getUserByName**](UserAPI.md#UserAPI_getUserByName) | **GET** /user/{username} | Get user by user name
1212
[**UserAPI_loginUser**](UserAPI.md#UserAPI_loginUser) | **GET** /user/login | Logs user into the system
1313
[**UserAPI_logoutUser**](UserAPI.md#UserAPI_logoutUser) | **GET** /user/logout | Logs out current logged in user session
14+
[**UserAPI_testInt32Int64FloatDouble**](UserAPI.md#UserAPI_testInt32Int64FloatDouble) | **GET** /user/test_int32_int64_float_double | test int32, int64 float and double query parameters in API
1415
[**UserAPI_testIntAndBool**](UserAPI.md#UserAPI_testIntAndBool) | **GET** /user/testIntAndBool | test integer and boolean query parameters in API
1516
[**UserAPI_updateUser**](UserAPI.md#UserAPI_updateUser) | **PUT** /user/{username} | Updated user
1617

@@ -218,6 +219,39 @@ No authorization required
218219
219220
[[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)
220221
222+
# **UserAPI_testInt32Int64FloatDouble**
223+
```c
224+
// test int32, int64 float and double query parameters in API
225+
//
226+
// This can test int32, int64 float and double query parameters in API.
227+
//
228+
void UserAPI_testInt32Int64FloatDouble(apiClient_t *apiClient, float floatnum, double doublenum, int *int32num, long int64num);
229+
```
230+
231+
### Parameters
232+
Name | Type | Description | Notes
233+
------------- | ------------- | ------------- | -------------
234+
**apiClient** | **apiClient_t \*** | context containing the client configuration |
235+
**floatnum** | **float** | A float number | [optional]
236+
**doublenum** | **double** | A double number | [optional]
237+
**int32num** | **int \*** | An int32 number | [optional]
238+
**int64num** | **long** | An int64 number | [optional]
239+
240+
### Return type
241+
242+
void
243+
244+
### Authorization
245+
246+
No authorization required
247+
248+
### HTTP request headers
249+
250+
- **Content-Type**: Not defined
251+
- **Accept**: Not defined
252+
253+
[[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)
254+
221255
# **UserAPI_testIntAndBool**
222256
```c
223257
// test integer and boolean query parameters in API

samples/client/petstore/c/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ Category | Method | HTTP request | Description
9292
*UserAPI* | [**UserAPI_getUserByName**](docs/UserAPI.md#UserAPI_getUserByName) | **GET** /user/{username} | Get user by user name
9393
*UserAPI* | [**UserAPI_loginUser**](docs/UserAPI.md#UserAPI_loginUser) | **GET** /user/login | Logs user into the system
9494
*UserAPI* | [**UserAPI_logoutUser**](docs/UserAPI.md#UserAPI_logoutUser) | **GET** /user/logout | Logs out current logged in user session
95+
*UserAPI* | [**UserAPI_testInt32Int64FloatDouble**](docs/UserAPI.md#UserAPI_testInt32Int64FloatDouble) | **GET** /user/test_int32_int64_float_double | test int32, int64 float and double query parameters in API
9596
*UserAPI* | [**UserAPI_testIntAndBool**](docs/UserAPI.md#UserAPI_testIntAndBool) | **GET** /user/testIntAndBool | test integer and boolean query parameters in API
9697
*UserAPI* | [**UserAPI_updateUser**](docs/UserAPI.md#UserAPI_updateUser) | **PUT** /user/{username} | Updated user
9798

0 commit comments

Comments
 (0)