diff --git a/bin/configs/c-useJsonUnformatted.yaml b/bin/configs/c-useJsonUnformatted.yaml index 76a0e130aee7..4f5c95785055 100644 --- a/bin/configs/c-useJsonUnformatted.yaml +++ b/bin/configs/c-useJsonUnformatted.yaml @@ -4,6 +4,7 @@ inputSpec: modules/openapi-generator/src/test/resources/2_0/c/petstore.yaml templateDir: modules/openapi-generator/src/main/resources/C-libcurl additionalProperties: useJsonUnformatted: true + declareNumberBooleanWithoutPointer: true modelNameMappings: another_model: MappedModel another_property: mappedProperty diff --git a/docs/generators/c.md b/docs/generators/c.md index 22f54ac1b53e..581128e95e9d 100644 --- a/docs/generators/c.md +++ b/docs/generators/c.md @@ -19,6 +19,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl | Option | Description | Values | Default | | ------ | ----------- | ------ | ------- | |allowUnicodeIdentifiers|boolean, toggles whether unicode identifiers are allowed in names or not, default is false| |false| +|declareNumberBooleanWithoutPointer|Declare number, boolean types without pointer using model-body, model-header templates from OpenAPI Generator v7.20.0.| |false| |disallowAdditionalPropertiesIfNotPresent|If false, the 'additionalProperties' implementation (set to true by default) is compliant with the OAS and JSON schema specifications. If true (default), keep the old (incorrect) behaviour that 'additionalProperties' is set to false by default.|
**false**
The 'additionalProperties' implementation is compliant with the OAS and JSON schema specifications.
**true**
Keep the old (incorrect) behaviour that 'additionalProperties' is set to false by default.
|true| |ensureUniqueParams|Whether to ensure parameter names are unique in an operation (rename parameters that are not).| |true| |enumUnknownDefaultCase|If the server adds new enum cases, that are unknown by an old spec/client, the client will fail to parse the network response.With this option enabled, each enum will have a new case, 'unknown_default_open_api', so that when the server sends an enum case that is not known by the client/spec, they can safely fallback to this case.|
**false**
No changes to the enum's are made, this is the default option.
**true**
With this option enabled, each enum will have a new case, 'unknown_default_open_api', so that when the enum case sent by the server is not known by the client/spec, can safely be decoded to this case.
|false| diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CLibcurlClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CLibcurlClientCodegen.java index b201cdfbc33f..406c32d0f965 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CLibcurlClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CLibcurlClientCodegen.java @@ -41,6 +41,9 @@ public class CLibcurlClientCodegen extends DefaultCodegen implements CodegenConf public static final String USE_JSON_UNFORMATTED = "useJsonUnformatted"; public static final String USE_JSON_UNFORMATTED_DESC = "Use cJSON_PrintUnformatted instead of cJSON_Print when creating the JSON string."; + public static final String DECLARE_NUMBER_BOOLEAN_WITHOUT_POINTER = "declareNumberBooleanWithoutPointer"; + public static final String DECLARE_NUMBER_BOOLEAN_WITHOUT_POINTER_DESC = "Declare number, boolean types without pointer using model-body, model-header templates from OpenAPI Generator v7.20.0."; + public static final String PROJECT_NAME = "projectName"; protected String moduleName; protected String projectName; @@ -270,7 +273,6 @@ public CLibcurlClientCodegen() { // primitives in C lang languageSpecificPrimitives.add("int"); languageSpecificPrimitives.add("short"); - languageSpecificPrimitives.add("int"); languageSpecificPrimitives.add("long"); languageSpecificPrimitives.add("float"); languageSpecificPrimitives.add("double"); @@ -315,6 +317,9 @@ public CLibcurlClientCodegen() { cliOptions.add(new CliOption(USE_JSON_UNFORMATTED, USE_JSON_UNFORMATTED_DESC). defaultValue(Boolean.FALSE.toString())); + + cliOptions.add(new CliOption(DECLARE_NUMBER_BOOLEAN_WITHOUT_POINTER, DECLARE_NUMBER_BOOLEAN_WITHOUT_POINTER_DESC). + defaultValue(Boolean.FALSE.toString())); } @Override @@ -336,6 +341,16 @@ public void processOpts() { additionalProperties.put("cJSONPrint", "cJSON_Print"); } + if (additionalProperties.containsKey(DECLARE_NUMBER_BOOLEAN_WITHOUT_POINTER)) { + if (Boolean.parseBoolean(additionalProperties.get(DECLARE_NUMBER_BOOLEAN_WITHOUT_POINTER).toString())) { + modelTemplateFiles.put("model-header-v7_20_0.mustache", ".h"); + modelTemplateFiles.put("model-body-v7_20_0.mustache", ".c"); + + modelTemplateFiles.remove("model-header.mustache"); + modelTemplateFiles.remove("model-body.mustache"); + } + } + // make api and model doc path available in mustache template additionalProperties.put("apiDocPath", apiDocPath); additionalProperties.put("modelDocPath", modelDocPath); diff --git a/modules/openapi-generator/src/main/resources/C-libcurl/model-body-v7_20_0.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/model-body-v7_20_0.mustache new file mode 100644 index 000000000000..70f2106ba847 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/C-libcurl/model-body-v7_20_0.mustache @@ -0,0 +1,1124 @@ +{{#models}} +{{#model}} +#include +#include +#include +#include "{{classFilename}}.h" + + +{{#isEnum}} +char* {{classFilename}}_{{classname}}_ToString({{projectName}}_{{classVarName}}_{{enumName}}_e {{classname}}) { + char *{{classname}}Array[] = { "NULL"{{#allowableValues}}{{#values}}, "{{.}}"{{/values}}{{/allowableValues}} }; + return {{classname}}Array[{{classname}}]; +} + +{{projectName}}_{{classVarName}}_{{enumName}}_e {{classFilename}}_{{classname}}_FromString(char* {{classname}}) { + int stringToReturn = 0; + char *{{classname}}Array[] = { "NULL"{{#allowableValues}}{{#values}}, "{{.}}"{{/values}}{{/allowableValues}} }; + size_t sizeofArray = sizeof({{classname}}Array) / sizeof({{classname}}Array[0]); + while(stringToReturn < sizeofArray) { + if(strcmp({{classname}}, {{classname}}Array[stringToReturn]) == 0) { + return stringToReturn; + } + stringToReturn++; + } + return 0; +} + +cJSON *{{classname}}_convertToJSON({{projectName}}_{{classVarName}}_{{enumName}}_e {{classname}}) { + cJSON *item = cJSON_CreateObject(); +{{#isString}} + if(cJSON_AddStringToObject(item, "{{{classname}}}", {{classFilename}}_{{{classname}}}_ToString({{{classname}}})) == NULL) { + goto fail; + } +{{/isString}} +{{#isNumeric}} + if(cJSON_AddNumberToObject(item, "{{{classname}}}", {{{classname}}}) == NULL) { + goto fail; + } +{{/isNumeric}} +{{#isBoolean}} + if(cJSON_AddBoolToObject(item, "{{{classname}}}", {{{classname}}}) == NULL) { + goto fail; + } +{{/isBoolean}} + return item; +fail: + cJSON_Delete(item); + return NULL; +} + +{{projectName}}_{{classVarName}}_{{enumName}}_e {{classname}}_parseFromJSON(cJSON *{{classname}}JSON) { +{{#isEnum}} +{{#isNumeric}} + if(!cJSON_IsNumber({{{classname}}}JSON)) { + return 0; + } + return {{classname}}JSON->valueint; +{{/isNumeric}} +{{#isString}} + if(!cJSON_IsString({{{classname}}}JSON) || ({{{classname}}}JSON->valuestring == NULL)) { + return 0; + } + return {{classFilename}}_{{classname}}_FromString({{{classname}}}JSON->valuestring); +{{/isString}} +{{/isEnum}} +} +{{/isEnum}} +{{^isEnum}} + {{#vars}} + {{^isContainer}} + {{#isPrimitiveType}} + {{^isModel}} + {{#isEnum}} +char* {{classname}}_{{name}}_ToString({{projectName}}_{{classVarName}}_{{enumName}}_e {{name}}) { + char* {{name}}Array[] = { "NULL"{{#allowableValues}}{{#values}}, "{{.}}"{{/values}}{{/allowableValues}} }; + return {{name}}Array[{{name}}]; +} + +{{projectName}}_{{classVarName}}_{{enumName}}_e {{classname}}_{{name}}_FromString(char* {{name}}){ + int stringToReturn = 0; + char *{{name}}Array[] = { "NULL"{{#allowableValues}}{{#values}}, "{{.}}"{{/values}}{{/allowableValues}} }; + size_t sizeofArray = sizeof({{name}}Array) / sizeof({{name}}Array[0]); + while(stringToReturn < sizeofArray) { + if(strcmp({{name}}, {{name}}Array[stringToReturn]) == 0) { + return stringToReturn; + } + stringToReturn++; + } + return 0; +} + {{/isEnum}} + {{/isModel}} + {{/isPrimitiveType}} + {{/isContainer}} + {{#isContainer}} + {{#items}} + {{^isModel}} + {{#isEnum}} +char* {{classname}}_{{name}}_ToString({{projectName}}_{{classVarName}}_{{enumName}}_e {{name}}) { + char *{{name}}Array[] = { "NULL"{{#allowableValues}}{{#values}}, "{{.}}"{{/values}}{{/allowableValues}} }; + return {{name}}Array[{{name}} - 1]; +} + +{{projectName}}_{{classVarName}}_{{enumName}}_e {{classname}}_{{name}}_FromString(char* {{name}}) { + int stringToReturn = 0; + char *{{name}}Array[] = { "NULL"{{#allowableValues}}{{#values}}, "{{.}}"{{/values}}{{/allowableValues}} }; + size_t sizeofArray = sizeof({{name}}Array) / sizeof({{name}}Array[0]); + while(stringToReturn < sizeofArray) { + if(strcmp({{name}}, {{name}}Array[stringToReturn]) == 0) { + return stringToReturn + 1; + } + stringToReturn++; + } + return 0; +} + {{/isEnum}} + {{/isModel}} + {{/items}} + {{/isContainer}} + {{/vars}} + +static {{classname}}_t *{{classname}}_create_internal( + {{#vars}} + {{^isContainer}} + {{^isPrimitiveType}} + {{#isModel}} + {{#isEnum}} + {{projectName}}_{{classVarName}}_{{enumName}}_e {{name}}{{^-last}},{{/-last}} + {{/isEnum}} + {{^isEnum}} + {{datatype}}_t *{{name}}{{^-last}},{{/-last}} + {{/isEnum}} + {{/isModel}} + {{^isModel}} + {{^isFreeFormObject}} + {{^isEnum}} + {{datatype}}_t *{{name}}{{^-last}},{{/-last}} + {{/isEnum}} + {{#isEnum}} + {{projectName}}_{{dataType}}_{{enumName}}_e {{name}}{{^-last}},{{/-last}} + {{/isEnum}} + {{/isFreeFormObject}} + {{/isModel}} + {{#isUuid}} + {{datatype}} *{{name}}{{^-last}},{{/-last}} + {{/isUuid}} + {{#isEmail}} + {{datatype}} *{{name}}{{^-last}},{{/-last}} + {{/isEmail}} + {{#isFreeFormObject}} + {{datatype}}_t *{{name}}{{^-last}},{{/-last}} + {{/isFreeFormObject}} + {{/isPrimitiveType}} + {{#isPrimitiveType}} + {{#isNumeric}} + {{datatype}} {{name}}{{^-last}},{{/-last}} + {{/isNumeric}} + {{#isBoolean}} + {{datatype}} {{name}}{{^-last}},{{/-last}} + {{/isBoolean}} + {{#isEnum}} + {{#isString}} + {{projectName}}_{{classVarName}}_{{enumName}}_e {{name}}{{^-last}},{{/-last}} + {{/isString}} + {{/isEnum}} + {{^isEnum}} + {{#isString}} + {{datatype}} *{{name}}{{^-last}},{{/-last}} + {{/isString}} + {{/isEnum}} + {{#isByteArray}} + {{datatype}} *{{name}}{{^-last}},{{/-last}} + {{/isByteArray}} + {{#isBinary}} + {{datatype}} {{name}}{{^-last}},{{/-last}} + {{/isBinary}} + {{#isDate}} + {{datatype}} *{{name}}{{^-last}},{{/-last}} + {{/isDate}} + {{#isDateTime}} + {{datatype}} *{{name}}{{^-last}},{{/-last}} + {{/isDateTime}} + {{/isPrimitiveType}} + {{/isContainer}} + {{#isContainer}} + {{#isArray}} + {{#isPrimitiveType}} + {{datatype}}_t *{{name}}{{^-last}},{{/-last}} + {{/isPrimitiveType}} + {{^isPrimitiveType}} + {{datatype}}_t *{{name}}{{^-last}},{{/-last}} + {{/isPrimitiveType}} + {{/isArray}} + {{#isMap}} + {{datatype}} {{name}}{{^-last}},{{/-last}} + {{/isMap}} + {{/isContainer}} + {{/vars}} + ) { + {{classname}}_t *{{classname}}_local_var = malloc(sizeof({{classname}}_t)); + if (!{{classname}}_local_var) { + return NULL; + } + {{#vars}} + {{classname}}_local_var->{{{name}}} = {{{name}}}; + {{/vars}} + + {{classname}}_local_var->_library_owned = 1; + return {{classname}}_local_var; +} + +__attribute__((deprecated)) {{classname}}_t *{{classname}}_create( + {{#vars}} + {{^isContainer}} + {{^isPrimitiveType}} + {{#isModel}} + {{#isEnum}} + {{projectName}}_{{classVarName}}_{{enumName}}_e {{name}}{{^-last}},{{/-last}} + {{/isEnum}} + {{^isEnum}} + {{datatype}}_t *{{name}}{{^-last}},{{/-last}} + {{/isEnum}} + {{/isModel}} + {{^isModel}} + {{^isFreeFormObject}} + {{^isEnum}} + {{datatype}}_t *{{name}}{{^-last}},{{/-last}} + {{/isEnum}} + {{#isEnum}} + {{projectName}}_{{dataType}}_{{enumName}}_e {{name}}{{^-last}},{{/-last}} + {{/isEnum}} + {{/isFreeFormObject}} + {{/isModel}} + {{#isUuid}} + {{datatype}} *{{name}}{{^-last}},{{/-last}} + {{/isUuid}} + {{#isEmail}} + {{datatype}} *{{name}}{{^-last}},{{/-last}} + {{/isEmail}} + {{#isFreeFormObject}} + {{datatype}}_t *{{name}}{{^-last}},{{/-last}} + {{/isFreeFormObject}} + {{/isPrimitiveType}} + {{#isPrimitiveType}} + {{#isNumeric}} + {{datatype}} {{name}}{{^-last}},{{/-last}} + {{/isNumeric}} + {{#isBoolean}} + {{datatype}} {{name}}{{^-last}},{{/-last}} + {{/isBoolean}} + {{#isEnum}} + {{#isString}} + {{projectName}}_{{classVarName}}_{{enumName}}_e {{name}}{{^-last}},{{/-last}} + {{/isString}} + {{/isEnum}} + {{^isEnum}} + {{#isString}} + {{datatype}} *{{name}}{{^-last}},{{/-last}} + {{/isString}} + {{/isEnum}} + {{#isByteArray}} + {{datatype}} *{{name}}{{^-last}},{{/-last}} + {{/isByteArray}} + {{#isBinary}} + {{datatype}} {{name}}{{^-last}},{{/-last}} + {{/isBinary}} + {{#isDate}} + {{datatype}} *{{name}}{{^-last}},{{/-last}} + {{/isDate}} + {{#isDateTime}} + {{datatype}} *{{name}}{{^-last}},{{/-last}} + {{/isDateTime}} + {{/isPrimitiveType}} + {{/isContainer}} + {{#isContainer}} + {{#isArray}} + {{#isPrimitiveType}} + {{datatype}}_t *{{name}}{{^-last}},{{/-last}} + {{/isPrimitiveType}} + {{^isPrimitiveType}} + {{datatype}}_t *{{name}}{{^-last}},{{/-last}} + {{/isPrimitiveType}} + {{/isArray}} + {{#isMap}} + {{datatype}} {{name}}{{^-last}},{{/-last}} + {{/isMap}} + {{/isContainer}} + {{/vars}} + ) { + return {{classname}}_create_internal ( + {{#vars}} + {{name}}{{^-last}},{{/-last}} + {{/vars}} + ); +} + +void {{classname}}_free({{classname}}_t *{{classname}}) { + if(NULL == {{classname}}){ + return ; + } + if({{classname}}->_library_owned != 1){ + fprintf(stderr, "WARNING: %s() does NOT free objects allocated by the user\n", "{{classname}}_free"); + return ; + } + listEntry_t *listEntry; + {{#vars}} + {{^isContainer}} + {{^isPrimitiveType}} + {{#isModel}} + {{^isEnum}} + if ({{{classname}}}->{{{name}}}) { + {{{complexType}}}_free({{{classname}}}->{{{name}}}); + {{classname}}->{{name}} = NULL; + } + {{/isEnum}} + {{/isModel}} + {{^isModel}} + {{^isFreeFormObject}} + {{^isEnum}} + if ({{{classname}}}->{{{name}}}) { + {{{complexType}}}_free({{{classname}}}->{{{name}}}); + {{classname}}->{{name}} = NULL; + } + {{/isEnum}} + {{/isFreeFormObject}} + {{/isModel}} + {{#isUuid}} + if ({{{classname}}}->{{{name}}}) { + free({{{classname}}}->{{{name}}}); + {{classname}}->{{name}} = NULL; + } + {{/isUuid}} + {{#isEmail}} + if ({{{classname}}}->{{{name}}}) { + free({{{classname}}}->{{{name}}}); + {{classname}}->{{name}} = NULL; + } + {{/isEmail}} + {{#isFreeFormObject}} + if ({{{classname}}}->{{{name}}}) { + object_free({{{classname}}}->{{{name}}}); + {{classname}}->{{name}} = NULL; + } + {{/isFreeFormObject}} + {{/isPrimitiveType}} + {{#isPrimitiveType}} + {{^isEnum}} + {{#isString}} + if ({{{classname}}}->{{{name}}}) { + free({{{classname}}}->{{{name}}}); + {{classname}}->{{name}} = NULL; + } + {{/isString}} + {{/isEnum}} + {{#isByteArray}} + if ({{{classname}}}->{{{name}}}) { + free({{{classname}}}->{{{name}}}); + {{classname}}->{{name}} = NULL; + } + {{/isByteArray}} + {{#isBinary}} + if ({{{classname}}}->{{{name}}}) { + free({{{classname}}}->{{{name}}}->data); + {{classname}}->{{name}} = NULL; + } + {{/isBinary}} + {{#isDate}} + if ({{{classname}}}->{{{name}}}) { + free({{{classname}}}->{{{name}}}); + {{classname}}->{{name}} = NULL; + } + {{/isDate}} + {{#isDateTime}} + if ({{{classname}}}->{{{name}}}) { + free({{{classname}}}->{{{name}}}); + {{classname}}->{{name}} = NULL; + } + {{/isDateTime}} + {{/isPrimitiveType}} + {{/isContainer}} + {{#isContainer}} + {{#isArray}} + {{#isPrimitiveType}} + if ({{{classname}}}->{{{name}}}) { + list_ForEach(listEntry, {{classname}}->{{name}}) { + free(listEntry->data); + } + list_freeList({{classname}}->{{name}}); + {{classname}}->{{name}} = NULL; + } + {{/isPrimitiveType}} + {{^isPrimitiveType}} + if ({{{classname}}}->{{{name}}}) { + list_ForEach(listEntry, {{classname}}->{{name}}) { + {{complexType}}_free(listEntry->data); + } + list_freeList({{classname}}->{{name}}); + {{classname}}->{{name}} = NULL; + } + {{/isPrimitiveType}} + {{/isArray}} + {{#isMap}} + if ({{{classname}}}->{{{name}}}) { + list_ForEach(listEntry, {{classname}}->{{name}}) { + keyValuePair_t *localKeyValue = listEntry->data; + free (localKeyValue->key); + free (localKeyValue->value); + keyValuePair_free(localKeyValue); + } + list_freeList({{classname}}->{{name}}); + {{classname}}->{{name}} = NULL; + } + {{/isMap}} + {{/isContainer}} + {{/vars}} + free({{classname}}); +} + +cJSON *{{classname}}_convertToJSON({{classname}}_t *{{classname}}) { + cJSON *item = cJSON_CreateObject(); + {{#vars}} + + // {{{classname}}}->{{{name}}} + {{#required}} + {{^isEnum}} + if (!{{{classname}}}->{{{name}}}) { + goto fail; + } + {{/isEnum}} + {{#isEnum}} + {{#isPrimitiveType}} + if ({{projectName}}_{{classVarName}}_{{enumName}}_NULL == {{{classname}}}->{{{name}}}) { + {{/isPrimitiveType}} + {{^isPrimitiveType}} + if ({{projectName}}_{{dataType}}_{{enumName}}_NULL == {{{classname}}}->{{{name}}}) { + {{/isPrimitiveType}} + goto fail; + } + {{/isEnum}} + {{/required}} + {{^required}} + {{^isEnum}} + if({{{classname}}}->{{{name}}}) { + {{/isEnum}} + {{#isEnum}} + {{#isPrimitiveType}} + if({{{classname}}}->{{{name}}} != {{projectName}}_{{classVarName}}_{{enumName}}_NULL) { + {{/isPrimitiveType}} + {{^isPrimitiveType}} + if({{{classname}}}->{{{name}}} != {{projectName}}_{{dataType}}_{{enumName}}_NULL) { + {{/isPrimitiveType}} + {{/isEnum}} + {{/required}} + {{^isContainer}} + {{#isPrimitiveType}} + {{#isNumeric}} + if(cJSON_AddNumberToObject(item, "{{{baseName}}}", {{{classname}}}->{{{name}}}) == NULL) { + goto fail; //Numeric + } + {{/isNumeric}} + {{#isBoolean}} + if(cJSON_AddBoolToObject(item, "{{{baseName}}}", {{{classname}}}->{{{name}}}) == NULL) { + goto fail; //Bool + } + {{/isBoolean}} + {{#isEnum}} + {{#isString}} + if(cJSON_AddStringToObject(item, "{{{baseName}}}", {{classname}}_{{name}}_ToString({{{classname}}}->{{{name}}})) == NULL) + { + goto fail; //Enum + } + {{/isString}} + {{/isEnum}} + {{^isEnum}} + {{#isString}} + if(cJSON_AddStringToObject(item, "{{{baseName}}}", {{{classname}}}->{{{name}}}) == NULL) { + goto fail; //String + } + {{/isString}} + {{/isEnum}} + {{#isByteArray}} + if(cJSON_AddStringToObject(item, "{{{baseName}}}", {{{classname}}}->{{{name}}}) == NULL) { + goto fail; //ByteArray + } + {{/isByteArray}} + {{#isBinary}} + char* encoded_str_{{{name}}} = base64encode({{{classname}}}->{{{name}}}->data,{{{classname}}}->{{{name}}}->len); + if(cJSON_AddStringToObject(item, "{{{baseName}}}", encoded_str_{{{name}}}) == NULL) { + goto fail; //Binary + } + free (encoded_str_{{{name}}}); + {{/isBinary}} + {{#isDate}} + if(cJSON_AddStringToObject(item, "{{{baseName}}}", {{{classname}}}->{{{name}}}) == NULL) { + goto fail; //Date + } + {{/isDate}} + {{#isDateTime}} + if(cJSON_AddStringToObject(item, "{{{baseName}}}", {{{classname}}}->{{{name}}}) == NULL) { + goto fail; //Date-Time + } + {{/isDateTime}} + {{/isPrimitiveType}} + {{^isPrimitiveType}} + {{#isModel}} + {{#isEnum}} + cJSON *{{{name}}}_enum_local_JSON = {{datatypeWithEnum}}_convertToJSON({{{classname}}}->{{{name}}}); + if({{{name}}}_enum_local_JSON == NULL) { + goto fail; // enum + } + cJSON_AddItemToObject(item, "{{{baseName}}}", {{{name}}}_enum_local_JSON); + if(item->child == NULL) { + goto fail; + } + {{/isEnum}} + {{^isEnum}} + cJSON *{{{name}}}_local_JSON = {{complexType}}{{#isFreeFormObject}}object{{/isFreeFormObject}}_convertToJSON({{{classname}}}->{{{name}}}); + if({{{name}}}_local_JSON == NULL) { + goto fail; //model + } + cJSON_AddItemToObject(item, "{{{baseName}}}", {{{name}}}_local_JSON); + if(item->child == NULL) { + goto fail; + } + {{/isEnum}} + {{/isModel}} + {{^isModel}} + {{^isFreeFormObject}} + cJSON *{{{name}}}_local_JSON = {{complexType}}_convertToJSON({{{classname}}}->{{{name}}}); + if({{{name}}}_local_JSON == NULL) { + goto fail; // custom + } + cJSON_AddItemToObject(item, "{{{baseName}}}", {{{name}}}_local_JSON); + if(item->child == NULL) { + goto fail; + } + {{/isFreeFormObject}} + {{/isModel}} + {{#isUuid}} + if(cJSON_AddStringToObject(item, "{{{baseName}}}", {{{classname}}}->{{{name}}}) == NULL) { + goto fail; //uuid + } + {{/isUuid}} + {{#isEmail}} + if(cJSON_AddStringToObject(item, "{{{baseName}}}", {{{classname}}}->{{{name}}}) == NULL) { + goto fail; //Email + } + {{/isEmail}} + {{#isFreeFormObject}} + cJSON *{{{name}}}_object = object_convertToJSON({{{classname}}}->{{{name}}}); + if({{{name}}}_object == NULL) { + goto fail; //model + } + cJSON_AddItemToObject(item, "{{{baseName}}}", {{{name}}}_object); + if(item->child == NULL) { + goto fail; + } + {{/isFreeFormObject}} + {{/isPrimitiveType}} + {{/isContainer}} + {{#isContainer}} + {{#isArray}} + {{#isPrimitiveType}} + cJSON *{{{name}}} = cJSON_AddArrayToObject(item, "{{{baseName}}}"); + if({{{name}}} == NULL) { + goto fail; //primitive container + } + + listEntry_t *{{{name}}}ListEntry; + list_ForEach({{{name}}}ListEntry, {{{classname}}}->{{{name}}}) { + {{#items}} + {{#isString}} + if(cJSON_AddStringToObject({{{name}}}, "", {{{name}}}ListEntry->data) == NULL) + { + goto fail; + } + {{/isString}} + {{#isBoolean}} + if(cJSON_AddBoolToObject({{{name}}}, "", *(cJSON_bool *){{{name}}}ListEntry->data) == NULL) + { + goto fail; + } + {{/isBoolean}} + {{#isNumeric}} + if(cJSON_AddNumberToObject({{{name}}}, "", *(double *){{{name}}}ListEntry->data) == NULL) + { + goto fail; + } + {{/isNumeric}} + {{/items}} + } + {{/isPrimitiveType}} + {{^isPrimitiveType}} + cJSON *{{{name}}} = cJSON_AddArrayToObject(item, "{{{baseName}}}"); + if({{{name}}} == NULL) { + goto fail; //nonprimitive container + } + + listEntry_t *{{{name}}}ListEntry; + if ({{{classname}}}->{{{name}}}) { + list_ForEach({{{name}}}ListEntry, {{classname}}->{{{name}}}) { + cJSON *itemLocal = {{complexType}}_convertToJSON({{#isEnum}}{{#items}}({{projectName}}_{{classVarName}}_{{enumName}}_e){{/items}}{{/isEnum}}{{{name}}}ListEntry->data); + if(itemLocal == NULL) { + goto fail; + } + cJSON_AddItemToArray({{{name}}}, itemLocal); + } + } + {{/isPrimitiveType}} + {{/isArray}} + {{#isMap}} + cJSON *{{{name}}} = cJSON_AddObjectToObject(item, "{{{baseName}}}"); + if({{{name}}} == NULL) { + goto fail; //primitive map container + } + cJSON *localMapObject = {{{name}}}; + listEntry_t *{{{name}}}ListEntry; + if ({{{classname}}}->{{{name}}}) { + list_ForEach({{{name}}}ListEntry, {{{classname}}}->{{{name}}}) { + keyValuePair_t *localKeyValue = {{{name}}}ListEntry->data; + {{#items}} + {{#isString}} + if(cJSON_AddStringToObject(localMapObject, localKeyValue->key, localKeyValue->value) == NULL) + { + goto fail; + } + {{/isString}} + {{#isByteArray}} + if(cJSON_AddStringToObject(localMapObject, localKeyValue->key, localKeyValue->value) == NULL) + { + goto fail; + } + {{/isByteArray}} + {{#isNumeric}} + if(cJSON_AddNumberToObject(localMapObject, localKeyValue->key, *(double *)localKeyValue->value) == NULL) + { + goto fail; + } + {{/isNumeric}} + {{#isBoolean}} + if(cJSON_AddBoolToObject(localMapObject, localKeyValue->key, *(cJSON_bool *)localKeyValue->value) == NULL) + { + goto fail; + } + {{/isBoolean}} + {{/items}} + } + } + {{/isMap}} + {{/isContainer}} + {{^required}} + } + {{/required}} + + {{/vars}} + return item; +fail: + if (item) { + cJSON_Delete(item); + } + return NULL; +} + +{{classname}}_t *{{classname}}_parseFromJSON(cJSON *{{classname}}JSON){ + + {{classname}}_t *{{classname}}_local_var = NULL; + + {{#vars}} + {{#isContainer}} + {{#isArray}} + // define the local list for {{{classname}}}->{{{name}}} + list_t *{{{name}}}List = NULL; + + {{/isArray}} + {{#isMap}} + // define the local map for {{{classname}}}->{{{name}}} + list_t *{{{name}}}List = NULL; + + {{/isMap}} + {{/isContainer}} + {{^isContainer}} + {{^isPrimitiveType}} + {{#isModel}} + {{^isEnum}} + // define the local variable for {{{classname}}}->{{{name}}} + {{^isFreeFormObject}}{{complexType}}{{/isFreeFormObject}}{{#isFreeFormObject}}object{{/isFreeFormObject}}_t *{{name}}_local_nonprim = NULL; + + {{/isEnum}} + {{/isModel}} + {{^isModel}} + {{^isFreeFormObject}} + // define the local variable for {{{classname}}}->{{{name}}} + {{^isEnum}} + {{complexType}}_t *{{name}}_local_nonprim = NULL; + {{/isEnum}} + {{#isEnum}} + {{projectName}}_{{dataType}}_{{enumName}}_e {{name}}_local_nonprim = 0; + {{/isEnum}} + + {{/isFreeFormObject}} + {{/isModel}} + {{/isPrimitiveType}} + {{/isContainer}} + {{/vars}} + {{#vars}} + // {{{classname}}}->{{{name}}} + cJSON *{{{name}}} = cJSON_GetObjectItemCaseSensitive({{classname}}JSON, "{{{baseName}}}"); + if (cJSON_IsNull({{{name}}})) { + {{{name}}} = NULL; + } + {{#required}} + if (!{{{name}}}) { + goto end; + } + + {{/required}} + {{^isContainer}} + {{#isPrimitiveType}} + {{#isNumeric}} + {{^required}}if ({{{name}}}) { {{/required}} + if(!cJSON_IsNumber({{{name}}})) + { + goto end; //Numeric + } + {{/isNumeric}} + {{#isBoolean}} + {{^required}}if ({{{name}}}) { {{/required}} + if(!cJSON_IsBool({{{name}}})) + { + goto end; //Bool + } + {{/isBoolean}} + {{#isEnum}} + {{#isString}} + {{projectName}}_{{classVarName}}_{{enumName}}_e {{name}}Variable; + {{^required}}if ({{{name}}}) { {{/required}} + if(!cJSON_IsString({{{name}}})) + { + goto end; //Enum + } + {{name}}Variable = {{classname}}_{{name}}_FromString({{{name}}}->valuestring); + {{/isString}} + {{/isEnum}} + {{^isEnum}} + {{#isString}} + {{^required}}if ({{{name}}}) { {{/required}} + if(!cJSON_IsString({{{name}}}){{^required}} && !cJSON_IsNull({{{name}}}){{/required}}) + { + goto end; //String + } + {{/isString}} + {{/isEnum}} + {{#isByteArray}} + {{^required}}if ({{{name}}}) { {{/required}} + if(!cJSON_IsString({{{name}}})) + { + goto end; //ByteArray + } + {{/isByteArray}} + {{#isBinary}} + binary_t* decoded_str_{{{name}}} = malloc(sizeof(struct binary_t)); + {{^required}}if ({{{name}}}) { {{/required}} + if(!cJSON_IsString({{{name}}})) + { + goto end; //Binary + } + decoded_str_{{{name}}}->data = base64decode({{{name}}}->valuestring, strlen({{{name}}}->valuestring), &decoded_str_{{{name}}}->len); + if (!decoded_str_{{{name}}}->data) { + goto end; + } + {{/isBinary}} + {{#isDate}} + {{^required}}if ({{{name}}}) { {{/required}} + if(!cJSON_IsString({{{name}}})) + { + goto end; //Date + } + {{/isDate}} + {{#isDateTime}} + {{^required}}if ({{{name}}}) { {{/required}} + if(!cJSON_IsString({{{name}}}) && !cJSON_IsNull({{{name}}})) + { + goto end; //DateTime + } + {{/isDateTime}} + {{/isPrimitiveType}} + {{^isPrimitiveType}} + {{#isModel}} + {{#isEnum}} + {{classFilename}}_{{datatypeWithEnum}}_e {{name}}_local_nonprim_enum; + {{^required}}if ({{{name}}}) { {{/required}} + {{{name}}}_local_nonprim_enum = {{datatypeWithEnum}}_parseFromJSON({{{name}}}); //enum model + {{/isEnum}} + {{^isEnum}} + {{^required}}if ({{{name}}}) { {{/required}} + {{{name}}}_local_nonprim = {{complexType}}{{#isFreeFormObject}}object{{/isFreeFormObject}}_parseFromJSON({{{name}}}); //nonprimitive + {{/isEnum}} + {{/isModel}} + {{^isModel}} + {{^isFreeFormObject}} + {{^required}}if ({{{name}}}) { {{/required}} + {{{name}}}_local_nonprim = {{complexType}}_parseFromJSON({{{name}}}); //custom + {{/isFreeFormObject}} + {{/isModel}} + {{#isUuid}} + {{^required}}if ({{{name}}}) { {{/required}} + if(!cJSON_IsString({{{name}}})) + { + goto end; //uuid + } + {{/isUuid}} + {{#isEmail}} + {{^required}}if ({{{name}}}) { {{/required}} + if(!cJSON_IsString({{{name}}})) + { + goto end; //email + } + {{/isEmail}} + {{#isFreeFormObject}} + object_t *{{name}}_local_object = NULL; + {{^required}}if ({{{name}}}) { {{/required}} + {{{name}}}_local_object = object_parseFromJSON({{{name}}}); //object + {{/isFreeFormObject}} + {{/isPrimitiveType}} + {{/isContainer}} + {{#isContainer}} + {{#isArray}} + {{#isPrimitiveType}} + {{^required}}if ({{{name}}}) { {{/required}} + cJSON *{{{name}}}_local = NULL; + if(!cJSON_IsArray({{{name}}})) { + goto end;//primitive container + } + {{{name}}}List = list_createList(); + + cJSON_ArrayForEach({{{name}}}_local, {{{name}}}) + { + {{#items}} + {{#isString}} + if(!cJSON_IsString({{{name}}}_local)) + { + goto end; + } + list_addElement({{{name}}}List , strdup({{{name}}}_local->valuestring)); + {{/isString}} + {{#isNumeric}} + if(!cJSON_IsNumber({{{name}}}_local)) + { + goto end; + } + double *{{{name}}}_local_value = calloc(1, sizeof(double)); + if(!{{{name}}}_local_value) + { + goto end; + } + *{{{name}}}_local_value = {{{name}}}_local->valuedouble; + list_addElement({{{name}}}List , {{{name}}}_local_value); + {{/isNumeric}} + {{#isBoolean}} + if(!cJSON_IsBool({{{name}}}_local)) + { + goto end; + } + list_addElement({{{name}}}List , {{{name}}}_local->valueint); + {{/isBoolean}} + {{/items}} + } + {{/isPrimitiveType}} + {{^isPrimitiveType}} + {{^required}}if ({{{name}}}) { {{/required}} + cJSON *{{{name}}}_local_nonprimitive = NULL; + if(!cJSON_IsArray({{{name}}})){ + goto end; //nonprimitive container + } + + {{{name}}}List = list_createList(); + + cJSON_ArrayForEach({{{name}}}_local_nonprimitive,{{{name}}} ) + { + if(!cJSON_IsObject({{{name}}}_local_nonprimitive)){ + goto end; + } + {{#isEnum}}{{#items}}{{classFilename}}_{{datatypeWithEnum}}_e {{/items}}{{/isEnum}}{{^isEnum}}{{complexType}}_t *{{/isEnum}}{{{name}}}Item = {{complexType}}_parseFromJSON({{{name}}}_local_nonprimitive); + + list_addElement({{{name}}}List, {{#isEnum}}{{#items}}(void *){{/items}}{{/isEnum}}{{{name}}}Item); + } + {{/isPrimitiveType}} + {{/isArray}} + {{#isMap}} + {{^required}}if ({{{name}}}) { {{/required}} + {{#isPrimitiveType}} + cJSON *{{{name}}}_local_map = NULL; + if(!cJSON_IsObject({{{name}}}) && !cJSON_IsNull({{{name}}})) + { + goto end;//primitive map container + } + if(cJSON_IsObject({{{name}}})) + { + {{{name}}}List = list_createList(); + keyValuePair_t *localMapKeyPair; + cJSON_ArrayForEach({{{name}}}_local_map, {{{name}}}) + { + cJSON *localMapObject = {{{name}}}_local_map; + {{#items}} + {{#isString}} + if(!cJSON_IsString(localMapObject)) + { + goto end; + } + localMapKeyPair = keyValuePair_create(strdup(localMapObject->string),strdup(localMapObject->valuestring)); + {{/isString}} + {{#isByteArray}} + if(!cJSON_IsString(localMapObject)) + { + goto end; + } + localMapKeyPair = keyValuePair_create(strdup(localMapObject->string),strdup(localMapObject->valuestring)); + {{/isByteArray}} + {{#isBoolean}} + if(!cJSON_IsBool(localMapObject)) + { + goto end; + } + localMapKeyPair = keyValuePair_create(strdup(localMapObject->string), &localMapObject->valueint); + {{/isBoolean}} + {{#isNumeric}} + if(!cJSON_IsNumber(localMapObject)) + { + goto end; + } + localMapKeyPair = keyValuePair_create(strdup(localMapObject->string),&localMapObject->valuedouble ); + {{/isNumeric}} + {{/items}} + list_addElement({{{name}}}List , localMapKeyPair); + } + } + {{/isPrimitiveType}} + {{^isPrimitiveType}} + + // The data type of the elements in {{{classname}}}->{{{name}}} is currently not supported. + + {{/isPrimitiveType}} + {{/isMap}} + {{/isContainer}} + {{^required}} + } + {{/required}} + + {{/vars}} + + {{classname}}_local_var = {{classname}}_create_internal ( + {{#vars}} + {{^isContainer}} + {{^isPrimitiveType}} + {{#isModel}} + {{#isEnum}} + {{^required}}{{{name}}} ? {{/required}}{{{name}}}_local_nonprim_enum{{^required}} : {{projectName}}_{{classVarName}}_{{enumName}}_NULL{{/required}}{{^-last}},{{/-last}} + {{/isEnum}} + {{^isEnum}} + {{^required}}{{{name}}} ? {{/required}}{{{name}}}_local_nonprim{{^required}} : NULL{{/required}}{{^-last}},{{/-last}} + {{/isEnum}} + {{/isModel}} + {{^isModel}} + {{^isFreeFormObject}} + {{^required}}{{{name}}} ? {{/required}}{{{name}}}_local_nonprim{{^required}} : {{^isEnum}}NULL{{/isEnum}}{{#isEnum}}0{{/isEnum}}{{/required}}{{^-last}},{{/-last}} + {{/isFreeFormObject}} + {{/isModel}} + {{#isUuid}} + {{^required}}{{{name}}} ? {{/required}}strdup({{{name}}}->valuestring){{^required}} : NULL{{/required}}{{^-last}},{{/-last}} + {{/isUuid}} + {{#isEmail}} + {{^required}}{{{name}}} ? {{/required}}strdup({{{name}}}->valuestring){{^required}} : NULL{{/required}}{{^-last}},{{/-last}} + {{/isEmail}} + {{#isFreeFormObject}} + {{^required}}{{{name}}} ? {{/required}}{{{name}}}_local_object{{^required}} : NULL{{/required}}{{^-last}},{{/-last}} + {{/isFreeFormObject}} + {{/isPrimitiveType}} + {{#isPrimitiveType}} + {{#isNumeric}} + {{^required}}{{{name}}} ? {{/required}}{{{name}}}->valuedouble{{^required}} : 0{{/required}}{{^-last}},{{/-last}} + {{/isNumeric}} + {{#isBoolean}} + {{^required}}{{{name}}} ? {{/required}}{{{name}}}->valueint{{^required}} : 0{{/required}}{{^-last}},{{/-last}} + {{/isBoolean}} + {{#isEnum}} + {{#isString}} + {{^required}}{{{name}}} ? {{/required}}{{name}}Variable{{^required}} : {{projectName}}_{{classVarName}}_{{enumName}}_NULL{{/required}}{{^-last}},{{/-last}} + {{/isString}} + {{/isEnum}} + {{^isEnum}} + {{#isString}} + {{^required}}{{{name}}} && !cJSON_IsNull({{{name}}}) ? {{/required}}strdup({{{name}}}->valuestring){{^required}} : NULL{{/required}}{{^-last}},{{/-last}} + {{/isString}} + {{/isEnum}} + {{#isByteArray}} + {{^required}}{{{name}}} ? {{/required}}strdup({{{name}}}->valuestring){{^required}} : NULL{{/required}}{{^-last}},{{/-last}} + {{/isByteArray}} + {{#isBinary}} + {{^required}}{{{name}}} ? {{/required}}decoded_str_{{{name}}}{{^required}} : NULL{{/required}}{{^-last}},{{/-last}} + {{/isBinary}} + {{#isDate}} + {{^required}}{{{name}}} ? {{/required}}strdup({{{name}}}->valuestring){{^required}} : NULL{{/required}}{{^-last}},{{/-last}} + {{/isDate}} + {{#isDateTime}} + {{^required}}{{{name}}} && !cJSON_IsNull({{{name}}}) ? {{/required}}strdup({{{name}}}->valuestring){{^required}} : NULL{{/required}}{{^-last}},{{/-last}} + {{/isDateTime}} + {{/isPrimitiveType}} + {{/isContainer}} + {{#isContainer}} + {{#isArray}} + {{#isPrimitiveType}} + {{^required}}{{{name}}} ? {{/required}}{{{name}}}List{{^required}} : NULL{{/required}}{{^-last}},{{/-last}} + {{/isPrimitiveType}} + {{^isPrimitiveType}} + {{^required}}{{{name}}} ? {{/required}}{{{name}}}List{{^required}} : NULL{{/required}}{{^-last}},{{/-last}} + {{/isPrimitiveType}} + {{/isArray}} + {{#isMap}} + {{^required}}{{{name}}} ? {{/required}}{{{name}}}List{{^required}} : NULL{{/required}}{{^-last}},{{/-last}} + {{/isMap}} + {{/isContainer}} + {{/vars}} + ); + + return {{classname}}_local_var; +end: + {{#vars}} + {{^isContainer}} + {{^isPrimitiveType}} + {{#isModel}} + {{^isEnum}} + if ({{{name}}}_local_nonprim) { + {{complexType}}_free({{{name}}}_local_nonprim); + {{{name}}}_local_nonprim = NULL; + } + {{/isEnum}} + {{/isModel}} + {{^isModel}} + {{^isFreeFormObject}} + if ({{{name}}}_local_nonprim) { + {{^isEnum}} + {{complexType}}_free({{{name}}}_local_nonprim); + {{/isEnum}} + {{{name}}}_local_nonprim = {{^isEnum}}NULL{{/isEnum}}{{#isEnum}}0{{/isEnum}}; + } + {{/isFreeFormObject}} + {{/isModel}} + {{/isPrimitiveType}} + {{/isContainer}} + {{#isContainer}} + {{#isArray}} + {{#isPrimitiveType}} + if ({{{name}}}List) { + {{#items}} + {{#isString}} + listEntry_t *listEntry = NULL; + list_ForEach(listEntry, {{{name}}}List) { + free(listEntry->data); + listEntry->data = NULL; + } + {{/isString}} + {{#isNumeric}} + listEntry_t *listEntry = NULL; + list_ForEach(listEntry, {{{name}}}List) { + free(listEntry->data); + listEntry->data = NULL; + } + {{/isNumeric}} + {{/items}} + list_freeList({{{name}}}List); + {{{name}}}List = NULL; + } + {{/isPrimitiveType}} + {{^isPrimitiveType}} + if ({{{name}}}List) { + listEntry_t *listEntry = NULL; + list_ForEach(listEntry, {{{name}}}List) { + {{complexType}}_free(listEntry->data); + listEntry->data = NULL; + } + list_freeList({{{name}}}List); + {{{name}}}List = NULL; + } + {{/isPrimitiveType}} + {{/isArray}} + {{#isMap}} + {{#isPrimitiveType}} + if ({{{name}}}List) { + listEntry_t *listEntry = NULL; + list_ForEach(listEntry, {{{name}}}List) { + keyValuePair_t *localKeyValue = listEntry->data; + free(localKeyValue->key); + localKeyValue->key = NULL; + {{#items}} + {{#isString}} + free(localKeyValue->value); + localKeyValue->value = NULL; + {{/isString}} + {{#isByteArray}} + free(localKeyValue->value); + localKeyValue->value = NULL; + {{/isByteArray}} + {{/items}} + keyValuePair_free(localKeyValue); + localKeyValue = NULL; + } + list_freeList({{{name}}}List); + {{{name}}}List = NULL; + } + {{/isPrimitiveType}} + {{^isPrimitiveType}} + + // The data type of the elements in {{{classname}}}->{{{name}}} is currently not supported. + + {{/isPrimitiveType}} + {{/isMap}} + {{/isContainer}} + {{/vars}} + return NULL; + +} +{{/isEnum}} +{{/model}} +{{/models}} diff --git a/modules/openapi-generator/src/main/resources/C-libcurl/model-header-v7_20_0.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/model-header-v7_20_0.mustache new file mode 100644 index 000000000000..236d9e8c3cba --- /dev/null +++ b/modules/openapi-generator/src/main/resources/C-libcurl/model-header-v7_20_0.mustache @@ -0,0 +1,247 @@ +{{#models}}{{#model}}/* + * {{classFilename}}.h + * + * {{description}} + */ + +#ifndef _{{classname}}_H_ +#define _{{classname}}_H_ + +#include +#include "../external/cJSON.h" +#include "../include/list.h" +#include "../include/keyValuePair.h" +#include "../include/binary.h" + +typedef struct {{classname}}_t {{classname}}_t; + +{{#imports}} +#include "{{{.}}}.h" +{{/imports}} + +{{#isEnum}} +{{#allowableValues}} +// Enum {{enumName}} for {{classVarName}} + +typedef enum { {{projectName}}_{{classVarName}}_{{enumName}}_NULL = 0{{#enumVars}}, {{projectName}}_{{classVarName}}_{{enumName}}_{{{value}}}{{/enumVars}} } {{projectName}}_{{classVarName}}_{{enumName}}_e; +{{/allowableValues}} + +char* {{classFilename}}_{{classname}}_ToString({{projectName}}_{{classVarName}}_{{enumName}}_e {{classname}}); + +{{projectName}}_{{classVarName}}_{{enumName}}_e {{classFilename}}_{{classname}}_FromString(char* {{classname}}); + +cJSON *{{classname}}_convertToJSON({{projectName}}_{{classVarName}}_{{enumName}}_e {{classname}}); + +{{projectName}}_{{classVarName}}_{{enumName}}_e {{classname}}_parseFromJSON(cJSON *{{classname}}JSON); + +{{/isEnum}} +{{^isEnum}} +{{#vars}} +{{^isContainer}} + {{#isPrimitiveType}} + {{^isModel}} + {{#isEnum}} +// Enum {{enumName}} for {{classVarName}} + + {{#allowableValues}} +typedef enum { {{projectName}}_{{classVarName}}_{{enumName}}_NULL = 0{{#enumVars}}, {{projectName}}_{{classVarName}}_{{enumName}}_{{{value}}}{{/enumVars}} } {{projectName}}_{{classVarName}}_{{enumName}}_e; + {{/allowableValues}} + +char* {{classFilename}}_{{name}}_ToString({{projectName}}_{{classVarName}}_{{enumName}}_e {{name}}); + +{{projectName}}_{{classVarName}}_{{enumName}}_e {{classFilename}}_{{name}}_FromString(char* {{name}}); + + {{/isEnum}} + {{/isModel}} + {{/isPrimitiveType}} + {{/isContainer}} + {{#isContainer}} + {{#items}} + {{^isModel}} + {{#isEnum}} +// Enum {{enumName}} for {{classVarName}} + + {{#allowableValues}} +typedef enum { {{projectName}}_{{classVarName}}_{{enumName}}_NULL = 0{{#enumVars}}, {{projectName}}_{{classVarName}}_{{enumName}}_{{{value}}}{{/enumVars}} } {{projectName}}_{{classVarName}}_{{enumName}}_e; + {{/allowableValues}} + +char* {{classFilename}}_{{name}}_ToString({{projectName}}_{{classVarName}}_{{enumName}}_e {{name}}); + +{{projectName}}_{{classVarName}}_{{enumName}}_e {{classFilename}}_{{name}}_FromString(char* {{name}}); + + {{/isEnum}} + {{/isModel}} + {{/items}} + {{/isContainer}} +{{/vars}} + + +typedef struct {{classname}}_t { + {{#vars}} + {{^isContainer}} + {{^isPrimitiveType}} + {{#isModel}} + {{#isEnum}} + {{projectName}}_{{classVarName}}_{{enumName}}_e {{name}}; //enum model + {{/isEnum}} + {{^isEnum}} + struct {{datatype}}_t *{{name}}; //model + {{/isEnum}} + {{/isModel}} + {{^isModel}} + {{^isFreeFormObject}} + {{^isEnum}} + {{datatype}}_t *{{name}}; // custom + {{/isEnum}} + {{#isEnum}} + {{projectName}}_{{dataType}}_{{enumName}}_e {{name}}; //referenced enum + {{/isEnum}} + {{/isFreeFormObject}} + {{/isModel}} + {{#isUuid}} + {{datatype}} *{{name}}; // uuid + {{/isUuid}} + {{#isEmail}} + {{datatype}} *{{name}}; // email + {{/isEmail}} + {{#isFreeFormObject}} + {{datatype}}_t *{{name}}; //object + {{/isFreeFormObject}} + {{/isPrimitiveType}} + {{#isPrimitiveType}} + {{#isNumeric}} + {{datatype}} {{name}}; //numeric + {{/isNumeric}} + {{#isBoolean}} + {{datatype}} {{name}}; //boolean + {{/isBoolean}} + {{#isEnum}} + {{#isString}} + {{projectName}}_{{classVarName}}_{{enumName}}_e {{name}}; //enum + {{/isString}} + {{/isEnum}} + {{^isEnum}} + {{#isString}} + {{datatype}} *{{name}}; // string + {{/isString}} + {{/isEnum}} + {{#isByteArray}} + {{datatype}} *{{name}}; //ByteArray + {{/isByteArray}} + {{#isBinary}} + {{datatype}} {{name}}; //binary + {{/isBinary}} + {{#isDate}} + {{datatype}} *{{name}}; //date + {{/isDate}} + {{#isDateTime}} + {{datatype}} *{{name}}; //date time + {{/isDateTime}} + {{/isPrimitiveType}} + {{/isContainer}} + {{#isContainer}} + {{#isArray}} + {{#isPrimitiveType}} + {{datatype}}_t *{{name}}; //primitive container + {{/isPrimitiveType}} + {{^isPrimitiveType}} + {{datatype}}_t *{{name}}; //nonprimitive container + {{/isPrimitiveType}} + {{/isArray}} + {{#isMap}} + {{datatype}} {{name}}; //map + {{/isMap}} + {{/isContainer}} + {{/vars}} + + int _library_owned; // Is the library responsible for freeing this object? +} {{classname}}_t; + +__attribute__((deprecated)) {{classname}}_t *{{classname}}_create( + {{#vars}} + {{^isContainer}} + {{^isPrimitiveType}} + {{#isModel}} + {{#isEnum}} + {{projectName}}_{{classVarName}}_{{enumName}}_e {{name}}{{^-last}},{{/-last}} + {{/isEnum}} + {{^isEnum}} + {{datatype}}_t *{{name}}{{^-last}},{{/-last}} + {{/isEnum}} + {{/isModel}} + {{^isModel}} + {{^isFreeFormObject}} + {{^isEnum}} + {{datatype}}_t *{{name}}{{^-last}},{{/-last}} + {{/isEnum}} + {{#isEnum}} + {{projectName}}_{{dataType}}_{{enumName}}_e {{name}}{{^-last}},{{/-last}} + {{/isEnum}} + {{/isFreeFormObject}} + {{/isModel}} + {{#isUuid}} + {{datatype}} *{{name}}{{^-last}},{{/-last}} + {{/isUuid}} + {{#isEmail}} + {{datatype}} *{{name}}{{^-last}},{{/-last}} + {{/isEmail}} + {{#isFreeFormObject}} + {{datatype}}_t *{{name}}{{^-last}},{{/-last}} + {{/isFreeFormObject}} + {{/isPrimitiveType}} + {{#isPrimitiveType}} + {{#isNumeric}} + {{datatype}} {{name}}{{^-last}},{{/-last}} + {{/isNumeric}} + {{#isBoolean}} + {{datatype}} {{name}}{{^-last}},{{/-last}} + {{/isBoolean}} + {{#isEnum}} + {{#isString}} + {{projectName}}_{{classVarName}}_{{enumName}}_e {{name}}{{^-last}},{{/-last}} + {{/isString}} + {{/isEnum}} + {{^isEnum}} + {{#isString}} + {{datatype}} *{{name}}{{^-last}},{{/-last}} + {{/isString}} + {{/isEnum}} + {{#isByteArray}} + {{datatype}} *{{name}}{{^-last}},{{/-last}} + {{/isByteArray}} + {{#isBinary}} + {{datatype}} {{name}}{{^-last}},{{/-last}} + {{/isBinary}} + {{#isDate}} + {{datatype}} *{{name}}{{^-last}},{{/-last}} + {{/isDate}} + {{#isDateTime}} + {{datatype}} *{{name}}{{^-last}},{{/-last}} + {{/isDateTime}} + {{/isPrimitiveType}} + {{/isContainer}} + {{#isContainer}} + {{#isArray}} + {{#isPrimitiveType}} + {{datatype}}_t *{{name}}{{^-last}},{{/-last}} + {{/isPrimitiveType}} + {{^isPrimitiveType}} + {{datatype}}_t *{{name}}{{^-last}},{{/-last}} + {{/isPrimitiveType}} + {{/isArray}} + {{#isMap}} + {{datatype}} {{name}}{{^-last}},{{/-last}} + {{/isMap}} + {{/isContainer}} + {{/vars}} +); + +void {{classname}}_free({{classname}}_t *{{classname}}); + +{{classname}}_t *{{classname}}_parseFromJSON(cJSON *{{classname}}JSON); + +cJSON *{{classname}}_convertToJSON({{classname}}_t *{{classname}}); + +{{/isEnum}} +#endif /* _{{classname}}_H_ */ +{{/model}}{{/models}} diff --git a/samples/client/petstore/c-useJsonUnformatted/model/api_response.c b/samples/client/petstore/c-useJsonUnformatted/model/api_response.c index e77d4101158e..d0169c84f5b2 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/api_response.c +++ b/samples/client/petstore/c-useJsonUnformatted/model/api_response.c @@ -6,7 +6,7 @@ static api_response_t *api_response_create_internal( - int *code, + int code, char *type, char *message ) { @@ -14,33 +14,24 @@ static api_response_t *api_response_create_internal( if (!api_response_local_var) { return NULL; } - memset(api_response_local_var, 0, sizeof(api_response_t)); - api_response_local_var->_library_owned = 1; api_response_local_var->code = code; api_response_local_var->type = type; api_response_local_var->message = message; + + api_response_local_var->_library_owned = 1; return api_response_local_var; } __attribute__((deprecated)) api_response_t *api_response_create( - int *code, + int code, char *type, char *message ) { - int *code_copy = NULL; - if (code) { - code_copy = malloc(sizeof(int)); - if (code_copy) *code_copy = *code; - } - api_response_t *result = api_response_create_internal ( - code_copy, + return api_response_create_internal ( + code, type, message ); - if (!result) { - free(code_copy); - } - return result; } void api_response_free(api_response_t *api_response) { @@ -52,10 +43,6 @@ 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; @@ -72,7 +59,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 } } @@ -105,13 +92,6 @@ 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; - - char *type_local_str = NULL; - - char *message_local_str = NULL; - // api_response->code cJSON *code = cJSON_GetObjectItemCaseSensitive(api_responseJSON, "code"); if (cJSON_IsNull(code)) { @@ -122,12 +102,6 @@ api_response_t *api_response_parseFromJSON(cJSON *api_responseJSON){ { goto end; //Numeric } - code_local_var = malloc(sizeof(int)); - if(!code_local_var) - { - goto end; - } - *code_local_var = code->valuedouble; } // api_response->type @@ -155,33 +129,14 @@ api_response_t *api_response_parseFromJSON(cJSON *api_responseJSON){ } - if (type && !cJSON_IsNull(type)) type_local_str = strdup(type->valuestring); - if (message && !cJSON_IsNull(message)) message_local_str = strdup(message->valuestring); - api_response_local_var = api_response_create_internal ( - code_local_var, - type_local_str, - message_local_str + code ? code->valuedouble : 0, + type && !cJSON_IsNull(type) ? strdup(type->valuestring) : NULL, + message && !cJSON_IsNull(message) ? strdup(message->valuestring) : NULL ); - if (!api_response_local_var) { - goto end; - } - return api_response_local_var; end: - if (code_local_var) { - free(code_local_var); - code_local_var = NULL; - } - if (type_local_str) { - free(type_local_str); - type_local_str = NULL; - } - if (message_local_str) { - free(message_local_str); - message_local_str = NULL; - } return NULL; } diff --git a/samples/client/petstore/c-useJsonUnformatted/model/api_response.h b/samples/client/petstore/c-useJsonUnformatted/model/api_response.h index 10d364f3c39b..3d9eb71ff5d5 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/api_response.h +++ b/samples/client/petstore/c-useJsonUnformatted/model/api_response.h @@ -19,7 +19,7 @@ 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 @@ -27,7 +27,7 @@ typedef struct api_response_t { } api_response_t; __attribute__((deprecated)) api_response_t *api_response_create( - int *code, + int code, char *type, char *message ); diff --git a/samples/client/petstore/c-useJsonUnformatted/model/category.c b/samples/client/petstore/c-useJsonUnformatted/model/category.c index de6198a3a4bf..2b060a568015 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/category.c +++ b/samples/client/petstore/c-useJsonUnformatted/model/category.c @@ -6,37 +6,28 @@ static category_t *category_create_internal( - long *id, + long id, char *name ) { category_t *category_local_var = malloc(sizeof(category_t)); if (!category_local_var) { return NULL; } - memset(category_local_var, 0, sizeof(category_t)); - category_local_var->_library_owned = 1; category_local_var->id = id; category_local_var->name = name; + + category_local_var->_library_owned = 1; return category_local_var; } __attribute__((deprecated)) category_t *category_create( - long *id, + long id, char *name ) { - long *id_copy = NULL; - if (id) { - id_copy = malloc(sizeof(long)); - if (id_copy) *id_copy = *id; - } - category_t *result = category_create_internal ( - id_copy, + return category_create_internal ( + id, name ); - if (!result) { - free(id_copy); - } - return result; } void category_free(category_t *category) { @@ -48,10 +39,6 @@ void category_free(category_t *category) { return ; } listEntry_t *listEntry; - if (category->id) { - free(category->id); - category->id = NULL; - } if (category->name) { free(category->name); category->name = NULL; @@ -64,7 +51,7 @@ cJSON *category_convertToJSON(category_t *category) { // category->id if(category->id) { - if(cJSON_AddNumberToObject(item, "id", *category->id) == NULL) { + if(cJSON_AddNumberToObject(item, "id", category->id) == NULL) { goto fail; //Numeric } } @@ -89,11 +76,6 @@ category_t *category_parseFromJSON(cJSON *categoryJSON){ category_t *category_local_var = NULL; - // define the local variable for category->id - long *id_local_var = NULL; - - char *name_local_str = NULL; - // category->id cJSON *id = cJSON_GetObjectItemCaseSensitive(categoryJSON, "id"); if (cJSON_IsNull(id)) { @@ -104,12 +86,6 @@ category_t *category_parseFromJSON(cJSON *categoryJSON){ { goto end; //Numeric } - id_local_var = malloc(sizeof(long)); - if(!id_local_var) - { - goto end; - } - *id_local_var = id->valuedouble; } // category->name @@ -125,27 +101,13 @@ category_t *category_parseFromJSON(cJSON *categoryJSON){ } - if (name && !cJSON_IsNull(name)) name_local_str = strdup(name->valuestring); - category_local_var = category_create_internal ( - id_local_var, - name_local_str + id ? id->valuedouble : 0, + name && !cJSON_IsNull(name) ? strdup(name->valuestring) : NULL ); - if (!category_local_var) { - goto end; - } - return category_local_var; end: - if (id_local_var) { - free(id_local_var); - id_local_var = NULL; - } - if (name_local_str) { - free(name_local_str); - name_local_str = NULL; - } return NULL; } diff --git a/samples/client/petstore/c-useJsonUnformatted/model/category.h b/samples/client/petstore/c-useJsonUnformatted/model/category.h index dcc265955832..bd27e27e35a3 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/category.h +++ b/samples/client/petstore/c-useJsonUnformatted/model/category.h @@ -19,14 +19,14 @@ typedef struct category_t category_t; typedef struct category_t { - long *id; //numeric + long id; //numeric char *name; // string int _library_owned; // Is the library responsible for freeing this object? } category_t; __attribute__((deprecated)) category_t *category_create( - long *id, + long id, char *name ); diff --git a/samples/client/petstore/c-useJsonUnformatted/model/mapped_model.c b/samples/client/petstore/c-useJsonUnformatted/model/mapped_model.c index b4bb82e1889d..3ab1e861c326 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/mapped_model.c +++ b/samples/client/petstore/c-useJsonUnformatted/model/mapped_model.c @@ -6,37 +6,28 @@ static MappedModel_t *MappedModel_create_internal( - int *another_property, + int another_property, char *uuid_property ) { MappedModel_t *MappedModel_local_var = malloc(sizeof(MappedModel_t)); if (!MappedModel_local_var) { return NULL; } - memset(MappedModel_local_var, 0, sizeof(MappedModel_t)); - MappedModel_local_var->_library_owned = 1; MappedModel_local_var->another_property = another_property; MappedModel_local_var->uuid_property = uuid_property; + + MappedModel_local_var->_library_owned = 1; return MappedModel_local_var; } __attribute__((deprecated)) MappedModel_t *MappedModel_create( - int *another_property, + int another_property, char *uuid_property ) { - int *another_property_copy = NULL; - if (another_property) { - another_property_copy = malloc(sizeof(int)); - if (another_property_copy) *another_property_copy = *another_property; - } - MappedModel_t *result = MappedModel_create_internal ( - another_property_copy, + return MappedModel_create_internal ( + another_property, uuid_property ); - if (!result) { - free(another_property_copy); - } - return result; } void MappedModel_free(MappedModel_t *MappedModel) { @@ -48,10 +39,6 @@ void MappedModel_free(MappedModel_t *MappedModel) { return ; } listEntry_t *listEntry; - if (MappedModel->another_property) { - free(MappedModel->another_property); - MappedModel->another_property = NULL; - } if (MappedModel->uuid_property) { free(MappedModel->uuid_property); MappedModel->uuid_property = NULL; @@ -64,7 +51,7 @@ cJSON *MappedModel_convertToJSON(MappedModel_t *MappedModel) { // MappedModel->another_property if(MappedModel->another_property) { - if(cJSON_AddNumberToObject(item, "another_property", *MappedModel->another_property) == NULL) { + if(cJSON_AddNumberToObject(item, "another_property", MappedModel->another_property) == NULL) { goto fail; //Numeric } } @@ -89,11 +76,6 @@ MappedModel_t *MappedModel_parseFromJSON(cJSON *MappedModelJSON){ MappedModel_t *MappedModel_local_var = NULL; - // define the local variable for MappedModel->another_property - int *another_property_local_var = NULL; - - char *uuid_property_local_str = NULL; - // MappedModel->another_property cJSON *another_property = cJSON_GetObjectItemCaseSensitive(MappedModelJSON, "another_property"); if (cJSON_IsNull(another_property)) { @@ -104,12 +86,6 @@ MappedModel_t *MappedModel_parseFromJSON(cJSON *MappedModelJSON){ { goto end; //Numeric } - another_property_local_var = malloc(sizeof(int)); - if(!another_property_local_var) - { - goto end; - } - *another_property_local_var = another_property->valuedouble; } // MappedModel->uuid_property @@ -125,27 +101,13 @@ MappedModel_t *MappedModel_parseFromJSON(cJSON *MappedModelJSON){ } - if (uuid_property && !cJSON_IsNull(uuid_property)) uuid_property_local_str = strdup(uuid_property->valuestring); - MappedModel_local_var = MappedModel_create_internal ( - another_property_local_var, - uuid_property_local_str + another_property ? another_property->valuedouble : 0, + uuid_property && !cJSON_IsNull(uuid_property) ? strdup(uuid_property->valuestring) : NULL ); - if (!MappedModel_local_var) { - goto end; - } - return MappedModel_local_var; end: - if (another_property_local_var) { - free(another_property_local_var); - another_property_local_var = NULL; - } - if (uuid_property_local_str) { - free(uuid_property_local_str); - uuid_property_local_str = NULL; - } return NULL; } diff --git a/samples/client/petstore/c-useJsonUnformatted/model/mapped_model.h b/samples/client/petstore/c-useJsonUnformatted/model/mapped_model.h index b2a2baab9741..b962632d647d 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/mapped_model.h +++ b/samples/client/petstore/c-useJsonUnformatted/model/mapped_model.h @@ -19,14 +19,14 @@ typedef struct MappedModel_t MappedModel_t; typedef struct MappedModel_t { - int *another_property; //numeric + int another_property; //numeric char *uuid_property; // string int _library_owned; // Is the library responsible for freeing this object? } MappedModel_t; __attribute__((deprecated)) MappedModel_t *MappedModel_create( - int *another_property, + int another_property, char *uuid_property ); diff --git a/samples/client/petstore/c-useJsonUnformatted/model/model_with_set_propertes.c b/samples/client/petstore/c-useJsonUnformatted/model/model_with_set_propertes.c index 5a9d85ab0fb6..934cdd587c85 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/model_with_set_propertes.c +++ b/samples/client/petstore/c-useJsonUnformatted/model/model_with_set_propertes.c @@ -13,10 +13,10 @@ static model_with_set_propertes_t *model_with_set_propertes_create_internal( if (!model_with_set_propertes_local_var) { return NULL; } - memset(model_with_set_propertes_local_var, 0, sizeof(model_with_set_propertes_t)); - model_with_set_propertes_local_var->_library_owned = 1; model_with_set_propertes_local_var->tag_set = tag_set; model_with_set_propertes_local_var->string_set = string_set; + + model_with_set_propertes_local_var->_library_owned = 1; return model_with_set_propertes_local_var; } @@ -24,13 +24,10 @@ __attribute__((deprecated)) model_with_set_propertes_t *model_with_set_propertes list_t *tag_set, list_t *string_set ) { - model_with_set_propertes_t *result = model_with_set_propertes_create_internal ( + return model_with_set_propertes_create_internal ( tag_set, string_set ); - if (!result) { - } - return result; } void model_with_set_propertes_free(model_with_set_propertes_t *model_with_set_propertes) { @@ -163,16 +160,11 @@ model_with_set_propertes_t *model_with_set_propertes_parseFromJSON(cJSON *model_ } - model_with_set_propertes_local_var = model_with_set_propertes_create_internal ( tag_set ? tag_setList : NULL, string_set ? string_setList : NULL ); - if (!model_with_set_propertes_local_var) { - goto end; - } - return model_with_set_propertes_local_var; end: if (tag_setList) { diff --git a/samples/client/petstore/c-useJsonUnformatted/model/order.c b/samples/client/petstore/c-useJsonUnformatted/model/order.c index dab67dd0dbec..d67d1d47c714 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/order.c +++ b/samples/client/petstore/c-useJsonUnformatted/model/order.c @@ -23,71 +23,44 @@ openapi_petstore_order_STATUS_e order_status_FromString(char* status){ } static order_t *order_create_internal( - long *id, - long *pet_id, - int *quantity, + long id, + long pet_id, + int quantity, char *ship_date, openapi_petstore_order_STATUS_e status, - int *complete + int complete ) { order_t *order_local_var = malloc(sizeof(order_t)); if (!order_local_var) { return NULL; } - memset(order_local_var, 0, sizeof(order_t)); - order_local_var->_library_owned = 1; order_local_var->id = id; order_local_var->pet_id = pet_id; order_local_var->quantity = quantity; order_local_var->ship_date = ship_date; order_local_var->status = status; order_local_var->complete = complete; + + order_local_var->_library_owned = 1; return order_local_var; } __attribute__((deprecated)) order_t *order_create( - long *id, - long *pet_id, - int *quantity, + long id, + long pet_id, + int quantity, char *ship_date, openapi_petstore_order_STATUS_e status, - int *complete + int complete ) { - long *id_copy = NULL; - if (id) { - id_copy = malloc(sizeof(long)); - if (id_copy) *id_copy = *id; - } - long *pet_id_copy = NULL; - if (pet_id) { - pet_id_copy = malloc(sizeof(long)); - if (pet_id_copy) *pet_id_copy = *pet_id; - } - int *quantity_copy = NULL; - if (quantity) { - quantity_copy = malloc(sizeof(int)); - if (quantity_copy) *quantity_copy = *quantity; - } - int *complete_copy = NULL; - if (complete) { - complete_copy = malloc(sizeof(int)); - if (complete_copy) *complete_copy = *complete; - } - order_t *result = order_create_internal ( - id_copy, - pet_id_copy, - quantity_copy, + return order_create_internal ( + id, + pet_id, + quantity, ship_date, status, - complete_copy + complete ); - if (!result) { - free(id_copy); - free(pet_id_copy); - free(quantity_copy); - free(complete_copy); - } - return result; } void order_free(order_t *order) { @@ -99,26 +72,10 @@ void order_free(order_t *order) { return ; } listEntry_t *listEntry; - if (order->id) { - free(order->id); - order->id = NULL; - } - if (order->pet_id) { - free(order->pet_id); - order->pet_id = NULL; - } - if (order->quantity) { - free(order->quantity); - order->quantity = NULL; - } if (order->ship_date) { free(order->ship_date); order->ship_date = NULL; } - if (order->complete) { - free(order->complete); - order->complete = NULL; - } free(order); } @@ -127,7 +84,7 @@ cJSON *order_convertToJSON(order_t *order) { // order->id if(order->id) { - if(cJSON_AddNumberToObject(item, "id", *order->id) == NULL) { + if(cJSON_AddNumberToObject(item, "id", order->id) == NULL) { goto fail; //Numeric } } @@ -135,7 +92,7 @@ cJSON *order_convertToJSON(order_t *order) { // 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 } } @@ -143,7 +100,7 @@ cJSON *order_convertToJSON(order_t *order) { // order->quantity if(order->quantity) { - if(cJSON_AddNumberToObject(item, "quantity", *order->quantity) == NULL) { + if(cJSON_AddNumberToObject(item, "quantity", order->quantity) == NULL) { goto fail; //Numeric } } @@ -168,7 +125,7 @@ cJSON *order_convertToJSON(order_t *order) { // order->complete if(order->complete) { - if(cJSON_AddBoolToObject(item, "complete", *order->complete) == NULL) { + if(cJSON_AddBoolToObject(item, "complete", order->complete) == NULL) { goto fail; //Bool } } @@ -185,20 +142,6 @@ order_t *order_parseFromJSON(cJSON *orderJSON){ order_t *order_local_var = NULL; - // define the local variable for order->id - long *id_local_var = NULL; - - // define the local variable for order->pet_id - long *pet_id_local_var = NULL; - - // define the local variable for order->quantity - int *quantity_local_var = NULL; - - char *ship_date_local_str = NULL; - - // define the local variable for order->complete - int *complete_local_var = NULL; - // order->id cJSON *id = cJSON_GetObjectItemCaseSensitive(orderJSON, "id"); if (cJSON_IsNull(id)) { @@ -209,12 +152,6 @@ order_t *order_parseFromJSON(cJSON *orderJSON){ { goto end; //Numeric } - id_local_var = malloc(sizeof(long)); - if(!id_local_var) - { - goto end; - } - *id_local_var = id->valuedouble; } // order->pet_id @@ -227,12 +164,6 @@ order_t *order_parseFromJSON(cJSON *orderJSON){ { goto end; //Numeric } - pet_id_local_var = malloc(sizeof(long)); - if(!pet_id_local_var) - { - goto end; - } - *pet_id_local_var = pet_id->valuedouble; } // order->quantity @@ -245,12 +176,6 @@ order_t *order_parseFromJSON(cJSON *orderJSON){ { goto end; //Numeric } - quantity_local_var = malloc(sizeof(int)); - if(!quantity_local_var) - { - goto end; - } - *quantity_local_var = quantity->valuedouble; } // order->ship_date @@ -289,52 +214,20 @@ order_t *order_parseFromJSON(cJSON *orderJSON){ { goto end; //Bool } - complete_local_var = malloc(sizeof(int)); - if(!complete_local_var) - { - goto end; - } - *complete_local_var = complete->valueint; } - if (ship_date && !cJSON_IsNull(ship_date)) ship_date_local_str = strdup(ship_date->valuestring); - order_local_var = order_create_internal ( - id_local_var, - pet_id_local_var, - quantity_local_var, - ship_date_local_str, + id ? id->valuedouble : 0, + pet_id ? pet_id->valuedouble : 0, + quantity ? quantity->valuedouble : 0, + ship_date && !cJSON_IsNull(ship_date) ? strdup(ship_date->valuestring) : NULL, status ? statusVariable : openapi_petstore_order_STATUS_NULL, - complete_local_var + complete ? complete->valueint : 0 ); - if (!order_local_var) { - goto end; - } - return order_local_var; end: - if (id_local_var) { - free(id_local_var); - id_local_var = NULL; - } - if (pet_id_local_var) { - free(pet_id_local_var); - pet_id_local_var = NULL; - } - if (quantity_local_var) { - free(quantity_local_var); - quantity_local_var = NULL; - } - if (ship_date_local_str) { - free(ship_date_local_str); - ship_date_local_str = NULL; - } - if (complete_local_var) { - free(complete_local_var); - complete_local_var = NULL; - } return NULL; } diff --git a/samples/client/petstore/c-useJsonUnformatted/model/order.h b/samples/client/petstore/c-useJsonUnformatted/model/order.h index 59ede1731710..1b0a47b3028e 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/order.h +++ b/samples/client/petstore/c-useJsonUnformatted/model/order.h @@ -27,23 +27,23 @@ openapi_petstore_order_STATUS_e order_status_FromString(char* status); typedef struct order_t { - long *id; //numeric - long *pet_id; //numeric - int *quantity; //numeric + long id; //numeric + long pet_id; //numeric + int quantity; //numeric char *ship_date; //date time openapi_petstore_order_STATUS_e status; //enum - int *complete; //boolean + int complete; //boolean int _library_owned; // Is the library responsible for freeing this object? } order_t; __attribute__((deprecated)) order_t *order_create( - long *id, - long *pet_id, - int *quantity, + long id, + long pet_id, + int quantity, char *ship_date, openapi_petstore_order_STATUS_e status, - int *complete + int complete ); void order_free(order_t *order); diff --git a/samples/client/petstore/c-useJsonUnformatted/model/pet.c b/samples/client/petstore/c-useJsonUnformatted/model/pet.c index 861a0167662e..b561634cb529 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/pet.c +++ b/samples/client/petstore/c-useJsonUnformatted/model/pet.c @@ -23,7 +23,7 @@ openapi_petstore_pet_STATUS_e pet_status_FromString(char* status){ } static pet_t *pet_create_internal( - long *id, + long id, category_t *category, char *name, list_t *photo_urls, @@ -34,42 +34,33 @@ static pet_t *pet_create_internal( if (!pet_local_var) { return NULL; } - memset(pet_local_var, 0, sizeof(pet_t)); - pet_local_var->_library_owned = 1; pet_local_var->id = id; pet_local_var->category = category; pet_local_var->name = name; pet_local_var->photo_urls = photo_urls; pet_local_var->tags = tags; pet_local_var->status = status; + + pet_local_var->_library_owned = 1; return pet_local_var; } __attribute__((deprecated)) pet_t *pet_create( - long *id, + long id, category_t *category, char *name, list_t *photo_urls, list_t *tags, openapi_petstore_pet_STATUS_e status ) { - long *id_copy = NULL; - if (id) { - id_copy = malloc(sizeof(long)); - if (id_copy) *id_copy = *id; - } - pet_t *result = pet_create_internal ( - id_copy, + return pet_create_internal ( + id, category, name, photo_urls, tags, status ); - if (!result) { - free(id_copy); - } - return result; } void pet_free(pet_t *pet) { @@ -81,10 +72,6 @@ void pet_free(pet_t *pet) { return ; } listEntry_t *listEntry; - if (pet->id) { - free(pet->id); - pet->id = NULL; - } if (pet->category) { category_free(pet->category); pet->category = NULL; @@ -115,7 +102,7 @@ cJSON *pet_convertToJSON(pet_t *pet) { // pet->id if(pet->id) { - if(cJSON_AddNumberToObject(item, "id", *pet->id) == NULL) { + if(cJSON_AddNumberToObject(item, "id", pet->id) == NULL) { goto fail; //Numeric } } @@ -201,14 +188,9 @@ pet_t *pet_parseFromJSON(cJSON *petJSON){ pet_t *pet_local_var = NULL; - // define the local variable for pet->id - long *id_local_var = NULL; - // define the local variable for pet->category category_t *category_local_nonprim = NULL; - char *name_local_str = NULL; - // define the local list for pet->photo_urls list_t *photo_urlsList = NULL; @@ -225,12 +207,6 @@ pet_t *pet_parseFromJSON(cJSON *petJSON){ { goto end; //Numeric } - id_local_var = malloc(sizeof(long)); - if(!id_local_var) - { - goto end; - } - *id_local_var = id->valuedouble; } // pet->category @@ -321,35 +297,21 @@ pet_t *pet_parseFromJSON(cJSON *petJSON){ } - if (name && !cJSON_IsNull(name)) name_local_str = strdup(name->valuestring); - pet_local_var = pet_create_internal ( - id_local_var, + id ? id->valuedouble : 0, category ? category_local_nonprim : NULL, - name_local_str, + strdup(name->valuestring), photo_urlsList, tags ? tagsList : NULL, status ? statusVariable : openapi_petstore_pet_STATUS_NULL ); - if (!pet_local_var) { - goto end; - } - return pet_local_var; end: - if (id_local_var) { - free(id_local_var); - id_local_var = NULL; - } if (category_local_nonprim) { category_free(category_local_nonprim); category_local_nonprim = NULL; } - if (name_local_str) { - free(name_local_str); - name_local_str = NULL; - } if (photo_urlsList) { listEntry_t *listEntry = NULL; list_ForEach(listEntry, photo_urlsList) { diff --git a/samples/client/petstore/c-useJsonUnformatted/model/pet.h b/samples/client/petstore/c-useJsonUnformatted/model/pet.h index e86cda79d16e..860197e63a53 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/pet.h +++ b/samples/client/petstore/c-useJsonUnformatted/model/pet.h @@ -29,7 +29,7 @@ openapi_petstore_pet_STATUS_e pet_status_FromString(char* status); typedef struct pet_t { - long *id; //numeric + long id; //numeric struct category_t *category; //model char *name; // string list_t *photo_urls; //primitive container @@ -40,7 +40,7 @@ typedef struct pet_t { } pet_t; __attribute__((deprecated)) pet_t *pet_create( - long *id, + long id, category_t *category, char *name, list_t *photo_urls, diff --git a/samples/client/petstore/c-useJsonUnformatted/model/tag.c b/samples/client/petstore/c-useJsonUnformatted/model/tag.c index ce7c30b8f49f..e4b4f94af53d 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/tag.c +++ b/samples/client/petstore/c-useJsonUnformatted/model/tag.c @@ -6,37 +6,28 @@ static tag_t *tag_create_internal( - long *id, + long id, char *name ) { tag_t *tag_local_var = malloc(sizeof(tag_t)); if (!tag_local_var) { return NULL; } - memset(tag_local_var, 0, sizeof(tag_t)); - tag_local_var->_library_owned = 1; tag_local_var->id = id; tag_local_var->name = name; + + tag_local_var->_library_owned = 1; return tag_local_var; } __attribute__((deprecated)) tag_t *tag_create( - long *id, + long id, char *name ) { - long *id_copy = NULL; - if (id) { - id_copy = malloc(sizeof(long)); - if (id_copy) *id_copy = *id; - } - tag_t *result = tag_create_internal ( - id_copy, + return tag_create_internal ( + id, name ); - if (!result) { - free(id_copy); - } - return result; } void tag_free(tag_t *tag) { @@ -48,10 +39,6 @@ void tag_free(tag_t *tag) { return ; } listEntry_t *listEntry; - if (tag->id) { - free(tag->id); - tag->id = NULL; - } if (tag->name) { free(tag->name); tag->name = NULL; @@ -64,7 +51,7 @@ cJSON *tag_convertToJSON(tag_t *tag) { // tag->id if(tag->id) { - if(cJSON_AddNumberToObject(item, "id", *tag->id) == NULL) { + if(cJSON_AddNumberToObject(item, "id", tag->id) == NULL) { goto fail; //Numeric } } @@ -89,11 +76,6 @@ tag_t *tag_parseFromJSON(cJSON *tagJSON){ tag_t *tag_local_var = NULL; - // define the local variable for tag->id - long *id_local_var = NULL; - - char *name_local_str = NULL; - // tag->id cJSON *id = cJSON_GetObjectItemCaseSensitive(tagJSON, "id"); if (cJSON_IsNull(id)) { @@ -104,12 +86,6 @@ tag_t *tag_parseFromJSON(cJSON *tagJSON){ { goto end; //Numeric } - id_local_var = malloc(sizeof(long)); - if(!id_local_var) - { - goto end; - } - *id_local_var = id->valuedouble; } // tag->name @@ -125,27 +101,13 @@ tag_t *tag_parseFromJSON(cJSON *tagJSON){ } - if (name && !cJSON_IsNull(name)) name_local_str = strdup(name->valuestring); - tag_local_var = tag_create_internal ( - id_local_var, - name_local_str + id ? id->valuedouble : 0, + name && !cJSON_IsNull(name) ? strdup(name->valuestring) : NULL ); - if (!tag_local_var) { - goto end; - } - return tag_local_var; end: - if (id_local_var) { - free(id_local_var); - id_local_var = NULL; - } - if (name_local_str) { - free(name_local_str); - name_local_str = NULL; - } return NULL; } diff --git a/samples/client/petstore/c-useJsonUnformatted/model/tag.h b/samples/client/petstore/c-useJsonUnformatted/model/tag.h index 0e39017928c1..d4b29e4d2e04 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/tag.h +++ b/samples/client/petstore/c-useJsonUnformatted/model/tag.h @@ -19,14 +19,14 @@ typedef struct tag_t tag_t; typedef struct tag_t { - long *id; //numeric + long id; //numeric char *name; // string int _library_owned; // Is the library responsible for freeing this object? } tag_t; __attribute__((deprecated)) tag_t *tag_create( - long *id, + long id, char *name ); diff --git a/samples/client/petstore/c-useJsonUnformatted/model/user.c b/samples/client/petstore/c-useJsonUnformatted/model/user.c index 9a560c7fea41..780347915886 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/user.c +++ b/samples/client/petstore/c-useJsonUnformatted/model/user.c @@ -6,14 +6,14 @@ static user_t *user_create_internal( - long *id, + long id, char *username, char *first_name, char *last_name, char *email, char *password, char *phone, - int *user_status, + int user_status, list_t* extra, openapi_petstore_preference__e preference ) { @@ -21,8 +21,6 @@ static user_t *user_create_internal( if (!user_local_var) { return NULL; } - memset(user_local_var, 0, sizeof(user_t)); - user_local_var->_library_owned = 1; user_local_var->id = id; user_local_var->username = username; user_local_var->first_name = first_name; @@ -33,48 +31,35 @@ static user_t *user_create_internal( user_local_var->user_status = user_status; user_local_var->extra = extra; user_local_var->preference = preference; + + user_local_var->_library_owned = 1; return user_local_var; } __attribute__((deprecated)) user_t *user_create( - long *id, + long id, char *username, char *first_name, char *last_name, char *email, char *password, char *phone, - int *user_status, + int user_status, list_t* extra, openapi_petstore_preference__e preference ) { - long *id_copy = NULL; - if (id) { - id_copy = malloc(sizeof(long)); - if (id_copy) *id_copy = *id; - } - int *user_status_copy = NULL; - if (user_status) { - user_status_copy = malloc(sizeof(int)); - if (user_status_copy) *user_status_copy = *user_status; - } - user_t *result = user_create_internal ( - id_copy, + return user_create_internal ( + id, username, first_name, last_name, email, password, phone, - user_status_copy, + user_status, extra, preference ); - if (!result) { - free(id_copy); - free(user_status_copy); - } - return result; } void user_free(user_t *user) { @@ -86,10 +71,6 @@ void user_free(user_t *user) { return ; } listEntry_t *listEntry; - if (user->id) { - free(user->id); - user->id = NULL; - } if (user->username) { free(user->username); user->username = NULL; @@ -114,10 +95,6 @@ void user_free(user_t *user) { free(user->phone); user->phone = NULL; } - if (user->user_status) { - free(user->user_status); - user->user_status = NULL; - } if (user->extra) { list_ForEach(listEntry, user->extra) { keyValuePair_t *localKeyValue = listEntry->data; @@ -136,7 +113,7 @@ cJSON *user_convertToJSON(user_t *user) { // user->id if(user->id) { - if(cJSON_AddNumberToObject(item, "id", *user->id) == NULL) { + if(cJSON_AddNumberToObject(item, "id", user->id) == NULL) { goto fail; //Numeric } } @@ -192,7 +169,7 @@ cJSON *user_convertToJSON(user_t *user) { // 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 } } @@ -238,24 +215,6 @@ user_t *user_parseFromJSON(cJSON *userJSON){ user_t *user_local_var = NULL; - // define the local variable for user->id - long *id_local_var = NULL; - - char *username_local_str = NULL; - - char *first_name_local_str = NULL; - - char *last_name_local_str = NULL; - - char *email_local_str = NULL; - - char *password_local_str = NULL; - - char *phone_local_str = NULL; - - // define the local variable for user->user_status - int *user_status_local_var = NULL; - // define the local map for user->extra list_t *extraList = NULL; @@ -272,12 +231,6 @@ user_t *user_parseFromJSON(cJSON *userJSON){ { goto end; //Numeric } - id_local_var = malloc(sizeof(long)); - if(!id_local_var) - { - goto end; - } - *id_local_var = id->valuedouble; } // user->username @@ -362,12 +315,6 @@ user_t *user_parseFromJSON(cJSON *userJSON){ { goto end; //Numeric } - user_status_local_var = malloc(sizeof(int)); - if(!user_status_local_var) - { - goto end; - } - *user_status_local_var = user_status->valuedouble; } // user->extra @@ -403,64 +350,21 @@ user_t *user_parseFromJSON(cJSON *userJSON){ } - if (username && !cJSON_IsNull(username)) username_local_str = strdup(username->valuestring); - if (first_name && !cJSON_IsNull(first_name)) first_name_local_str = strdup(first_name->valuestring); - if (last_name && !cJSON_IsNull(last_name)) last_name_local_str = strdup(last_name->valuestring); - if (email && !cJSON_IsNull(email)) email_local_str = strdup(email->valuestring); - if (password && !cJSON_IsNull(password)) password_local_str = strdup(password->valuestring); - if (phone && !cJSON_IsNull(phone)) phone_local_str = strdup(phone->valuestring); - user_local_var = user_create_internal ( - id_local_var, - username_local_str, - first_name_local_str, - last_name_local_str, - email_local_str, - password_local_str, - phone_local_str, - user_status_local_var, + id ? id->valuedouble : 0, + 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, extra ? extraList : NULL, preference ? preference_local_nonprim : 0 ); - if (!user_local_var) { - goto end; - } - return user_local_var; end: - if (id_local_var) { - free(id_local_var); - id_local_var = NULL; - } - if (username_local_str) { - free(username_local_str); - username_local_str = NULL; - } - if (first_name_local_str) { - free(first_name_local_str); - first_name_local_str = NULL; - } - if (last_name_local_str) { - free(last_name_local_str); - last_name_local_str = NULL; - } - if (email_local_str) { - free(email_local_str); - email_local_str = NULL; - } - if (password_local_str) { - free(password_local_str); - password_local_str = NULL; - } - if (phone_local_str) { - free(phone_local_str); - phone_local_str = NULL; - } - if (user_status_local_var) { - free(user_status_local_var); - user_status_local_var = NULL; - } if (extraList) { listEntry_t *listEntry = NULL; list_ForEach(listEntry, extraList) { diff --git a/samples/client/petstore/c-useJsonUnformatted/model/user.h b/samples/client/petstore/c-useJsonUnformatted/model/user.h index 333195656bb0..4643e020a5fe 100644 --- a/samples/client/petstore/c-useJsonUnformatted/model/user.h +++ b/samples/client/petstore/c-useJsonUnformatted/model/user.h @@ -21,14 +21,14 @@ typedef struct user_t user_t; typedef struct user_t { - long *id; //numeric + long id; //numeric char *username; // string char *first_name; // string char *last_name; // string char *email; // string char *password; // string char *phone; // string - int *user_status; //numeric + int user_status; //numeric list_t* extra; //map openapi_petstore_preference__e preference; //referenced enum @@ -36,14 +36,14 @@ typedef struct user_t { } user_t; __attribute__((deprecated)) user_t *user_create( - long *id, + long id, char *username, char *first_name, char *last_name, char *email, char *password, char *phone, - int *user_status, + int user_status, list_t* extra, openapi_petstore_preference__e preference );