Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[C][Client] Allow cJSON_IsNull() for a string if it is not mandatory #14332

Merged
merged 1 commit into from
Dec 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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}}
Expand Down
8 changes: 4 additions & 4 deletions samples/client/petstore/c/model/api_response.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions samples/client/petstore/c/model/category.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions samples/client/petstore/c/model/tag.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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;
Expand Down
24 changes: 12 additions & 12 deletions samples/client/petstore/c/model/user.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
);

Expand Down