Skip to content

Commit 2ece4c0

Browse files
authored
Merge pull request #12259 from swagger-api/douglasbgray-Issue-8412
Douglasbgray issue 8412
2 parents 87ad143 + a1ba22c commit 2ece4c0

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

modules/swagger-codegen/src/main/java/io/swagger/codegen/v3/utils/URLPathUtil.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,23 @@
33
import io.swagger.codegen.v3.CodegenConfig;
44
import io.swagger.v3.oas.models.OpenAPI;
55
import io.swagger.v3.oas.models.servers.Server;
6+
import io.swagger.v3.oas.models.servers.ServerVariables;
67
import org.apache.commons.lang3.StringUtils;
8+
import org.apache.commons.lang3.text.StrSubstitutor;
79
import org.slf4j.Logger;
810
import org.slf4j.LoggerFactory;
911

1012
import java.net.MalformedURLException;
1113
import java.net.URL;
1214
import java.util.List;
15+
import java.util.Map;
16+
import java.util.stream.Collectors;
1317

1418
public class URLPathUtil {
1519

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

2025
public static URL getServerURL(OpenAPI openAPI) {
@@ -30,7 +35,8 @@ public static URL getServerURL(OpenAPI openAPI) {
3035
url = LOCAL_HOST + url;
3136
}
3237
try {
33-
return new URL(url);
38+
String varReplacedUrl = replaceServerVarsWthDefaultValues(url, server.getVariables());
39+
return new URL(varReplacedUrl);
3440
} catch (MalformedURLException e) {
3541
LOGGER.warn("Not valid URL: " + server.getUrl(), e);
3642
return null;
@@ -67,7 +73,8 @@ public static URL getServerURL(OpenAPI openAPI, CodegenConfig config) {
6773
serverUrl = inputURL;
6874
break;
6975
}
70-
return new URL(serverUrl);
76+
String varReplacedUrl = replaceServerVarsWthDefaultValues(serverUrl, server.getVariables());
77+
return new URL(varReplacedUrl);
7178
} catch (Exception e) {
7279
LOGGER.warn("Not valid URL: " + server.getUrl(), e);
7380
return null;
@@ -94,4 +101,16 @@ public static String getHost(OpenAPI openAPI){
94101
}
95102
return LOCAL_HOST;
96103
}
104+
105+
private static String replaceServerVarsWthDefaultValues(String url, ServerVariables vars) {
106+
if (vars != null && vars.size() > 0) {
107+
Map<String, Object> defaultValues = vars.entrySet()
108+
.stream()
109+
.collect(Collectors.toMap(
110+
Map.Entry::getKey,
111+
e -> e.getValue().getDefault() != null ? e.getValue().getDefault() : DEFAULT_PORT));
112+
return StrSubstitutor.replace(url, defaultValues, "{", "}");
113+
}
114+
return url;
115+
}
97116
}

modules/swagger-codegen/src/test/java/io/swagger/codegen/v3/utils/URLPathUtilTest.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import io.swagger.codegen.v3.CodegenConfig;
44
import io.swagger.v3.oas.models.OpenAPI;
55
import io.swagger.v3.oas.models.servers.Server;
6+
import io.swagger.v3.oas.models.servers.ServerVariable;
7+
import io.swagger.v3.oas.models.servers.ServerVariables;
68
import org.mockito.Mock;
79
import org.mockito.MockitoAnnotations;
810
import org.testng.Assert;
@@ -118,4 +120,36 @@ public void testRelativeServerURLv3() {
118120
verify(servers).isEmpty();
119121
verify(config).getInputURL();
120122
}
123+
124+
@Test (description = "verify a url with variable substitutions.")
125+
public void testVariableSubstitution() throws Exception {
126+
ServerVariable portVariable = new ServerVariable();
127+
portVariable.setDefault("8080");
128+
129+
ServerVariables vars = new ServerVariables();
130+
vars.addServerVariable("port", portVariable);
131+
132+
when(server.getVariables()).thenReturn(vars);
133+
when(server.getUrl()).thenReturn("http://myhost:{port}/mypath");
134+
135+
URL url = URLPathUtil.getServerURL(openAPI, config);
136+
137+
Assert.assertEquals(new URL("http://myhost:8080/mypath"), url);
138+
}
139+
140+
@Test (description = "verify a url with variable substitutions when default is missing.")
141+
public void testVariableSubstitutionMissingDefault() throws Exception {
142+
ServerVariable portVariable = new ServerVariable();
143+
144+
ServerVariables vars = new ServerVariables();
145+
vars.addServerVariable("port", portVariable);
146+
147+
when(server.getVariables()).thenReturn(vars);
148+
when(server.getUrl()).thenReturn("http://myhost:{port}/mypath");
149+
150+
// No default for port, resulting URL is invalid.
151+
URL url = URLPathUtil.getServerURL(openAPI, config);
152+
153+
Assert.assertEquals(new URL("http://myhost:8080/mypath"), url);
154+
}
121155
}

0 commit comments

Comments
 (0)