Add withXml option for Go language (#7566)

* Added support for application/xml content-type for GO language
Issue #7463

* Added test scripts for Go lang "withXml" feature

* Added samples for Go land "withXml" feature.

* "withXml" feature for Go language is only available for client.
This commit is contained in:
Alexander Ovchinnikov
2018-02-08 08:47:47 +02:00
committed by William Cheng
parent 8668175879
commit ee561fcd63
102 changed files with 6928 additions and 3 deletions

View File

@@ -21,6 +21,8 @@ public abstract class AbstractGoCodegen extends DefaultCodegen implements Codege
protected static Logger LOGGER = LoggerFactory.getLogger(AbstractGoCodegen.class);
protected boolean withXml = false;
protected String packageName = "swagger";
public AbstractGoCodegen() {
@@ -84,7 +86,6 @@ public abstract class AbstractGoCodegen extends DefaultCodegen implements Codege
.defaultValue("swagger"));
cliOptions.add(new CliOption(CodegenConstants.HIDE_GENERATION_TIMESTAMP, "hides the timestamp when files were generated")
.defaultValue(Boolean.TRUE.toString()));
}
/**
@@ -305,10 +306,12 @@ public abstract class AbstractGoCodegen extends DefaultCodegen implements Codege
iterator.remove();
}
// if their is a return type, import encoding/json
// if their is a return type, import encoding/json and if needed encoding/xml
for (CodegenOperation operation : operations) {
if(operation.returnBaseType != null ) {
imports.add(createMapping("import", "encoding/json"));
if (withXml)
imports.add(createMapping("import", "encoding/xml"));
break; //just need to import once
}
}
@@ -470,4 +473,7 @@ public abstract class AbstractGoCodegen extends DefaultCodegen implements Codege
}
}
public void setWithXml(boolean withXml) {
this.withXml = withXml;
}
}

View File

@@ -19,6 +19,7 @@ public class GoClientCodegen extends AbstractGoCodegen {
protected String packageVersion = "1.0.0";
protected String apiDocPath = "docs/";
protected String modelDocPath = "docs/";
public static final String WITH_XML = "withXml";
public GoClientCodegen() {
super();
@@ -49,6 +50,7 @@ public class GoClientCodegen extends AbstractGoCodegen {
cliOptions.add(new CliOption(CodegenConstants.PACKAGE_VERSION, "Go package version.")
.defaultValue("1.0.0"));
cliOptions.add(CliOption.newBoolean(WITH_XML, "whether to include support for application/xml content type and include XML annotations in the model (works with libraries that provide support for JSON and XML)"));
}
@@ -95,6 +97,13 @@ public class GoClientCodegen extends AbstractGoCodegen {
supportingFiles.add(new SupportingFile("client.mustache", "", "client.go"));
supportingFiles.add(new SupportingFile("response.mustache", "", "response.go"));
supportingFiles.add(new SupportingFile(".travis.yml", "", ".travis.yml"));
if(additionalProperties.containsKey(WITH_XML)) {
setWithXml(Boolean.parseBoolean(additionalProperties.get(WITH_XML).toString()));
if ( withXml ) {
additionalProperties.put(WITH_XML, "true");
}
}
}
/**

View File

@@ -215,6 +215,16 @@ func (a *{{{classname}}}Service) {{{nickname}}}(ctx context.Context{{#hasParams}
return {{#returnType}}successPayload, {{/returnType}}localVarHttpResponse, reportError("Status: %v, Body: %s", localVarHttpResponse.Status, bodyBytes)
}
{{#returnType}}
{{#withXml}}
contentType := localVarHttpResponse.Header.Get("content-type")
if strings.Contains(contentType, "application/xml") {
if err = xml.NewDecoder(localVarHttpResponse.Body).Decode(&successPayload); err != nil {
return successPayload, localVarHttpResponse, err
}
return successPayload, localVarHttpResponse, err
}
{{/withXml}}
if err = json.NewDecoder(localVarHttpResponse.Body).Decode(&successPayload); err != nil {
return {{#returnType}}successPayload, {{/returnType}}localVarHttpResponse, err

View File

@@ -27,6 +27,6 @@ type {{classname}} struct {
{{#description}}
// {{{description}}}
{{/description}}
{{name}} {{^isEnum}}{{^isPrimitiveType}}{{^isContainer}}{{^isDateTime}}*{{/isDateTime}}{{/isContainer}}{{/isPrimitiveType}}{{/isEnum}}{{{datatype}}} `json:"{{baseName}}{{^required}},omitempty{{/required}}"`
{{name}} {{^isEnum}}{{^isPrimitiveType}}{{^isContainer}}{{^isDateTime}}*{{/isDateTime}}{{/isContainer}}{{/isPrimitiveType}}{{/isEnum}}{{{datatype}}} `json:"{{baseName}}{{^required}},omitempty{{/required}}"{{#withXml}} xml:"{{baseName}}"{{/withXml}}`
{{/vars}}
}{{/isEnum}}{{/model}}{{/models}}

View File

@@ -30,6 +30,8 @@ public class GoClientOptionsTest extends AbstractOptionsTest {
times = 1;
clientCodegen.setPackageName(GoClientOptionsProvider.PACKAGE_NAME_VALUE);
times = 1;
clientCodegen.setWithXml(GoClientOptionsProvider.WITH_XML_VALUE);
times = 1;
}};
}
}

View File

@@ -11,6 +11,7 @@ public class GoClientOptionsProvider implements OptionsProvider {
public static final String PACKAGE_VERSION_VALUE = "1.0.0";
public static final String PACKAGE_NAME_VALUE = "Go";
public static final boolean WITH_XML_VALUE = true;
@Override
public String getLanguage() {
@@ -24,6 +25,7 @@ public class GoClientOptionsProvider implements OptionsProvider {
.put(CodegenConstants.PACKAGE_VERSION, PACKAGE_VERSION_VALUE)
.put(CodegenConstants.PACKAGE_NAME, PACKAGE_NAME_VALUE)
.put(CodegenConstants.HIDE_GENERATION_TIMESTAMP, "true")
.put(CodegenConstants.WITH_XML, "true")
.build();
}