[C][Client] Check cJSON_IsNull when the data type is string (#14332)

This commit is contained in:
Hui Yu 2022-12-28 16:32:13 +08:00 committed by GitHub
parent 88fa9ef3c2
commit 04ebe9e1a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 22 additions and 22 deletions

View File

@ -635,7 +635,7 @@ fail:
{{^isEnum}}
{{#isString}}
{{^required}}if ({{{name}}}) { {{/required}}
if(!cJSON_IsString({{{name}}}))
if(!cJSON_IsString({{{name}}}){{^required}} && !cJSON_IsNull({{{name}}}){{/required}})
{
goto end; //String
}
@ -880,7 +880,7 @@ fail:
{{/isEnum}}
{{^isEnum}}
{{#isString}}
{{^required}}{{{name}}} ? {{/required}}strdup({{{name}}}->valuestring){{^required}} : NULL{{/required}}{{^-last}},{{/-last}}
{{^required}}{{{name}}} && !cJSON_IsNull({{{name}}}) ? {{/required}}strdup({{{name}}}->valuestring){{^required}} : NULL{{/required}}{{^-last}},{{/-last}}
{{/isString}}
{{/isEnum}}
{{#isByteArray}}

View File

@ -88,7 +88,7 @@ api_response_t *api_response_parseFromJSON(cJSON *api_responseJSON){
// api_response->type
cJSON *type = cJSON_GetObjectItemCaseSensitive(api_responseJSON, "type");
if (type) {
if(!cJSON_IsString(type))
if(!cJSON_IsString(type) && !cJSON_IsNull(type))
{
goto end; //String
}
@ -97,7 +97,7 @@ api_response_t *api_response_parseFromJSON(cJSON *api_responseJSON){
// api_response->message
cJSON *message = cJSON_GetObjectItemCaseSensitive(api_responseJSON, "message");
if (message) {
if(!cJSON_IsString(message))
if(!cJSON_IsString(message) && !cJSON_IsNull(message))
{
goto end; //String
}
@ -106,8 +106,8 @@ api_response_t *api_response_parseFromJSON(cJSON *api_responseJSON){
api_response_local_var = api_response_create (
code ? code->valuedouble : 0,
type ? strdup(type->valuestring) : NULL,
message ? strdup(message->valuestring) : NULL
type && !cJSON_IsNull(type) ? strdup(type->valuestring) : NULL,
message && !cJSON_IsNull(message) ? strdup(message->valuestring) : NULL
);
return api_response_local_var;

View File

@ -74,7 +74,7 @@ category_t *category_parseFromJSON(cJSON *categoryJSON){
// category->name
cJSON *name = cJSON_GetObjectItemCaseSensitive(categoryJSON, "name");
if (name) {
if(!cJSON_IsString(name))
if(!cJSON_IsString(name) && !cJSON_IsNull(name))
{
goto end; //String
}
@ -83,7 +83,7 @@ category_t *category_parseFromJSON(cJSON *categoryJSON){
category_local_var = category_create (
id ? id->valuedouble : 0,
name ? strdup(name->valuestring) : NULL
name && !cJSON_IsNull(name) ? strdup(name->valuestring) : NULL
);
return category_local_var;

View File

@ -74,7 +74,7 @@ tag_t *tag_parseFromJSON(cJSON *tagJSON){
// tag->name
cJSON *name = cJSON_GetObjectItemCaseSensitive(tagJSON, "name");
if (name) {
if(!cJSON_IsString(name))
if(!cJSON_IsString(name) && !cJSON_IsNull(name))
{
goto end; //String
}
@ -83,7 +83,7 @@ tag_t *tag_parseFromJSON(cJSON *tagJSON){
tag_local_var = tag_create (
id ? id->valuedouble : 0,
name ? strdup(name->valuestring) : NULL
name && !cJSON_IsNull(name) ? strdup(name->valuestring) : NULL
);
return tag_local_var;

View File

@ -154,7 +154,7 @@ user_t *user_parseFromJSON(cJSON *userJSON){
// user->username
cJSON *username = cJSON_GetObjectItemCaseSensitive(userJSON, "username");
if (username) {
if(!cJSON_IsString(username))
if(!cJSON_IsString(username) && !cJSON_IsNull(username))
{
goto end; //String
}
@ -163,7 +163,7 @@ user_t *user_parseFromJSON(cJSON *userJSON){
// user->first_name
cJSON *first_name = cJSON_GetObjectItemCaseSensitive(userJSON, "firstName");
if (first_name) {
if(!cJSON_IsString(first_name))
if(!cJSON_IsString(first_name) && !cJSON_IsNull(first_name))
{
goto end; //String
}
@ -172,7 +172,7 @@ user_t *user_parseFromJSON(cJSON *userJSON){
// user->last_name
cJSON *last_name = cJSON_GetObjectItemCaseSensitive(userJSON, "lastName");
if (last_name) {
if(!cJSON_IsString(last_name))
if(!cJSON_IsString(last_name) && !cJSON_IsNull(last_name))
{
goto end; //String
}
@ -181,7 +181,7 @@ user_t *user_parseFromJSON(cJSON *userJSON){
// user->email
cJSON *email = cJSON_GetObjectItemCaseSensitive(userJSON, "email");
if (email) {
if(!cJSON_IsString(email))
if(!cJSON_IsString(email) && !cJSON_IsNull(email))
{
goto end; //String
}
@ -190,7 +190,7 @@ user_t *user_parseFromJSON(cJSON *userJSON){
// user->password
cJSON *password = cJSON_GetObjectItemCaseSensitive(userJSON, "password");
if (password) {
if(!cJSON_IsString(password))
if(!cJSON_IsString(password) && !cJSON_IsNull(password))
{
goto end; //String
}
@ -199,7 +199,7 @@ user_t *user_parseFromJSON(cJSON *userJSON){
// user->phone
cJSON *phone = cJSON_GetObjectItemCaseSensitive(userJSON, "phone");
if (phone) {
if(!cJSON_IsString(phone))
if(!cJSON_IsString(phone) && !cJSON_IsNull(phone))
{
goto end; //String
}
@ -217,12 +217,12 @@ user_t *user_parseFromJSON(cJSON *userJSON){
user_local_var = user_create (
id ? id->valuedouble : 0,
username ? strdup(username->valuestring) : NULL,
first_name ? strdup(first_name->valuestring) : NULL,
last_name ? strdup(last_name->valuestring) : NULL,
email ? strdup(email->valuestring) : NULL,
password ? strdup(password->valuestring) : NULL,
phone ? strdup(phone->valuestring) : NULL,
username && !cJSON_IsNull(username) ? strdup(username->valuestring) : NULL,
first_name && !cJSON_IsNull(first_name) ? strdup(first_name->valuestring) : NULL,
last_name && !cJSON_IsNull(last_name) ? strdup(last_name->valuestring) : NULL,
email && !cJSON_IsNull(email) ? strdup(email->valuestring) : NULL,
password && !cJSON_IsNull(password) ? strdup(password->valuestring) : NULL,
phone && !cJSON_IsNull(phone) ? strdup(phone->valuestring) : NULL,
user_status ? user_status->valuedouble : 0
);