Skip to content

Commit ee561fc

Browse files
mr1sunshinewing328
authored andcommitted
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.
1 parent 8668175 commit ee561fc

File tree

102 files changed

+6928
-3
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

102 files changed

+6928
-3
lines changed

bin/go-petstore-withxml.sh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/bin/sh
2+
3+
SCRIPT="$0"
4+
5+
while [ -h "$SCRIPT" ] ; do
6+
ls=`ls -ld "$SCRIPT"`
7+
link=`expr "$ls" : '.*-> \(.*\)$'`
8+
if expr "$link" : '/.*' > /dev/null; then
9+
SCRIPT="$link"
10+
else
11+
SCRIPT=`dirname "$SCRIPT"`/"$link"
12+
fi
13+
done
14+
15+
if [ ! -d "${APP_DIR}" ]; then
16+
APP_DIR=`dirname "$SCRIPT"`/..
17+
APP_DIR=`cd "${APP_DIR}"; pwd`
18+
fi
19+
20+
executable="./modules/swagger-codegen-cli/target/swagger-codegen-cli.jar"
21+
22+
if [ ! -f "$executable" ]
23+
then
24+
mvn clean package
25+
fi
26+
27+
# if you've executed sbt assembly previously it will use that instead.
28+
export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties"
29+
ags="$@ generate -t modules/swagger-codegen/src/main/resources/go -i modules/swagger-codegen/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml -l go -o samples/client/petstore/go/go-petstore-withXml -DpackageName=petstore,withXml=true "
30+
31+
java $JAVA_OPTS -jar $executable $ags

bin/windows/go-petstore-withxml.bat

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
set executable=.\modules\swagger-codegen-cli\target\swagger-codegen-cli.jar
2+
3+
If Not Exist %executable% (
4+
mvn clean package
5+
)
6+
7+
REM set JAVA_OPTS=%JAVA_OPTS% -Xmx1024M -DloggerPath=conf/log4j.properties
8+
set ags=generate -t modules\swagger-codegen\src\main\resources\go -i modules\swagger-codegen\src\test\resources\2_0\petstore-with-fake-endpoints-models-for-testing.yaml -l go -o samples\client\petstore\go\go-petstore-withXml -DpackageName=petstore,withXml=true
9+
10+
java %JAVA_OPTS% -jar %executable% %ags%

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AbstractGoCodegen.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ public abstract class AbstractGoCodegen extends DefaultCodegen implements Codege
2121

2222
protected static Logger LOGGER = LoggerFactory.getLogger(AbstractGoCodegen.class);
2323

24+
protected boolean withXml = false;
25+
2426
protected String packageName = "swagger";
2527

2628
public AbstractGoCodegen() {
@@ -84,7 +86,6 @@ public AbstractGoCodegen() {
8486
.defaultValue("swagger"));
8587
cliOptions.add(new CliOption(CodegenConstants.HIDE_GENERATION_TIMESTAMP, "hides the timestamp when files were generated")
8688
.defaultValue(Boolean.TRUE.toString()));
87-
8889
}
8990

