Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -153,10 +153,10 @@ static {{classname}}_t *{{classname}}_create_internal(
{{/isPrimitiveType}}
{{#isPrimitiveType}}
{{#isNumeric}}
{{datatype}} {{name}}{{^-last}},{{/-last}}
{{datatype}} *{{name}}{{^-last}},{{/-last}}
{{/isNumeric}}
{{#isBoolean}}
{{datatype}} {{name}}{{^-last}},{{/-last}}
{{datatype}} *{{name}}{{^-last}},{{/-last}}
{{/isBoolean}}
{{#isEnum}}
{{#isString}}
Expand Down Expand Up @@ -201,8 +201,43 @@ static {{classname}}_t *{{classname}}_create_internal(
if (!{{classname}}_local_var) {
return NULL;
}
memset({{classname}}_local_var, 0, sizeof({{classname}}_t));
{{#vars}}
{{^isContainer}}
{{#isPrimitiveType}}
{{#isNumeric}}
if ({{{name}}}) {
{{classname}}_local_var->{{{name}}} = malloc(sizeof({{datatype}}));
if (!{{classname}}_local_var->{{{name}}}) {
{{classname}}_free({{classname}}_local_var);
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Outdated
return NULL;
}
*{{classname}}_local_var->{{{name}}} = *{{{name}}};
}
{{/isNumeric}}
{{#isBoolean}}
if ({{{name}}}) {
{{classname}}_local_var->{{{name}}} = malloc(sizeof({{datatype}}));
if (!{{classname}}_local_var->{{{name}}}) {
{{classname}}_free({{classname}}_local_var);
return NULL;
}
*{{classname}}_local_var->{{{name}}} = *{{{name}}};
}
{{/isBoolean}}
{{^isNumeric}}
{{^isBoolean}}
{{classname}}_local_var->{{{name}}} = {{{name}}};
{{/isBoolean}}
{{/isNumeric}}
{{/isPrimitiveType}}
{{^isPrimitiveType}}
{{classname}}_local_var->{{{name}}} = {{{name}}};
{{/isPrimitiveType}}
{{/isContainer}}
{{#isContainer}}
{{classname}}_local_var->{{{name}}} = {{{name}}};
{{/isContainer}}
{{/vars}}

{{classname}}_local_var->_library_owned = 1;
Expand Down Expand Up @@ -243,10 +278,10 @@ __attribute__((deprecated)) {{classname}}_t *{{classname}}_create(
{{/isPrimitiveType}}
{{#isPrimitiveType}}
{{#isNumeric}}
{{datatype}} {{name}}{{^-last}},{{/-last}}
{{datatype}} *{{name}}{{^-last}},{{/-last}}
{{/isNumeric}}
{{#isBoolean}}
{{datatype}} {{name}}{{^-last}},{{/-last}}
{{datatype}} *{{name}}{{^-last}},{{/-last}}
{{/isBoolean}}
{{#isEnum}}
{{#isString}}
Expand Down Expand Up @@ -344,6 +379,18 @@ void {{classname}}_free({{classname}}_t *{{classname}}) {
{{/isFreeFormObject}}
{{/isPrimitiveType}}
{{#isPrimitiveType}}
{{#isNumeric}}
if ({{{classname}}}->{{{name}}}) {
free({{{classname}}}->{{{name}}});
{{classname}}->{{name}} = NULL;
}
{{/isNumeric}}
{{#isBoolean}}
if ({{{classname}}}->{{{name}}}) {
free({{{classname}}}->{{{name}}});
{{classname}}->{{name}} = NULL;
}
{{/isBoolean}}
{{^isEnum}}
{{#isString}}
if ({{{classname}}}->{{{name}}}) {
Expand Down Expand Up @@ -454,12 +501,12 @@ cJSON *{{classname}}_convertToJSON({{classname}}_t *{{classname}}) {
{{^isContainer}}
{{#isPrimitiveType}}
{{#isNumeric}}
if(cJSON_AddNumberToObject(item, "{{{baseName}}}", {{{classname}}}->{{{name}}}) == NULL) {
if(cJSON_AddNumberToObject(item, "{{{baseName}}}", *{{{classname}}}->{{{name}}}) == NULL) {
goto fail; //Numeric
}
{{/isNumeric}}
{{#isBoolean}}
if(cJSON_AddBoolToObject(item, "{{{baseName}}}", {{{classname}}}->{{{name}}}) == NULL) {
if(cJSON_AddBoolToObject(item, "{{{baseName}}}", *{{{classname}}}->{{{name}}}) == NULL) {
goto fail; //Bool
}
{{/isBoolean}}
Expand Down Expand Up @@ -700,6 +747,18 @@ fail:
{{/isFreeFormObject}}
{{/isModel}}
{{/isPrimitiveType}}
{{#isPrimitiveType}}
{{#isNumeric}}
// define the local variable for {{{classname}}}->{{{name}}}
{{datatype}} *{{name}}_local_var = NULL;

{{/isNumeric}}
{{#isBoolean}}
// define the local variable for {{{classname}}}->{{{name}}}
{{datatype}} *{{name}}_local_var = NULL;

{{/isBoolean}}
{{/isPrimitiveType}}
{{/isContainer}}
{{/vars}}
{{#vars}}
Expand All @@ -722,13 +781,25 @@ fail:
{
goto end; //Numeric
}
{{name}}_local_var = malloc(sizeof({{datatype}}));
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
if(!{{name}}_local_var)
{
goto end;
}
*{{name}}_local_var = {{{name}}}->valuedouble;
{{/isNumeric}}
{{#isBoolean}}
{{^required}}if ({{{name}}}) { {{/required}}
if(!cJSON_IsBool({{{name}}}))
{
goto end; //Bool
}
{{name}}_local_var = malloc(sizeof({{datatype}}));
if(!{{name}}_local_var)
{
goto end;
}
*{{name}}_local_var = {{{name}}}->valueint;
{{/isBoolean}}
{{#isEnum}}
{{#isString}}
Expand Down Expand Up @@ -977,10 +1048,10 @@ fail:
{{/isPrimitiveType}}
{{#isPrimitiveType}}
{{#isNumeric}}
{{^required}}{{{name}}} ? {{/required}}{{{name}}}->valuedouble{{^required}} : 0{{/required}}{{^-last}},{{/-last}}
{{name}}_local_var{{^-last}},{{/-last}}
{{/isNumeric}}
{{#isBoolean}}
{{^required}}{{{name}}} ? {{/required}}{{{name}}}->valueint{{^required}} : 0{{/required}}{{^-last}},{{/-last}}
{{name}}_local_var{{^-last}},{{/-last}}
{{/isBoolean}}
{{#isEnum}}
{{#isString}}
Expand Down Expand Up @@ -1046,6 +1117,20 @@ end:
{{/isFreeFormObject}}
{{/isModel}}
{{/isPrimitiveType}}
{{#isPrimitiveType}}
{{#isNumeric}}
if ({{{name}}}_local_var) {
free({{{name}}}_local_var);
{{{name}}}_local_var = NULL;
}
{{/isNumeric}}
{{#isBoolean}}
if ({{{name}}}_local_var) {
free({{{name}}}_local_var);
{{{name}}}_local_var = NULL;
}
{{/isBoolean}}
{{/isPrimitiveType}}
{{/isContainer}}
{{#isContainer}}
{{#isArray}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,10 @@ typedef struct {{classname}}_t {
{{/isPrimitiveType}}
{{#isPrimitiveType}}
{{#isNumeric}}
{{datatype}} {{name}}; //numeric
{{datatype}} *{{name}}; //numeric
{{/isNumeric}}
{{#isBoolean}}
{{datatype}} {{name}}; //boolean
{{datatype}} *{{name}}; //boolean
{{/isBoolean}}
{{#isEnum}}
{{#isString}}
Expand Down Expand Up @@ -191,10 +191,10 @@ __attribute__((deprecated)) {{classname}}_t *{{classname}}_create(
{{/isPrimitiveType}}
{{#isPrimitiveType}}
{{#isNumeric}}
{{datatype}} {{name}}{{^-last}},{{/-last}}
{{datatype}} *{{name}}{{^-last}},{{/-last}}
{{/isNumeric}}
{{#isBoolean}}
{{datatype}} {{name}}{{^-last}},{{/-last}}
{{datatype}} *{{name}}{{^-last}},{{/-last}}
{{/isBoolean}}
{{#isEnum}}
{{#isString}}
Expand Down
35 changes: 30 additions & 5 deletions samples/client/petstore/c-useJsonUnformatted/model/api_response.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,23 @@


static api_response_t *api_response_create_internal(
int code,
int *code,
char *type,
char *message
) {
api_response_t *api_response_local_var = malloc(sizeof(api_response_t));
if (!api_response_local_var) {
return NULL;
}
api_response_local_var->code = code;
memset(api_response_local_var, 0, sizeof(api_response_t));
if (code) {
api_response_local_var->code = malloc(sizeof(int));
if (!api_response_local_var->code) {
api_response_free(api_response_local_var);
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Outdated
return NULL;
}
*api_response_local_var->code = *code;
}
api_response_local_var->type = type;
api_response_local_var->message = message;

Expand All @@ -23,7 +31,7 @@ static api_response_t *api_response_create_internal(
}

__attribute__((deprecated)) api_response_t *api_response_create(
int code,
int *code,
char *type,
char *message
) {
Expand All @@ -43,6 +51,10 @@ void api_response_free(api_response_t *api_response) {
return ;
}
listEntry_t *listEntry;
if (api_response->code) {
free(api_response->code);
api_response->code = NULL;
}
if (api_response->type) {
free(api_response->type);
api_response->type = NULL;
Expand All @@ -59,7 +71,7 @@ cJSON *api_response_convertToJSON(api_response_t *api_response) {

// 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
}
}
Expand Down Expand Up @@ -92,6 +104,9 @@ api_response_t *api_response_parseFromJSON(cJSON *api_responseJSON){

api_response_t *api_response_local_var = NULL;

// define the local variable for api_response->code
int *code_local_var = NULL;

// api_response->code
cJSON *code = cJSON_GetObjectItemCaseSensitive(api_responseJSON, "code");
if (cJSON_IsNull(code)) {
Expand All @@ -102,6 +117,12 @@ api_response_t *api_response_parseFromJSON(cJSON *api_responseJSON){
{
goto end; //Numeric
}
code_local_var = malloc(sizeof(int));
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
if(!code_local_var)
{
goto end;
}
*code_local_var = code->valuedouble;
}

// api_response->type
Expand Down Expand Up @@ -130,13 +151,17 @@ api_response_t *api_response_parseFromJSON(cJSON *api_responseJSON){


api_response_local_var = api_response_create_internal (
code ? code->valuedouble : 0,
code_local_var,
type && !cJSON_IsNull(type) ? strdup(type->valuestring) : NULL,
message && !cJSON_IsNull(message) ? strdup(message->valuestring) : NULL
);

return api_response_local_var;
end:
if (code_local_var) {
free(code_local_var);
code_local_var = NULL;
}
return NULL;

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ typedef struct api_response_t api_response_t;


typedef struct api_response_t {
int code; //numeric
int *code; //numeric
char *type; // string
char *message; // string

int _library_owned; // Is the library responsible for freeing this object?
} api_response_t;

__attribute__((deprecated)) api_response_t *api_response_create(
int code,
int *code,
char *type,
char *message
);
Expand Down
Loading
Loading