From d42ff75cebeb7d7aab80f4b48e6a77f3ac5e489a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20Bresson?= Date: Mon, 23 Jul 2018 06:20:41 +0200 Subject: [PATCH] Handle variables in server declaration (#614) --- .../codegen/utils/URLPathUtils.java | 43 ++++++++++++++++++- .../codegen/utils/URLPathUtilsTest.java | 39 +++++++++++++++++ 2 files changed, 80 insertions(+), 2 deletions(-) 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 4fa6ffd9c6a..91aba80d82c 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 @@ -19,6 +19,8 @@ package org.openapitools.codegen.utils; 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.openapitools.codegen.CodegenConfig; import org.slf4j.Logger; @@ -26,12 +28,17 @@ import org.slf4j.LoggerFactory; import java.net.MalformedURLException; import java.net.URL; +import java.util.HashSet; import java.util.List; +import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; public class URLPathUtils { private static final Logger LOGGER = LoggerFactory.getLogger(URLPathUtils.class); public static final String LOCAL_HOST = "http://localhost"; + public static final Pattern VARIABLE_PATTERN = Pattern.compile("\\{([^\\}]+)\\}"); public static URL getServerURL(OpenAPI openAPI) { final List servers = openAPI.getServers(); @@ -40,8 +47,40 @@ public class URLPathUtils { return getDefaultUrl(); } // TODO need a way to obtain all server URLs - final Server server = servers.get(0); - String url = sanitizeUrl(server.getUrl()); + return getServerURL(servers.get(0)); + } + + static URL getServerURL(final Server server) { + String url = server.getUrl(); + ServerVariables variables = server.getVariables(); + if(variables == null) { + variables = new ServerVariables(); + } + Set replacedVariables = new HashSet<>(); + Matcher matcher = VARIABLE_PATTERN.matcher(url); + while(matcher.find()) { + if(!replacedVariables.contains(matcher.group())) { + ServerVariable variable = variables.get(matcher.group(1)); + String replacement; + if(variable != null) { + if(variable.getDefault() != null) { + replacement = variable.getDefault(); + } 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()); + replacement = ""; + } + } else { + LOGGER.warn("No variable '{}' found in server definition '{}', default to empty string.", matcher.group(1), server.getUrl()); + replacement = ""; + } + url = url.replace(matcher.group(), replacement); + replacedVariables.add(matcher.group()); + matcher = VARIABLE_PATTERN.matcher(url); + } + } + url = sanitizeUrl(url); try { return new URL(url); 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 26c42eb2e59..f6625cc427e 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 @@ -19,11 +19,14 @@ package org.openapitools.codegen.utils; 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.testng.Assert; import org.testng.annotations.Test; import java.net.URL; +import java.util.Arrays; public class URLPathUtilsTest { @@ -75,4 +78,40 @@ public class URLPathUtilsTest { Assert.assertEquals(URLPathUtils.getServerURL(openAPI).toString(), t[1]); } } + + @Test + public void testGetServerURLWithVariables() throws Exception { + 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/"); + + Server s2 = new Server().url("http://{version}.test.me/{version}").variables(new ServerVariables().addServerVariable("version", new ServerVariable()._default("v1"))); + Assert.assertEquals(URLPathUtils.getServerURL(s2).toString(), "http://v1.test.me/v1"); + + Server s3 = new Server().url("http://localhost:{port}/{version}").variables( + new ServerVariables().addServerVariable("version", new ServerVariable()._default("v4")) + .addServerVariable("port", new ServerVariable()._default("8080")) + .addServerVariable("other", new ServerVariable()._default("something")) + ); + Assert.assertEquals(URLPathUtils.getServerURL(s3).toString(), "http://localhost:8080/v4"); + + 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"))); + Assert.assertEquals(URLPathUtils.getServerURL(s4).toString(), "http://91.161.147.64/prd"); + + Server s5 = new Server().url("https://api.stats.com/{country1}").variables(new ServerVariables().addServerVariable("country1", new ServerVariable()._enum(Arrays.asList("france", "germany", "italy")))); + Assert.assertEquals(URLPathUtils.getServerURL(s5).toString(), "https://api.stats.com/france"); + + Server s6 = new Server().url("https://api.example.com/{wrong}"); + Assert.assertEquals(URLPathUtils.getServerURL(s6).toString(), "https://api.example.com/"); + + Server s7 = new Server().url("https://api.example.com/{wrong}").variables(new ServerVariables()); + Assert.assertEquals(URLPathUtils.getServerURL(s7).toString(), "https://api.example.com/"); + + Server s8 = new Server().url("https://api.example.com/{wrong}").variables(new ServerVariables().addServerVariable("other", new ServerVariable()._default("something"))); + Assert.assertEquals(URLPathUtils.getServerURL(s8).toString(), "https://api.example.com/"); + + Server s9 = new Server().url("https://{user}.example.com/{version}").variables( + new ServerVariables().addServerVariable("version", new ServerVariable()._default("v1")) + .addServerVariable("user", new ServerVariable()._default("{user}"))); + Assert.assertEquals(URLPathUtils.getServerURL(s9).toString(), "https://{user}.example.com/v1"); + } }