Skip to content

Commit d42ff75

Browse files
authored
Handle variables in server declaration (#614)
1 parent 4156bb9 commit d42ff75

2 files changed

Lines changed: 80 additions & 2 deletions

File tree

modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/URLPathUtils.java

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,26 @@
1919

2020
import io.swagger.v3.oas.models.OpenAPI;
2121
import io.swagger.v3.oas.models.servers.Server;
22+
import io.swagger.v3.oas.models.servers.ServerVariable;
23+
import io.swagger.v3.oas.models.servers.ServerVariables;
2224

2325
import org.openapitools.codegen.CodegenConfig;
2426
import org.slf4j.Logger;
2527
import org.slf4j.LoggerFactory;
2628

2729
import java.net.MalformedURLException;
2830
import java.net.URL;
31+
import java.util.HashSet;
2932
import java.util.List;
33+
import java.util.Set;
34+
import java.util.regex.Matcher;
35+
import java.util.regex.Pattern;
3036

3137
public class URLPathUtils {
3238

3339
private static final Logger LOGGER = LoggerFactory.getLogger(URLPathUtils.class);
3440
public static final String LOCAL_HOST = "http://localhost";
41+
public static final Pattern VARIABLE_PATTERN = Pattern.compile("\\{([^\\}]+)\\}");
3542

3643
public static URL getServerURL(OpenAPI openAPI) {
3744
final List<Server> servers = openAPI.getServers();
@@ -40,8 +47,40 @@ public static URL getServerURL(OpenAPI openAPI) {
4047
return getDefaultUrl();
4148
}
4249
// TODO need a way to obtain all server URLs
43-
final Server server = servers.get(0);
44-
String url = sanitizeUrl(server.getUrl());
50+
return getServerURL(servers.get(0));
51+
}
52+
53+
static URL getServerURL(final Server server) {
54+
String url = server.getUrl();
55+
ServerVariables variables = server.getVariables();
56+
if(variables == null) {
57+
variables = new ServerVariables();
58+
}
59+
Set<String> replacedVariables = new HashSet<>();
60+
Matcher matcher = VARIABLE_PATTERN.matcher(url);
61+
while(matcher.find()) {
62+
if(!replacedVariables.contains(matcher.group())) {
63+
ServerVariable variable = variables.get(matcher.group(1));
64+
String replacement;
65+
if(variable != null) {
66+
if(variable.getDefault() != null) {
67+
replacement = variable.getDefault();
68+
} else if(variable.getEnum() != null && !variable.getEnum().isEmpty()) {
69+
replacement = variable.getEnum().get(0);
70+
} else {
71+
LOGGER.warn("No value found for variable '{}' in server definition '{}', default to empty string.", matcher.group(1), server.getUrl());
72+
replacement = "";
73+
}
74+
} else {
75+
LOGGER.warn("No variable '{}' found in server definition '{}', default to empty string.", matcher.group(1), server.getUrl());
76+
replacement = "";
77+
}
78+
url = url.replace(matcher.group(), replacement);
79+
replacedVariables.add(matcher.group());
80+
matcher = VARIABLE_PATTERN.matcher(url);
81+
}
82+
}
83+
url = sanitizeUrl(url);
4584

4685
try {
4786
return new URL(url);

modules/openapi-generator/src/test/java/org/openapitools/codegen/utils/URLPathUtilsTest.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,14 @@
1919

2020
import io.swagger.v3.oas.models.OpenAPI;
2121
import io.swagger.v3.oas.models.servers.Server;
22+
import io.swagger.v3.oas.models.servers.ServerVariable;
23+
import io.swagger.v3.oas.models.servers.ServerVariables;
2224

2325
import org.testng.Assert;
2426
import org.testng.annotations.Test;
2527

2628
import java.net.URL;
29+
import java.util.Arrays;
2730

2831
public class URLPathUtilsTest {
2932

@@ -75,4 +78,40 @@ public void testSanitizeUrl() throws Exception {
7578
Assert.assertEquals(URLPathUtils.getServerURL(openAPI).toString(), t[1]);
7679
}
7780
}
81+
82+
@Test
83+
public void testGetServerURLWithVariables() throws Exception {
84+
Server s1 = new Server().url("http://localhost:{port}/").variables(new ServerVariables().addServerVariable("port", new ServerVariable()._default("8080").description("the server port")));
85+
Assert.assertEquals(URLPathUtils.getServerURL(s1).toString(), "http://localhost:8080/");
86+
87+
Server s2 = new Server().url("http://{version}.test.me/{version}").variables(new ServerVariables().addServerVariable("version", new ServerVariable()._default("v1")));
88+
Assert.assertEquals(URLPathUtils.getServerURL(s2).toString(), "http://v1.test.me/v1");
89+
90+
Server s3 = new Server().url("http://localhost:{port}/{version}").variables(
91+
new ServerVariables().addServerVariable("version", new ServerVariable()._default("v4"))
92+
.addServerVariable("port", new ServerVariable()._default("8080"))
93+
.addServerVariable("other", new ServerVariable()._default("something"))
94+
);
95+
Assert.assertEquals(URLPathUtils.getServerURL(s3).toString(), "http://localhost:8080/v4");
96+
97+
Server s4 = new Server().url("http://91.161.147.64/{targetEnv}").variables(new ServerVariables().addServerVariable("targetEnv", new ServerVariable().description("target environment")._enum(Arrays.asList("dev", "int", "prd"))._default("prd")));
98+
Assert.assertEquals(URLPathUtils.getServerURL(s4).toString(), "http://91.161.147.64/prd");
99+
100+
Server s5 = new Server().url("https://api.stats.com/{country1}").variables(new ServerVariables().addServerVariable("country1", new ServerVariable()._enum(Arrays.asList("france", "germany", "italy"))));
101+
Assert.assertEquals(URLPathUtils.getServerURL(s5).toString(), "https://api.stats.com/france");
102+
103+
Server s6 = new Server().url("https://api.example.com/{wrong}");
104+
Assert.assertEquals(URLPathUtils.getServerURL(s6).toString(), "https://api.example.com/");
105+
106+
Server s7 = new Server().url("https://api.example.com/{wrong}").variables(new ServerVariables());
107+
Assert.assertEquals(URLPathUtils.getServerURL(s7).toString(), "https://api.example.com/");
108+
109+
Server s8 = new Server().url("https://api.example.com/{wrong}").variables(new ServerVariables().addServerVariable("other", new ServerVariable()._default("something")));
110+
Assert.assertEquals(URLPathUtils.getServerURL(s8).toString(), "https://api.example.com/");
111+
112+
Server s9 = new Server().url("https://{user}.example.com/{version}").variables(
113+
new ServerVariables().addServerVariable("version", new ServerVariable()._default("v1"))
114+
.addServerVariable("user", new ServerVariable()._default("{user}")));
115+
Assert.assertEquals(URLPathUtils.getServerURL(s9).toString(), "https://{user}.example.com/v1");
116+
}
78117
}

0 commit comments

Comments
 (0)