Skip to content

Douglasbgray issue 8412 #12259

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,23 @@
import io.swagger.codegen.v3.CodegenConfig;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.servers.Server;
import io.swagger.v3.oas.models.servers.ServerVariables;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.text.StrSubstitutor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class URLPathUtil {

protected static final Logger LOGGER = LoggerFactory.getLogger(URLPathUtil.class);
public static String DEFAULT_PATH = "/";
public static String DEFAULT_PORT = "8080";
public static final String LOCAL_HOST = "http://localhost:8080";

public static URL getServerURL(OpenAPI openAPI) {
Expand All @@ -30,7 +35,8 @@ public static URL getServerURL(OpenAPI openAPI) {
url = LOCAL_HOST + url;
}
try {
return new URL(url);
String varReplacedUrl = replaceServerVarsWthDefaultValues(url, server.getVariables());
return new URL(varReplacedUrl);
} catch (MalformedURLException e) {
LOGGER.warn("Not valid URL: " + server.getUrl(), e);
return null;
Expand Down Expand Up @@ -67,7 +73,8 @@ public static URL getServerURL(OpenAPI openAPI, CodegenConfig config) {
serverUrl = inputURL;
break;
}
return new URL(serverUrl);
String varReplacedUrl = replaceServerVarsWthDefaultValues(serverUrl, server.getVariables());
return new URL(varReplacedUrl);
} catch (Exception e) {
LOGGER.warn("Not valid URL: " + server.getUrl(), e);
return null;
Expand All @@ -94,4 +101,16 @@ public static String getHost(OpenAPI openAPI){
}
return LOCAL_HOST;
}

private static String replaceServerVarsWthDefaultValues(String url, ServerVariables vars) {
if (vars != null && vars.size() > 0) {
Map<String, Object> defaultValues = vars.entrySet()
.stream()
.collect(Collectors.toMap(
Map.Entry::getKey,
e -> e.getValue().getDefault() != null ? e.getValue().getDefault() : DEFAULT_PORT));
return StrSubstitutor.replace(url, defaultValues, "{", "}");
}
return url;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import io.swagger.codegen.v3.CodegenConfig;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.servers.Server;
import io.swagger.v3.oas.models.servers.ServerVariable;
import io.swagger.v3.oas.models.servers.ServerVariables;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.testng.Assert;
Expand Down Expand Up @@ -118,4 +120,36 @@ public void testRelativeServerURLv3() {
verify(servers).isEmpty();
verify(config).getInputURL();
}

@Test (description = "verify a url with variable substitutions.")
public void testVariableSubstitution() throws Exception {
ServerVariable portVariable = new ServerVariable();
portVariable.setDefault("8080");

ServerVariables vars = new ServerVariables();
vars.addServerVariable("port", portVariable);

when(server.getVariables()).thenReturn(vars);
when(server.getUrl()).thenReturn("http://myhost:{port}/mypath");

URL url = URLPathUtil.getServerURL(openAPI, config);

Assert.assertEquals(new URL("http://myhost:8080/mypath"), url);
}

@Test (description = "verify a url with variable substitutions when default is missing.")
public void testVariableSubstitutionMissingDefault() throws Exception {
ServerVariable portVariable = new ServerVariable();

ServerVariables vars = new ServerVariables();
vars.addServerVariable("port", portVariable);

when(server.getVariables()).thenReturn(vars);
when(server.getUrl()).thenReturn("http://myhost:{port}/mypath");

// No default for port, resulting URL is invalid.
URL url = URLPathUtil.getServerURL(openAPI, config);

Assert.assertEquals(new URL("http://myhost:8080/mypath"), url);
}
}