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; goto fail;
} }
{{/isEnum}} {{/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}}
{{^required}}{{^isEnum}}if({{{classname}}}->{{{name}}}) { {{/isEnum}}{{/required}}
{{^isContainer}} {{^isContainer}}
{{#isPrimitiveType}} {{#isPrimitiveType}}
{{#isNumeric}} {{#isNumeric}}
@ -536,7 +548,7 @@ cJSON *{{classname}}_convertToJSON({{classname}}_t *{{classname}}) {
{{/isMap}} {{/isMap}}
{{/isContainer}} {{/isContainer}}
{{^required}} {{^required}}
{{^isEnum}} } {{/isEnum}} }
{{/required}} {{/required}}
{{/vars}} {{/vars}}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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