9091
/**
@@ -305,10 +306,12 @@ public Map<String, Object> postProcessOperations(Map<String, Object> objs) {
305306
iterator.remove();
306307
}
307308

308-
// if their is a return type, import encoding/json
309+
// if their is a return type, import encoding/json and if needed encoding/xml
309310
for (CodegenOperation operation : operations) {
310311
if(operation.returnBaseType != null ) {
311312
imports.add(createMapping("import", "encoding/json"));
313+
if (withXml)
314+
imports.add(createMapping("import", "encoding/xml"));
312315
break; //just need to import once
313316
}
314317
}
@@ -470,4 +473,7 @@ public String toEnumName(CodegenProperty property) {
470473
}
471474
}
472475

476+
public void setWithXml(boolean withXml) {
477+
this.withXml = withXml;
478+
}
473479
}

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/GoClientCodegen.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public class GoClientCodegen extends AbstractGoCodegen {
1919
protected String packageVersion = "1.0.0";
2020
protected String apiDocPath = "docs/";
2121
protected String modelDocPath = "docs/";
22+
public static final String WITH_XML = "withXml";
2223

2324
public GoClientCodegen() {
2425
super();
@@ -49,6 +50,7 @@ public GoClientCodegen() {
4950

5051
cliOptions.add(new CliOption(CodegenConstants.PACKAGE_VERSION, "Go package version.")
5152
.defaultValue("1.0.0"));
53+
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)"));
5254

5355
}
5456

@@ -95,6 +97,13 @@ public void processOpts() {
9597
supportingFiles.add(new SupportingFile("client.mustache", "", "client.go"));
9698
supportingFiles.add(new SupportingFile("response.mustache", "", "response.go"));
9799
supportingFiles.add(new SupportingFile(".travis.yml", "", ".travis.yml"));
100+
101+
if(additionalProperties.containsKey(WITH_XML)) {
102+
setWithXml(Boolean.parseBoolean(additionalProperties.get(WITH_XML).toString()));
103+
if ( withXml ) {
104+
additionalProperties.put(WITH_XML, "true");
105+
}
106+
}
98107
}
99108

100109
/**

modules/swagger-codegen/src/main/resources/go/api.mustache

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,16 @@ func (a *{{{classname}}}Service) {{{nickname}}}(ctx context.Context{{#hasParams}
215215
return {{#returnType}}successPayload, {{/returnType}}localVarHttpResponse, reportError("Status: %v, Body: %s", localVarHttpResponse.Status, bodyBytes)
216216
}
217217
{{#returnType}}
218+
{{#withXml}}
219+
contentType := localVarHttpResponse.Header.Get("content-type")
220+
if strings.Contains(contentType, "application/xml") {
221+
if err = xml.NewDecoder(localVarHttpResponse.Body).Decode(&successPayload); err != nil {
222+
return successPayload, localVarHttpResponse, err
223+
}
224+
225+
return successPayload, localVarHttpResponse, err
226+
}
227+
{{/withXml}}
218228

219229
if err = json.NewDecoder(localVarHttpResponse.Body).Decode(&successPayload); err != nil {
220230
return {{#returnType}}successPayload, {{/returnType}}localVarHttpResponse, err

modules/swagger-codegen/src/main/resources/go/model.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@ type {{classname}} struct {
2727
{{#description}}
2828
// {{{description}}}
2929
{{/description}}
30-
{{name}} {{^isEnum}}{{^isPrimitiveType}}{{^isContainer}}{{^isDateTime}}*{{/isDateTime}}{{/isContainer}}{{/isPrimitiveType}}{{/isEnum}}{{{datatype}}} `json:"{{baseName}}{{^required}},omitempty{{/required}}"`
30+
{{name}} {{^isEnum}}{{^isPrimitiveType}}{{^isContainer}}{{^isDateTime}}*{{/isDateTime}}{{/isContainer}}{{/isPrimitiveType}}{{/isEnum}}{{{datatype}}} `json:"{{baseName}}{{^required}},omitempty{{/required}}"{{#withXml}} xml:"{{baseName}}"{{/withXml}}`
3131
{{/vars}}
3232
}{{/isEnum}}{{/model}}{{/models}}

modules/swagger-codegen/src/test/java/io/swagger/codegen/go/GoClientOptionsTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ protected void setExpectations() {
3030
times = 1;
3131
clientCodegen.setPackageName(GoClientOptionsProvider.PACKAGE_NAME_VALUE);
3232
times = 1;
33+
clientCodegen.setWithXml(GoClientOptionsProvider.WITH_XML_VALUE);
34+
times = 1;
3335
}};
3436
}
3537
}

modules/swagger-codegen/src/test/java/io/swagger/codegen/options/GoClientOptionsProvider.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public class GoClientOptionsProvider implements OptionsProvider {
1111

1212
public static final String PACKAGE_VERSION_VALUE = "1.0.0";
1313
public static final String PACKAGE_NAME_VALUE = "Go";
14+
public static final boolean WITH_XML_VALUE = true;
1415

1516
@Override
1617
public String getLanguage() {
@@ -24,6 +25,7 @@ public Map<String, String> createOptions() {
2425
.put(CodegenConstants.PACKAGE_VERSION, PACKAGE_VERSION_VALUE)
2526
.put(CodegenConstants.PACKAGE_NAME, PACKAGE_NAME_VALUE)
2627
.put(CodegenConstants.HIDE_GENERATION_TIMESTAMP, "true")
28+
.put(CodegenConstants.WITH_XML, "true")
2729
.build();
2830
}
2931

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Compiled Object files, Static and Dynamic libs (Shared Objects)
2+
*.o
3+
*.a
4+
*.so
5+
6+
# Folders
7+
_obj
8+
_test
9+
10+
# Architecture specific extensions/prefixes
11+
*.[568vq]
12+
[568vq].out
13+
14+
*.cgo1.go
15+
*.cgo2.c
16+
_cgo_defun.c
17+
_cgo_gotypes.go
18+
_cgo_export.*
19+
20+
_testmain.go
21+
22+
*.exe
23+
*.test
24+
*.prof
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Swagger Codegen Ignore
2+
# Generated by swagger-codegen https://github.com/swagger-api/swagger-codegen
3+
4+
# Use this file to prevent files from being overwritten by the generator.
5+
# The patterns follow closely to .gitignore or .dockerignore.
6+
7+
# As an example, the C# client generator defines ApiClient.cs.
8+
# You can make changes and tell Swagger Codgen to ignore just this file by uncommenting the following line:
9+
#ApiClient.cs
10+
11+
# You can match any string of characters against a directory, file or extension with a single asterisk (*):
12+
#foo/*/qux
13+
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux
14+
15+
# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
16+
#foo/**/qux
17+
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux
18+
19+
# You can also negate patterns with an exclamation (!).
20+
# For example, you can ignore all files in a docs folder with the file extension .md:
21+
#docs/*.md
22+
# Then explicitly reverse the ignore rule for a single file:
23+
#!docs/README.md

0 commit comments

Comments
 (0)