diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/URLPathUtils.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/URLPathUtils.java index ddeb58d8ca6..c72b60f22e9 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/URLPathUtils.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/URLPathUtils.java @@ -21,6 +21,7 @@ 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.apache.commons.lang3.StringUtils; import org.openapitools.codegen.CodegenConfig; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -52,19 +53,33 @@ public class URLPathUtils { public static URL getServerURL(final Server server) { String url = server.getUrl(); ServerVariables variables = server.getVariables(); - if(variables == null) { + if (variables == null) { variables = new ServerVariables(); } + if (StringUtils.isNotBlank(url)) { + url = extractUrl(server, url, variables); + url = sanitizeUrl(url); + + try { + return new URL(url); + } catch (MalformedURLException e) { + LOGGER.warn("Not valid URL: {}. Default to {}.", server.getUrl(), LOCAL_HOST); + } + } + return getDefaultUrl(); + } + + private static String extractUrl(Server server, String url, ServerVariables variables) { Set replacedVariables = new HashSet<>(); Matcher matcher = VARIABLE_PATTERN.matcher(url); - while(matcher.find()) { - if(!replacedVariables.contains(matcher.group())) { + while (matcher.find()) { + if (!replacedVariables.contains(matcher.group())) { ServerVariable variable = variables.get(matcher.group(1)); String replacement; - if(variable != null) { - if(variable.getDefault() != null) { + if (variable != null) { + if (variable.getDefault() != null) { replacement = variable.getDefault(); - } else if(variable.getEnum() != null && !variable.getEnum().isEmpty()) { + } else if (variable.getEnum() != null && !variable.getEnum().isEmpty()) { replacement = variable.getEnum().get(0); } else { LOGGER.warn("No value found for variable '{}' in server definition '{}', default to empty string.", matcher.group(1), server.getUrl()); @@ -79,14 +94,7 @@ public class URLPathUtils { matcher = VARIABLE_PATTERN.matcher(url); } } - url = sanitizeUrl(url); - - try { - return new URL(url); - } catch (MalformedURLException e) { - LOGGER.warn("Not valid URL: {}. Default to {}.", server.getUrl(), LOCAL_HOST); - return getDefaultUrl(); - } + return url; } public static String getScheme(OpenAPI openAPI, CodegenConfig config) { @@ -110,7 +118,8 @@ public class URLPathUtils { /** * Return the port, example value 8080 - * @param url server url + * + * @param url server url * @param defaultPort if the port is not set * @return port */ @@ -120,7 +129,8 @@ public class URLPathUtils { /** * Return the port, example value 8080 - * @param url server url + * + * @param url server url * @param defaultPort if the port is not set * @return port */ @@ -134,7 +144,8 @@ public class URLPathUtils { /** * Return the path, example value /abcdef/xyz - * @param url server url + * + * @param url server url * @param defaultPath if the path is not empty * @return path */ @@ -148,6 +159,7 @@ public class URLPathUtils { /** * Get the protocol and the host, example value https://www.abcdef.xyz + * * @param url server url * @return protocolAndHost */ @@ -156,12 +168,13 @@ public class URLPathUtils { return LOCAL_HOST; } else { String protocol = (url.getProtocol() == null) ? "http" : url.getProtocol(); - return protocol + "://"+ url.getHost(); + return protocol + "://" + url.getHost(); } } /** * Return the first complete URL from the OpenAPI specification + * * @param openAPI current OpenAPI specification * @return host */ diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/utils/URLPathUtilsTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/utils/URLPathUtilsTest.java index 1c2869a85ec..b2e61573077 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/utils/URLPathUtilsTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/utils/URLPathUtilsTest.java @@ -30,7 +30,7 @@ import java.util.Arrays; public class URLPathUtilsTest { @Test - public void testDefaultValues() throws Exception { + public void testDefaultValues() { OpenAPI openAPI = new OpenAPI(); URL serverURL = URLPathUtils.getServerURL(openAPI); @@ -44,7 +44,7 @@ public class URLPathUtilsTest { } @Test - public void testUrl() throws Exception { + public void testUrl() { OpenAPI openAPI = new OpenAPI(); openAPI.addServersItem(new Server().url("https://abcdef.xyz:9999/some/path")); URL serverURL = URLPathUtils.getServerURL(openAPI); @@ -59,7 +59,7 @@ public class URLPathUtilsTest { } @Test - public void testSanitizeUrl() throws Exception { + public void testSanitizeUrl() { String[][] testData = { { "https://abc1.xyz:9999/some/path", "https://abc1.xyz:9999/some/path" }, { "HTTPS://abc2.xyz:9999/some/path", "https://abc2.xyz:9999/some/path" }, @@ -79,7 +79,7 @@ public class URLPathUtilsTest { } @Test - public void testGetServerURLWithVariables() throws Exception { + public void testGetServerURLWithVariables() { Server s1 = new Server().url("http://localhost:{port}/").variables(new ServerVariables().addServerVariable("port", new ServerVariable()._default("8080").description("the server port"))); Assert.assertEquals(URLPathUtils.getServerURL(s1).toString(), "http://localhost:8080/"); @@ -113,4 +113,12 @@ public class URLPathUtilsTest { .addServerVariable("user", new ServerVariable()._default("{user}"))); Assert.assertEquals(URLPathUtils.getServerURL(s9).toString(), "https://{user}.example.com/v1"); } + + @Test + public void useDefaultUrlWhenServerUrlIsNull() { + Server server = new Server().url(null); + + URL serverURL = URLPathUtils.getServerURL(server); + Assert.assertEquals(serverURL.toString(), "http://localhost"); + } }