Skip to content
Prev Previous commit
Next Next commit
throw ArrayIndexOutOfBoundsException
  • Loading branch information
jirikuncar committed Jan 16, 2020
commit 5b93d63ac9b0bc3e5336669a1c49621de601f375
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public class ApiClient {
private Map<String, String> defaultHeaderMap = new HashMap<String, String>();
private Map<String, String> defaultCookieMap = new HashMap<String, String>();
private String basePath = "{{{basePath}}}";
protected ServerConfiguration[] servers{{#servers}}{{#-first}} = {
protected List<ServerConfiguration> servers = new ArrayList<ServerConfiguration>({{#servers}}{{#-first}}Arrays.asList(
{{/-first}} new ServerConfiguration(
"{{{url}}}",
"{{{description}}}{{^description}}No description provided{{/description}}",
Expand All @@ -91,7 +91,7 @@ public class ApiClient {
}}{{/-last}}{{/variables}}
){{^-last}},{{/-last}}
{{#-last}}
}{{/-last}}{{/servers}};
){{/-last}}{{/servers}});
protected Integer serverIndex = 0;
protected Map<String, String> serverVariables = null;
private boolean debugging = false;
Expand Down Expand Up @@ -208,11 +208,11 @@ public class ApiClient {
return this;
}

public ServerConfiguration[] getServers() {
public List<ServerConfiguration> getServers() {
return servers;
}

public ApiClient setServers(ServerConfiguration[] servers) {
public ApiClient setServers(List<ServerConfiguration> servers) {
this.servers = servers;
return this;
}
Expand Down Expand Up @@ -686,7 +686,12 @@ public class ApiClient {
private String buildUrl(String path, List<Pair> queryParams, List<Pair> collectionQueryParams) {
String baseURL;
if (serverIndex != null) {
baseURL = servers[serverIndex].URL(serverVariables);
if (index < 0 || index >= servers.size()) {
throw new ArrayIndexOutOfBoundsException(String.format(
"Invalid index %d when selecting the host settings. Must be less than %d", index, servers.size()
));
}
baseURL = servers.get(serverIndex).URL(serverVariables);
} else {
baseURL = basePath;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public class ApiClient {
protected Map<String, String> defaultHeaderMap = new HashMap<String, String>();
protected Map<String, String> defaultCookieMap = new HashMap<String, String>();
protected String basePath = "{{{basePath}}}";
protected ServerConfiguration[] servers{{#servers}}{{#-first}} = {
protected List<ServerConfiguration> servers = new ArrayList<ServerConfiguration>({{#servers}}{{#-first}}Arrays.asList(
{{/-first}} new ServerConfiguration(
"{{{url}}}",
"{{{description}}}{{^description}}No description provided{{/description}}",
Expand All @@ -93,7 +93,7 @@ public class ApiClient {
}}{{/-last}}{{/variables}}
){{^-last}},{{/-last}}
{{#-last}}
}{{/-last}}{{/servers}};
){{/-last}}{{/servers}});
protected Integer serverIndex = 0;
protected Map<String, String> serverVariables = null;
protected Map<String, List<ServerConfiguration>> operationServers = new HashMap<String, List<ServerConfiguration>>() {{
Expand Down Expand Up @@ -194,11 +194,11 @@ public class ApiClient {
return this;
}

public ServerConfiguration[] getServers() {
public List<ServerConfiguration> getServers() {
return servers;
}

public ApiClient setServers(ServerConfiguration[] servers) {
public ApiClient setServers(List<ServerConfiguration> servers) {
this.servers = servers;
return this;
}
Expand Down Expand Up @@ -792,13 +792,25 @@ public class ApiClient {
// to support (constant) query string in `path`, e.g. "/posts?draft=1"
String targetURL;
if (serverIndex != null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Notice here to consumers that if new server variable functionality causes issue, one may call setServerIndex(null) to revert to previous behavior. Current default behavior with the server index 0 behaves in the same way as before, but moves the URL configuration to the servers array.

Integer index;
List<ServerConfiguration> serverConfigurations;
Map<String, String> variables;

if (operationServers.containsKey(operation)) {
Integer index = operationServerIndex.getOrDefault(operation, serverIndex);
Map<String, String> variables = operationServerVariables.getOrDefault(operation, serverVariables);
targetURL = operationServers.get(operation).get(index).URL(variables);
index = operationServerIndex.getOrDefault(operation, serverIndex);
variables = operationServerVariables.getOrDefault(operation, serverVariables);
serverConfigurations = operationServers.get(operation);
} else {
targetURL = servers[serverIndex].URL(serverVariables) + path;
index = serverIndex;
variables = serverVariables;
serverConfigurations = servers;
}
if (index < 0 || index >= serverConfigurations.size()) {
throw new ArrayIndexOutOfBoundsException(String.format(
"Invalid index %d when selecting the host settings. Must be less than %d", index, serverConfigurations.size()
));
}
targetURL = serverConfigurations.get(index).URL(variables) + path;
} else {
targetURL = this.basePath + path;
}
Expand Down