Skip to content

Commit 9450984

Browse files
authored
[Java][Client] Generate servers for okhttp-gson (#14179)
* [Java][Client] Generate servers for okhttp-gson * Update sample tests
1 parent 344c49d commit 9450984

File tree

7 files changed

+397
-7
lines changed
  • modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson
  • samples/client
    • others/java/okhttp-gson-streaming/src/main/java/org/openapitools/client
    • petstore/java
      • okhttp-gson-dynamicOperations/src/main/java/org/openapitools/client
      • okhttp-gson-group-parameter/src/main/java/org/openapitools/client
      • okhttp-gson-parcelableModel/src/main/java/org/openapitools/client
      • okhttp-gson-swagger1/src/main/java/org/openapitools/client
      • okhttp-gson/src/main/java/org/openapitools/client

7 files changed

+397
-7
lines changed

modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/ApiClient.mustache

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,33 @@ import {{invokerPackage}}.auth.OAuthFlow;
7272
public class ApiClient {
7373
7474
private String basePath = "{{{basePath}}}";
75+
protected List<ServerConfiguration> servers = new ArrayList<ServerConfiguration>({{#servers}}{{#-first}}Arrays.asList(
76+
{{/-first}} new ServerConfiguration(
77+
"{{{url}}}",
78+
"{{{description}}}{{^description}}No description provided{{/description}}",
79+
new HashMap<String, ServerVariable>(){{#variables}}{{#-first}} {{
80+
{{/-first}} put("{{{name}}}", new ServerVariable(
81+
"{{{description}}}{{^description}}No description provided{{/description}}",
82+
"{{{defaultValue}}}",
83+
new HashSet<String>(
84+
{{#enumValues}}
85+
{{#-first}}
86+
Arrays.asList(
87+
{{/-first}}
88+
"{{{.}}}"{{^-last}},{{/-last}}
89+
{{#-last}}
90+
)
91+
{{/-last}}
92+
{{/enumValues}}
93+
)
94+
));
95+
{{#-last}}
96+
}}{{/-last}}{{/variables}}
97+
){{^-last}},{{/-last}}
98+
{{#-last}}
99+
){{/-last}}{{/servers}});
100+
protected Integer serverIndex = 0;
101+
protected Map<String, String> serverVariables = null;
75102
private boolean debugging = false;
76103
private Map<String, String> defaultHeaderMap = new HashMap<String, String>();
77104
private Map<String, String> defaultCookieMap = new HashMap<String, String>();
@@ -261,6 +288,33 @@ public class ApiClient {
261288
return this;
262289
}
263290

291+
public List<ServerConfiguration> getServers() {
292+
return servers;
293+
}
294+
295+
public ApiClient setServers(List<ServerConfiguration> servers) {
296+
this.servers = servers;
297+
return this;
298+
}
299+
300+
public Integer getServerIndex() {
301+
return serverIndex;
302+
}
303+
304+
public ApiClient setServerIndex(Integer serverIndex) {
305+
this.serverIndex = serverIndex;
306+
return this;
307+
}
308+
309+
public Map<String, String> getServerVariables() {
310+
return serverVariables;
311+
}
312+
313+
public ApiClient setServerVariables(Map<String, String> serverVariables) {
314+
this.serverVariables = serverVariables;
315+
return this;
316+
}
317+
264318
/**
265319
* Get HTTP client
266320
*
@@ -1399,7 +1453,18 @@ public class ApiClient {
13991453
if (baseUrl != null) {
14001454
url.append(baseUrl).append(path);
14011455
} else {
1402-
url.append(basePath).append(path);
1456+
String baseURL;
1457+
if (serverIndex != null) {
1458+
if (serverIndex < 0 || serverIndex >= servers.size()) {
1459+
throw new ArrayIndexOutOfBoundsException(String.format(
1460+
"Invalid index %d when selecting the host settings. Must be less than %d", serverIndex, servers.size()
1461+
));
1462+
}
1463+
baseURL = servers.get(serverIndex).URL(serverVariables);
1464+
} else {
1465+
baseURL = basePath;
1466+
}
1467+
url.append(baseURL).append(path);
14031468
}
14041469

14051470
if (queryParams != null && !queryParams.isEmpty()) {

samples/client/others/java/okhttp-gson-streaming/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,15 @@
6161
public class ApiClient {
6262

6363
private String basePath = "http://localhost:8082";
64+
protected List<ServerConfiguration> servers = new ArrayList<ServerConfiguration>(Arrays.asList(
65+
new ServerConfiguration(
66+
"http://localhost:8082",
67+
"No description provided",
68+
new HashMap<String, ServerVariable>()
69+
)
70+
));
71+
protected Integer serverIndex = 0;
72+
protected Map<String, String> serverVariables = null;
6473
private boolean debugging = false;
6574
private Map<String, String> defaultHeaderMap = new HashMap<String, String>();
6675
private Map<String, String> defaultCookieMap = new HashMap<String, String>();
@@ -154,6 +163,33 @@ public ApiClient setBasePath(String basePath) {
154163
return this;
155164
}
156165

166+
public List<ServerConfiguration> getServers() {
167+
return servers;
168+
}
169+
170+
public ApiClient setServers(List<ServerConfiguration> servers) {
171+
this.servers = servers;
172+
return this;
173+
}
174+
175+
public Integer getServerIndex() {
176+
return serverIndex;
177+
}
178+
179+
public ApiClient setServerIndex(Integer serverIndex) {
180+
this.serverIndex = serverIndex;
181+
return this;
182+
}
183+
184+
public Map<String, String> getServerVariables() {
185+
return serverVariables;
186+
}
187+
188+
public ApiClient setServerVariables(Map<String, String> serverVariables) {
189+
this.serverVariables = serverVariables;
190+
return this;
191+
}
192+
157193
/**
158194
* Get HTTP client
159195
*
@@ -1196,7 +1232,18 @@ public String buildUrl(String baseUrl, String path, List<Pair> queryParams, List
11961232
if (baseUrl != null) {
11971233
url.append(baseUrl).append(path);
11981234
} else {
1199-
url.append(basePath).append(path);
1235+
String baseURL;
1236+
if (serverIndex != null) {
1237+
if (serverIndex < 0 || serverIndex >= servers.size()) {
1238+
throw new ArrayIndexOutOfBoundsException(String.format(
1239+
"Invalid index %d when selecting the host settings. Must be less than %d", serverIndex, servers.size()
1240+
));
1241+
}
1242+
baseURL = servers.get(serverIndex).URL(serverVariables);
1243+
} else {
1244+
baseURL = basePath;
1245+
}
1246+
url.append(baseURL).append(path);
12001247
}
12011248

12021249
if (queryParams != null && !queryParams.isEmpty()) {

samples/client/petstore/java/okhttp-gson-dynamicOperations/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,15 @@
7272
public class ApiClient {
7373

7474
private String basePath = "http://petstore.swagger.io:80/v2";
75+
protected List<ServerConfiguration> servers = new ArrayList<ServerConfiguration>(Arrays.asList(
76+
new ServerConfiguration(
77+
"http://petstore.swagger.io:80/v2",
78+
"No description provided",
79+
new HashMap<String, ServerVariable>()
80+
)
81+
));
82+
protected Integer serverIndex = 0;
83+
protected Map<String, String> serverVariables = null;
7584
private boolean debugging = false;
7685
private Map<String, String> defaultHeaderMap = new HashMap<String, String>();
7786
private Map<String, String> defaultCookieMap = new HashMap<String, String>();
@@ -247,6 +256,33 @@ public ApiClient setBasePath(String basePath) {
247256
return this;
248257
}
249258

259+
public List<ServerConfiguration> getServers() {
260+
return servers;
261+
}
262+
263+
public ApiClient setServers(List<ServerConfiguration> servers) {
264+
this.servers = servers;
265+
return this;
266+
}
267+
268+
public Integer getServerIndex() {
269+
return serverIndex;
270+
}
271+
272+
public ApiClient setServerIndex(Integer serverIndex) {
273+
this.serverIndex = serverIndex;
274+
return this;
275+
}
276+
277+
public Map<String, String> getServerVariables() {
278+
return serverVariables;
279+
}
280+
281+
public ApiClient setServerVariables(Map<String, String> serverVariables) {
282+
this.serverVariables = serverVariables;
283+
return this;
284+
}
285+
250286
/**
251287
* Get HTTP client
252288
*
@@ -1274,7 +1310,18 @@ public String buildUrl(String baseUrl, String path, List<Pair> queryParams, List
12741310
if (baseUrl != null) {
12751311
url.append(baseUrl).append(path);
12761312
} else {
1277-
url.append(basePath).append(path);
1313+
String baseURL;
1314+
if (serverIndex != null) {
1315+
if (serverIndex < 0 || serverIndex >= servers.size()) {
1316+
throw new ArrayIndexOutOfBoundsException(String.format(
1317+
"Invalid index %d when selecting the host settings. Must be less than %d", serverIndex, servers.size()
1318+
));
1319+
}
1320+
baseURL = servers.get(serverIndex).URL(serverVariables);
1321+
} else {
1322+
baseURL = basePath;
1323+
}
1324+
url.append(baseURL).append(path);
12781325
}
12791326

12801327
if (queryParams != null && !queryParams.isEmpty()) {

samples/client/petstore/java/okhttp-gson-group-parameter/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,15 @@
6666
public class ApiClient {
6767

6868
private String basePath = "http://petstore.swagger.io/v2";
69+
protected List<ServerConfiguration> servers = new ArrayList<ServerConfiguration>(Arrays.asList(
70+
new ServerConfiguration(
71+
"http://petstore.swagger.io/v2",
72+
"No description provided",
73+
new HashMap<String, ServerVariable>()
74+
)
75+
));
76+
protected Integer serverIndex = 0;
77+
protected Map<String, String> serverVariables = null;
6978
private boolean debugging = false;
7079
private Map<String, String> defaultHeaderMap = new HashMap<String, String>();
7180
private Map<String, String> defaultCookieMap = new HashMap<String, String>();
@@ -230,6 +239,33 @@ public ApiClient setBasePath(String basePath) {
230239
return this;
231240
}
232241

242+
public List<ServerConfiguration> getServers() {
243+
return servers;
244+
}
245+
246+
public ApiClient setServers(List<ServerConfiguration> servers) {
247+
this.servers = servers;
248+
return this;
249+
}
250+
251+
public Integer getServerIndex() {
252+
return serverIndex;
253+
}
254+
255+
public ApiClient setServerIndex(Integer serverIndex) {
256+
this.serverIndex = serverIndex;
257+
return this;
258+
}
259+
260+
public Map<String, String> getServerVariables() {
261+
return serverVariables;
262+
}
263+
264+
public ApiClient setServerVariables(Map<String, String> serverVariables) {
265+
this.serverVariables = serverVariables;
266+
return this;
267+
}
268+
233269
/**
234270
* Get HTTP client
235271
*
@@ -1269,7 +1305,18 @@ public String buildUrl(String baseUrl, String path, List<Pair> queryParams, List
12691305
if (baseUrl != null) {
12701306
url.append(baseUrl).append(path);
12711307
} else {
1272-
url.append(basePath).append(path);
1308+
String baseURL;
1309+
if (serverIndex != null) {
1310+
if (serverIndex < 0 || serverIndex >= servers.size()) {
1311+
throw new ArrayIndexOutOfBoundsException(String.format(
1312+
"Invalid index %d when selecting the host settings. Must be less than %d", serverIndex, servers.size()
1313+
));
1314+
}
1315+
baseURL = servers.get(serverIndex).URL(serverVariables);
1316+
} else {
1317+
baseURL = basePath;
1318+
}
1319+
url.append(baseURL).append(path);
12731320
}
12741321

12751322
if (queryParams != null && !queryParams.isEmpty()) {

samples/client/petstore/java/okhttp-gson-parcelableModel/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,15 @@
6666
public class ApiClient {
6767

6868
private String basePath = "http://petstore.swagger.io:80/v2";
69+
protected List<ServerConfiguration> servers = new ArrayList<ServerConfiguration>(Arrays.asList(
70+
new ServerConfiguration(
71+
"http://petstore.swagger.io:80/v2",
72+
"No description provided",
73+
new HashMap<String, ServerVariable>()
74+
)
75+
));
76+
protected Integer serverIndex = 0;
77+
protected Map<String, String> serverVariables = null;
6978
private boolean debugging = false;
7079
private Map<String, String> defaultHeaderMap = new HashMap<String, String>();
7180
private Map<String, String> defaultCookieMap = new HashMap<String, String>();
@@ -236,6 +245,33 @@ public ApiClient setBasePath(String basePath) {
236245
return this;
237246
}
238247

248+
public List<ServerConfiguration> getServers() {
249+
return servers;
250+
}
251+
252+
public ApiClient setServers(List<ServerConfiguration> servers) {
253+
this.servers = servers;
254+
return this;
255+
}
256+
257+
public Integer getServerIndex() {
258+
return serverIndex;
259+
}
260+
261+
public ApiClient setServerIndex(Integer serverIndex) {
262+
this.serverIndex = serverIndex;
263+
return this;
264+
}
265+
266+
public Map<String, String> getServerVariables() {
267+
return serverVariables;
268+
}
269+
270+
public ApiClient setServerVariables(Map<String, String> serverVariables) {
271+
this.serverVariables = serverVariables;
272+
return this;
273+
}
274+
239275
/**
240276
* Get HTTP client
241277
*
@@ -1275,7 +1311,18 @@ public String buildUrl(String baseUrl, String path, List<Pair> queryParams, List
12751311
if (baseUrl != null) {
12761312
url.append(baseUrl).append(path);
12771313
} else {
1278-
url.append(basePath).append(path);
1314+
String baseURL;
1315+
if (serverIndex != null) {
1316+
if (serverIndex < 0 || serverIndex >= servers.size()) {
1317+
throw new ArrayIndexOutOfBoundsException(String.format(
1318+
"Invalid index %d when selecting the host settings. Must be less than %d", serverIndex, servers.size()
1319+
));
1320+
}
1321+
baseURL = servers.get(serverIndex).URL(serverVariables);
1322+
} else {
1323+
baseURL = basePath;
1324+
}
1325+
url.append(baseURL).append(path);
12791326
}
12801327

12811328
if (queryParams != null && !queryParams.isEmpty()) {

0 commit comments

Comments
 (0)