Skip to content

Commit

Permalink
more fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
wing328 committed Aug 8, 2018
1 parent 085f917 commit 21df220
Show file tree
Hide file tree
Showing 40 changed files with 1,331 additions and 660 deletions.
2 changes: 1 addition & 1 deletion bin/c-petstore.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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()));
Expand All @@ -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);
Expand Down Expand Up @@ -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).";
}


Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#include <stdlib.h>
#include <stdio.h>
#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}}
Loading

0 comments on commit 21df220

Please sign in to comment.