do not put the invalid value of the enum to a JSON structure (#12133)

This commit is contained in:
Hui Yu 2022-04-16 16:28:28 +08:00 committed by GitHub
parent 4485ba27c6
commit 16ab5feeb9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 64 additions and 54 deletions

View File

@ -336,8 +336,20 @@ cJSON *{{classname}}_convertToJSON({{classname}}_t *{{classname}}) {
goto fail;
}
{{/isEnum}}
{{#isEnum}}
if ({{projectName}}_{{classVarName}}_{{enumName}}_NULL == {{{classname}}}->{{{name}}}) {
goto fail;
}
{{/isEnum}}
{{/required}}
{{^required}}
{{^isEnum}}
if({{{classname}}}->{{{name}}}) {
{{/isEnum}}
{{#isEnum}}
if({{{classname}}}->{{{name}}} != {{projectName}}_{{classVarName}}_{{enumName}}_NULL) {
{{/isEnum}}
{{/required}}
{{^required}}{{^isEnum}}if({{{classname}}}->{{{name}}}) { {{/isEnum}}{{/required}}
{{^isContainer}}
{{#isPrimitiveType}}
{{#isNumeric}}
@ -536,7 +548,7 @@ cJSON *{{classname}}_convertToJSON({{classname}}_t *{{classname}}) {
{{/isMap}}
{{/isContainer}}
{{^required}}
{{^isEnum}} } {{/isEnum}}
}
{{/required}}
{{/vars}}

View File

@ -46,7 +46,7 @@ cJSON *api_response_convertToJSON(api_response_t *api_response) {
if(cJSON_AddNumberToObject(item, "code", api_response->code) == NULL) {
goto fail; //Numeric
}
}
}
// api_response->type
@ -54,7 +54,7 @@ cJSON *api_response_convertToJSON(api_response_t *api_response) {
if(cJSON_AddStringToObject(item, "type", api_response->type) == NULL) {
goto fail; //String
}
}
}
// api_response->message
@ -62,7 +62,7 @@ cJSON *api_response_convertToJSON(api_response_t *api_response) {
if(cJSON_AddStringToObject(item, "message", api_response->message) == NULL) {
goto fail; //String
}
}
}
return item;
fail:

View File

@ -40,7 +40,7 @@ cJSON *category_convertToJSON(category_t *category) {
if(cJSON_AddNumberToObject(item, "id", category->id) == NULL) {
goto fail; //Numeric
}
}
}
// category->name
@ -48,7 +48,7 @@ cJSON *category_convertToJSON(category_t *category) {
if(cJSON_AddStringToObject(item, "name", category->name) == NULL) {
goto fail; //String
}
}
}
return item;
fail:

View File

@ -65,7 +65,7 @@ cJSON *order_convertToJSON(order_t *order) {
if(cJSON_AddNumberToObject(item, "id", order->id) == NULL) {
goto fail; //Numeric
}
}
}
// order->pet_id
@ -73,7 +73,7 @@ cJSON *order_convertToJSON(order_t *order) {
if(cJSON_AddNumberToObject(item, "petId", order->pet_id) == NULL) {
goto fail; //Numeric
}
}
}
// order->quantity
@ -81,7 +81,7 @@ cJSON *order_convertToJSON(order_t *order) {
if(cJSON_AddNumberToObject(item, "quantity", order->quantity) == NULL) {
goto fail; //Numeric
}
}
}
// order->ship_date
@ -89,16 +89,16 @@ cJSON *order_convertToJSON(order_t *order) {
if(cJSON_AddStringToObject(item, "shipDate", order->ship_date) == NULL) {
goto fail; //Date-Time
}
}
}
// order->status
if(order->status != openapi_petstore_order_STATUS_NULL) {
if(cJSON_AddStringToObject(item, "status", statusorder_ToString(order->status)) == NULL)
{
goto fail; //Enum
}
}
// order->complete
@ -106,7 +106,7 @@ cJSON *order_convertToJSON(order_t *order) {
if(cJSON_AddBoolToObject(item, "complete", order->complete) == NULL) {
goto fail; //Bool
}
}
}
return item;
fail:

View File

@ -83,7 +83,7 @@ cJSON *pet_convertToJSON(pet_t *pet) {
if(cJSON_AddNumberToObject(item, "id", pet->id) == NULL) {
goto fail; //Numeric
}
}
}
// pet->category
@ -96,14 +96,13 @@ cJSON *pet_convertToJSON(pet_t *pet) {
if(item->child == NULL) {
goto fail;
}
}
}
// pet->name
if (!pet->name) {
goto fail;
}
if(cJSON_AddStringToObject(item, "name", pet->name) == NULL) {
goto fail; //String
}
@ -113,7 +112,6 @@ cJSON *pet_convertToJSON(pet_t *pet) {
if (!pet->photo_urls) {
goto fail;
}
cJSON *photo_urls = cJSON_AddArrayToObject(item, "photoUrls");
if(photo_urls == NULL) {
goto fail; //primitive container
@ -145,16 +143,16 @@ cJSON *pet_convertToJSON(pet_t *pet) {
cJSON_AddItemToArray(tags, itemLocal);
}
}
}
}
// pet->status
if(pet->status != openapi_petstore_pet_STATUS_NULL) {
if(cJSON_AddStringToObject(item, "status", statuspet_ToString(pet->status)) == NULL)
{
goto fail; //Enum
}
}
return item;
fail:

