Skip to content

Commit

Permalink
Merge branch 'Issue-8412' of https://github.com/douglasbgray/swagger-…
Browse files Browse the repository at this point in the history
…codegen into douglasbgray-Issue-8412
  • Loading branch information
HugoMario committed Oct 26, 2023
2 parents 87ad143 + b28421f commit 6841e15
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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<String, Object> defaultValues = vars.entrySet()
.stream()
.collect(Collectors.toMap(
Map.Entry::getKey,
e -> e.getValue().getDefault()));
return StrSubstitutor.replace(url, defaultValues, "{", "}");
}
return url;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 6841e15

Please sign in to comment.