Skip to content

Commit b0b46d5

Browse files
Support for additionalProperties in the C generator "Client: C" solves #5395 (#5440)
* Support for additionalProperties in the C generator. * Support for additionalProperties in the C generator.
1 parent 50d21cb commit b0b46d5

File tree

15 files changed

+205
-99
lines changed

15 files changed

+205
-99
lines changed

modules/openapi-generator/src/main/resources/C-libcurl/apiKey.c.mustache

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ keyValuePair_t *keyValuePair_create(char *key, void *value) {
99
return keyValuePair;
1010
}
1111

12+
keyValuePair_t* keyValuePair_create_allocate(char* key, double value) {
13+
double* boolpointer = malloc(sizeof(value));
14+
memcpy(boolpointer, &value, sizeof(value));
15+
return keyValuePair_create(key, boolpointer);
16+
}
17+
1218
void keyValuePair_free(keyValuePair_t *keyValuePair) {
1319
free(keyValuePair);
1420
}

modules/openapi-generator/src/main/resources/C-libcurl/keyValuePair.h.mustache

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ typedef struct keyValuePair_t {
1010

1111
keyValuePair_t *keyValuePair_create(char *key, void *value);
1212

13+
keyValuePair_t* keyValuePair_create_allocate(char* key, double value);
14+
1315
void keyValuePair_free(keyValuePair_t *keyValuePair);
1416

1517
#endif /* _keyValuePair_H_ */

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ cJSON *{{classname}}_convertToJSON({{classname}}_t *{{classname}}) {
423423
if({{{name}}} == NULL) {
424424
goto fail; //primitive map container
425425
}
426-
cJSON *localMapObject = cJSON_CreateObject(); //Memory free to be implemented in user code
426+
cJSON *localMapObject = {{{name}}};
427427
listEntry_t *{{{name}}}ListEntry;
428428
if ({{{classname}}}->{{{name}}}) {
429429
list_ForEach({{{name}}}ListEntry, {{{classname}}}->{{{name}}}) {
@@ -442,7 +442,6 @@ cJSON *{{classname}}_convertToJSON({{classname}}_t *{{classname}}) {
442442
}
443443
{{/isString}}
444444
{{/items}}
445-
cJSON_AddItemToObject({{{name}}},"", localMapObject);
446445
}
447446
}
448447
{{/isMapContainer}}
@@ -643,22 +642,24 @@ fail:
643642
keyValuePair_t *localMapKeyPair;
644643
cJSON_ArrayForEach({{{name}}}_local_map, {{{name}}})
645644
{
645+
cJSON *localMapObject = {{{name}}}_local_map;
646+
{{#items}}
646647
{{#isString}}
647-
if(!cJSON_IsString({{{name}}}_local_map))
648+
if(!cJSON_IsString(localMapObject))
648649
{
649650
goto end;
650651
}
651-
localMapKeyPair = keyValuePair_create(strdup({{{name}}}_local_map->string),strdup({{{name}}}_local_map->valuestring))
652-
list_addElement({{{name}}}List , localMapKeyPair);
652+
localMapKeyPair = keyValuePair_create(strdup(localMapObject->string),strdup(localMapObject->valuestring));
653653
{{/isString}}
654654
{{^isString}}
655-
if(!cJSON_IsNumber({{{name}}}_local_map))
655+
if(!cJSON_IsNumber(localMapObject))
656656
{
657657
goto end;
658658
}
659-
localMapKeyPair = keyValuePair_create(strdup({{{name}}}_local_map->string),&{{{name}}}_local_map->valuedouble );
660-
list_addElement({{{name}}}List , localMapKeyPair);
659+
localMapKeyPair = keyValuePair_create(strdup(localMapObject->string),&localMapObject->valuedouble );
661660
{{/isString}}
661+
{{/items}}
662+
list_addElement({{{name}}}List , localMapKeyPair);
662663
}
663664
{{/isMapContainer}}
664665
{{/isContainer}}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4.2.2-SNAPSHOT
1+
4.3.0-SNAPSHOT

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

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -386,8 +386,8 @@ UserAPI_loginUser(apiClient_t *apiClient ,char * username ,char * password)
386386

387387

388388
// query parameters
389-
char *keyQuery_username;
390-
char * valueQuery_username;
389+
char *keyQuery_username = NULL;
390+
char * valueQuery_username = NULL;
391391
keyValuePair_t *keyPairQuery_username = 0;
392392
if (username)
393393
{
@@ -398,8 +398,8 @@ UserAPI_loginUser(apiClient_t *apiClient ,char * username ,char * password)
398398
}
399399

400400
// query parameters
401-
char *keyQuery_password;
402-
char * valueQuery_password;
401+
char *keyQuery_password = NULL;
402+
char * valueQuery_password = NULL;
403403
keyValuePair_t *keyPairQuery_password = 0;
404404
if (password)
405405
{
@@ -438,12 +438,30 @@ UserAPI_loginUser(apiClient_t *apiClient ,char * username ,char * password)
438438
list_free(localVarHeaderType);
439439

440440
free(localVarPath);
441-
free(keyQuery_username);
442-
free(valueQuery_username);
443-
keyValuePair_free(keyPairQuery_username);
444-
free(keyQuery_password);
445-
free(valueQuery_password);
446-
keyValuePair_free(keyPairQuery_password);
441+
if(keyQuery_username){
442+
free(keyQuery_username);
443+
keyQuery_username = NULL;
444+
}
445+
if(valueQuery_username){
446+
free(valueQuery_username);
447+
valueQuery_username = NULL;
448+
}
449+
if(keyPairQuery_username){
450+
keyValuePair_free(keyPairQuery_username);
451+
keyPairQuery_username = NULL;
452+
}
453+
if(keyQuery_password){
454+
free(keyQuery_password);
455+
keyQuery_password = NULL;
456+
}
457+
if(valueQuery_password){
458+
free(valueQuery_password);
459+
valueQuery_password = NULL;
460+
}
461+
if(keyPairQuery_password){
462+
keyValuePair_free(keyPairQuery_password);
463+
keyPairQuery_password = NULL;
464+
}
447465
return elementToReturn;
448466
end:
449467
return NULL;

samples/client/petstore/c/include/apiClient.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
typedef struct apiClient_t {
1313
char *basePath;
14+
char *caPath;
1415
void *dataReceived;
1516
long response_code;
1617
list_t *apiKeys;
@@ -25,6 +26,11 @@ typedef struct binary_t
2526

2627
apiClient_t* apiClient_create();
2728

29+
apiClient_t* apiClient_create_with_base_path(const char *basePath
30+
, const char *caPath
31+
, list_t *apiKeys
32+
);
33+
2834
void apiClient_free(apiClient_t *apiClient);
2935

3036
void apiClient_invoke(apiClient_t *apiClient,char* operationParameter, list_t *queryParameters, list_t *headerParameters, list_t *formParameters,list_t *headerType,list_t *contentType, char *bodyParameters, char *requestType);

samples/client/petstore/c/include/keyValuePair.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ typedef struct keyValuePair_t {
1010

1111
keyValuePair_t *keyValuePair_create(char *key, void *value);
1212

13+
keyValuePair_t* keyValuePair_create_allocate(char* key, double value);
14+
1315
void keyValuePair_free(keyValuePair_t *keyValuePair);
1416

1517
#endif /* _keyValuePair_H_ */

samples/client/petstore/c/model/order.c

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424

2525
order_t *order_create(
2626
long id,
27-
long petId,
27+
long pet_id,
2828
int quantity,
29-
char *shipDate,
29+
char *ship_date,
3030
status_e status,
3131
int complete
3232
) {
@@ -35,9 +35,9 @@ order_t *order_create(
3535
return NULL;
3636
}
3737
order_local_var->id = id;
38-
order_local_var->petId = petId;
38+
order_local_var->pet_id = pet_id;
3939
order_local_var->quantity = quantity;
40-
order_local_var->shipDate = shipDate;
40+
order_local_var->ship_date = ship_date;
4141
order_local_var->status = status;
4242
order_local_var->complete = complete;
4343

@@ -47,7 +47,7 @@ order_t *order_create(
4747

4848
void order_free(order_t *order) {
4949
listEntry_t *listEntry;
50-
free(order->shipDate);
50+
free(order->ship_date);
5151
free(order);
5252
}
5353

@@ -62,9 +62,9 @@ cJSON *order_convertToJSON(order_t *order) {
6262
}
6363

6464

65-
// order->petId
66-
if(order->petId) {
67-
if(cJSON_AddNumberToObject(item, "petId", order->petId) == NULL) {
65+
// order->pet_id
66+
if(order->pet_id) {
67+
if(cJSON_AddNumberToObject(item, "petId", order->pet_id) == NULL) {
6868
goto fail; //Numeric
6969
}
7070
}
@@ -78,9 +78,9 @@ cJSON *order_convertToJSON(order_t *order) {
7878
}
7979

8080

81-
// order->shipDate
82-
if(order->shipDate) {
83-
if(cJSON_AddStringToObject(item, "shipDate", order->shipDate) == NULL) {
81+
// order->ship_date
82+
if(order->ship_date) {
83+
if(cJSON_AddStringToObject(item, "shipDate", order->ship_date) == NULL) {
8484
goto fail; //Date-Time
8585
}
8686
}
@@ -123,10 +123,10 @@ order_t *order_parseFromJSON(cJSON *orderJSON){
123123
}
124124
}
125125

126-
// order->petId
127-
cJSON *petId = cJSON_GetObjectItemCaseSensitive(orderJSON, "petId");
128-
if (petId) {
129-
if(!cJSON_IsNumber(petId))
126+
// order->pet_id
127+
cJSON *pet_id = cJSON_GetObjectItemCaseSensitive(orderJSON, "petId");
128+
if (pet_id) {
129+
if(!cJSON_IsNumber(pet_id))
130130
{
131131
goto end; //Numeric
132132
}
@@ -141,10 +141,10 @@ order_t *order_parseFromJSON(cJSON *orderJSON){
141141
}
142142
}
143143

144-
// order->shipDate
145-
cJSON *shipDate = cJSON_GetObjectItemCaseSensitive(orderJSON, "shipDate");
146-
if (shipDate) {
147-
if(!cJSON_IsString(shipDate))
144+
// order->ship_date
145+
cJSON *ship_date = cJSON_GetObjectItemCaseSensitive(orderJSON, "shipDate");
146+
if (ship_date) {
147+
if(!cJSON_IsString(ship_date))
148148
{
149149
goto end; //DateTime
150150
}
@@ -173,9 +173,9 @@ order_t *order_parseFromJSON(cJSON *orderJSON){
173173

174174
order_local_var = order_create (
175175
id ? id->valuedouble : 0,
176-
petId ? petId->valuedouble : 0,
176+
pet_id ? pet_id->valuedouble : 0,
177177
quantity ? quantity->valuedouble : 0,
178-
shipDate ? strdup(shipDate->valuestring) : NULL,
178+
ship_date ? strdup(ship_date->valuestring) : NULL,
179179
status ? statusVariable : -1,
180180
complete ? complete->valueint : 0
181181
);

samples/client/petstore/c/model/order.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,19 @@
2121

2222
typedef struct order_t {
2323
long id; //numeric
24-
long petId; //numeric
24+
long pet_id; //numeric
2525
int quantity; //numeric
26-
char *shipDate; //date time
26+
char *ship_date; //date time
2727
status_e status; //enum
2828
int complete; //boolean
2929

3030
} order_t;
3131

3232
order_t *order_create(
3333
long id,
34-
long petId,
34+
long pet_id,
3535
int quantity,
36-
char *shipDate,
36+
char *ship_date,
3737
status_e status,
3838
int complete
3939
);

samples/client/petstore/c/model/pet.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pet_t *pet_create(
2626
long id,
2727
category_t *category,
2828
char *name,
29-
list_t *photoUrls,
29+
list_t *photo_urls,
3030
list_t *tags,
3131
status_e status
3232
) {
@@ -37,7 +37,7 @@ pet_t *pet_create(
3737
pet_local_var->id = id;
3838
pet_local_var->category = category;
3939
pet_local_var->name = name;
40-
pet_local_var->photoUrls = photoUrls;
40+
pet_local_var->photo_urls = photo_urls;
4141
pet_local_var->tags = tags;
4242
pet_local_var->status = status;
4343

@@ -49,10 +49,10 @@ void pet_free(pet_t *pet) {
4949
listEntry_t *listEntry;
5050
category_free(pet->category);
5151
free(pet->name);
52-
list_ForEach(listEntry, pet->photoUrls) {
52+
list_ForEach(listEntry, pet->photo_urls) {
5353
free(listEntry->data);
5454
}
55-
list_free(pet->photoUrls);
55+
list_free(pet->photo_urls);
5656
list_ForEach(listEntry, pet->tags) {
5757
tag_free(listEntry->data);
5858
}
@@ -94,8 +94,8 @@ cJSON *pet_convertToJSON(pet_t *pet) {
9494
}
9595

9696

97-
// pet->photoUrls
98-
if (!pet->photoUrls) {
97+
// pet->photo_urls
98+
if (!pet->photo_urls) {
9999
goto fail;
100100
}
101101

@@ -105,7 +105,7 @@ cJSON *pet_convertToJSON(pet_t *pet) {
105105
}
106106

107107
listEntry_t *photo_urlsListEntry;
108-
list_ForEach(photo_urlsListEntry, pet->photoUrls) {
108+
list_ForEach(photo_urlsListEntry, pet->photo_urls) {
109109
if(cJSON_AddStringToObject(photo_urls, "", (char*)photo_urlsListEntry->data) == NULL)
110110
{
111111
goto fail;
@@ -181,21 +181,21 @@ pet_t *pet_parseFromJSON(cJSON *petJSON){
181181
goto end; //String
182182
}
183183

184-
// pet->photoUrls
185-
cJSON *photoUrls = cJSON_GetObjectItemCaseSensitive(petJSON, "photoUrls");
186-
if (!photoUrls) {
184+
// pet->photo_urls
185+
cJSON *photo_urls = cJSON_GetObjectItemCaseSensitive(petJSON, "photoUrls");
186+
if (!photo_urls) {
187187
goto end;
188188
}
189189

190190
list_t *photo_urlsList;
191191

192192
cJSON *photo_urls_local;
193-
if(!cJSON_IsArray(photoUrls)) {
193+
if(!cJSON_IsArray(photo_urls)) {
194194
goto end;//primitive container
195195
}
196196
photo_urlsList = list_create();
197197

198-
cJSON_ArrayForEach(photo_urls_local, photoUrls)
198+
cJSON_ArrayForEach(photo_urls_local, photo_urls)
199199
{
200200
if(!cJSON_IsString(photo_urls_local))
201201
{

samples/client/petstore/c/model/pet.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323

2424
typedef struct pet_t {
2525
long id; //numeric
26-
category_t *category; //model
26+
struct category_t *category; //model
2727
char *name; // string
28-
list_t *photoUrls; //primitive container
28+
list_t *photo_urls; //primitive container
2929
list_t *tags; //nonprimitive container
3030
status_e status; //enum
3131

@@ -35,7 +35,7 @@ pet_t *pet_create(
3535
long id,
3636
category_t *category,
3737
char *name,
38-
list_t *photoUrls,
38+
list_t *photo_urls,
3939
list_t *tags,
4040
status_e status
4141
);

0 commit comments

Comments
 (0)