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

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

View File

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

View File

@ -61,52 +61,52 @@ cJSON *order_convertToJSON(order_t *order) {
cJSON *item = cJSON_CreateObject();
// order->id
if(order->id) {
if(order->id) {
if(cJSON_AddNumberToObject(item, "id", order->id) == NULL) {
goto fail; //Numeric
}
}
}
// order->pet_id
if(order->pet_id) {
if(order->pet_id) {
if(cJSON_AddNumberToObject(item, "petId", order->pet_id) == NULL) {
goto fail; //Numeric
}
}
}
// order->quantity
if(order->quantity) {
if(order->quantity) {
if(cJSON_AddNumberToObject(item, "quantity", order->quantity) == NULL) {
goto fail; //Numeric
}
}
}
// order->ship_date
if(order->ship_date) {
if(order->ship_date) {
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
if(order->complete) {
if(order->complete) {
if(cJSON_AddBoolToObject(item, "complete", order->complete) == NULL) {
goto fail; //Bool
}
}
}
return item;
fail:

View File

@ -79,15 +79,15 @@ cJSON *pet_convertToJSON(pet_t *pet) {
cJSON *item = cJSON_CreateObject();
// pet->id
if(pet->id) {
if(pet->id) {
if(cJSON_AddNumberToObject(item, "id", pet->id) == NULL) {
goto fail; //Numeric
}
}
}
// pet->category
if(pet->category) {
if(pet->category) {
cJSON *category_local_JSON = category_convertToJSON(pet->category);
if(category_local_JSON == NULL) {
goto fail; //model
@ -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
@ -129,7 +127,7 @@ cJSON *pet_convertToJSON(pet_t *pet) {
// pet->tags
if(pet->tags) {
if(pet->tags) {
cJSON *tags = cJSON_AddArrayToObject(item, "tags");
if(tags == NULL) {
goto fail; //nonprimitive 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

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

View File

@ -68,67 +68,67 @@ cJSON *user_convertToJSON(user_t *user) {
cJSON *item = cJSON_CreateObject();
// user->id
if(user->id) {
if(user->id) {
if(cJSON_AddNumberToObject(item, "id", user->id) == NULL) {
goto fail; //Numeric
}
}
}
// user->username
if(user->username) {
if(user->username) {
if(cJSON_AddStringToObject(item, "username", user->username) == NULL) {
goto fail; //String
}
}
}
// user->first_name
if(user->first_name) {
if(user->first_name) {
if(cJSON_AddStringToObject(item, "firstName", user->first_name) == NULL) {
goto fail; //String
}
}
}
// user->last_name
if(user->last_name) {
if(user->last_name) {
if(cJSON_AddStringToObject(item, "lastName", user->last_name) == NULL) {
goto fail; //String
}
}
}
// user->email
if(user->email) {
if(user->email) {
if(cJSON_AddStringToObject(item, "email", user->email) == NULL) {
goto fail; //String
}
}
}
// user->password
if(user->password) {
if(user->password) {
if(cJSON_AddStringToObject(item, "password", user->password) == NULL) {
goto fail; //String
}
}
}
// user->phone
if(user->phone) {
if(user->phone) {
if(cJSON_AddStringToObject(item, "phone", user->phone) == NULL) {
goto fail; //String
}
}
}
// user->user_status
if(user->user_status) {
if(user->user_status) {
if(cJSON_AddNumberToObject(item, "userStatus", user->user_status) == NULL) {
goto fail; //Numeric
}
}
}
return item;
fail: