Skip to content

Commit 16ab5fe

Browse files
authored
do not put the invalid value of the enum to a JSON structure (#12133)
1 parent 4485ba2 commit 16ab5fe

File tree

7 files changed

+64
-54
lines changed

7 files changed

+64
-54
lines changed

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,8 +336,20 @@ cJSON *{{classname}}_convertToJSON({{classname}}_t *{{classname}}) {
336336
goto fail;
337337
}
338338
{{/isEnum}}
339+
{{#isEnum}}
340+
if ({{projectName}}_{{classVarName}}_{{enumName}}_NULL == {{{classname}}}->{{{name}}}) {
341+
goto fail;
342+
}
343+
{{/isEnum}}
344+
{{/required}}
345+
{{^required}}
346+
{{^isEnum}}
347+
if({{{classname}}}->{{{name}}}) {
348+
{{/isEnum}}
349+
{{#isEnum}}
350+
if({{{classname}}}->{{{name}}} != {{projectName}}_{{classVarName}}_{{enumName}}_NULL) {
351+
{{/isEnum}}
339352
{{/required}}
340-
{{^required}}{{^isEnum}}if({{{classname}}}->{{{name}}}) { {{/isEnum}}{{/required}}
341353
{{^isContainer}}
342354
{{#isPrimitiveType}}
343355
{{#isNumeric}}
@@ -536,7 +548,7 @@ cJSON *{{classname}}_convertToJSON({{classname}}_t *{{classname}}) {
536548
{{/isMap}}
537549
{{/isContainer}}
538550
{{^required}}
539-
{{^isEnum}} } {{/isEnum}}
551+
}
540552
{{/required}}
541553

542554
{{/vars}}

samples/client/petstore/c/model/api_response.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,27 +42,27 @@ cJSON *api_response_convertToJSON(api_response_t *api_response) {
4242
cJSON *item = cJSON_CreateObject();
4343

4444
// api_response->code
45-
if(api_response->code) {
45+
if(api_response->code) {
4646
if(cJSON_AddNumberToObject(item, "code", api_response->code) == NULL) {
4747
goto fail; //Numeric
4848
}
49-
}
49+
}
5050

5151

5252
// api_response->type
53-
if(api_response->type) {
53+
if(api_response->type) {
5454
if(cJSON_AddStringToObject(item, "type", api_response->type) == NULL) {
5555
goto fail; //String
5656
}
57-
}
57+
}
5858

5959

6060
// api_response->message
61-
if(api_response->message) {
61+
if(api_response->message) {
6262
if(cJSON_AddStringToObject(item, "message", api_response->message) == NULL) {
6363
goto fail; //String
6464
}
65-
}
65+
}
6666

6767
return item;
6868
fail:

samples/client/petstore/c/model/category.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,19 @@ cJSON *category_convertToJSON(category_t *category) {
3636
cJSON *item = cJSON_CreateObject();
3737

3838
// category->id
39-
if(category->id) {
39+
if(category->id) {
4040
if(cJSON_AddNumberToObject(item, "id", category->id) == NULL) {
4141
goto fail; //Numeric
4242
}
43-
}
43+
}
4444

4545

4646
// category->name
47-
if(category->name) {
47+
if(category->name) {
4848
if(cJSON_AddStringToObject(item, "name", category->name) == NULL) {
4949
goto fail; //String
5050
}
51-
}
51+
}
5252

5353
return item;
5454
fail:

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,52 +61,52 @@ cJSON *order_convertToJSON(order_t *order) {
6161
cJSON *item = cJSON_CreateObject();
6262

6363
// order->id
64-
if(order->id) {
64+
if(order->id) {
6565
if(cJSON_AddNumberToObject(item, "id", order->id) == NULL) {
6666
goto fail; //Numeric
6767
}
68-
}
68+
}
6969

7070

7171
// order->pet_id
72-
if(order->pet_id) {
72+
if(order->pet_id) {
7373
if(cJSON_AddNumberToObject(item, "petId", order->pet_id) == NULL) {
7474
goto fail; //Numeric
7575
}
76-
}
76+
}
7777

7878

7979
// order->quantity
80-
if(order->quantity) {
80+
if(order->quantity) {
8181
if(cJSON_AddNumberToObject(item, "quantity", order->quantity) == NULL) {
8282
goto fail; //Numeric
8383
}
84-
}
84+
}
8585

8686

8787
// order->ship_date
88-
if(order->ship_date) {
88+
if(order->ship_date) {
8989
if(cJSON_AddStringToObject(item, "shipDate", order->ship_date) == NULL) {
9090
goto fail; //Date-Time
9191
}
92-
}
92+
}
9393

9494

9595
// order->status
96-
96+
if(order->status != openapi_petstore_order_STATUS_NULL) {
9797
if(cJSON_AddStringToObject(item, "status", statusorder_ToString(order->status)) == NULL)
9898
{
9999
goto fail; //Enum
100100
}
101-
101+
}
102102

103103

104104
// order->complete
105-
if(order->complete) {
105+
if(order->complete) {
106106
if(cJSON_AddBoolToObject(item, "complete", order->complete) == NULL) {
107107
goto fail; //Bool
108108
}
109-
}
109+
}
110110

111111
return item;
112112
fail:

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,15 @@ cJSON *pet_convertToJSON(pet_t *pet) {
7979
cJSON *item = cJSON_CreateObject();
8080

8181
// pet->id
82-
if(pet->id) {
82+
if(pet->id) {
8383
if(cJSON_AddNumberToObject(item, "id", pet->id) == NULL) {
8484
goto fail; //Numeric
8585
}
86-
}
86+
}
8787

8888

8989
// pet->category
90-
if(pet->category) {
90+
if(pet->category) {
9191
cJSON *category_local_JSON = category_convertToJSON(pet->category);
9292
if(category_local_JSON == NULL) {
9393
goto fail; //model
@@ -96,14 +96,13 @@ cJSON *pet_convertToJSON(pet_t *pet) {
9696
if(item->child == NULL) {
9797
goto fail;
9898
}
99-
}
99+
}
100100

101101

102102
// pet->name
103103
if (!pet->name) {
104104
goto fail;
105105
}
106-
107106
if(cJSON_AddStringToObject(item, "name", pet->name) == NULL) {
108107
goto fail; //String
109108
}
@@ -113,7 +112,6 @@ cJSON *pet_convertToJSON(pet_t *pet) {
113112
if (!pet->photo_urls) {
114113
goto fail;
115114
}
116-
117115
cJSON *photo_urls = cJSON_AddArrayToObject(item, "photoUrls");
118116
if(photo_urls == NULL) {
119117
goto fail; //primitive container
@@ -129,7 +127,7 @@ cJSON *pet_convertToJSON(pet_t *pet) {
129127

130128

131129
// pet->tags
132-
if(pet->tags) {
130+
if(pet->tags) {
133131
cJSON *tags = cJSON_AddArrayToObject(item, "tags");
134132
if(tags == NULL) {
135133
goto fail; //nonprimitive container
@@ -145,16 +143,16 @@ cJSON *pet_convertToJSON(pet_t *pet) {
145143
cJSON_AddItemToArray(tags, itemLocal);
146144
}
147145
}
148-
}
146+
}
149147

150148

151149
// pet->status
152-
150+
if(pet->status != openapi_petstore_pet_STATUS_NULL) {
153151
if(cJSON_AddStringToObject(item, "status", statuspet_ToString(pet->status)) == NULL)
154152
{
155153
goto fail; //Enum
156154
}
157-
155+
}
158156

159157
return item;
160158
fail:

samples/client/petstore/c/model/tag.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,19 @@ cJSON *tag_convertToJSON(tag_t *tag) {
3636
cJSON *item = cJSON_CreateObject();
3737

3838
// tag->id
39-
if(tag->id) {
39+
if(tag->id) {
4040
if(cJSON_AddNumberToObject(item, "id", tag->id) == NULL) {
4141
goto fail; //Numeric
4242
}
43-
}
43+
}
4444

4545

4646
// tag->name
47-
if(tag->name) {
47+
if(tag->name) {
4848
if(cJSON_AddStringToObject(item, "name", tag->name) == NULL) {
4949
goto fail; //String
5050
}
51-
}
51+
}
5252

5353
return item;
5454
fail:

samples/client/petstore/c/model/user.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -68,67 +68,67 @@ cJSON *user_convertToJSON(user_t *user) {
6868
cJSON *item = cJSON_CreateObject();
6969

7070
// user->id
71-
if(user->id) {
71+
if(user->id) {
7272
if(cJSON_AddNumberToObject(item, "id", user->id) == NULL) {
7373
goto fail; //Numeric
7474
}
75-
}
75+
}
7676

7777

7878
// user->username
79-
if(user->username) {
79+
if(user->username) {
8080
if(cJSON_AddStringToObject(item, "username", user->username) == NULL) {
8181
goto fail; //String
8282
}
83-
}
83+
}
8484

8585

8686
// user->first_name
87-
if(user->first_name) {
87+
if(user->first_name) {
8888
if(cJSON_AddStringToObject(item, "firstName", user->first_name) == NULL) {
8989
goto fail; //String
9090
}
91-
}
91+
}
9292

9393

9494
// user->last_name
95-
if(user->last_name) {
95+
if(user->last_name) {
9696
if(cJSON_AddStringToObject(item, "lastName", user->last_name) == NULL) {
9797
goto fail; //String
9898
}
99-
}
99+
}
100100

101101

102102
// user->email
103-
if(user->email) {
103+
if(user->email) {
104104
if(cJSON_AddStringToObject(item, "email", user->email) == NULL) {
105105
goto fail; //String
106106
}
107-
}
107+
}
108108

109109

110110
// user->password
111-
if(user->password) {
111+
if(user->password) {
112112
if(cJSON_AddStringToObject(item, "password", user->password) == NULL) {
113113
goto fail; //String
114114
}
115-
}
115+
}
116116

117117

118118
// user->phone
119-
if(user->phone) {
119+
if(user->phone) {
120120
if(cJSON_AddStringToObject(item, "phone", user->phone) == NULL) {
121121
goto fail; //String
122122
}
123-
}
123+
}
124124

125125

126126
// user->user_status
127-
if(user->user_status) {
127+
if(user->user_status) {
128128
if(cJSON_AddNumberToObject(item, "userStatus", user->user_status) == NULL) {
129129
goto fail; //Numeric
130130
}
131-
}
131+
}
132132

133133
return item;
134134
fail:

0 commit comments

Comments
 (0)