Skip to content

Commit cf5ac3a

Browse files
committed
Fix the API generation to use the "produces" property to decide which media type to set for the Accept header
1 parent 881dded commit cf5ac3a

6 files changed

Lines changed: 104 additions & 25 deletions

File tree

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class ApiUtils {
1818
}
1919
.build()
2020

21-
void invokeApi(onSuccess, onFailure, basePath, versionPath, resourcePath, queryParams, headerParams, bodyParams, contentType, method, container, type) {
21+
void invokeApi(onSuccess, onFailure, basePath, versionPath, resourcePath, queryParams, headerParams, bodyParams, accept, contentType, method, container, type) {
2222
def (url, uriPath) = buildUrlAndUriPath(basePath, versionPath, resourcePath)
2323
println "url=$url uriPath=$uriPath"
2424
def http = configure {
@@ -43,7 +43,7 @@ class ApiUtils {
4343
if (bodyParams != null) {
4444
request.body = bodyParams
4545
}
46-
request.accept = ['application/json']
46+
request.accept = accept
4747
request.contentType = contentType
4848

4949
response.success { resp, json ->
@@ -77,4 +77,12 @@ class ApiUtils {
7777
}
7878
}
7979

80+
private def selectHeaderAccept(accepts) {
81+
def jsonMime = 'application/json'
82+
if (accepts.find { it.toLowerCase().startsWith(jsonMime) }) {
83+
return [jsonMime]
84+
}
85+
return accepts
86+
}
87+
8088
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class {{classname}} {
1818
def queryParams = [:]
1919
def headerParams = [:]
2020
def bodyParams
21+
def accept
2122
def contentType
2223
2324
{{#allParams}}
@@ -71,7 +72,9 @@ class {{classname}} {
7172
{{/formParams}}
7273
{{/hasFormParams}}
7374

74-
apiUtils.invokeApi(onSuccess, onFailure, basePath, versionPath, resourcePath, queryParams, headerParams, bodyParams, contentType,
75+
accept = apiUtils.selectHeaderAccept([{{#produces}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/produces}}])
76+
77+
apiUtils.invokeApi(onSuccess, onFailure, basePath, versionPath, resourcePath, queryParams, headerParams, bodyParams, accept, contentType,
7578
"{{httpMethod}}", "{{returnContainer}}",
7679
{{#returnBaseType}}{{{.}}}.class {{/returnBaseType}}{{^returnBaseType}}null {{/returnBaseType}})
7780

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class ApiUtils {
1818
}
1919
.build()
2020

21-
void invokeApi(onSuccess, onFailure, basePath, versionPath, resourcePath, queryParams, headerParams, bodyParams, contentType, method, container, type) {
21+
void invokeApi(onSuccess, onFailure, basePath, versionPath, resourcePath, queryParams, headerParams, bodyParams, accept, contentType, method, container, type) {
2222
def (url, uriPath) = buildUrlAndUriPath(basePath, versionPath, resourcePath)
2323
println "url=$url uriPath=$uriPath"
2424
def http = configure {
@@ -43,7 +43,7 @@ class ApiUtils {
4343
if (bodyParams != null) {
4444
request.body = bodyParams
4545
}
46-
request.accept = ['application/json']
46+
request.accept = accept
4747
request.contentType = contentType
4848

4949
response.success { resp, json ->
@@ -77,4 +77,12 @@ class ApiUtils {
7777
}
7878
}
7979

80+
private def selectHeaderAccept(accepts) {
81+
def jsonMime = 'application/json'
82+
if (accepts.find { it.toLowerCase().startsWith(jsonMime) }) {
83+
return [jsonMime]
84+
}
85+
return accepts
86+
}
87+
8088
}

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

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class PetApi {
1616
def queryParams = [:]
1717
def headerParams = [:]
1818
def bodyParams
19+
def accept
1920
def contentType
2021

2122
// verify required params are set
@@ -29,7 +30,9 @@ class PetApi {
2930
bodyParams = pet
3031

3132

32-
apiUtils.invokeApi(onSuccess, onFailure, basePath, versionPath, resourcePath, queryParams, headerParams, bodyParams, contentType,
33+
accept = apiUtils.selectHeaderAccept(["application/xml", "application/json"])
34+
35+
apiUtils.invokeApi(onSuccess, onFailure, basePath, versionPath, resourcePath, queryParams, headerParams, bodyParams, accept, contentType,
3336
"POST", "",
3437
Pet.class )
3538

@@ -42,6 +45,7 @@ class PetApi {
4245
def queryParams = [:]
4346
def headerParams = [:]
4447
def bodyParams
48+
def accept
4549
def contentType
4650

4751
// verify required params are set
@@ -56,7 +60,9 @@ class PetApi {
5660

5761

5862

59-
apiUtils.invokeApi(onSuccess, onFailure, basePath, versionPath, resourcePath, queryParams, headerParams, bodyParams, contentType,
63+
accept = apiUtils.selectHeaderAccept([])
64+
65+
apiUtils.invokeApi(onSuccess, onFailure, basePath, versionPath, resourcePath, queryParams, headerParams, bodyParams, accept, contentType,
6066
"DELETE", "",
6167
null )
6268

@@ -69,6 +75,7 @@ class PetApi {
6975
def queryParams = [:]
7076
def headerParams = [:]
7177
def bodyParams
78+
def accept
7279
def contentType
7380

7481
// verify required params are set
@@ -83,7 +90,9 @@ class PetApi {
8390

8491

8592

86-
apiUtils.invokeApi(onSuccess, onFailure, basePath, versionPath, resourcePath, queryParams, headerParams, bodyParams, contentType,
93+
accept = apiUtils.selectHeaderAccept(["application/xml", "application/json"])
94+
95+
apiUtils.invokeApi(onSuccess, onFailure, basePath, versionPath, resourcePath, queryParams, headerParams, bodyParams, accept, contentType,
8796
"GET", "array",
8897
Pet.class )
8998

@@ -96,6 +105,7 @@ class PetApi {
96105
def queryParams = [:]
97106
def headerParams = [:]
98107
def bodyParams
108+
def accept
99109
def contentType
100110

101111
// verify required params are set
@@ -110,7 +120,9 @@ class PetApi {
110120

111121

112122

113-
apiUtils.invokeApi(onSuccess, onFailure, basePath, versionPath, resourcePath, queryParams, headerParams, bodyParams, contentType,
123+
accept = apiUtils.selectHeaderAccept(["application/xml", "application/json"])
124+
125+
apiUtils.invokeApi(onSuccess, onFailure, basePath, versionPath, resourcePath, queryParams, headerParams, bodyParams, accept, contentType,
114126
"GET", "array",
115127
Pet.class )
116128

@@ -123,6 +135,7 @@ class PetApi {
123135
def queryParams = [:]
124136
def headerParams = [:]
125137
def bodyParams
138+
def accept
126139
def contentType
127140

128141
// verify required params are set
@@ -134,7 +147,9 @@ class PetApi {
134147

135148

136149

137-
apiUtils.invokeApi(onSuccess, onFailure, basePath, versionPath, resourcePath, queryParams, headerParams, bodyParams, contentType,
150+
accept = apiUtils.selectHeaderAccept(["application/xml", "application/json"])
151+
152+
apiUtils.invokeApi(onSuccess, onFailure, basePath, versionPath, resourcePath, queryParams, headerParams, bodyParams, accept, contentType,
138153
"GET", "",
139154
Pet.class )
140155

@@ -147,6 +162,7 @@ class PetApi {
147162
def queryParams = [:]
148163
def headerParams = [:]
149164
def bodyParams
165+
def accept
150166
def contentType
151167

152168
// verify required params are set
@@ -160,7 +176,9 @@ class PetApi {
160176
bodyParams = pet
161177

162178

163-
apiUtils.invokeApi(onSuccess, onFailure, basePath, versionPath, resourcePath, queryParams, headerParams, bodyParams, contentType,
179+
accept = apiUtils.selectHeaderAccept(["application/xml", "application/json"])
180+
181+
apiUtils.invokeApi(onSuccess, onFailure, basePath, versionPath, resourcePath, queryParams, headerParams, bodyParams, accept, contentType,
164182
"PUT", "",
165183
Pet.class )
166184

@@ -173,6 +191,7 @@ class PetApi {
173191
def queryParams = [:]
174192
def headerParams = [:]
175193
def bodyParams
194+
def accept
176195
def contentType
177196

178197
// verify required params are set
@@ -188,7 +207,9 @@ class PetApi {
188207
bodyParams.put("name", name)
189208
bodyParams.put("status", status)
190209

191-
apiUtils.invokeApi(onSuccess, onFailure, basePath, versionPath, resourcePath, queryParams, headerParams, bodyParams, contentType,
210+
accept = apiUtils.selectHeaderAccept([])
211+
212+
apiUtils.invokeApi(onSuccess, onFailure, basePath, versionPath, resourcePath, queryParams, headerParams, bodyParams, accept, contentType,
192213
"POST", "",
193214
null )
194215

@@ -201,6 +222,7 @@ class PetApi {
201222
def queryParams = [:]
202223
def headerParams = [:]
203224
def bodyParams
225+
def accept
204226
def contentType
205227

206228
// verify required params are set
@@ -216,7 +238,9 @@ class PetApi {
216238
bodyParams.put("additionalMetadata", additionalMetadata)
217239
bodyParams.put("file", _file)
218240

219-
apiUtils.invokeApi(onSuccess, onFailure, basePath, versionPath, resourcePath, queryParams, headerParams, bodyParams, contentType,
241+
accept = apiUtils.selectHeaderAccept(["application/json"])
242+
243+
apiUtils.invokeApi(onSuccess, onFailure, basePath, versionPath, resourcePath, queryParams, headerParams, bodyParams, accept, contentType,
220244
"POST", "",
221245
ModelApiResponse.class )
222246

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class StoreApi {
1515
def queryParams = [:]
1616
def headerParams = [:]
1717
def bodyParams
18+
def accept
1819
def contentType
1920

2021
// verify required params are set
@@ -26,7 +27,9 @@ class StoreApi {
2627

2728

2829

29-
apiUtils.invokeApi(onSuccess, onFailure, basePath, versionPath, resourcePath, queryParams, headerParams, bodyParams, contentType,
30+
accept = apiUtils.selectHeaderAccept([])
31+
32+
apiUtils.invokeApi(onSuccess, onFailure, basePath, versionPath, resourcePath, queryParams, headerParams, bodyParams, accept, contentType,
3033
"DELETE", "",
3134
null )
3235

@@ -39,14 +42,17 @@ class StoreApi {
3942
def queryParams = [:]
4043
def headerParams = [:]
4144
def bodyParams
45+
def accept
4246
def contentType
4347

4448

4549

4650

4751

4852

49-
apiUtils.invokeApi(onSuccess, onFailure, basePath, versionPath, resourcePath, queryParams, headerParams, bodyParams, contentType,
53+
accept = apiUtils.selectHeaderAccept(["application/json"])
54+
55+
apiUtils.invokeApi(onSuccess, onFailure, basePath, versionPath, resourcePath, queryParams, headerParams, bodyParams, accept, contentType,
5056
"GET", "map",
5157
Integer.class )
5258

@@ -59,6 +65,7 @@ class StoreApi {
5965
def queryParams = [:]
6066
def headerParams = [:]
6167
def bodyParams
68+
def accept
6269
def contentType
6370

6471
// verify required params are set
@@ -70,7 +77,9 @@ class StoreApi {
7077

7178

7279

73-
apiUtils.invokeApi(onSuccess, onFailure, basePath, versionPath, resourcePath, queryParams, headerParams, bodyParams, contentType,
80+
accept = apiUtils.selectHeaderAccept(["application/xml", "application/json"])
81+
82+
apiUtils.invokeApi(onSuccess, onFailure, basePath, versionPath, resourcePath, queryParams, headerParams, bodyParams, accept, contentType,
7483
"GET", "",
7584
Order.class )
7685

@@ -83,6 +92,7 @@ class StoreApi {
8392
def queryParams = [:]
8493
def headerParams = [:]
8594
def bodyParams
95+
def accept
8696
def contentType
8797

8898
// verify required params are set
@@ -96,7 +106,9 @@ class StoreApi {
96106
bodyParams = order
97107

98108

99-
apiUtils.invokeApi(onSuccess, onFailure, basePath, versionPath, resourcePath, queryParams, headerParams, bodyParams, contentType,
109+
accept = apiUtils.selectHeaderAccept(["application/xml", "application/json"])
110+
111+
apiUtils.invokeApi(onSuccess, onFailure, basePath, versionPath, resourcePath, queryParams, headerParams, bodyParams, accept, contentType,
100112
"POST", "",
101113
Order.class )
102114

0 commit comments

Comments
 (0)