From b28421f3f41ed7c888c0cb133a35fc0ddb887f70 Mon Sep 17 00:00:00 2001 From: Doug Date: Sun, 15 Nov 2020 18:22:50 -0800 Subject: [PATCH] Issue 8412: Replace vars with defaults in server evaluation. --- .../swagger/codegen/v3/utils/URLPathUtil.java | 22 ++++++++++-- .../codegen/v3/utils/URLPathUtilTest.java | 34 +++++++++++++++++++ 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/v3/utils/URLPathUtil.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/v3/utils/URLPathUtil.java index 95ab319e28c..ee4149bde1a 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/v3/utils/URLPathUtil.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/v3/utils/URLPathUtil.java @@ -3,13 +3,17 @@ 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 { @@ -30,7 +34,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; @@ -67,7 +72,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; @@ -94,4 +100,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 defaultValues = vars.entrySet() + .stream() + .collect(Collectors.toMap( + Map.Entry::getKey, + e -> e.getValue().getDefault())); + return StrSubstitutor.replace(url, defaultValues, "{", "}"); + } + return url; + } } diff --git a/modules/swagger-codegen/src/test/java/io/swagger/codegen/v3/utils/URLPathUtilTest.java b/modules/swagger-codegen/src/test/java/io/swagger/codegen/v3/utils/URLPathUtilTest.java index 822801eaa40..21b72363228 100644 --- a/modules/swagger-codegen/src/test/java/io/swagger/codegen/v3/utils/URLPathUtilTest.java +++ b/modules/swagger-codegen/src/test/java/io/swagger/codegen/v3/utils/URLPathUtilTest.java @@ -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; @@ -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.assertNull(url); + } }