From b28421f3f41ed7c888c0cb133a35fc0ddb887f70 Mon Sep 17 00:00:00 2001 From: Doug Date: Sun, 15 Nov 2020 18:22:50 -0800 Subject: [PATCH 1/2] 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); + } } From a1ba22c7c096810fe2a9f48239fba4677ecdfa2d Mon Sep 17 00:00:00 2001 From: Hugo Mercado Date: Thu, 26 Oct 2023 00:18:32 -0500 Subject: [PATCH 2/2] added default port for server var --- .../src/main/java/io/swagger/codegen/v3/utils/URLPathUtil.java | 3 ++- .../test/java/io/swagger/codegen/v3/utils/URLPathUtilTest.java | 2 +- 2 files changed, 3 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 ee4149bde1a..ea14e55bdbe 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 @@ -19,6 +19,7 @@ public class URLPathUtil { protected static final Logger LOGGER = LoggerFactory.getLogger(URLPathUtil.class); public static String DEFAULT_PATH = "/"; + public static String DEFAULT_PORT = "8080"; public static final String LOCAL_HOST = "http://localhost:8080"; public static URL getServerURL(OpenAPI openAPI) { @@ -107,7 +108,7 @@ private static String replaceServerVarsWthDefaultValues(String url, ServerVariab .stream() .collect(Collectors.toMap( Map.Entry::getKey, - e -> e.getValue().getDefault())); + e -> e.getValue().getDefault() != null ? e.getValue().getDefault() : DEFAULT_PORT)); 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 0f1c6fb5c51..c4bc5693ff8 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 @@ -150,6 +150,6 @@ public void testVariableSubstitutionMissingDefault() throws Exception { // No default for port, resulting URL is invalid. URL url = URLPathUtil.getServerURL(openAPI, config); - Assert.assertNull(url); + Assert.assertEquals(new URL("http://myhost:8080/mypath"), url); } }