View File

@ -40,7 +40,7 @@ cJSON *tag_convertToJSON(tag_t *tag) {
if(cJSON_AddNumberToObject(item, "id", tag->id) == NULL) {
goto fail; //Numeric
}
}
}
// tag->name
@ -48,7 +48,7 @@ cJSON *tag_convertToJSON(tag_t *tag) {
if(cJSON_AddStringToObject(item, "name", tag->name) == NULL) {
goto fail; //String
}
}
}
return item;
fail:

View File

@ -72,7 +72,7 @@ cJSON *user_convertToJSON(user_t *user) {
if(cJSON_AddNumberToObject(item, "id", user->id) == NULL) {
goto fail; //Numeric
}
}
}
// user->username
@ -80,7 +80,7 @@ cJSON *user_convertToJSON(user_t *user) {
if(cJSON_AddStringToObject(item, "username", user->username) == NULL) {
goto fail; //String
}
}
}
// user->first_name
@ -88,7 +88,7 @@ cJSON *user_convertToJSON(user_t *user) {
if(cJSON_AddStringToObject(item, "firstName", user->first_name) == NULL) {
goto fail; //String
}
}
}
// user->last_name
@ -96,7 +96,7 @@ cJSON *user_convertToJSON(user_t *user) {
if(cJSON_AddStringToObject(item, "lastName", user->last_name) == NULL) {
goto fail; //String
}
}
}
// user->email
@ -104,7 +104,7 @@ cJSON *user_convertToJSON(user_t *user) {
if(cJSON_AddStringToObject(item, "email", user->email) == NULL) {
goto fail; //String
}
}
}
// user->password
@ -112,7 +112,7 @@ cJSON *user_convertToJSON(user_t *user) {
if(cJSON_AddStringToObject(item, "password", user->password) == NULL) {
goto fail; //String
}
}
}
// user->phone
@ -120,7 +120,7 @@ cJSON *user_convertToJSON(user_t *user) {
if(cJSON_AddStringToObject(item, "phone", user->phone) == NULL) {
goto fail; //String
}
}
}
// user->user_status
@ -128,7 +128,7 @@ cJSON *user_convertToJSON(user_t *user) {
if(cJSON_AddNumberToObject(item, "userStatus", user->user_status) == NULL) {
goto fail; //Numeric
}
}
}
return item;
fail: