Skip to content

Commit 58a9620

Browse files
committed
Support the application/xml media type for both requests and responses
1 parent ed61716 commit 58a9620

File tree

4 files changed

+59
-24
lines changed

4 files changed

+59
-24
lines changed

modules/openapi-generator/src/main/resources/Groovy/ApiUtils.mustache

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
package {{invokerPackage}}
22

3+
{{#withXml}}
4+
import java.io.StringReader
5+
import {{javaxPackage}}.xml.bind.JAXB
6+
{{/withXml}}
37
import groovy.json.JsonBuilder
48
import groovy.json.JsonGenerator
59
import groovyx.net.http.ChainedHttpConfig
610
import groovyx.net.http.ContentTypes
711
import groovyx.net.http.NativeHandlers
12+
import groovyx.net.http.FromServer
813
import groovyx.net.http.ToServer
914

1015
import static groovyx.net.http.HttpBuilder.configure
@@ -25,17 +30,35 @@ class ApiUtils {
2530
request.uri = url
2631
request.uri.path = uriPath
2732
request.encoder(ContentTypes.JSON, { final ChainedHttpConfig config, final ToServer ts ->
28-
final ChainedHttpConfig.ChainedRequest request = config.getChainedRequest();
33+
final ChainedHttpConfig.ChainedRequest request = config.getChainedRequest()
2934
if (NativeHandlers.Encoders.handleRawUpload(config, ts)) {
30-
return;
35+
return
3136
}
3237

33-
final Object body = NativeHandlers.Encoders.checkNull(request.actualBody());
38+
final Object body = NativeHandlers.Encoders.checkNull(request.actualBody())
3439
final String json = ((body instanceof String || body instanceof GString)
3540
? body.toString()
36-
: new JsonBuilder(body, jsonGenerator).toString());
37-
ts.toServer(NativeHandlers.Encoders.stringToStream(json, request.actualCharset()));
41+
: new JsonBuilder(body, jsonGenerator).toString())
42+
ts.toServer(NativeHandlers.Encoders.stringToStream(json, request.actualCharset()))
3843
})
44+
{{#withXml}}
45+
request.encoder(ContentTypes.XML, { final ChainedHttpConfig config, final ToServer ts ->
46+
final ChainedHttpConfig.ChainedRequest request = config.getChainedRequest()
47+
if (NativeHandlers.Encoders.handleRawUpload(config, ts)) {
48+
return
49+
}
50+
51+
final Object body = NativeHandlers.Encoders.checkNull(request.actualBody())
52+
String xml
53+
if (body instanceof String || body instanceof GString) {
54+
xml = body.toString()
55+
} else {
56+
StringWriter writer = new StringWriter()
57+
JAXB.marshal(body, writer)
58+
xml = writer.toString()
59+
}
60+
ts.toServer(NativeHandlers.Encoders.stringToStream(xml, request.actualCharset()))
61+
}){{/withXml}}
3962
}
4063
.invokeMethod(String.valueOf(method).toLowerCase()) {
4164
request.uri.query = queryParams
@@ -45,10 +68,14 @@ class ApiUtils {
4568
}
4669
request.accept = accept
4770
request.contentType = contentType
48-
49-
response.success { resp, json ->
71+
{{#withXml}}
72+
response.parser(ContentTypes.XML) { final ChainedHttpConfig cfg, final FromServer fs ->
73+
fs.inputStream.text
74+
}
75+
{{/withXml}}
76+
response.success { resp, body ->
5077
if (type != null) {
51-
onSuccess(parse(json, container, type))
78+
onSuccess(parse(resp, body, container, type))
5279
}
5380
}
5481
response.failure { resp ->
@@ -69,10 +96,15 @@ class ApiUtils {
6996
[basePath-pathOnly, pathOnly+versionPath+resourcePath]
7097
}
7198

72-
private def parse(object, container, clazz) {
99+
private def parse(response, object, container, clazz) {
100+
{{#withXml}}
101+
if (response.getContentType().toLowerCase().contains("xml")) {
102+
return JAXB.unmarshal(new StringReader(object), clazz)
103+
}
104+
{{/withXml}}
73105
if (container == "array") {
74-
return object.collect {parse(it, "", clazz)}
75-
} else {
106+
return object.collect { parse(it, "", clazz) }
107+
} else {
76108
return clazz.newInstance(object)
77109
}
78110
}
@@ -84,5 +116,4 @@ class ApiUtils {
84116
}
85117
return accepts
86118
}
87-
88119
}

modules/openapi-generator/src/main/resources/Groovy/build.gradle.mustache

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
apply plugin: 'groovy'
22
apply plugin: 'idea'
33
apply plugin: 'eclipse'
4+
apply plugin: 'com.github.johnrengelman.shadow'
45

56
group = '{{groupId}}'
67
version = '{{artifactVersion}}'
@@ -18,6 +19,7 @@ buildscript {
1819
}
1920
dependencies {
2021
classpath(group: 'org.jfrog.buildinfo', name: 'build-info-extractor-gradle', version: '4.24.20')
22+
classpath('com.github.johnrengelman.shadow:com.github.johnrengelman.shadow.gradle.plugin:4.0.4')
2123
}
2224
}
2325

samples/client/petstore/groovy/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
apply plugin: 'groovy'
22
apply plugin: 'idea'
33
apply plugin: 'eclipse'
4+
apply plugin: 'com.github.johnrengelman.shadow'
45

56
group = 'org.openapitools'
67
version = '1.0.0'
@@ -18,6 +19,7 @@ buildscript {
1819
}
1920
dependencies {
2021
classpath(group: 'org.jfrog.buildinfo', name: 'build-info-extractor-gradle', version: '4.24.20')
22+
classpath('com.github.johnrengelman.shadow:com.github.johnrengelman.shadow.gradle.plugin:4.0.4')
2123
}
2224
}
2325

samples/client/petstore/groovy/src/main/groovy/org/openapitools/api/ApiUtils.groovy

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import groovy.json.JsonGenerator
55
import groovyx.net.http.ChainedHttpConfig
66
import groovyx.net.http.ContentTypes
77
import groovyx.net.http.NativeHandlers
8+
import groovyx.net.http.FromServer
89
import groovyx.net.http.ToServer
910

1011
import static groovyx.net.http.HttpBuilder.configure
@@ -25,17 +26,18 @@ class ApiUtils {
2526
request.uri = url
2627
request.uri.path = uriPath
2728
request.encoder(ContentTypes.JSON, { final ChainedHttpConfig config, final ToServer ts ->
28-
final ChainedHttpConfig.ChainedRequest request = config.getChainedRequest();
29+
final ChainedHttpConfig.ChainedRequest request = config.getChainedRequest()
2930
if (NativeHandlers.Encoders.handleRawUpload(config, ts)) {
30-
return;
31+
return
3132
}
3233

33-
final Object body = NativeHandlers.Encoders.checkNull(request.actualBody());
34+
final Object body = NativeHandlers.Encoders.checkNull(request.actualBody())
3435
final String json = ((body instanceof String || body instanceof GString)
3536
? body.toString()
36-
: new JsonBuilder(body, jsonGenerator).toString());
37-
ts.toServer(NativeHandlers.Encoders.stringToStream(json, request.actualCharset()));
37+
: new JsonBuilder(body, jsonGenerator).toString())
38+
ts.toServer(NativeHandlers.Encoders.stringToStream(json, request.actualCharset()))
3839
})
40+
3941
}
4042
.invokeMethod(String.valueOf(method).toLowerCase()) {
4143
request.uri.query = queryParams
@@ -45,10 +47,9 @@ class ApiUtils {
4547
}
4648
request.accept = accept
4749
request.contentType = contentType
48-
49-
response.success { resp, json ->
50+
response.success { resp, body ->
5051
if (type != null) {
51-
onSuccess(parse(json, container, type))
52+
onSuccess(parse(resp, body, container, type))
5253
}
5354
}
5455
response.failure { resp ->
@@ -69,10 +70,10 @@ class ApiUtils {
6970
[basePath-pathOnly, pathOnly+versionPath+resourcePath]
7071
}
7172

72-
private def parse(object, container, clazz) {
73+
private def parse(response, object, container, clazz) {
7374
if (container == "array") {
74-
return object.collect {parse(it, "", clazz)}
75-
} else {
75+
return object.collect { parse(it, "", clazz) }
76+
} else {
7677
return clazz.newInstance(object)
7778
}
7879
}
@@ -84,5 +85,4 @@ class ApiUtils {
8485
}
8586
return accepts
8687
}
87-
8888
}

0 commit comments

Comments
 (0)