Skip to content

Commit bf6e83e

Browse files
authored
feat(java): Generate servers for restclient library (#21699)
1 parent 7e97427 commit bf6e83e

File tree

9 files changed

+658
-9
lines changed
  • modules/openapi-generator/src/main/resources/Java/libraries/restclient
  • samples/client
    • echo_api/java/restclient/src/main/java/org/openapitools/client
    • others/java
      • restclient-enum-in-multipart/src/main/java/org/openapitools/client
      • restclient-useAbstractionForFiles/src/main/java/org/openapitools/client
    • petstore/java
      • restclient-nullable-arrays/src/main/java/org/openapitools/client
      • restclient-swagger2/src/main/java/org/openapitools/client
      • restclient-useSingleRequestParameter-static/src/main/java/org/openapitools/client
      • restclient-useSingleRequestParameter/src/main/java/org/openapitools/client
      • restclient/src/main/java/org/openapitools/client

9 files changed

+658
-9
lines changed

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

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,12 @@ import java.util.Optional;
4242
import java.text.DateFormat;
4343
import java.text.ParseException;
4444
import java.util.ArrayList;
45+
import java.util.Arrays;
4546
import java.util.Collection;
4647
import java.util.Collections;
4748
import java.util.Date;
4849
import java.util.HashMap;
50+
import java.util.HashSet;
4951
import java.util.List;
5052
import java.util.Map;
5153
import java.util.Map.Entry;
@@ -89,6 +91,33 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
8991
protected final MultiValueMap<String, String> defaultCookies = new LinkedMultiValueMap<>();
9092

9193
protected String basePath = "{{basePath}}";
94+
protected List<ServerConfiguration> servers = new ArrayList<ServerConfiguration>({{#servers}}{{#-first}}Arrays.asList(
95+
{{/-first}} new ServerConfiguration(
96+
"{{{url}}}",
97+
"{{{description}}}{{^description}}No description provided{{/description}}",
98+
new HashMap<String, ServerVariable>(){{#variables}}{{#-first}} {{
99+
{{/-first}} put("{{{name}}}", new ServerVariable(
100+
"{{{description}}}{{^description}}No description provided{{/description}}",
101+
"{{{defaultValue}}}",
102+
new HashSet<String>(
103+
{{#enumValues}}
104+
{{#-first}}
105+
Arrays.asList(
106+
{{/-first}}
107+
"{{{.}}}"{{^-last}},{{/-last}}
108+
{{#-last}}
109+
)
110+
{{/-last}}
111+
{{/enumValues}}
112+
)
113+
));
114+
{{#-last}}
115+
}}{{/-last}}{{/variables}}
116+
){{^-last}},{{/-last}}
117+
{{#-last}}
118+
){{/-last}}{{/servers}});
119+
protected Integer serverIndex = 0;
120+
protected Map<String, String> serverVariables = null;
92121

93122
protected final RestClient restClient;
94123
protected final DateFormat dateFormat;
@@ -219,6 +248,34 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
219248
*/
220249
public ApiClient setBasePath(String basePath) {
221250
this.basePath = basePath;
251+
this.serverIndex = null;
252+
return this;
253+
}
254+
255+
public List<ServerConfiguration> getServers() {
256+
return servers;
257+
}
258+
259+
public ApiClient setServers(List<ServerConfiguration> servers) {
260+
this.servers = servers;
261+
return this;
262+
}
263+
264+
public Integer getServerIndex() {
265+
return serverIndex;
266+
}
267+
268+
public ApiClient setServerIndex(Integer serverIndex) {
269+
this.serverIndex = serverIndex;
270+
return this;
271+
}
272+
273+
public Map<String, String> getServerVariables() {
274+
return serverVariables;
275+
}
276+
277+
public ApiClient setServerVariables(Map<String, String> serverVariables) {
278+
this.serverVariables = serverVariables;
222279
return this;
223280
}
224281

@@ -659,7 +716,19 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
659716
MediaType contentType, String[] authNames) {
660717
updateParamsForAuth(authNames, queryParams, headerParams, cookieParams);
661718
662-
final UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(basePath).path(path);
719+
String baseUrl = basePath;
720+
if (serverIndex != null) {
721+
if (serverIndex < 0 || serverIndex >= servers.size()) {
722+
throw new ArrayIndexOutOfBoundsException(String.format(
723+
"Invalid index %d when selecting the host settings. Must be less than %d", serverIndex, servers.size()
724+
));
725+
}
726+
baseUrl = servers.get(serverIndex).URL(serverVariables);
727+
}
728+
729+
final UriComponentsBuilder builder = UriComponentsBuilder
730+
.fromUriString(baseUrl)
731+
.path(path);
663732

664733
String finalUri = builder.build(false).toUriString();
665734
Map<String, Object> uriParams = new HashMap<>();

samples/client/echo_api/java/restclient/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,12 @@
3838
import java.text.DateFormat;
3939
import java.text.ParseException;
4040
import java.util.ArrayList;
41+
import java.util.Arrays;
4142
import java.util.Collection;
4243
import java.util.Collections;
4344
import java.util.Date;
4445
import java.util.HashMap;
46+
import java.util.HashSet;
4547
import java.util.List;
4648
import java.util.Map;
4749
import java.util.Map.Entry;
@@ -76,6 +78,15 @@ protected String collectionToString(Collection<?> collection) {
7678
protected final MultiValueMap<String, String> defaultCookies = new LinkedMultiValueMap<>();
7779

7880
protected String basePath = "http://localhost:3000";
81+
protected List<ServerConfiguration> servers = new ArrayList<ServerConfiguration>(Arrays.asList(
82+
new ServerConfiguration(
83+
"http://localhost:3000",
84+
"No description provided",
85+
new HashMap<String, ServerVariable>()
86+
)
87+
));
88+
protected Integer serverIndex = 0;
89+
protected Map<String, String> serverVariables = null;
7990

8091
protected final RestClient restClient;
8192
protected final DateFormat dateFormat;
@@ -188,6 +199,34 @@ public String getBasePath() {
188199
*/
189200
public ApiClient setBasePath(String basePath) {
190201
this.basePath = basePath;
202+
this.serverIndex = null;
203+
return this;
204+
}
205+
206+
public List<ServerConfiguration> getServers() {
207+
return servers;
208+
}
209+
210+
public ApiClient setServers(List<ServerConfiguration> servers) {
211+
this.servers = servers;
212+
return this;
213+
}
214+
215+
public Integer getServerIndex() {
216+
return serverIndex;
217+
}
218+
219+
public ApiClient setServerIndex(Integer serverIndex) {
220+
this.serverIndex = serverIndex;
221+
return this;
222+
}
223+
224+
public Map<String, String> getServerVariables() {
225+
return serverVariables;
226+
}
227+
228+
public ApiClient setServerVariables(Map<String, String> serverVariables) {
229+
this.serverVariables = serverVariables;
191230
return this;
192231
}
193232

@@ -612,7 +651,19 @@ protected RestClient.RequestBodySpec prepareRequest(String path, HttpMethod meth
612651
MediaType contentType, String[] authNames) {
613652
updateParamsForAuth(authNames, queryParams, headerParams, cookieParams);
614653

615-
final UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(basePath).path(path);
654+
String baseUrl = basePath;
655+
if (serverIndex != null) {
656+
if (serverIndex < 0 || serverIndex >= servers.size()) {
657+
throw new ArrayIndexOutOfBoundsException(String.format(
658+
"Invalid index %d when selecting the host settings. Must be less than %d", serverIndex, servers.size()
659+
));
660+
}
661+
baseUrl = servers.get(serverIndex).URL(serverVariables);
662+
}
663+
664+
final UriComponentsBuilder builder = UriComponentsBuilder
665+
.fromUriString(baseUrl)
666+
.path(path);
616667

617668
String finalUri = builder.build(false).toUriString();
618669
Map<String, Object> uriParams = new HashMap<>();

samples/client/others/java/restclient-enum-in-multipart/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,12 @@
3838
import java.text.DateFormat;
3939
import java.text.ParseException;
4040
import java.util.ArrayList;
41+
import java.util.Arrays;
4142
import java.util.Collection;
4243
import java.util.Collections;
4344
import java.util.Date;
4445
import java.util.HashMap;
46+
import java.util.HashSet;
4547
import java.util.List;
4648
import java.util.Map;
4749
import java.util.Map.Entry;
@@ -76,6 +78,15 @@ protected String collectionToString(Collection<?> collection) {
7678
protected final MultiValueMap<String, String> defaultCookies = new LinkedMultiValueMap<>();
7779

7880
protected String basePath = "http://localhost:8080";
81+
protected List<ServerConfiguration> servers = new ArrayList<ServerConfiguration>(Arrays.asList(
82+
new ServerConfiguration(
83+
"http://localhost:8080",
84+
"Localhost, used when testing",
85+
new HashMap<String, ServerVariable>()
86+
)
87+
));
88+
protected Integer serverIndex = 0;
89+
protected Map<String, String> serverVariables = null;
7990

8091
protected final RestClient restClient;
8192
protected final DateFormat dateFormat;
@@ -187,6 +198,34 @@ public String getBasePath() {
187198
*/
188199
public ApiClient setBasePath(String basePath) {
189200
this.basePath = basePath;
201+
this.serverIndex = null;
202+
return this;
203+
}
204+
205+
public List<ServerConfiguration> getServers() {
206+
return servers;
207+
}
208+
209+
public ApiClient setServers(List<ServerConfiguration> servers) {
210+
this.servers = servers;
211+
return this;
212+
}
213+
214+
public Integer getServerIndex() {
215+
return serverIndex;
216+
}
217+
218+
public ApiClient setServerIndex(Integer serverIndex) {
219+
this.serverIndex = serverIndex;
220+
return this;
221+
}
222+
223+
public Map<String, String> getServerVariables() {
224+
return serverVariables;
225+
}
226+
227+
public ApiClient setServerVariables(Map<String, String> serverVariables) {
228+
this.serverVariables = serverVariables;
190229
return this;
191230
}
192231

@@ -611,7 +650,19 @@ protected RestClient.RequestBodySpec prepareRequest(String path, HttpMethod meth
611650
MediaType contentType, String[] authNames) {
612651
updateParamsForAuth(authNames, queryParams, headerParams, cookieParams);
613652

614-
final UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(basePath).path(path);
653+
String baseUrl = basePath;
654+
if (serverIndex != null) {
655+
if (serverIndex < 0 || serverIndex >= servers.size()) {
656+
throw new ArrayIndexOutOfBoundsException(String.format(
657+
"Invalid index %d when selecting the host settings. Must be less than %d", serverIndex, servers.size()
658+
));
659+
}
660+
baseUrl = servers.get(serverIndex).URL(serverVariables);
661+
}
662+
663+
final UriComponentsBuilder builder = UriComponentsBuilder
664+
.fromUriString(baseUrl)
665+
.path(path);
615666

616667
String finalUri = builder.build(false).toUriString();
617668
Map<String, Object> uriParams = new HashMap<>();

samples/client/others/java/restclient-useAbstractionForFiles/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,12 @@
3838
import java.text.DateFormat;
3939
import java.text.ParseException;
4040
import java.util.ArrayList;
41+
import java.util.Arrays;
4142
import java.util.Collection;
4243
import java.util.Collections;
4344
import java.util.Date;
4445
import java.util.HashMap;
46+
import java.util.HashSet;
4547
import java.util.List;
4648
import java.util.Map;
4749
import java.util.Map.Entry;
@@ -76,6 +78,15 @@ protected String collectionToString(Collection<?> collection) {
7678
protected final MultiValueMap<String, String> defaultCookies = new LinkedMultiValueMap<>();
7779

7880
protected String basePath = "http://localhost";
81+
protected List<ServerConfiguration> servers = new ArrayList<ServerConfiguration>(Arrays.asList(
82+
new ServerConfiguration(
83+
"",
84+
"No description provided",
85+
new HashMap<String, ServerVariable>()
86+
)
87+
));
88+
protected Integer serverIndex = 0;
89+
protected Map<String, String> serverVariables = null;
7990

8091
protected final RestClient restClient;
8192
protected final DateFormat dateFormat;
@@ -186,6 +197,34 @@ public String getBasePath() {
186197
*/
187198
public ApiClient setBasePath(String basePath) {
188199
this.basePath = basePath;
200+
this.serverIndex = null;
201+
return this;
202+
}
203+
204+
public List<ServerConfiguration> getServers() {
205+
return servers;
206+
}
207+
208+
public ApiClient setServers(List<ServerConfiguration> servers) {
209+
this.servers = servers;
210+
return this;
211+
}
212+
213+
public Integer getServerIndex() {
214+
return serverIndex;
215+
}
216+
217+
public ApiClient setServerIndex(Integer serverIndex) {
218+
this.serverIndex = serverIndex;
219+
return this;
220+
}
221+
222+
public Map<String, String> getServerVariables() {
223+
return serverVariables;
224+
}
225+
226+
public ApiClient setServerVariables(Map<String, String> serverVariables) {
227+
this.serverVariables = serverVariables;
189228
return this;
190229
}
191230

@@ -610,7 +649,19 @@ protected RestClient.RequestBodySpec prepareRequest(String path, HttpMethod meth
610649
MediaType contentType, String[] authNames) {
611650
updateParamsForAuth(authNames, queryParams, headerParams, cookieParams);
612651

613-
final UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(basePath).path(path);
652+
String baseUrl = basePath;
653+
if (serverIndex != null) {
654+
if (serverIndex < 0 || serverIndex >= servers.size()) {
655+
throw new ArrayIndexOutOfBoundsException(String.format(
656+
"Invalid index %d when selecting the host settings. Must be less than %d", serverIndex, servers.size()
657+
));
658+
}
659+
baseUrl = servers.get(serverIndex).URL(serverVariables);
660+
}
661+
662+
final UriComponentsBuilder builder = UriComponentsBuilder
663+
.fromUriString(baseUrl)
664+
.path(path);
614665

615666
String finalUri = builder.build(false).toUriString();
616667
Map<String, Object> uriParams = new HashMap<>();

0 commit comments

Comments
 (0)