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

@ -93,12 +93,12 @@ cJSON *order_convertToJSON(order_t *order) {
// 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

View File

@ -103,7 +103,6 @@ cJSON *pet_convertToJSON(pet_t *pet) {
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
@ -149,12 +147,12 @@ cJSON *pet_convertToJSON(pet_t *pet) {
// 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: