diff --git a/modules/openapi-generator/src/main/resources/Java/libraries/restclient/ApiClient.mustache b/modules/openapi-generator/src/main/resources/Java/libraries/restclient/ApiClient.mustache index 2eaf301646c9..850aa6d9147e 100644 --- a/modules/openapi-generator/src/main/resources/Java/libraries/restclient/ApiClient.mustache +++ b/modules/openapi-generator/src/main/resources/Java/libraries/restclient/ApiClient.mustache @@ -42,10 +42,12 @@ import java.util.Optional; import java.text.DateFormat; import java.text.ParseException; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.Date; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -89,6 +91,33 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} { protected final MultiValueMap defaultCookies = new LinkedMultiValueMap<>(); protected String basePath = "{{basePath}}"; + protected List servers = new ArrayList({{#servers}}{{#-first}}Arrays.asList( +{{/-first}} new ServerConfiguration( + "{{{url}}}", + "{{{description}}}{{^description}}No description provided{{/description}}", + new HashMap(){{#variables}}{{#-first}} {{ +{{/-first}} put("{{{name}}}", new ServerVariable( + "{{{description}}}{{^description}}No description provided{{/description}}", + "{{{defaultValue}}}", + new HashSet( + {{#enumValues}} + {{#-first}} + Arrays.asList( + {{/-first}} + "{{{.}}}"{{^-last}},{{/-last}} + {{#-last}} + ) + {{/-last}} + {{/enumValues}} + ) + )); + {{#-last}} + }}{{/-last}}{{/variables}} + ){{^-last}},{{/-last}} + {{#-last}} + ){{/-last}}{{/servers}}); + protected Integer serverIndex = 0; + protected Map serverVariables = null; protected final RestClient restClient; protected final DateFormat dateFormat; @@ -219,6 +248,34 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} { */ public ApiClient setBasePath(String basePath) { this.basePath = basePath; + this.serverIndex = null; + return this; + } + + public List getServers() { + return servers; + } + + public ApiClient setServers(List servers) { + this.servers = servers; + return this; + } + + public Integer getServerIndex() { + return serverIndex; + } + + public ApiClient setServerIndex(Integer serverIndex) { + this.serverIndex = serverIndex; + return this; + } + + public Map getServerVariables() { + return serverVariables; + } + + public ApiClient setServerVariables(Map serverVariables) { + this.serverVariables = serverVariables; return this; } @@ -659,7 +716,19 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} { MediaType contentType, String[] authNames) { updateParamsForAuth(authNames, queryParams, headerParams, cookieParams); - final UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(basePath).path(path); + String baseUrl = basePath; + if (serverIndex != null) { + if (serverIndex < 0 || serverIndex >= servers.size()) { + throw new ArrayIndexOutOfBoundsException(String.format( + "Invalid index %d when selecting the host settings. Must be less than %d", serverIndex, servers.size() + )); + } + baseUrl = servers.get(serverIndex).URL(serverVariables); + } + + final UriComponentsBuilder builder = UriComponentsBuilder + .fromUriString(baseUrl) + .path(path); String finalUri = builder.build(false).toUriString(); Map uriParams = new HashMap<>(); diff --git a/samples/client/echo_api/java/restclient/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/echo_api/java/restclient/src/main/java/org/openapitools/client/ApiClient.java index f5c9f7608999..ba45aa4009fb 100644 --- a/samples/client/echo_api/java/restclient/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/echo_api/java/restclient/src/main/java/org/openapitools/client/ApiClient.java @@ -38,10 +38,12 @@ import java.text.DateFormat; import java.text.ParseException; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.Date; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -76,6 +78,15 @@ protected String collectionToString(Collection collection) { protected final MultiValueMap defaultCookies = new LinkedMultiValueMap<>(); protected String basePath = "http://localhost:3000"; + protected List servers = new ArrayList(Arrays.asList( + new ServerConfiguration( + "http://localhost:3000", + "No description provided", + new HashMap() + ) + )); + protected Integer serverIndex = 0; + protected Map serverVariables = null; protected final RestClient restClient; protected final DateFormat dateFormat; @@ -188,6 +199,34 @@ public String getBasePath() { */ public ApiClient setBasePath(String basePath) { this.basePath = basePath; + this.serverIndex = null; + return this; + } + + public List getServers() { + return servers; + } + + public ApiClient setServers(List servers) { + this.servers = servers; + return this; + } + + public Integer getServerIndex() { + return serverIndex; + } + + public ApiClient setServerIndex(Integer serverIndex) { + this.serverIndex = serverIndex; + return this; + } + + public Map getServerVariables() { + return serverVariables; + } + + public ApiClient setServerVariables(Map serverVariables) { + this.serverVariables = serverVariables; return this; } @@ -612,7 +651,19 @@ protected RestClient.RequestBodySpec prepareRequest(String path, HttpMethod meth MediaType contentType, String[] authNames) { updateParamsForAuth(authNames, queryParams, headerParams, cookieParams); - final UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(basePath).path(path); + String baseUrl = basePath; + if (serverIndex != null) { + if (serverIndex < 0 || serverIndex >= servers.size()) { + throw new ArrayIndexOutOfBoundsException(String.format( + "Invalid index %d when selecting the host settings. Must be less than %d", serverIndex, servers.size() + )); + } + baseUrl = servers.get(serverIndex).URL(serverVariables); + } + + final UriComponentsBuilder builder = UriComponentsBuilder + .fromUriString(baseUrl) + .path(path); String finalUri = builder.build(false).toUriString(); Map uriParams = new HashMap<>(); diff --git a/samples/client/others/java/restclient-enum-in-multipart/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/others/java/restclient-enum-in-multipart/src/main/java/org/openapitools/client/ApiClient.java index 7551ed6a2627..ddb25c6f36f7 100644 --- a/samples/client/others/java/restclient-enum-in-multipart/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/others/java/restclient-enum-in-multipart/src/main/java/org/openapitools/client/ApiClient.java @@ -38,10 +38,12 @@ import java.text.DateFormat; import java.text.ParseException; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.Date; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -76,6 +78,15 @@ protected String collectionToString(Collection collection) { protected final MultiValueMap defaultCookies = new LinkedMultiValueMap<>(); protected String basePath = "http://localhost:8080"; + protected List servers = new ArrayList(Arrays.asList( + new ServerConfiguration( + "http://localhost:8080", + "Localhost, used when testing", + new HashMap() + ) + )); + protected Integer serverIndex = 0; + protected Map serverVariables = null; protected final RestClient restClient; protected final DateFormat dateFormat; @@ -187,6 +198,34 @@ public String getBasePath() { */ public ApiClient setBasePath(String basePath) { this.basePath = basePath; + this.serverIndex = null; + return this; + } + + public List getServers() { + return servers; + } + + public ApiClient setServers(List servers) { + this.servers = servers; + return this; + } + + public Integer getServerIndex() { + return serverIndex; + } + + public ApiClient setServerIndex(Integer serverIndex) { + this.serverIndex = serverIndex; + return this; + } + + public Map getServerVariables() { + return serverVariables; + } + + public ApiClient setServerVariables(Map serverVariables) { + this.serverVariables = serverVariables; return this; } @@ -611,7 +650,19 @@ protected RestClient.RequestBodySpec prepareRequest(String path, HttpMethod meth MediaType contentType, String[] authNames) { updateParamsForAuth(authNames, queryParams, headerParams, cookieParams); - final UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(basePath).path(path); + String baseUrl = basePath; + if (serverIndex != null) { + if (serverIndex < 0 || serverIndex >= servers.size()) { + throw new ArrayIndexOutOfBoundsException(String.format( + "Invalid index %d when selecting the host settings. Must be less than %d", serverIndex, servers.size() + )); + } + baseUrl = servers.get(serverIndex).URL(serverVariables); + } + + final UriComponentsBuilder builder = UriComponentsBuilder + .fromUriString(baseUrl) + .path(path); String finalUri = builder.build(false).toUriString(); Map uriParams = new HashMap<>(); diff --git a/samples/client/others/java/restclient-useAbstractionForFiles/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/others/java/restclient-useAbstractionForFiles/src/main/java/org/openapitools/client/ApiClient.java index d8854fc4c5d1..aab195efcbb2 100644 --- a/samples/client/others/java/restclient-useAbstractionForFiles/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/others/java/restclient-useAbstractionForFiles/src/main/java/org/openapitools/client/ApiClient.java @@ -38,10 +38,12 @@ import java.text.DateFormat; import java.text.ParseException; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.Date; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -76,6 +78,15 @@ protected String collectionToString(Collection collection) { protected final MultiValueMap defaultCookies = new LinkedMultiValueMap<>(); protected String basePath = "http://localhost"; + protected List servers = new ArrayList(Arrays.asList( + new ServerConfiguration( + "", + "No description provided", + new HashMap() + ) + )); + protected Integer serverIndex = 0; + protected Map serverVariables = null; protected final RestClient restClient; protected final DateFormat dateFormat; @@ -186,6 +197,34 @@ public String getBasePath() { */ public ApiClient setBasePath(String basePath) { this.basePath = basePath; + this.serverIndex = null; + return this; + } + + public List getServers() { + return servers; + } + + public ApiClient setServers(List servers) { + this.servers = servers; + return this; + } + + public Integer getServerIndex() { + return serverIndex; + } + + public ApiClient setServerIndex(Integer serverIndex) { + this.serverIndex = serverIndex; + return this; + } + + public Map getServerVariables() { + return serverVariables; + } + + public ApiClient setServerVariables(Map serverVariables) { + this.serverVariables = serverVariables; return this; } @@ -610,7 +649,19 @@ protected RestClient.RequestBodySpec prepareRequest(String path, HttpMethod meth MediaType contentType, String[] authNames) { updateParamsForAuth(authNames, queryParams, headerParams, cookieParams); - final UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(basePath).path(path); + String baseUrl = basePath; + if (serverIndex != null) { + if (serverIndex < 0 || serverIndex >= servers.size()) { + throw new ArrayIndexOutOfBoundsException(String.format( + "Invalid index %d when selecting the host settings. Must be less than %d", serverIndex, servers.size() + )); + } + baseUrl = servers.get(serverIndex).URL(serverVariables); + } + + final UriComponentsBuilder builder = UriComponentsBuilder + .fromUriString(baseUrl) + .path(path); String finalUri = builder.build(false).toUriString(); Map uriParams = new HashMap<>(); diff --git a/samples/client/petstore/java/restclient-nullable-arrays/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/restclient-nullable-arrays/src/main/java/org/openapitools/client/ApiClient.java index 67e82c5369c8..e9af525edffb 100644 --- a/samples/client/petstore/java/restclient-nullable-arrays/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/restclient-nullable-arrays/src/main/java/org/openapitools/client/ApiClient.java @@ -38,10 +38,12 @@ import java.text.DateFormat; import java.text.ParseException; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.Date; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -76,6 +78,15 @@ protected String collectionToString(Collection collection) { protected final MultiValueMap defaultCookies = new LinkedMultiValueMap<>(); protected String basePath = "http://localhost"; + protected List servers = new ArrayList(Arrays.asList( + new ServerConfiguration( + "", + "No description provided", + new HashMap() + ) + )); + protected Integer serverIndex = 0; + protected Map serverVariables = null; protected final RestClient restClient; protected final DateFormat dateFormat; @@ -186,6 +197,34 @@ public String getBasePath() { */ public ApiClient setBasePath(String basePath) { this.basePath = basePath; + this.serverIndex = null; + return this; + } + + public List getServers() { + return servers; + } + + public ApiClient setServers(List servers) { + this.servers = servers; + return this; + } + + public Integer getServerIndex() { + return serverIndex; + } + + public ApiClient setServerIndex(Integer serverIndex) { + this.serverIndex = serverIndex; + return this; + } + + public Map getServerVariables() { + return serverVariables; + } + + public ApiClient setServerVariables(Map serverVariables) { + this.serverVariables = serverVariables; return this; } @@ -610,7 +649,19 @@ protected RestClient.RequestBodySpec prepareRequest(String path, HttpMethod meth MediaType contentType, String[] authNames) { updateParamsForAuth(authNames, queryParams, headerParams, cookieParams); - final UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(basePath).path(path); + String baseUrl = basePath; + if (serverIndex != null) { + if (serverIndex < 0 || serverIndex >= servers.size()) { + throw new ArrayIndexOutOfBoundsException(String.format( + "Invalid index %d when selecting the host settings. Must be less than %d", serverIndex, servers.size() + )); + } + baseUrl = servers.get(serverIndex).URL(serverVariables); + } + + final UriComponentsBuilder builder = UriComponentsBuilder + .fromUriString(baseUrl) + .path(path); String finalUri = builder.build(false).toUriString(); Map uriParams = new HashMap<>(); diff --git a/samples/client/petstore/java/restclient-swagger2/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/restclient-swagger2/src/main/java/org/openapitools/client/ApiClient.java index e785d06609f0..2c17a23d4886 100644 --- a/samples/client/petstore/java/restclient-swagger2/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/restclient-swagger2/src/main/java/org/openapitools/client/ApiClient.java @@ -38,10 +38,12 @@ import java.text.DateFormat; import java.text.ParseException; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.Date; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -77,6 +79,58 @@ protected String collectionToString(Collection collection) { protected final MultiValueMap defaultCookies = new LinkedMultiValueMap<>(); protected String basePath = "http://petstore.swagger.io:80/v2"; + protected List servers = new ArrayList(Arrays.asList( + new ServerConfiguration( + "http://{server}.swagger.io:{port}/v2", + "petstore server", + new HashMap() {{ + put("server", new ServerVariable( + "No description provided", + "petstore", + new HashSet( + Arrays.asList( + "petstore", + "qa-petstore", + "dev-petstore" + ) + ) + )); + put("port", new ServerVariable( + "No description provided", + "80", + new HashSet( + Arrays.asList( + "80", + "8080" + ) + ) + )); + }} + ), + new ServerConfiguration( + "https://localhost:8080/{version}", + "The local server", + new HashMap() {{ + put("version", new ServerVariable( + "No description provided", + "v2", + new HashSet( + Arrays.asList( + "v1", + "v2" + ) + ) + )); + }} + ), + new ServerConfiguration( + "https://127.0.0.1/no_varaible", + "The local server without variables", + new HashMap() + ) + )); + protected Integer serverIndex = 0; + protected Map serverVariables = null; protected final RestClient restClient; protected final DateFormat dateFormat; @@ -192,6 +246,34 @@ public String getBasePath() { */ public ApiClient setBasePath(String basePath) { this.basePath = basePath; + this.serverIndex = null; + return this; + } + + public List getServers() { + return servers; + } + + public ApiClient setServers(List servers) { + this.servers = servers; + return this; + } + + public Integer getServerIndex() { + return serverIndex; + } + + public ApiClient setServerIndex(Integer serverIndex) { + this.serverIndex = serverIndex; + return this; + } + + public Map getServerVariables() { + return serverVariables; + } + + public ApiClient setServerVariables(Map serverVariables) { + this.serverVariables = serverVariables; return this; } @@ -630,7 +712,19 @@ protected RestClient.RequestBodySpec prepareRequest(String path, HttpMethod meth MediaType contentType, String[] authNames) { updateParamsForAuth(authNames, queryParams, headerParams, cookieParams); - final UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(basePath).path(path); + String baseUrl = basePath; + if (serverIndex != null) { + if (serverIndex < 0 || serverIndex >= servers.size()) { + throw new ArrayIndexOutOfBoundsException(String.format( + "Invalid index %d when selecting the host settings. Must be less than %d", serverIndex, servers.size() + )); + } + baseUrl = servers.get(serverIndex).URL(serverVariables); + } + + final UriComponentsBuilder builder = UriComponentsBuilder + .fromUriString(baseUrl) + .path(path); String finalUri = builder.build(false).toUriString(); Map uriParams = new HashMap<>(); diff --git a/samples/client/petstore/java/restclient-useSingleRequestParameter-static/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/restclient-useSingleRequestParameter-static/src/main/java/org/openapitools/client/ApiClient.java index e785d06609f0..2c17a23d4886 100644 --- a/samples/client/petstore/java/restclient-useSingleRequestParameter-static/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/restclient-useSingleRequestParameter-static/src/main/java/org/openapitools/client/ApiClient.java @@ -38,10 +38,12 @@ import java.text.DateFormat; import java.text.ParseException; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.Date; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -77,6 +79,58 @@ protected String collectionToString(Collection collection) { protected final MultiValueMap defaultCookies = new LinkedMultiValueMap<>(); protected String basePath = "http://petstore.swagger.io:80/v2"; + protected List servers = new ArrayList(Arrays.asList( + new ServerConfiguration( + "http://{server}.swagger.io:{port}/v2", + "petstore server", + new HashMap() {{ + put("server", new ServerVariable( + "No description provided", + "petstore", + new HashSet( + Arrays.asList( + "petstore", + "qa-petstore", + "dev-petstore" + ) + ) + )); + put("port", new ServerVariable( + "No description provided", + "80", + new HashSet( + Arrays.asList( + "80", + "8080" + ) + ) + )); + }} + ), + new ServerConfiguration( + "https://localhost:8080/{version}", + "The local server", + new HashMap() {{ + put("version", new ServerVariable( + "No description provided", + "v2", + new HashSet( + Arrays.asList( + "v1", + "v2" + ) + ) + )); + }} + ), + new ServerConfiguration( + "https://127.0.0.1/no_varaible", + "The local server without variables", + new HashMap() + ) + )); + protected Integer serverIndex = 0; + protected Map serverVariables = null; protected final RestClient restClient; protected final DateFormat dateFormat; @@ -192,6 +246,34 @@ public String getBasePath() { */ public ApiClient setBasePath(String basePath) { this.basePath = basePath; + this.serverIndex = null; + return this; + } + + public List getServers() { + return servers; + } + + public ApiClient setServers(List servers) { + this.servers = servers; + return this; + } + + public Integer getServerIndex() { + return serverIndex; + } + + public ApiClient setServerIndex(Integer serverIndex) { + this.serverIndex = serverIndex; + return this; + } + + public Map getServerVariables() { + return serverVariables; + } + + public ApiClient setServerVariables(Map serverVariables) { + this.serverVariables = serverVariables; return this; } @@ -630,7 +712,19 @@ protected RestClient.RequestBodySpec prepareRequest(String path, HttpMethod meth MediaType contentType, String[] authNames) { updateParamsForAuth(authNames, queryParams, headerParams, cookieParams); - final UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(basePath).path(path); + String baseUrl = basePath; + if (serverIndex != null) { + if (serverIndex < 0 || serverIndex >= servers.size()) { + throw new ArrayIndexOutOfBoundsException(String.format( + "Invalid index %d when selecting the host settings. Must be less than %d", serverIndex, servers.size() + )); + } + baseUrl = servers.get(serverIndex).URL(serverVariables); + } + + final UriComponentsBuilder builder = UriComponentsBuilder + .fromUriString(baseUrl) + .path(path); String finalUri = builder.build(false).toUriString(); Map uriParams = new HashMap<>(); diff --git a/samples/client/petstore/java/restclient-useSingleRequestParameter/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/restclient-useSingleRequestParameter/src/main/java/org/openapitools/client/ApiClient.java index e785d06609f0..2c17a23d4886 100644 --- a/samples/client/petstore/java/restclient-useSingleRequestParameter/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/restclient-useSingleRequestParameter/src/main/java/org/openapitools/client/ApiClient.java @@ -38,10 +38,12 @@ import java.text.DateFormat; import java.text.ParseException; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.Date; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -77,6 +79,58 @@ protected String collectionToString(Collection collection) { protected final MultiValueMap defaultCookies = new LinkedMultiValueMap<>(); protected String basePath = "http://petstore.swagger.io:80/v2"; + protected List servers = new ArrayList(Arrays.asList( + new ServerConfiguration( + "http://{server}.swagger.io:{port}/v2", + "petstore server", + new HashMap() {{ + put("server", new ServerVariable( + "No description provided", + "petstore", + new HashSet( + Arrays.asList( + "petstore", + "qa-petstore", + "dev-petstore" + ) + ) + )); + put("port", new ServerVariable( + "No description provided", + "80", + new HashSet( + Arrays.asList( + "80", + "8080" + ) + ) + )); + }} + ), + new ServerConfiguration( + "https://localhost:8080/{version}", + "The local server", + new HashMap() {{ + put("version", new ServerVariable( + "No description provided", + "v2", + new HashSet( + Arrays.asList( + "v1", + "v2" + ) + ) + )); + }} + ), + new ServerConfiguration( + "https://127.0.0.1/no_varaible", + "The local server without variables", + new HashMap() + ) + )); + protected Integer serverIndex = 0; + protected Map serverVariables = null; protected final RestClient restClient; protected final DateFormat dateFormat; @@ -192,6 +246,34 @@ public String getBasePath() { */ public ApiClient setBasePath(String basePath) { this.basePath = basePath; + this.serverIndex = null; + return this; + } + + public List getServers() { + return servers; + } + + public ApiClient setServers(List servers) { + this.servers = servers; + return this; + } + + public Integer getServerIndex() { + return serverIndex; + } + + public ApiClient setServerIndex(Integer serverIndex) { + this.serverIndex = serverIndex; + return this; + } + + public Map getServerVariables() { + return serverVariables; + } + + public ApiClient setServerVariables(Map serverVariables) { + this.serverVariables = serverVariables; return this; } @@ -630,7 +712,19 @@ protected RestClient.RequestBodySpec prepareRequest(String path, HttpMethod meth MediaType contentType, String[] authNames) { updateParamsForAuth(authNames, queryParams, headerParams, cookieParams); - final UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(basePath).path(path); + String baseUrl = basePath; + if (serverIndex != null) { + if (serverIndex < 0 || serverIndex >= servers.size()) { + throw new ArrayIndexOutOfBoundsException(String.format( + "Invalid index %d when selecting the host settings. Must be less than %d", serverIndex, servers.size() + )); + } + baseUrl = servers.get(serverIndex).URL(serverVariables); + } + + final UriComponentsBuilder builder = UriComponentsBuilder + .fromUriString(baseUrl) + .path(path); String finalUri = builder.build(false).toUriString(); Map uriParams = new HashMap<>(); diff --git a/samples/client/petstore/java/restclient/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/restclient/src/main/java/org/openapitools/client/ApiClient.java index e785d06609f0..2c17a23d4886 100644 --- a/samples/client/petstore/java/restclient/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/restclient/src/main/java/org/openapitools/client/ApiClient.java @@ -38,10 +38,12 @@ import java.text.DateFormat; import java.text.ParseException; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.Date; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -77,6 +79,58 @@ protected String collectionToString(Collection collection) { protected final MultiValueMap defaultCookies = new LinkedMultiValueMap<>(); protected String basePath = "http://petstore.swagger.io:80/v2"; + protected List servers = new ArrayList(Arrays.asList( + new ServerConfiguration( + "http://{server}.swagger.io:{port}/v2", + "petstore server", + new HashMap() {{ + put("server", new ServerVariable( + "No description provided", + "petstore", + new HashSet( + Arrays.asList( + "petstore", + "qa-petstore", + "dev-petstore" + ) + ) + )); + put("port", new ServerVariable( + "No description provided", + "80", + new HashSet( + Arrays.asList( + "80", + "8080" + ) + ) + )); + }} + ), + new ServerConfiguration( + "https://localhost:8080/{version}", + "The local server", + new HashMap() {{ + put("version", new ServerVariable( + "No description provided", + "v2", + new HashSet( + Arrays.asList( + "v1", + "v2" + ) + ) + )); + }} + ), + new ServerConfiguration( + "https://127.0.0.1/no_varaible", + "The local server without variables", + new HashMap() + ) + )); + protected Integer serverIndex = 0; + protected Map serverVariables = null; protected final RestClient restClient; protected final DateFormat dateFormat; @@ -192,6 +246,34 @@ public String getBasePath() { */ public ApiClient setBasePath(String basePath) { this.basePath = basePath; + this.serverIndex = null; + return this; + } + + public List getServers() { + return servers; + } + + public ApiClient setServers(List servers) { + this.servers = servers; + return this; + } + + public Integer getServerIndex() { + return serverIndex; + } + + public ApiClient setServerIndex(Integer serverIndex) { + this.serverIndex = serverIndex; + return this; + } + + public Map getServerVariables() { + return serverVariables; + } + + public ApiClient setServerVariables(Map serverVariables) { + this.serverVariables = serverVariables; return this; } @@ -630,7 +712,19 @@ protected RestClient.RequestBodySpec prepareRequest(String path, HttpMethod meth MediaType contentType, String[] authNames) { updateParamsForAuth(authNames, queryParams, headerParams, cookieParams); - final UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(basePath).path(path); + String baseUrl = basePath; + if (serverIndex != null) { + if (serverIndex < 0 || serverIndex >= servers.size()) { + throw new ArrayIndexOutOfBoundsException(String.format( + "Invalid index %d when selecting the host settings. Must be less than %d", serverIndex, servers.size() + )); + } + baseUrl = servers.get(serverIndex).URL(serverVariables); + } + + final UriComponentsBuilder builder = UriComponentsBuilder + .fromUriString(baseUrl) + .path(path); String finalUri = builder.build(false).toUriString(); Map uriParams = new HashMap<>();