From 21df220e641c928f019b4b1afc32aca4d015b2b8 Mon Sep 17 00:00:00 2001 From: William Cheng Date: Wed, 8 Aug 2018 19:33:42 +0800 Subject: [PATCH] more fixes --- bin/c-petstore.sh | 2 +- ...odegen.java => CLibcurlClientCodegen.java} | 169 +-------- .../{C => C-libcurl}/CMakeLists.txt.mustache | 0 .../src/main/resources/C-libcurl/api.mustache | 97 ++++++ .../{C => C-libcurl}/apiClient.c.mustache | 143 ++++++-- .../{C => C-libcurl}/apiClient.h.mustache | 10 +- .../resources/C-libcurl/apiKey.c.mustache | 14 + .../{C => C-libcurl}/apiKey.h.mustache | 0 .../{C => C-libcurl}/api_test.mustache | 0 .../{C => C-libcurl}/cJSON.c.mustache | 0 .../{C => C-libcurl}/cJSON.h.mustache | 0 .../{C => C-libcurl}/cJSON.licence.mustache | 0 .../{C => C-libcurl}/libcurl.licence.mustache | 0 .../{C => C-libcurl}/list.c.mustache | 2 +- .../{C => C-libcurl}/list.h.mustache | 0 .../main/resources/C-libcurl/model.mustache | 135 +++++++ .../{C => C-libcurl}/model_test.mustache | 0 .../uncrustify-rules.cfg.mustache | 0 .../src/main/resources/C/api.mustache | 85 ----- .../src/main/resources/C/apiKey.c.mustache | 14 - .../src/main/resources/C/model.mustache | 62 ---- .../org.openapitools.codegen.CodegenConfig | 2 +- .../petstore/C/.openapi-generator/VERSION | 2 +- samples/client/petstore/C/api/PetAPI.c | 328 ++++++++++++++++-- samples/client/petstore/C/api/StoreAPI.c | 168 +++++++-- samples/client/petstore/C/api/UserAPI.c | 314 ++++++++++++++--- .../client/petstore/C/model/api_response.c | 27 +- samples/client/petstore/C/model/category.c | 22 +- samples/client/petstore/C/model/order.c | 42 ++- samples/client/petstore/C/model/pet.c | 74 +++- samples/client/petstore/C/model/tag.c | 22 +- samples/client/petstore/C/model/user.c | 54 ++- samples/client/petstore/c/include/apiClient.h | 2 +- .../client/petstore/c/include/keyValuePair.h | 7 - samples/client/petstore/c/src/apiClient.c | 2 +- samples/client/petstore/c/src/apiKey.c | 8 +- samples/client/petstore/c/src/list.c | 2 +- samples/client/petstore/c/unit-tests/apiKey.c | 27 +- .../c/unit-tests/test-api-client-parameters.c | 68 ---- .../petstore/c/unit-tests/test-api-client.c | 87 ----- 40 files changed, 1331 insertions(+), 660 deletions(-) rename modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/{CClientCodegen.java => CLibcurlClientCodegen.java} (75%) rename modules/openapi-generator/src/main/resources/{C => C-libcurl}/CMakeLists.txt.mustache (100%) create mode 100644 modules/openapi-generator/src/main/resources/C-libcurl/api.mustache rename modules/openapi-generator/src/main/resources/{C => C-libcurl}/apiClient.c.mustache (52%) rename modules/openapi-generator/src/main/resources/{C => C-libcurl}/apiClient.h.mustache (75%) create mode 100644 modules/openapi-generator/src/main/resources/C-libcurl/apiKey.c.mustache rename modules/openapi-generator/src/main/resources/{C => C-libcurl}/apiKey.h.mustache (100%) rename modules/openapi-generator/src/main/resources/{C => C-libcurl}/api_test.mustache (100%) rename modules/openapi-generator/src/main/resources/{C => C-libcurl}/cJSON.c.mustache (100%) rename modules/openapi-generator/src/main/resources/{C => C-libcurl}/cJSON.h.mustache (100%) rename modules/openapi-generator/src/main/resources/{C => C-libcurl}/cJSON.licence.mustache (100%) rename modules/openapi-generator/src/main/resources/{C => C-libcurl}/libcurl.licence.mustache (100%) rename modules/openapi-generator/src/main/resources/{C => C-libcurl}/list.c.mustache (99%) rename modules/openapi-generator/src/main/resources/{C => C-libcurl}/list.h.mustache (100%) create mode 100644 modules/openapi-generator/src/main/resources/C-libcurl/model.mustache rename modules/openapi-generator/src/main/resources/{C => C-libcurl}/model_test.mustache (100%) rename modules/openapi-generator/src/main/resources/{C => C-libcurl}/uncrustify-rules.cfg.mustache (100%) delete mode 100644 modules/openapi-generator/src/main/resources/C/api.mustache delete mode 100644 modules/openapi-generator/src/main/resources/C/apiKey.c.mustache delete mode 100644 modules/openapi-generator/src/main/resources/C/model.mustache delete mode 100644 samples/client/petstore/c/include/keyValuePair.h delete mode 100644 samples/client/petstore/c/unit-tests/test-api-client-parameters.c delete mode 100644 samples/client/petstore/c/unit-tests/test-api-client.c diff --git a/bin/c-petstore.sh b/bin/c-petstore.sh index b5223ba6f9bb..c5af7b03ea26 100755 --- a/bin/c-petstore.sh +++ b/bin/c-petstore.sh @@ -27,7 +27,7 @@ fi # if you've executed sbt assembly previously it will use that instead. export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties" -ags="generate -t modules/openapi-generator/src/main/resources/C -i modules/openapi-generator/src/test/resources/2_0/petstore.yaml -g c -o samples/client/petstore/C $@" +ags="generate -t modules/openapi-generator/src/main/resources/C-libcurl -i modules/openapi-generator/src/test/resources/2_0/petstore.yaml -g c -o samples/client/petstore/C $@" # purge folder echo "Purge folder samples/client/petstore/C" diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CLibcurlClientCodegen.java similarity index 75% rename from modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CClientCodegen.java rename to modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CLibcurlClientCodegen.java index 864a54f7bcbf..6ebb1389d470 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CLibcurlClientCodegen.java @@ -30,51 +30,28 @@ import java.util.Iterator; import java.util.Map; -public class CClientCodegen extends DefaultCodegen implements CodegenConfig { - private static final Logger LOGGER = LoggerFactory.getLogger(CClientCodegen.class); - public static final String GEM_NAME = "gemName"; - public static final String MODULE_NAME = "moduleName"; - public static final String GEM_VERSION = "gemVersion"; - public static final String GEM_LICENSE = "gemLicense"; - public static final String GEM_REQUIRED_RUBY_VERSION = "gemRequiredRubyVersion"; - public static final String GEM_HOMEPAGE = "gemHomepage"; - public static final String GEM_SUMMARY = "gemSummary"; - public static final String GEM_DESCRIPTION = "gemDescription"; - public static final String GEM_AUTHOR = "gemAuthor"; - public static final String GEM_AUTHOR_EMAIL = "gemAuthorEmail"; - - protected String gemName; +public class CLibcurlClientCodegen extends DefaultCodegen implements CodegenConfig { + private static final Logger LOGGER = LoggerFactory.getLogger(CLibcurlClientCodegen.class); + protected String moduleName; - protected String gemVersion = "1.0.0"; protected String specFolder = "spec"; protected String libFolder = "lib"; - protected String gemLicense = "proprietary"; - protected String gemRequiredRubyVersion = ">= 1.9"; - protected String gemHomepage = "http://org.openapitools"; - protected String gemSummary = "A ruby wrapper for the REST APIs"; - protected String gemDescription = "This gem maps to a REST API"; - protected String gemAuthor = ""; - protected String gemAuthorEmail = ""; protected String apiDocPath = "docs/"; protected String modelDocPath = "docs/"; protected static int emptyMethodNameCounter = 0; - public CClientCodegen() { + public CLibcurlClientCodegen() { super(); - // clear import mapping (from default generator) as ruby does not use it - // at the moment - importMapping.clear(); - modelPackage = "models"; apiPackage = "api"; - outputFolder = "generated-code" + File.separator + "C"; + outputFolder = "generated-code" + File.separator + "C-libcurl"; modelTemplateFiles.put("model.mustache", ".c"); apiTemplateFiles.put("api.mustache", ".c"); //modelDocTemplateFiles.put("model_doc.mustache", ".md"); //apiDocTemplateFiles.put("api_doc.mustache", ".md"); - embeddedTemplateDir = templateDir = "C"; + embeddedTemplateDir = templateDir = "C-libcurl"; modelTestTemplateFiles.put("model_test.mustache", ".c"); apiTestTemplateFiles.put("api_test.mustache", ".c"); @@ -174,30 +151,9 @@ public CClientCodegen() { itr.remove(); } } - cliOptions.add(new CliOption(GEM_NAME, "gem name (convention: underscore_case)."). - defaultValue("openapi_client")); - cliOptions.add(new CliOption(MODULE_NAME, "top module name (convention: CamelCase, usually corresponding" + - " to gem name).").defaultValue("OpenAPIClient")); - cliOptions.add(new CliOption(GEM_VERSION, "gem version.").defaultValue("1.0.0")); - - cliOptions.add(new CliOption(GEM_LICENSE, "gem license. "). - defaultValue("proprietary")); - - cliOptions.add(new CliOption(GEM_REQUIRED_RUBY_VERSION, "gem required Ruby version. "). - defaultValue(">= 1.9")); - - cliOptions.add(new CliOption(GEM_HOMEPAGE, "gem homepage. "). - defaultValue("http://org.openapitools")); - - cliOptions.add(new CliOption(GEM_SUMMARY, "gem summary. "). - defaultValue("A ruby wrapper for the REST APIs")); + //cliOptions.add(new CliOption(GEM_NAME, "gem name (convention: underscore_case)."). + // defaultValue("openapi_client")); - cliOptions.add(new CliOption(GEM_DESCRIPTION, "gem description. "). - defaultValue("This gem maps to a REST API")); - - cliOptions.add(new CliOption(GEM_AUTHOR, "gem author (only one is supported).")); - - cliOptions.add(new CliOption(GEM_AUTHOR_EMAIL, "gem author email (only one is supported).")); cliOptions.add(new CliOption(CodegenConstants.HIDE_GENERATION_TIMESTAMP, CodegenConstants.HIDE_GENERATION_TIMESTAMP_DESC). defaultValue(Boolean.TRUE.toString())); @@ -208,60 +164,6 @@ public CClientCodegen() { public void processOpts() { super.processOpts(); - if (additionalProperties.containsKey(GEM_NAME)) { - setGemName((String) additionalProperties.get(GEM_NAME)); - } - if (additionalProperties.containsKey(MODULE_NAME)) { - setModuleName((String) additionalProperties.get(MODULE_NAME)); - } - - if (gemName == null && moduleName == null) { - setGemName("openapi_client"); - //setModuleName(generateModuleName(gemName)); - } else if (gemName == null) { - //setGemName(generateGemName(moduleName)); - } else if (moduleName == null) { - //setModuleName(generateModuleName(gemName)); - } - - additionalProperties.put(GEM_NAME, gemName); - additionalProperties.put(MODULE_NAME, moduleName); - - if (additionalProperties.containsKey(GEM_VERSION)) { - setGemVersion((String) additionalProperties.get(GEM_VERSION)); - } else { - // not set, pass the default value to template - additionalProperties.put(GEM_VERSION, gemVersion); - } - - if (additionalProperties.containsKey(GEM_LICENSE)) { - setGemLicense((String) additionalProperties.get(GEM_LICENSE)); - } - - if (additionalProperties.containsKey(GEM_REQUIRED_RUBY_VERSION)) { - setGemRequiredRubyVersion((String) additionalProperties.get(GEM_REQUIRED_RUBY_VERSION)); - } - - if (additionalProperties.containsKey(GEM_HOMEPAGE)) { - setGemHomepage((String) additionalProperties.get(GEM_HOMEPAGE)); - } - - if (additionalProperties.containsKey(GEM_SUMMARY)) { - setGemSummary((String) additionalProperties.get(GEM_SUMMARY)); - } - - if (additionalProperties.containsKey(GEM_DESCRIPTION)) { - setGemDescription((String) additionalProperties.get(GEM_DESCRIPTION)); - } - - if (additionalProperties.containsKey(GEM_AUTHOR)) { - setGemAuthor((String) additionalProperties.get(GEM_AUTHOR)); - } - - if (additionalProperties.containsKey(GEM_AUTHOR_EMAIL)) { - setGemAuthorEmail((String) additionalProperties.get(GEM_AUTHOR_EMAIL)); - } - // make api and model doc path available in mustache template additionalProperties.put("apiDocPath", apiDocPath); additionalProperties.put("modelDocPath", modelDocPath); @@ -305,7 +207,7 @@ public String getName() { @Override public String getHelp() { - return "Generates a C client library (beta)."; + return "Generates a C (libcurl) client library (beta)."; } @@ -561,17 +463,24 @@ public String toOperationId(String operationId) { // method name cannot use reserved keyword, e.g. return if (isReservedWord(operationId)) { - String newOperationId = camelize("call_" + operationId); + String newOperationId = camelize(sanitizeName("call_" + operationId), true); LOGGER.warn(operationId + " (reserved word) cannot be used as method name. Renamed to " + newOperationId); return newOperationId; } - return camelize(sanitizeName(operationId)); + // operationId starts with a number + if (operationId.matches("^\\d.*")) { + String newOperationId = camelize(sanitizeName("call_" + operationId), true); + LOGGER.warn(operationId + " (starting with a number) cannot be used as method name. Renamed to " + newOperationId); + return newOperationId; + } + + return camelize(sanitizeName(operationId), true); } @Override public String toApiImport(String name) { - return gemName + "/" + apiPackage() + "/" + toApiFilename(name); + return apiPackage() + "/" + toApiFilename(name); } @Override @@ -638,46 +547,6 @@ public void setParameterExampleValue(CodegenParameter p) { p.example = example; } - public void setGemName(String gemName) { - this.gemName = gemName; - } - - public void setModuleName(String moduleName) { - this.moduleName = moduleName; - } - - public void setGemVersion(String gemVersion) { - this.gemVersion = gemVersion; - } - - public void setGemDescription(String gemDescription) { - this.gemDescription = gemDescription; - } - - public void setGemSummary(String gemSummary) { - this.gemSummary = gemSummary; - } - - public void setGemLicense(String gemLicense) { - this.gemLicense = gemLicense; - } - - public void setGemRequiredRubyVersion(String gemRequiredRubyVersion) { - this.gemRequiredRubyVersion = gemRequiredRubyVersion; - } - - public void setGemHomepage(String gemHomepage) { - this.gemHomepage = gemHomepage; - } - - public void setGemAuthor(String gemAuthor) { - this.gemAuthor = gemAuthor; - } - - public void setGemAuthorEmail(String gemAuthorEmail) { - this.gemAuthorEmail = gemAuthorEmail; - } - @Override public boolean shouldOverwrite(String filename) { // skip spec file as the file might have been updated with new test cases diff --git a/modules/openapi-generator/src/main/resources/C/CMakeLists.txt.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/CMakeLists.txt.mustache similarity index 100% rename from modules/openapi-generator/src/main/resources/C/CMakeLists.txt.mustache rename to modules/openapi-generator/src/main/resources/C-libcurl/CMakeLists.txt.mustache diff --git a/modules/openapi-generator/src/main/resources/C-libcurl/api.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/api.mustache new file mode 100644 index 000000000000..f96800649d61 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/C-libcurl/api.mustache @@ -0,0 +1,97 @@ +#include +#include +#include "apiClient.h" +#include "cJSON.h" +{{#imports}} +#include "{{{import}}}.h" // TODO will fix the import later +{{/imports}} + +#define MAX_BUFFER_LENGTH 4096 + +{{#operations}} +{{#operation}} +{{#summary}} +// {{{summary}}} +// +{{/summary}} +{{#notes}} +// {{{notes}}} +// +{{/notes}} +{{#returnType}}{{{.}}}_t{{/returnType}}{{^returnType}}void{{/returnType}} *{{{classname}}}_{{{operationId}}}(apiClient_t *apiClient{{#allParams}}, {{{dataType}}} {{paramName}}{{/allParams}}) { + list_t *localVarQueryParameters, + list_t *localVarHeaderParameters, + list_t *localVarFormParameters, + char *localVarBodyParameters, + + // create the path + char *localVarPath = malloc(MAX_BUFFER_LENGTH); + snprintf(localVarPath, MAX_BUFFER_LENGTH, "{{{path}}}"); + + {{#pathParams}} + // TODO path parameter {{paramName}} ({{baseName}}) not yet supported + // TODO base path = {{{basePath}}} + replace_str(localVarPath, "{" + "{{baseName}}" + "}", {{paramName}})// TODO need to revise + {{/pathParams}} + + {{#headerParams}} + // TODO header parameters + // header parameter {{paramName}} ({{baseName}}) not yet supported + {{/headerParams}} + + {{#queryParams}} + // TODO query parameters + // query parameter {{paramName}} ({{baseName}}) not yet supported + char *{{baseName}} = malloc(MAX_BUFFER_LENGTH); + + {{/queryParams}} + {{#formParams}} + // TODO form parameters + // form parameter {{paramName}} ({{baseName}}) not yet supported + + {{/formParams}} + {{#bodyParam}} + // JSON HTTP Request - {{{baseName}}} + cJSON *{{{paramName}}}JSONObject; + {{{paramName}}}JSONObject = {{dataType}}_convertToJSON({{paramName}}); + localVarBodyParameters = cJSON_Print({{{paramName}}}JSONObject); + + {{/bodyParam}} + apiClient_invoke(apiClient, + "pet", + localVarPath, + localVarQueryParameters, + localVarHeaderParameters, + localVarFormParameters, + localVarBodyParameters, + "{{{httpMethod}}}"); + + free(apiClient->dataReceived); + {{#allParams}} + {{^bodyParam}} + free({{{paramName}}}String); + {{/bodyParam}} + {{#bodyParam}} + free(localVarBodyParameters); + cJSON_Delete() + {{/bodyParam}} + {{/allParams}} + {{#returnType}} + localVar{{{returnType}}} = {{complexType}}_parseFromJSON(apiClient->dataReceived); + if(localVar{{{returnType}}} == NULL) { + return 0; + } else { + cJSON *jsonObject = {{complexType}}_convertToJSON({{complexType}}); + cJSON_Delete(jsonObject); + } + + return localVar{{{returnType}}}; + {{/returnType}} + {{^returnType}} + return; + {{/returnType}} + +} + +{{/operation}} +{{/operations}} \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/C/apiClient.c.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.c.mustache similarity index 52% rename from modules/openapi-generator/src/main/resources/C/apiClient.c.mustache rename to modules/openapi-generator/src/main/resources/C-libcurl/apiClient.c.mustache index 679407f86813..58a593e28f27 100644 --- a/modules/openapi-generator/src/main/resources/C/apiClient.c.mustache +++ b/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.c.mustache @@ -3,10 +3,8 @@ #include #include "apiClient.h" #include "pet.h" +#include "keyValuePair.h" -#ifdef API_KEY -#include "apiKey.h" -#endif size_t writeDataCallback(void *buffer, size_t size, size_t nmemb, void *userp); apiClient_t *apiClient_create() { @@ -28,25 +26,77 @@ void apiClient_free(apiClient_t *apiClient) { curl_global_cleanup(); } +void replaceSpaceWithPlus(char *stringToProcess) { + for(int i = 0; i < strlen(stringToProcess); i++) { + if(stringToProcess[i] == ' ') { + stringToProcess[i] = '+'; + } + } +} + char *assembleTargetUrl(char *basePath, char *operationName, - char *operationParameter) { + char *operationParameter, + list_t *queryParameters) { + int neededBufferSizeForQueryParameters = 0; + listEntry_t *listEntry; + + if(queryParameters != NULL) { + list_ForEach(listEntry, queryParameters) { + keyValuePair_t *pair = listEntry->data; + neededBufferSizeForQueryParameters += + strlen(pair->key) + strlen(pair->value); + } + + neededBufferSizeForQueryParameters += + (queryParameters->count * 2); // each keyValuePair is separated by a = and a & except the last, but this makes up for the ? at the beginning + } + + int operationParameterLength = 0; + int basePathLength = strlen(basePath); + bool slashNeedsToBeAppendedToBasePath = false; + + if(operationParameter != NULL) { + operationParameterLength = (1 + strlen(operationParameter)); + } + if(basePath[strlen(basePath) - 1] != '/') { + slashNeedsToBeAppendedToBasePath = true; + basePathLength++; + } + char *targetUrl = - malloc(strlen(operationName) + strlen( - basePath) + - ((operationParameter == NULL) ? 1 : (2 + strlen( - operationParameter)))); + malloc(strlen( + operationName) + neededBufferSizeForQueryParameters + basePathLength + operationParameterLength + 1 + ); strcpy(targetUrl, basePath); + if(slashNeedsToBeAppendedToBasePath) { + strcat(targetUrl, "/"); + } strcat(targetUrl, operationName); if(operationParameter != NULL) { strcat(targetUrl, "/"); strcat(targetUrl, operationParameter); } + if(queryParameters != NULL) { + strcat(targetUrl, "?"); + list_ForEach(listEntry, queryParameters) { + keyValuePair_t *pair = listEntry->data; + replaceSpaceWithPlus(pair->key); + strcat(targetUrl, pair->key); + strcat(targetUrl, "="); + replaceSpaceWithPlus(pair->value); + strcat(targetUrl, pair->value); + if(listEntry->nextListEntry != NULL) { + strcat(targetUrl, "&"); + } + } + } + return targetUrl; } -char *assembleHeader(char *key, char *value) { +char *assembleHeaderField(char *key, char *value) { char *header = malloc(strlen(key) + strlen(value) + 3); strcpy(header, key), @@ -56,40 +106,83 @@ char *assembleHeader(char *key, char *value) { return header; } -void postData(CURL *handle, char *dataToPost) { - curl_easy_setopt(handle, CURLOPT_POSTFIELDS, dataToPost); +void postData(CURL *handle, char *bodyParameters) { + curl_easy_setopt(handle, CURLOPT_POSTFIELDS, bodyParameters); curl_easy_setopt(handle, CURLOPT_POSTFIELDSIZE_LARGE, - strlen(dataToPost)); - curl_easy_setopt(handle, CURLOPT_CUSTOMREQUEST, "POST"); + strlen(bodyParameters)); } void apiClient_invoke(apiClient_t *apiClient, char *operationName, char *operationParameter, - char *dataToPost) { + list_t *queryParameters, + list_t *headerParameters, + list_t *formParameters, + char *bodyParameters, + char *requestType) { CURL *handle = curl_easy_init(); CURLcode res; if(handle) { + listEntry_t *listEntry; + curl_mime *mime = NULL; struct curl_slist *headers = NULL; + headers = curl_slist_append(headers, "accept: application/json"); headers = curl_slist_append(headers, "Content-Type: application/json"); + if(requestType != NULL) { + curl_easy_setopt(handle, + CURLOPT_CUSTOMREQUEST, + requestType); + } + if(formParameters != NULL) { + mime = curl_mime_init(handle); + + list_ForEach(listEntry, formParameters) { + keyValuePair_t *keyValuePair = listEntry->data; + if((keyValuePair->key != NULL) && + (keyValuePair->value != NULL) ) + { + curl_mimepart *part = curl_mime_addpart( + mime); + curl_mime_data(part, + keyValuePair->key, + CURL_ZERO_TERMINATED); + curl_mime_name(part, + keyValuePair->value); + } + } + curl_easy_setopt(handle, CURLOPT_MIMEPOST, mime); + } + list_ForEach(listEntry, headerParameters) { + keyValuePair_t *keyValuePair = listEntry->data; + if((keyValuePair->key != NULL) && + (keyValuePair->value != NULL) ) + { + char *headerValueToWrite = + assembleHeaderField( + keyValuePair->key, + keyValuePair->value); + curl_slist_append(headers, headerValueToWrite); + free(headerValueToWrite); + } + } // this would only be generated for apiKey authentication #ifdef API_KEY - listEntry_t *listEntry; list_ForEach(listEntry, apiClient->apiKeys) { - apiKey_t *apiKey = listEntry->data; + keyValuePair_t *apiKey = listEntry->data; if((apiKey->key != NULL) && (apiKey->value != NULL) ) { - char *headerValueToWrite = assembleHeader( - apiKey->key, - apiKey->value); + char *headerValueToWrite = + assembleHeaderField( + apiKey->key, + apiKey->value); curl_slist_append(headers, headerValueToWrite); free(headerValueToWrite); } @@ -99,7 +192,8 @@ void apiClient_invoke(apiClient_t *apiClient, char *targetUrl = assembleTargetUrl(apiClient->basePath, operationName, - operationParameter); + operationParameter, + queryParameters); curl_easy_setopt(handle, CURLOPT_URL, targetUrl); curl_easy_setopt(handle, @@ -148,8 +242,8 @@ void apiClient_invoke(apiClient_t *apiClient, #endif // BASIC_AUTH - if(dataToPost != NULL) { - postData(handle, dataToPost); + if(bodyParameters != NULL) { + postData(handle, bodyParameters); } res = curl_easy_perform(handle); @@ -170,6 +264,9 @@ void apiClient_invoke(apiClient_t *apiClient, } #endif // BASIC_AUTH curl_easy_cleanup(handle); + if(formParameters != NULL) { + curl_mime_free(mime); + } } } @@ -177,4 +274,4 @@ size_t writeDataCallback(void *buffer, size_t size, size_t nmemb, void *userp) { *(char **) userp = strdup(buffer); return size * nmemb; -} +} \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/C/apiClient.h.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.h.mustache similarity index 75% rename from modules/openapi-generator/src/main/resources/C/apiClient.h.mustache rename to modules/openapi-generator/src/main/resources/C-libcurl/apiClient.h.mustache index 75feef0aaae4..7b493a422865 100644 --- a/modules/openapi-generator/src/main/resources/C/apiClient.h.mustache +++ b/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.h.mustache @@ -1,9 +1,11 @@ #ifndef INCLUDE_API_CLIENT_H #define INCLUDE_API_CLIENT_H -#ifdef API_KEY #include "list.h" -#endif // API_KEY + +typedef int bool; +#define true 1 +#define false 0 typedef struct apiClient_t { char *basePath; @@ -25,6 +27,6 @@ typedef struct apiClient_t { apiClient_t* apiClient_create(); void apiClient_free(apiClient_t *apiClient); -void apiClient_invoke(apiClient_t *apiClient, char* operationName, char* operationParameter, char *dataToPost); +void apiClient_invoke(apiClient_t *apiClient, char* operationName, char* operationParameter, list_t *queryParameters, list_t *headerParameters, list_t *formParameters, char *bodyParameters, char *requestType); -#endif // INCLUDE_API_CLIENT_H +#endif // INCLUDE_API_CLIENT_H \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/C-libcurl/apiKey.c.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/apiKey.c.mustache new file mode 100644 index 000000000000..f62311304872 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/C-libcurl/apiKey.c.mustache @@ -0,0 +1,14 @@ +#include +#include "keyValuePair.h" + +keyValuePair_t *keyValuePair_create(char *key, char *value) { + keyValuePair_t *keyValuePair = malloc(sizeof(keyValuePair_t)); + keyValuePair->key = key; + keyValuePair->value = value; + + return keyValuePair; +} + +void keyValuePair_free(keyValuePair_t *keyValuePair) { + free(keyValuePair); +} \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/C/apiKey.h.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/apiKey.h.mustache similarity index 100% rename from modules/openapi-generator/src/main/resources/C/apiKey.h.mustache rename to modules/openapi-generator/src/main/resources/C-libcurl/apiKey.h.mustache diff --git a/modules/openapi-generator/src/main/resources/C/api_test.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/api_test.mustache similarity index 100% rename from modules/openapi-generator/src/main/resources/C/api_test.mustache rename to modules/openapi-generator/src/main/resources/C-libcurl/api_test.mustache diff --git a/modules/openapi-generator/src/main/resources/C/cJSON.c.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/cJSON.c.mustache similarity index 100% rename from modules/openapi-generator/src/main/resources/C/cJSON.c.mustache rename to modules/openapi-generator/src/main/resources/C-libcurl/cJSON.c.mustache diff --git a/modules/openapi-generator/src/main/resources/C/cJSON.h.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/cJSON.h.mustache similarity index 100% rename from modules/openapi-generator/src/main/resources/C/cJSON.h.mustache rename to modules/openapi-generator/src/main/resources/C-libcurl/cJSON.h.mustache diff --git a/modules/openapi-generator/src/main/resources/C/cJSON.licence.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/cJSON.licence.mustache similarity index 100% rename from modules/openapi-generator/src/main/resources/C/cJSON.licence.mustache rename to modules/openapi-generator/src/main/resources/C-libcurl/cJSON.licence.mustache diff --git a/modules/openapi-generator/src/main/resources/C/libcurl.licence.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/libcurl.licence.mustache similarity index 100% rename from modules/openapi-generator/src/main/resources/C/libcurl.licence.mustache rename to modules/openapi-generator/src/main/resources/C-libcurl/libcurl.licence.mustache diff --git a/modules/openapi-generator/src/main/resources/C/list.c.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/list.c.mustache similarity index 99% rename from modules/openapi-generator/src/main/resources/C/list.c.mustache rename to modules/openapi-generator/src/main/resources/C-libcurl/list.c.mustache index bc32c43e92f2..0d680d48ee4d 100644 --- a/modules/openapi-generator/src/main/resources/C/list.c.mustache +++ b/modules/openapi-generator/src/main/resources/C-libcurl/list.c.mustache @@ -166,4 +166,4 @@ listEntry_t *list_getElementAt(list_t *list, long indexOfElement) { return currentListEntry; } -} +} \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/C/list.h.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/list.h.mustache similarity index 100% rename from modules/openapi-generator/src/main/resources/C/list.h.mustache rename to modules/openapi-generator/src/main/resources/C-libcurl/list.h.mustache diff --git a/modules/openapi-generator/src/main/resources/C-libcurl/model.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/model.mustache new file mode 100644 index 000000000000..70c0abe36920 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/C-libcurl/model.mustache @@ -0,0 +1,135 @@ +{{#models}} +{{#model}} +#include +#include +#include +#include "cJSON.h" +{{#imports}} +#include "{{{.}}}h" +{{/imports}} + + +{{classname}}_t *{{classname}}_create( + {{#vars}} + {{{dataType}}} *{{{baseName}}}{{#hasMore}},{{/hasMore}} + {{/vars}} + ) { + {{classname}}_t *{{classname}} = malloc(sizeof({{classname}}_t)); + {{#vars}} + {{classname}}->{{{baseName}}} = {{{baseName}}}; + {{/vars}} + + return {{classname}}; +} + + +void {{classname}}_free({{classname}}_t *{{classname}}) { + {{#vars}} + {{^isContainer}} + {{^isPrimitiveType}} + {{{name}}}_free({{{classname}}}->{{{baseName}}}) + {{/isPrimitiveType}} + {{#isPrimitiveType}} + {{#isString}} + free({{{classname}}}->{{{baseName}}}) + {{/isString}} + {{/isPrimitiveType}} + {{/isContainer}} + {{#isContainer}} + {{#isListContainer}} + {{#isPrimitiveType}} + listEntry_t *{{{baseName}}}ListEntry; + list_ForEach({{{baseName}}}ListEntry, {{classname}}->{{baseName}}) { + free({{{baseName}}}ListEntry->data); + } + list_free({{classname}}->{{baseName}}); + {{/isPrimitiveType}} + {{^isPrimitiveType}} + list_ForEach({{{baseName}}}ListEntry, {{classname}}->{{baseName}}) { + {{baseName}}_free({{{baseName}}}ListEntry->data); + } + list_free({{classname}}->{{baseName}}); + {{/isPrimitiveType}} + {{/isListContainer}} + {{#isMapContainer}} + // TODO map/hash not supported at the moment + {{/isMapContainer}} + {{/isContainer}} + + {{/vars}} + free({{classname}}); +} + +cJSON *{{classname}}_convertToJSON({{classname}}_t *{{classname}}) { + cJSON *item = cJSON_CreateObject(); + {{#vars}} + // {{{classname}}}->{{{baseName}}} + {{^isContainer}} + {{#isPrimitiveType}} + {{#isNumeric}} + if(cJSON_AddNumberToObject(item, "{{{baseName}}}", {{{classname}}}->{{{baseName}}}) == NULL) { + goto fail; + } + + {{/isNumeric}} + {{#isString}} + if(cJSON_AddStringToObject(item, "{{{baseName}}}", {{{classname}}}->{{{baseName}}}) == NULL) { + goto fail; + } + {{/isString}} + {{/isPrimitiveType}} + {{^isPrimitiveType}} + if(cJSON_AddStringToObject(item, "{{{baseName}}}", {{{classname}}}->{{{baseName}}}) == NULL) { + goto fail; + } + {{/isPrimitiveType}} + {{/isContainer}} + {{#isContainer}} + {{#isListContainer}} + {{#isPrimitiveType}} + list_ForEach(listEntry, {{classname}}->{{baseName}}) { + free(listEntry->data); + } + + cJSON *{{{baseName}}} = cJSON_AddArrayToObject(item, "{{{baseName}}}"); + if({{{baseName}}} == NULL) { + goto fail; + } + + listEntry_t *{{{baseName}}}ListEntry; + list_ForEach({{{baseName}}}ListEntry, pet->photoUrls) { + if(cJSON_AddStringToObject({{{baseName}}}, "", {{{baseName}}}ListEntry->data) == NULL) + { + goto fail; + } + } + {{/isPrimitiveType}} + {{^isPrimitiveType}} + cJSON *{{{baseName}}} = cJSON_AddArrayToObject(item, "{{{baseName}}}"); + if({{{baseName}}} == NULL) { + goto fail; + } + + listEntry_t *{{{baseName}}}ListEntry; + list_ForEach({{{baseName}}}ListEntry, {{classname}}->{{{baseName}}}) { + cJSON *item = tag_convertToJSON({{{baseName}}}ListEntry->data); + if(item == NULL) { + goto fail; + } + cJSON_AddItemToArray({{{baseName}}}, item); + } + {{/isPrimitiveType}} + {{/isListContainer}} + {{#isMapContainer}} + // TODO map/hash not yet supported + {{/isMapContainer}} + {{/isContainer}} + + {{/vars}} + return item; +fail: + cJSON_Delete(item); + return NULL; +} +{{/model}} +{{/models}} \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/C/model_test.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/model_test.mustache similarity index 100% rename from modules/openapi-generator/src/main/resources/C/model_test.mustache rename to modules/openapi-generator/src/main/resources/C-libcurl/model_test.mustache diff --git a/modules/openapi-generator/src/main/resources/C/uncrustify-rules.cfg.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/uncrustify-rules.cfg.mustache similarity index 100% rename from modules/openapi-generator/src/main/resources/C/uncrustify-rules.cfg.mustache rename to modules/openapi-generator/src/main/resources/C-libcurl/uncrustify-rules.cfg.mustache diff --git a/modules/openapi-generator/src/main/resources/C/api.mustache b/modules/openapi-generator/src/main/resources/C/api.mustache deleted file mode 100644 index 2b3a069c0ef2..000000000000 --- a/modules/openapi-generator/src/main/resources/C/api.mustache +++ /dev/null @@ -1,85 +0,0 @@ -#include -#include -#include "apiClient.h" -#include "cJSON.h" -{{#imports}} -#include "{{{.}}}.h" -{{/imports}} - -#define MAX_BUFFER_LENGTH 4096 - -{{#returnType}}{{{.}}}{{/returnType}}{{^returnType}}void{{/returnType}} *{{{classname}}}_{{{operationId}}}(apiClient_t *apiClient{{#allParams}},{{{dataType}}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) { - pet_t *pet; - - - char *petIdString = malloc(MAX_BUFFER_LENGTH); - - snprintf(petIdString, MAX_BUFFER_LENGTH, "%li", petId); - - - - // TODO header parameters - {{#headerParams}} - // header parameter {{paramName}} ({{baseName}}) not yet supported - {{/headerParams}} - - // TODO query parameters - {{#queryParams}} - // query parameter {{paramName}} ({{baseName}}) not yet supported - {{/queryParams}} - - // TODO form parameters - {{#formParams}} - // form parameter {{paramName}} ({{baseName}}) not yet supported - {{/formParams}} - - // http body (model) - {{^bodyParam}} - post_body = nil - {{/bodyParam}} - {{#bodyParam}} - {{paramName}}JSONObject = {{dataType}}_convertToJSON({{paramName}}); - {{/bodyParam}} - - apiClient_invoke(apiClient, - "pet", - petIdString, - NULL); - pet = pet_parseFromJSON(apiClient->dataReceived); - free(apiClient->dataReceived); - if(pet == NULL) { - return 0; - } else { - cJSON *jsonObject = pet_convertToJSON(pet); - cJSON_Delete(jsonObject); - } - - {{#allParams}} - free({{{paramName}}}String); - {{/allParams}} - {{#returnType}} - return {{{returnType}}}; - {{/returnType}} - {{^returnType}} - return; - {{/returnType}} - -} - - -void *petApi_addPet(apiClient_t *apiClient, pet_t *pet) { - cJSON *petJSONObject; - char *petJSONString; - - petJSONObject = pet_convertToJSON(pet); - petJSONString = cJSON_Print(petJSONObject); - apiClient_invoke(apiClient, - "pet", - NULL, - petJSONString); - free(apiClient->dataReceived); - free(petJSONString); - cJSON_Delete(petJSONObject); - - return pet; -} \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/C/apiKey.c.mustache b/modules/openapi-generator/src/main/resources/C/apiKey.c.mustache deleted file mode 100644 index e901bc37c5c7..000000000000 --- a/modules/openapi-generator/src/main/resources/C/apiKey.c.mustache +++ /dev/null @@ -1,14 +0,0 @@ -#include -#include "apiKey.h" - -apiKey_t *apiKey_create(char *key, char *value) { - apiKey_t *apiKey = malloc(sizeof(apiKey_t)); - apiKey->key = key; - apiKey->value = value; - - return apiKey; -} - -void apiKey_free(apiKey_t *apiKey) { - free(apiKey); -} diff --git a/modules/openapi-generator/src/main/resources/C/model.mustache b/modules/openapi-generator/src/main/resources/C/model.mustache deleted file mode 100644 index 4b7580ccf92e..000000000000 --- a/modules/openapi-generator/src/main/resources/C/model.mustache +++ /dev/null @@ -1,62 +0,0 @@ -{{#models}} -{{#model}} -#include -#include -#include -#include "cJSON.h" -{{#imports}} -#include "{{{.}}}h" -{{/imports}} - - -{{classname}}_t *{{classname}}_create( - {{#vars}} - {{{dataType}}} *{{{baseName}}}{{#hasMore}},{{/hasMore}} - {{/vars}} - ) { - {{classname}}_t *{{classname}} = malloc(sizeof({{classname}}_t)); - {{#vars}} - {{classname}}->{{{baseName}}} = {{{baseName}}}; - {{/vars}} - - return {{classname}}; -} - - -void {{classname}}_free({{classname}}_t *{{classname}}) { - listEntry_t *listEntry; - - {{#vars}} - {{^isContainer}} - {{^isPrimitiveType}} - {{{name}}}_free({{{classname}}}->{{{baseName}}}) - {{/isPrimitiveType}} - {{#isPrimitiveType}} - free({{{classname}}}->{{{baseName}}}) - {{/isPrimitiveType}} - {{/isContainer}} - {{#isContainer}} - {{#isListContainer}} - {{#isPrimitiveType}} - list_ForEach(listEntry, {{classname}}->{{baseName}}) { - free(listEntry->data); - } - list_free({{classname}}->{{baseName}}); - {{/isPrimitiveType}} - {{^isPrimitiveType}} - list_ForEach(listEntry, {{classname}}->{{baseName}}) { - {{baseName}}_free(listEntry->data); - } - list_free({{classname}}->{{baseName}}); - {{/isPrimitiveType}} - {{/isListContainer}} - {{#isMapContainer}} - // TODO map/hash not supported at the moment - {{/isMapContainer}} - {{/isContainer}} - - {{/vars}} - free({{classname}}); -} -{{/model}} -{{/models}} \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/META-INF/services/org.openapitools.codegen.CodegenConfig b/modules/openapi-generator/src/main/resources/META-INF/services/org.openapitools.codegen.CodegenConfig index 65fe50ce1e65..040f3699c71e 100644 --- a/modules/openapi-generator/src/main/resources/META-INF/services/org.openapitools.codegen.CodegenConfig +++ b/modules/openapi-generator/src/main/resources/META-INF/services/org.openapitools.codegen.CodegenConfig @@ -5,7 +5,7 @@ org.openapitools.codegen.languages.Apache2ConfigCodegen org.openapitools.codegen.languages.ApexClientCodegen org.openapitools.codegen.languages.AspNetCoreServerCodegen org.openapitools.codegen.languages.BashClientCodegen -org.openapitools.codegen.languages.CClientCodegen +org.openapitools.codegen.languages.CLibcurlClientCodegen org.openapitools.codegen.languages.ClojureClientCodegen org.openapitools.codegen.languages.ConfluenceWikiCodegen org.openapitools.codegen.languages.CppQt5ClientCodegen diff --git a/samples/client/petstore/C/.openapi-generator/VERSION b/samples/client/petstore/C/.openapi-generator/VERSION index dde25ef08e8c..14900cee60e8 100644 --- a/samples/client/petstore/C/.openapi-generator/VERSION +++ b/samples/client/petstore/C/.openapi-generator/VERSION @@ -1 +1 @@ -3.1.1-SNAPSHOT \ No newline at end of file +3.2.1-SNAPSHOT \ No newline at end of file diff --git a/samples/client/petstore/C/api/PetAPI.c b/samples/client/petstore/C/api/PetAPI.c index 420e066276bf..eb54358a175f 100644 --- a/samples/client/petstore/C/api/PetAPI.c +++ b/samples/client/petstore/C/api/PetAPI.c @@ -2,62 +2,330 @@ #include #include "apiClient.h" #include "cJSON.h" -#include "{import=models.api_response}.h" -#include "{import=models.file}.h" -#include "{import=models.pet}.h" +#include "models.api_response.h" // TODO will fix the import later +#include "models.file.h" // TODO will fix the import later +#include "models.pet.h" // TODO will fix the import later #define MAX_BUFFER_LENGTH 4096 -void *PetAPI_(apiClient_t *apiClient) { - pet_t *pet; +// Add a new pet to the store +// +void *PetAPI_addPet(apiClient_t *apiClient, pet Pet) { + list_t *localVarQueryParameters, + list_t *localVarHeaderParameters, + list_t *localVarFormParameters, + char *localVarBodyParameters, + // create the path + char *localVarPath = malloc(MAX_BUFFER_LENGTH); + snprintf(localVarPath, MAX_BUFFER_LENGTH, "/pet"); - char *petIdString = malloc(MAX_BUFFER_LENGTH); - snprintf(petIdString, MAX_BUFFER_LENGTH, "%li", petId); + // JSON HTTP Request - pet + cJSON *PetJSONObject; + PetJSONObject = pet_convertToJSON(Pet); + localVarBodyParameters = cJSON_Print(PetJSONObject); + apiClient_invoke(apiClient, + "pet", + localVarPath, + localVarQueryParameters, + localVarHeaderParameters, + localVarFormParameters, + localVarBodyParameters, + "POST"); + + free(apiClient->dataReceived); + free(localVarBodyParameters); + cJSON_Delete() + return; + +} + +// Deletes a pet +// +void *PetAPI_deletePet(apiClient_t *apiClient, long PetId, char ApiKey) { + list_t *localVarQueryParameters, + list_t *localVarHeaderParameters, + list_t *localVarFormParameters, + char *localVarBodyParameters, + + // create the path + char *localVarPath = malloc(MAX_BUFFER_LENGTH); + snprintf(localVarPath, MAX_BUFFER_LENGTH, "/pet/{petId}"); + + // TODO path parameter PetId (petId) not yet supported + // TODO base path = http://petstore.swagger.io/v2 + replace_str(localVarPath, "{" + "petId" + "}", PetId)// TODO need to revise // TODO header parameters + // header parameter ApiKey (api_key) not yet supported + + apiClient_invoke(apiClient, + "pet", + localVarPath, + localVarQueryParameters, + localVarHeaderParameters, + localVarFormParameters, + localVarBodyParameters, + "DELETE"); + + free(apiClient->dataReceived); + free(PetIdString); + free(ApiKeyString); + return; + +} + +// Finds Pets by status +// +// Multiple status values can be provided with comma separated strings +// +array_t *PetAPI_findPetsByStatus(apiClient_t *apiClient, array Status) { + list_t *localVarQueryParameters, + list_t *localVarHeaderParameters, + list_t *localVarFormParameters, + char *localVarBodyParameters, + + // create the path + char *localVarPath = malloc(MAX_BUFFER_LENGTH); + snprintf(localVarPath, MAX_BUFFER_LENGTH, "/pet/findByStatus"); + + // TODO query parameters + // query parameter Status (status) not yet supported + char *status = malloc(MAX_BUFFER_LENGTH); - // TODO form parameters + apiClient_invoke(apiClient, + "pet", + localVarPath, + localVarQueryParameters, + localVarHeaderParameters, + localVarFormParameters, + localVarBodyParameters, + "GET"); + + free(apiClient->dataReceived); + free(StatusString); + localVararray = _parseFromJSON(apiClient->dataReceived); + if(localVararray == NULL) { + return 0; + } else { + cJSON *jsonObject = _convertToJSON(); + cJSON_Delete(jsonObject); + } + + return localVararray; + +} + +// Finds Pets by tags +// +// Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing. +// +array_t *PetAPI_findPetsByTags(apiClient_t *apiClient, array Tags) { + list_t *localVarQueryParameters, + list_t *localVarHeaderParameters, + list_t *localVarFormParameters, + char *localVarBodyParameters, + + // create the path + char *localVarPath = malloc(MAX_BUFFER_LENGTH); + snprintf(localVarPath, MAX_BUFFER_LENGTH, "/pet/findByTags"); + + + + // TODO query parameters + // query parameter Tags (tags) not yet supported + char *tags = malloc(MAX_BUFFER_LENGTH); + + apiClient_invoke(apiClient, + "pet", + localVarPath, + localVarQueryParameters, + localVarHeaderParameters, + localVarFormParameters, + localVarBodyParameters, + "GET"); + + free(apiClient->dataReceived); + free(TagsString); + localVararray = _parseFromJSON(apiClient->dataReceived); + if(localVararray == NULL) { + return 0; + } else { + cJSON *jsonObject = _convertToJSON(); + cJSON_Delete(jsonObject); + } + + return localVararray; + +} + +// Find pet by ID +// +// Returns a single pet +// +pet_t *PetAPI_getPetById(apiClient_t *apiClient, long PetId) { + list_t *localVarQueryParameters, + list_t *localVarHeaderParameters, + list_t *localVarFormParameters, + char *localVarBodyParameters, + + // create the path + char *localVarPath = malloc(MAX_BUFFER_LENGTH); + snprintf(localVarPath, MAX_BUFFER_LENGTH, "/pet/{petId}"); + + // TODO path parameter PetId (petId) not yet supported + // TODO base path = http://petstore.swagger.io/v2 + replace_str(localVarPath, "{" + "petId" + "}", PetId)// TODO need to revise - // http body (model) - post_body = nil apiClient_invoke(apiClient, "pet", - petIdString, - NULL); - pet = pet_parseFromJSON(apiClient->dataReceived); + localVarPath, + localVarQueryParameters, + localVarHeaderParameters, + localVarFormParameters, + localVarBodyParameters, + "GET"); + free(apiClient->dataReceived); - if(pet == NULL) { - return 0; - } else { - cJSON *jsonObject = pet_convertToJSON(pet); - cJSON_Delete(jsonObject); - } + free(PetIdString); + localVarpet = _parseFromJSON(apiClient->dataReceived); + if(localVarpet == NULL) { + return 0; + } else { + cJSON *jsonObject = _convertToJSON(); + cJSON_Delete(jsonObject); + } + + return localVarpet; + +} + +// Update an existing pet +// +void *PetAPI_updatePet(apiClient_t *apiClient, pet Pet) { + list_t *localVarQueryParameters, + list_t *localVarHeaderParameters, + list_t *localVarFormParameters, + char *localVarBodyParameters, + + // create the path + char *localVarPath = malloc(MAX_BUFFER_LENGTH); + snprintf(localVarPath, MAX_BUFFER_LENGTH, "/pet"); + + + // JSON HTTP Request - pet + cJSON *PetJSONObject; + PetJSONObject = pet_convertToJSON(Pet); + localVarBodyParameters = cJSON_Print(PetJSONObject); + + apiClient_invoke(apiClient, + "pet", + localVarPath, + localVarQueryParameters, + localVarHeaderParameters, + localVarFormParameters, + localVarBodyParameters, + "PUT"); + + free(apiClient->dataReceived); + free(localVarBodyParameters); + cJSON_Delete() return; } +// Updates a pet in the store with form data +// +void *PetAPI_updatePetWithForm(apiClient_t *apiClient, long PetId, char Name, char Status) { + list_t *localVarQueryParameters, + list_t *localVarHeaderParameters, + list_t *localVarFormParameters, + char *localVarBodyParameters, -void *petApi_addPet(apiClient_t *apiClient, pet_t *pet) { - cJSON *petJSONObject; - char *petJSONString; + // create the path + char *localVarPath = malloc(MAX_BUFFER_LENGTH); + snprintf(localVarPath, MAX_BUFFER_LENGTH, "/pet/{petId}"); + + // TODO path parameter PetId (petId) not yet supported + // TODO base path = http://petstore.swagger.io/v2 + replace_str(localVarPath, "{" + "petId" + "}", PetId)// TODO need to revise + + + // TODO form parameters + // form parameter Name (name) not yet supported + + // TODO form parameters + // form parameter Status (status) not yet supported - petJSONObject = pet_convertToJSON(pet); - petJSONString = cJSON_Print(petJSONObject); apiClient_invoke(apiClient, "pet", - NULL, - petJSONString); + localVarPath, + localVarQueryParameters, + localVarHeaderParameters, + localVarFormParameters, + localVarBodyParameters, + "POST"); + free(apiClient->dataReceived); - free(petJSONString); - cJSON_Delete(petJSONObject); + free(PetIdString); + free(NameString); + free(StatusString); + return; + +} + +// uploads an image +// +api_response_t *PetAPI_uploadFile(apiClient_t *apiClient, long PetId, char AdditionalMetadata, File File) { + list_t *localVarQueryParameters, + list_t *localVarHeaderParameters, + list_t *localVarFormParameters, + char *localVarBodyParameters, + + // create the path + char *localVarPath = malloc(MAX_BUFFER_LENGTH); + snprintf(localVarPath, MAX_BUFFER_LENGTH, "/pet/{petId}/uploadImage"); + + // TODO path parameter PetId (petId) not yet supported + // TODO base path = http://petstore.swagger.io/v2 + replace_str(localVarPath, "{" + "petId" + "}", PetId)// TODO need to revise + + + // TODO form parameters + // form parameter AdditionalMetadata (additionalMetadata) not yet supported + + // TODO form parameters + // form parameter File (file) not yet supported + + apiClient_invoke(apiClient, + "pet", + localVarPath, + localVarQueryParameters, + localVarHeaderParameters, + localVarFormParameters, + localVarBodyParameters, + "POST"); + + free(apiClient->dataReceived); + free(PetIdString); + free(AdditionalMetadataString); + free(FileString); + localVarapi_response = _parseFromJSON(apiClient->dataReceived); + if(localVarapi_response == NULL) { + return 0; + } else { + cJSON *jsonObject = _convertToJSON(); + cJSON_Delete(jsonObject); + } + + return localVarapi_response; + +} - return pet; -} \ No newline at end of file diff --git a/samples/client/petstore/C/api/StoreAPI.c b/samples/client/petstore/C/api/StoreAPI.c index afc6e7c36c8b..c938f028c84b 100644 --- a/samples/client/petstore/C/api/StoreAPI.c +++ b/samples/client/petstore/C/api/StoreAPI.c @@ -2,61 +2,165 @@ #include #include "apiClient.h" #include "cJSON.h" -#include "{import=models.map}.h" -#include "{import=models.order}.h" +#include "models.map.h" // TODO will fix the import later +#include "models.order.h" // TODO will fix the import later #define MAX_BUFFER_LENGTH 4096 -void *StoreAPI_(apiClient_t *apiClient) { - pet_t *pet; +// Delete purchase order by ID +// +// For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors +// +void *StoreAPI_deleteOrder(apiClient_t *apiClient, char OrderId) { + list_t *localVarQueryParameters, + list_t *localVarHeaderParameters, + list_t *localVarFormParameters, + char *localVarBodyParameters, + // create the path + char *localVarPath = malloc(MAX_BUFFER_LENGTH); + snprintf(localVarPath, MAX_BUFFER_LENGTH, "/store/order/{orderId}"); - char *petIdString = malloc(MAX_BUFFER_LENGTH); + // TODO path parameter OrderId (orderId) not yet supported + // TODO base path = http://petstore.swagger.io/v2 + replace_str(localVarPath, "{" + "orderId" + "}", OrderId)// TODO need to revise - snprintf(petIdString, MAX_BUFFER_LENGTH, "%li", petId); + apiClient_invoke(apiClient, + "pet", + localVarPath, + localVarQueryParameters, + localVarHeaderParameters, + localVarFormParameters, + localVarBodyParameters, + "DELETE"); + free(apiClient->dataReceived); + free(OrderIdString); + return; - // TODO header parameters +} + +// Returns pet inventories by status +// +// Returns a map of status codes to quantities +// +map_t *StoreAPI_getInventory(apiClient_t *apiClient) { + list_t *localVarQueryParameters, + list_t *localVarHeaderParameters, + list_t *localVarFormParameters, + char *localVarBodyParameters, - // TODO query parameters + // create the path + char *localVarPath = malloc(MAX_BUFFER_LENGTH); + snprintf(localVarPath, MAX_BUFFER_LENGTH, "/store/inventory"); - // TODO form parameters - // http body (model) - post_body = nil apiClient_invoke(apiClient, "pet", - petIdString, - NULL); - pet = pet_parseFromJSON(apiClient->dataReceived); + localVarPath, + localVarQueryParameters, + localVarHeaderParameters, + localVarFormParameters, + localVarBodyParameters, + "GET"); + free(apiClient->dataReceived); - if(pet == NULL) { - return 0; - } else { - cJSON *jsonObject = pet_convertToJSON(pet); - cJSON_Delete(jsonObject); - } + localVarmap = _parseFromJSON(apiClient->dataReceived); + if(localVarmap == NULL) { + return 0; + } else { + cJSON *jsonObject = _convertToJSON(); + cJSON_Delete(jsonObject); + } - return; + return localVarmap; + +} + +// Find purchase order by ID +// +// For valid response try integer IDs with value <= 5 or > 10. Other values will generated exceptions +// +order_t *StoreAPI_getOrderById(apiClient_t *apiClient, long OrderId) { + list_t *localVarQueryParameters, + list_t *localVarHeaderParameters, + list_t *localVarFormParameters, + char *localVarBodyParameters, + + // create the path + char *localVarPath = malloc(MAX_BUFFER_LENGTH); + snprintf(localVarPath, MAX_BUFFER_LENGTH, "/store/order/{orderId}"); + + // TODO path parameter OrderId (orderId) not yet supported + // TODO base path = http://petstore.swagger.io/v2 + replace_str(localVarPath, "{" + "orderId" + "}", OrderId)// TODO need to revise + + + apiClient_invoke(apiClient, + "pet", + localVarPath, + localVarQueryParameters, + localVarHeaderParameters, + localVarFormParameters, + localVarBodyParameters, + "GET"); + + free(apiClient->dataReceived); + free(OrderIdString); + localVarorder = _parseFromJSON(apiClient->dataReceived); + if(localVarorder == NULL) { + return 0; + } else { + cJSON *jsonObject = _convertToJSON(); + cJSON_Delete(jsonObject); + } + + return localVarorder; } +// Place an order for a pet +// +order_t *StoreAPI_placeOrder(apiClient_t *apiClient, order Order) { + list_t *localVarQueryParameters, + list_t *localVarHeaderParameters, + list_t *localVarFormParameters, + char *localVarBodyParameters, -void *petApi_addPet(apiClient_t *apiClient, pet_t *pet) { - cJSON *petJSONObject; - char *petJSONString; + // create the path + char *localVarPath = malloc(MAX_BUFFER_LENGTH); + snprintf(localVarPath, MAX_BUFFER_LENGTH, "/store/order"); + + + + // JSON HTTP Request - order + cJSON *OrderJSONObject; + OrderJSONObject = order_convertToJSON(Order); + localVarBodyParameters = cJSON_Print(OrderJSONObject); - petJSONObject = pet_convertToJSON(pet); - petJSONString = cJSON_Print(petJSONObject); apiClient_invoke(apiClient, "pet", - NULL, - petJSONString); + localVarPath, + localVarQueryParameters, + localVarHeaderParameters, + localVarFormParameters, + localVarBodyParameters, + "POST"); + free(apiClient->dataReceived); - free(petJSONString); - cJSON_Delete(petJSONObject); + free(localVarBodyParameters); + cJSON_Delete() + localVarorder = _parseFromJSON(apiClient->dataReceived); + if(localVarorder == NULL) { + return 0; + } else { + cJSON *jsonObject = _convertToJSON(); + cJSON_Delete(jsonObject); + } + + return localVarorder; + +} - return pet; -} \ No newline at end of file diff --git a/samples/client/petstore/C/api/UserAPI.c b/samples/client/petstore/C/api/UserAPI.c index 4a977ebeeb15..e29839b07af2 100644 --- a/samples/client/petstore/C/api/UserAPI.c +++ b/samples/client/petstore/C/api/UserAPI.c @@ -2,69 +2,305 @@ #include #include "apiClient.h" #include "cJSON.h" -#include "{import=models.array}.h" -#include "{import=models.user}.h" +#include "models.array.h" // TODO will fix the import later +#include "models.user.h" // TODO will fix the import later #define MAX_BUFFER_LENGTH 4096 -void *UserAPI_(apiClient_t *apiClient) { - pet_t *pet; +// Create user +// +// This can only be done by the logged in user. +// +void *UserAPI_createUser(apiClient_t *apiClient, user User) { + list_t *localVarQueryParameters, + list_t *localVarHeaderParameters, + list_t *localVarFormParameters, + char *localVarBodyParameters, + // create the path + char *localVarPath = malloc(MAX_BUFFER_LENGTH); + snprintf(localVarPath, MAX_BUFFER_LENGTH, "/user"); - char *petIdString = malloc(MAX_BUFFER_LENGTH); - snprintf(petIdString, MAX_BUFFER_LENGTH, "%li", petId); + // JSON HTTP Request - user + cJSON *UserJSONObject; + UserJSONObject = user_convertToJSON(User); + localVarBodyParameters = cJSON_Print(UserJSONObject); + apiClient_invoke(apiClient, + "pet", + localVarPath, + localVarQueryParameters, + localVarHeaderParameters, + localVarFormParameters, + localVarBodyParameters, + "POST"); - // TODO header parameters + free(apiClient->dataReceived); + free(localVarBodyParameters); + cJSON_Delete() + return; - // TODO query parameters +} + +// Creates list of users with given input array +// +void *UserAPI_createUsersWithArrayInput(apiClient_t *apiClient, array User) { + list_t *localVarQueryParameters, + list_t *localVarHeaderParameters, + list_t *localVarFormParameters, + char *localVarBodyParameters, + + // create the path + char *localVarPath = malloc(MAX_BUFFER_LENGTH); + snprintf(localVarPath, MAX_BUFFER_LENGTH, "/user/createWithArray"); - // TODO form parameters - // http body (model) - post_body = nil + + // JSON HTTP Request - user + cJSON *UserJSONObject; + UserJSONObject = array_convertToJSON(User); + localVarBodyParameters = cJSON_Print(UserJSONObject); apiClient_invoke(apiClient, "pet", - petIdString, - NULL, - NULL, - NULL, - NULL, - NULL); - pet = pet_parseFromJSON(apiClient->dataReceived); - free(apiClient->dataReceived); - if(pet == NULL) { - return 0; - } else { - cJSON *jsonObject = pet_convertToJSON(pet); - cJSON_Delete(jsonObject); - } + localVarPath, + localVarQueryParameters, + localVarHeaderParameters, + localVarFormParameters, + localVarBodyParameters, + "POST"); + free(apiClient->dataReceived); + free(localVarBodyParameters); + cJSON_Delete() return; } +// Creates list of users with given input array +// +void *UserAPI_createUsersWithListInput(apiClient_t *apiClient, array User) { + list_t *localVarQueryParameters, + list_t *localVarHeaderParameters, + list_t *localVarFormParameters, + char *localVarBodyParameters, + + // create the path + char *localVarPath = malloc(MAX_BUFFER_LENGTH); + snprintf(localVarPath, MAX_BUFFER_LENGTH, "/user/createWithList"); -void *petApi_addPet(apiClient_t *apiClient, pet_t *pet) { - cJSON *petJSONObject; - char *petJSONString; - petJSONObject = pet_convertToJSON(pet); - petJSONString = cJSON_Print(petJSONObject); + + // JSON HTTP Request - user + cJSON *UserJSONObject; + UserJSONObject = array_convertToJSON(User); + localVarBodyParameters = cJSON_Print(UserJSONObject); + apiClient_invoke(apiClient, "pet", - NULL, - NULL, - NULL, - NULL, - petJSONString, + localVarPath, + localVarQueryParameters, + localVarHeaderParameters, + localVarFormParameters, + localVarBodyParameters, "POST"); + free(apiClient->dataReceived); - free(petJSONString); - cJSON_Delete(petJSONObject); + free(localVarBodyParameters); + cJSON_Delete() + return; + +} + +// Delete user +// +// This can only be done by the logged in user. +// +void *UserAPI_deleteUser(apiClient_t *apiClient, char Username) { + list_t *localVarQueryParameters, + list_t *localVarHeaderParameters, + list_t *localVarFormParameters, + char *localVarBodyParameters, + + // create the path + char *localVarPath = malloc(MAX_BUFFER_LENGTH); + snprintf(localVarPath, MAX_BUFFER_LENGTH, "/user/{username}"); + + // TODO path parameter Username (username) not yet supported + // TODO base path = http://petstore.swagger.io/v2 + replace_str(localVarPath, "{" + "username" + "}", Username)// TODO need to revise + + + apiClient_invoke(apiClient, + "pet", + localVarPath, + localVarQueryParameters, + localVarHeaderParameters, + localVarFormParameters, + localVarBodyParameters, + "DELETE"); + + free(apiClient->dataReceived); + free(UsernameString); + return; + +} + +// Get user by user name +// +user_t *UserAPI_getUserByName(apiClient_t *apiClient, char Username) { + list_t *localVarQueryParameters, + list_t *localVarHeaderParameters, + list_t *localVarFormParameters, + char *localVarBodyParameters, + + // create the path + char *localVarPath = malloc(MAX_BUFFER_LENGTH); + snprintf(localVarPath, MAX_BUFFER_LENGTH, "/user/{username}"); + + // TODO path parameter Username (username) not yet supported + // TODO base path = http://petstore.swagger.io/v2 + replace_str(localVarPath, "{" + "username" + "}", Username)// TODO need to revise + + + apiClient_invoke(apiClient, + "pet", + localVarPath, + localVarQueryParameters, + localVarHeaderParameters, + localVarFormParameters, + localVarBodyParameters, + "GET"); + + free(apiClient->dataReceived); + free(UsernameString); + localVaruser = _parseFromJSON(apiClient->dataReceived); + if(localVaruser == NULL) { + return 0; + } else { + cJSON *jsonObject = _convertToJSON(); + cJSON_Delete(jsonObject); + } + + return localVaruser; + +} + +// Logs user into the system +// +char_t *UserAPI_loginUser(apiClient_t *apiClient, char Username, char Password) { + list_t *localVarQueryParameters, + list_t *localVarHeaderParameters, + list_t *localVarFormParameters, + char *localVarBodyParameters, + + // create the path + char *localVarPath = malloc(MAX_BUFFER_LENGTH); + snprintf(localVarPath, MAX_BUFFER_LENGTH, "/user/login"); + + + + // TODO query parameters + // query parameter Username (username) not yet supported + char *username = malloc(MAX_BUFFER_LENGTH); + + // TODO query parameters + // query parameter Password (password) not yet supported + char *password = malloc(MAX_BUFFER_LENGTH); + + apiClient_invoke(apiClient, + "pet", + localVarPath, + localVarQueryParameters, + localVarHeaderParameters, + localVarFormParameters, + localVarBodyParameters, + "GET"); + + free(apiClient->dataReceived); + free(UsernameString); + free(PasswordString); + localVarchar = _parseFromJSON(apiClient->dataReceived); + if(localVarchar == NULL) { + return 0; + } else { + cJSON *jsonObject = _convertToJSON(); + cJSON_Delete(jsonObject); + } + + return localVarchar; + +} + +// Logs out current logged in user session +// +void *UserAPI_logoutUser(apiClient_t *apiClient) { + list_t *localVarQueryParameters, + list_t *localVarHeaderParameters, + list_t *localVarFormParameters, + char *localVarBodyParameters, + + // create the path + char *localVarPath = malloc(MAX_BUFFER_LENGTH); + snprintf(localVarPath, MAX_BUFFER_LENGTH, "/user/logout"); + + + + apiClient_invoke(apiClient, + "pet", + localVarPath, + localVarQueryParameters, + localVarHeaderParameters, + localVarFormParameters, + localVarBodyParameters, + "GET"); + + free(apiClient->dataReceived); + return; + +} + +// Updated user +// +// This can only be done by the logged in user. +// +void *UserAPI_updateUser(apiClient_t *apiClient, char Username, user User) { + list_t *localVarQueryParameters, + list_t *localVarHeaderParameters, + list_t *localVarFormParameters, + char *localVarBodyParameters, + + // create the path + char *localVarPath = malloc(MAX_BUFFER_LENGTH); + snprintf(localVarPath, MAX_BUFFER_LENGTH, "/user/{username}"); + + // TODO path parameter Username (username) not yet supported + // TODO base path = http://petstore.swagger.io/v2 + replace_str(localVarPath, "{" + "username" + "}", Username)// TODO need to revise + + + // JSON HTTP Request - user + cJSON *UserJSONObject; + UserJSONObject = user_convertToJSON(User); + localVarBodyParameters = cJSON_Print(UserJSONObject); + + apiClient_invoke(apiClient, + "pet", + localVarPath, + localVarQueryParameters, + localVarHeaderParameters, + localVarFormParameters, + localVarBodyParameters, + "PUT"); + + free(apiClient->dataReceived); + free(localVarBodyParameters); + cJSON_Delete() + free(localVarBodyParameters); + cJSON_Delete() + return; + +} - return pet; -} \ No newline at end of file diff --git a/samples/client/petstore/C/model/api_response.c b/samples/client/petstore/C/model/api_response.c index 54f65712fb3d..014a76506afb 100644 --- a/samples/client/petstore/C/model/api_response.c +++ b/samples/client/petstore/C/model/api_response.c @@ -19,9 +19,6 @@ api_response_t *api_response_create( void api_response_free(api_response_t *api_response) { - listEntry_t *listEntry; - - free(api_response->code) free(api_response->type) @@ -29,3 +26,27 @@ void api_response_free(api_response_t *api_response) { free(api_response); } + +cJSON *api_response_convertToJSON(api_response_t *api_response) { + cJSON *item = cJSON_CreateObject(); + // api_response->code + if(cJSON_AddNumberToObject(item, "code", api_response->code) == NULL) { + goto fail; + } + + + // api_response->type + if(cJSON_AddStringToObject(item, "type", api_response->type) == NULL) { + goto fail; + } + + // api_response->message + if(cJSON_AddStringToObject(item, "message", api_response->message) == NULL) { + goto fail; + } + + return item; +fail: + cJSON_Delete(item); + return NULL; +} diff --git a/samples/client/petstore/C/model/category.c b/samples/client/petstore/C/model/category.c index 79828abaca15..4f2c864b6ccb 100644 --- a/samples/client/petstore/C/model/category.c +++ b/samples/client/petstore/C/model/category.c @@ -17,11 +17,27 @@ category_t *category_create( void category_free(category_t *category) { - listEntry_t *listEntry; - - free(category->id) free(category->name) free(category); } + +cJSON *category_convertToJSON(category_t *category) { + cJSON *item = cJSON_CreateObject(); + // category->id + if(cJSON_AddNumberToObject(item, "id", category->id) == NULL) { + goto fail; + } + + + // category->name + if(cJSON_AddStringToObject(item, "name", category->name) == NULL) { + goto fail; + } + + return item; +fail: + cJSON_Delete(item); + return NULL; +} diff --git a/samples/client/petstore/C/model/order.c b/samples/client/petstore/C/model/order.c index 1caaccf6cf5a..ade7d93ebfae 100644 --- a/samples/client/petstore/C/model/order.c +++ b/samples/client/petstore/C/model/order.c @@ -25,19 +25,47 @@ order_t *order_create( void order_free(order_t *order) { - listEntry_t *listEntry; - free(order->id) - free(order->petId) - free(order->quantity) - - free(order->shipDate) free(order->status) - free(order->complete) free(order); } + +cJSON *order_convertToJSON(order_t *order) { + cJSON *item = cJSON_CreateObject(); + // order->id + if(cJSON_AddNumberToObject(item, "id", order->id) == NULL) { + goto fail; + } + + + // order->petId + if(cJSON_AddNumberToObject(item, "petId", order->petId) == NULL) { + goto fail; + } + + + // order->quantity + if(cJSON_AddNumberToObject(item, "quantity", order->quantity) == NULL) { + goto fail; + } + + + // order->shipDate + + // order->status + if(cJSON_AddStringToObject(item, "status", order->status) == NULL) { + goto fail; + } + + // order->complete + + return item; +fail: + cJSON_Delete(item); + return NULL; +} diff --git a/samples/client/petstore/C/model/pet.c b/samples/client/petstore/C/model/pet.c index 1bd0d555f780..ceff1a9c1ecf 100644 --- a/samples/client/petstore/C/model/pet.c +++ b/samples/client/petstore/C/model/pet.c @@ -28,21 +28,19 @@ pet_t *pet_create( void pet_free(pet_t *pet) { - listEntry_t *listEntry; - - free(pet->id) category_free(pet->category) free(pet->name) - list_ForEach(listEntry, pet->photoUrls) { - free(listEntry->data); + listEntry_t *photoUrlsListEntry; + list_ForEach(photoUrlsListEntry, pet->photoUrls) { + free(photoUrlsListEntry->data); } list_free(pet->photoUrls); - list_ForEach(listEntry, pet->tags) { - tags_free(listEntry->data); + list_ForEach(tagsListEntry, pet->tags) { + tags_free(tagsListEntry->data); } list_free(pet->tags); @@ -50,3 +48,65 @@ void pet_free(pet_t *pet) { free(pet); } + +cJSON *pet_convertToJSON(pet_t *pet) { + cJSON *item = cJSON_CreateObject(); + // pet->id + if(cJSON_AddNumberToObject(item, "id", pet->id) == NULL) { + goto fail; + } + + + // pet->category + if(cJSON_AddStringToObject(item, "category", pet->category) == NULL) { + goto fail; + } + + // pet->name + if(cJSON_AddStringToObject(item, "name", pet->name) == NULL) { + goto fail; + } + + // pet->photoUrls + list_ForEach(listEntry, pet->photoUrls) { + free(listEntry->data); + } + + cJSON *photoUrls = cJSON_AddArrayToObject(item, "photoUrls"); + if(photoUrls == NULL) { + goto fail; + } + + listEntry_t *photoUrlsListEntry; + list_ForEach(photoUrlsListEntry, pet->photoUrls) { + if(cJSON_AddStringToObject(photoUrls, "", photoUrlsListEntry->data) == NULL) + { + goto fail; + } + } + + // pet->tags + cJSON *tags = cJSON_AddArrayToObject(item, "tags"); + if(tags == NULL) { + goto fail; + } + + listEntry_t *tagsListEntry; + list_ForEach(tagsListEntry, pet->tags) { + cJSON *item = tag_convertToJSON(tagsListEntry->data); + if(item == NULL) { + goto fail; + } + cJSON_AddItemToArray(tags, item); + } + + // pet->status + if(cJSON_AddStringToObject(item, "status", pet->status) == NULL) { + goto fail; + } + + return item; +fail: + cJSON_Delete(item); + return NULL; +} diff --git a/samples/client/petstore/C/model/tag.c b/samples/client/petstore/C/model/tag.c index 184b05a525a1..b32ada152cc9 100644 --- a/samples/client/petstore/C/model/tag.c +++ b/samples/client/petstore/C/model/tag.c @@ -17,11 +17,27 @@ tag_t *tag_create( void tag_free(tag_t *tag) { - listEntry_t *listEntry; - - free(tag->id) free(tag->name) free(tag); } + +cJSON *tag_convertToJSON(tag_t *tag) { + cJSON *item = cJSON_CreateObject(); + // tag->id + if(cJSON_AddNumberToObject(item, "id", tag->id) == NULL) { + goto fail; + } + + + // tag->name + if(cJSON_AddStringToObject(item, "name", tag->name) == NULL) { + goto fail; + } + + return item; +fail: + cJSON_Delete(item); + return NULL; +} diff --git a/samples/client/petstore/C/model/user.c b/samples/client/petstore/C/model/user.c index 540fa2f7bfe2..0d69f2d732eb 100644 --- a/samples/client/petstore/C/model/user.c +++ b/samples/client/petstore/C/model/user.c @@ -29,9 +29,6 @@ user_t *user_create( void user_free(user_t *user) { - listEntry_t *listEntry; - - free(user->id) free(user->username) @@ -45,7 +42,56 @@ void user_free(user_t *user) { free(user->phone) - free(user->userStatus) free(user); } + +cJSON *user_convertToJSON(user_t *user) { + cJSON *item = cJSON_CreateObject(); + // user->id + if(cJSON_AddNumberToObject(item, "id", user->id) == NULL) { + goto fail; + } + + + // user->username + if(cJSON_AddStringToObject(item, "username", user->username) == NULL) { + goto fail; + } + + // user->firstName + if(cJSON_AddStringToObject(item, "firstName", user->firstName) == NULL) { + goto fail; + } + + // user->lastName + if(cJSON_AddStringToObject(item, "lastName", user->lastName) == NULL) { + goto fail; + } + + // user->email + if(cJSON_AddStringToObject(item, "email", user->email) == NULL) { + goto fail; + } + + // user->password + if(cJSON_AddStringToObject(item, "password", user->password) == NULL) { + goto fail; + } + + // user->phone + if(cJSON_AddStringToObject(item, "phone", user->phone) == NULL) { + goto fail; + } + + // user->userStatus + if(cJSON_AddNumberToObject(item, "userStatus", user->userStatus) == NULL) { + goto fail; + } + + + return item; +fail: + cJSON_Delete(item); + return NULL; +} diff --git a/samples/client/petstore/c/include/apiClient.h b/samples/client/petstore/c/include/apiClient.h index 873f401bfdd5..7b493a422865 100644 --- a/samples/client/petstore/c/include/apiClient.h +++ b/samples/client/petstore/c/include/apiClient.h @@ -29,4 +29,4 @@ apiClient_t* apiClient_create(); void apiClient_free(apiClient_t *apiClient); void apiClient_invoke(apiClient_t *apiClient, char* operationName, char* operationParameter, list_t *queryParameters, list_t *headerParameters, list_t *formParameters, char *bodyParameters, char *requestType); -#endif // INCLUDE_API_CLIENT_H +#endif // INCLUDE_API_CLIENT_H \ No newline at end of file diff --git a/samples/client/petstore/c/include/keyValuePair.h b/samples/client/petstore/c/include/keyValuePair.h deleted file mode 100644 index bcc212d82d1a..000000000000 --- a/samples/client/petstore/c/include/keyValuePair.h +++ /dev/null @@ -1,7 +0,0 @@ -typedef struct keyValuePair_t { - char* key; - char* value; -} keyValuePair_t; - -keyValuePair_t *keyValuePair_create(char *key, char *value); -void keyValuePair_free(keyValuePair_t *keyValuePair); \ No newline at end of file diff --git a/samples/client/petstore/c/src/apiClient.c b/samples/client/petstore/c/src/apiClient.c index 99466761f45b..58a593e28f27 100644 --- a/samples/client/petstore/c/src/apiClient.c +++ b/samples/client/petstore/c/src/apiClient.c @@ -274,4 +274,4 @@ size_t writeDataCallback(void *buffer, size_t size, size_t nmemb, void *userp) { *(char **) userp = strdup(buffer); return size * nmemb; -} +} \ No newline at end of file diff --git a/samples/client/petstore/c/src/apiKey.c b/samples/client/petstore/c/src/apiKey.c index abb58b23eb94..f62311304872 100644 --- a/samples/client/petstore/c/src/apiKey.c +++ b/samples/client/petstore/c/src/apiKey.c @@ -9,12 +9,6 @@ keyValuePair_t *keyValuePair_create(char *key, char *value) { return keyValuePair; } -<<<<<<< HEAD -void apiKey_free(apiKey_t *apiKey) { - free(apiKey); -} -======= void keyValuePair_free(keyValuePair_t *keyValuePair) { free(keyValuePair); -} ->>>>>>> origin/master +} \ No newline at end of file diff --git a/samples/client/petstore/c/src/list.c b/samples/client/petstore/c/src/list.c index bc32c43e92f2..0d680d48ee4d 100644 --- a/samples/client/petstore/c/src/list.c +++ b/samples/client/petstore/c/src/list.c @@ -166,4 +166,4 @@ listEntry_t *list_getElementAt(list_t *list, long indexOfElement) { return currentListEntry; } -} +} \ No newline at end of file diff --git a/samples/client/petstore/c/unit-tests/apiKey.c b/samples/client/petstore/c/unit-tests/apiKey.c index 3ad631d5415e..f62311304872 100644 --- a/samples/client/petstore/c/unit-tests/apiKey.c +++ b/samples/client/petstore/c/unit-tests/apiKey.c @@ -1,23 +1,14 @@ -<<<<<<< HEAD #include -#include "apiKey.h" +#include "keyValuePair.h" -apiKey_t *apiKey_create(char *key, char *value) { - apiKey_t *apiKey = malloc(sizeof(apiKey_t)); - apiKey->key = key; - apiKey->value = value; +keyValuePair_t *keyValuePair_create(char *key, char *value) { + keyValuePair_t *keyValuePair = malloc(sizeof(keyValuePair_t)); + keyValuePair->key = key; + keyValuePair->value = value; - return apiKey; + return keyValuePair; } -void apiKey_free(apiKey_t *apiKey) { - free(apiKey); -} -======= -#include "keyValuePair.h" - -int main() { - keyValuePair_t *keyValuePair = keyValuePair_create("key", "value"); - keyValuePair_free(keyValuePair); -} ->>>>>>> origin/master +void keyValuePair_free(keyValuePair_t *keyValuePair) { + free(keyValuePair); +} \ No newline at end of file diff --git a/samples/client/petstore/c/unit-tests/test-api-client-parameters.c b/samples/client/petstore/c/unit-tests/test-api-client-parameters.c deleted file mode 100644 index 869989d7dc20..000000000000 --- a/samples/client/petstore/c/unit-tests/test-api-client-parameters.c +++ /dev/null @@ -1,68 +0,0 @@ -#include -#include -#include -#include -#include "apiClient.h" -#include "list.h" -#include "keyValuePair.h" - -#define EXAMPLE_BASE_PATH "localhost" -#define EXAMPLE_OPERATION_NAME "pets" -#define OPERATION_PARAMETER "5" -#define EXAMPLE_KEY_1 "skin color" -#define EXAMPLE_VALUE_1 "red" -#define EXAMPLE_KEY_2 "legs" -#define EXAMPLE_VALUE_2 "4" - -#define OUTPUT_URL_1 "localhost/pets/5?skin+color=red" -#define OUTPUT_URL_2 "localhost/pets/5?skin+color=red&legs=4" - -char *assembleTargetUrl(char *basePath, - char *operationName, - char *operationParameter, - list_t *queryParameters); - -int main() { - char *keyOne = malloc(strlen(EXAMPLE_KEY_1) + 1); - char *valueOne = malloc(strlen(EXAMPLE_VALUE_1) + 1); - - strcpy(keyOne, EXAMPLE_KEY_1); - strcpy(valueOne, EXAMPLE_VALUE_1); - - keyValuePair_t *keyValuePairOne = keyValuePair_create(keyOne, valueOne); - list_t *list = list_create(); - list_addElement(list, keyValuePairOne); - - char *exampleUrlOne = assembleTargetUrl(EXAMPLE_BASE_PATH, - EXAMPLE_OPERATION_NAME, - OPERATION_PARAMETER, - list); - - assert(strcmp(exampleUrlOne, OUTPUT_URL_1) == 0); - - char *keyTwo = malloc(strlen(EXAMPLE_KEY_2) + 1); - char *valueTwo = malloc(strlen(EXAMPLE_VALUE_2) + 1); - - strcpy(keyTwo, EXAMPLE_KEY_2); - strcpy(valueTwo, EXAMPLE_VALUE_2); - - keyValuePair_t *keyValuePairTwo = keyValuePair_create(keyTwo, valueTwo); - list_addElement(list, keyValuePairTwo); - - char *exampleUrlTwo = assembleTargetUrl(EXAMPLE_BASE_PATH, - EXAMPLE_OPERATION_NAME, - OPERATION_PARAMETER, - list); - - assert(strcmp(exampleUrlTwo, OUTPUT_URL_2) == 0); - - free(keyOne); - free(keyTwo); - free(valueOne); - free(valueTwo); - free(exampleUrlOne); - free(exampleUrlTwo); - keyValuePair_free(keyValuePairOne); - keyValuePair_free(keyValuePairTwo); - list_free(list); -} \ No newline at end of file diff --git a/samples/client/petstore/c/unit-tests/test-api-client.c b/samples/client/petstore/c/unit-tests/test-api-client.c deleted file mode 100644 index c63dbf358f0c..000000000000 --- a/samples/client/petstore/c/unit-tests/test-api-client.c +++ /dev/null @@ -1,87 +0,0 @@ -#include -#include -#include "apiClient.h" -#include "cJSON.h" -#include "pet.h" -#include "list.h" -#include "keyValuePair.h" -#ifdef DEBUG -#include -#endif // DEBUG - - -#define EXAMPLE_OPERATION_NAME "pet" -#define EXAMPLE_OPERATION_PARAMETER "5" -#define EXAMPLE_KEYNAME_1 "MyExampleKey" -#define EXAMPLE_VALUENAME_1 "MyExampleValue" -#define EXAMPLE_KEYNAME_2 "MyExampleKeyTwo" -#define EXAMPLE_VALUENAME_2 "MyExampleValueTwo" - -int main() { - apiClient_t *apiClient = apiClient_create(); - #ifdef OAUTH2 - apiClient->accessToken = "thisIsMyExampleAccessToken"; - #endif // OAUTH2 - #ifdef API_KEY - apiClient->apiKeys = list_create(); - keyValuePair_t *apiKey = apiKey_create("X-API-Key", "abcdef12345"); - list_addElement(apiClient->apiKeys, apiKey); - #endif // API_KEY - - list_t *customHeaderFields = list_create(); - char *keyOne = malloc(strlen(EXAMPLE_KEYNAME_1) + 1); - char *valueOne = malloc(strlen(EXAMPLE_VALUENAME_1) + 1); - strcpy(keyOne, EXAMPLE_KEYNAME_1); - strcpy(valueOne, EXAMPLE_VALUENAME_1); - - char *keyTwo = malloc(strlen(EXAMPLE_KEYNAME_2) + 1); - char *valueTwo = malloc(strlen(EXAMPLE_VALUENAME_2) + 1); - strcpy(keyTwo, EXAMPLE_KEYNAME_2); - strcpy(valueTwo, EXAMPLE_VALUENAME_2); - - keyValuePair_t *firstCustomField = - keyValuePair_create(keyOne, valueOne); - keyValuePair_t *secondCustomField = - keyValuePair_create(keyTwo, valueTwo); - - list_addElement(customHeaderFields, firstCustomField); - list_addElement(customHeaderFields, secondCustomField); - - apiClient_invoke(apiClient, - EXAMPLE_OPERATION_NAME, - EXAMPLE_OPERATION_PARAMETER, - NULL, - customHeaderFields, - NULL, - NULL, - NULL); - pet_t *pet = pet_parseFromJSON(apiClient->dataReceived); - if(pet == NULL) { - free(apiClient); - return 0; - } else { - cJSON *petJSONObject = pet_convertToJSON(pet); - - #ifdef DEBUG - char *jsonString = cJSON_Print(petJSONObject); - puts(jsonString); - free(jsonString); - #endif - cJSON_Delete(petJSONObject); - } - free(apiClient->dataReceived); - - #ifdef API_KEY - free(apiKey); - list_free(apiClient->apiKeys); - #endif // API_KEY - free(keyOne); - free(valueOne); - free(keyTwo); - free(valueTwo); - keyValuePair_free(firstCustomField); - keyValuePair_free(secondCustomField); - list_free(customHeaderFields); - apiClient_free(apiClient); - pet_free(pet); -} \ No newline at end of file