Skip to content

Commit

Permalink
Merge pull request #12259 from swagger-api/douglasbgray-Issue-8412
Browse files Browse the repository at this point in the history
Douglasbgray issue 8412
  • Loading branch information
HugoMario committed Oct 26, 2023
2 parents 87ad143 + a1ba22c commit 2ece4c0
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
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);
}
}

0 comments on commit 2ece4c0

Please sign in to comment.