Skip to content

Commit 6841e15

Browse files
committed
Merge branch 'Issue-8412' of https://github.com/douglasbgray/swagger-codegen into douglasbgray-Issue-8412
2 parents 87ad143 + b28421f commit 6841e15

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@
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

@@ -30,7 +34,8 @@ public static URL getServerURL(OpenAPI openAPI) {
3034
url = LOCAL_HOST + url;
3135
}
3236
try {
33-
return new URL(url);
37+
String varReplacedUrl = replaceServerVarsWthDefaultValues(url, server.getVariables());
38+
return new URL(varReplacedUrl);
3439
} catch (MalformedURLException e) {
3540
LOGGER.warn("Not valid URL: " + server.getUrl(), e);
3641
return null;
@@ -67,7 +72,8 @@ public static URL getServerURL(OpenAPI openAPI, CodegenConfig config) {
6772
serverUrl = inputURL;
6873
break;
6974
}
70-
return new URL(serverUrl);
75+
String varReplacedUrl = replaceServerVarsWthDefaultValues(serverUrl, server.getVariables());
76+
return new URL(varReplacedUrl);
7177
} catch (Exception e) {
7278
LOGGER.warn("Not valid URL: " + server.getUrl(), e);
7379
return null;
@@ -94,4 +100,16 @@ public static String getHost(OpenAPI openAPI){
94100
}
95101
return LOCAL_HOST;
96102
}
103+
104+
private static String replaceServerVarsWthDefaultValues(String url, ServerVariables vars) {
105+
if (vars != null && vars.size() > 0) {
106+
Map<String, Object> defaultValues = vars.entrySet()
107+
.stream()
108+
.collect(Collectors.toMap(
109+
Map.Entry::getKey,
110+
e -> e.getValue().getDefault()));
111+
return StrSubstitutor.replace(url, defaultValues, "{", "}");
112+
}
113+
return url;
114+
}
97115
}

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.assertNull(url);
154+
}
121155
}

0 commit comments

Comments
 (0)