Fix NPE when server url is null (#2628)

Fix #2625
This commit is contained in:
Thibault Duperron 2019-04-09 04:36:15 +02:00 committed by William Cheng
parent c88174eafd
commit 2c26fd3a89
2 changed files with 43 additions and 22 deletions

View File

@ -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<String> 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 <code>8080</code>
* @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 <code>8080</code>
* @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 <code>/abcdef/xyz</code>
* @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 <code>https://www.abcdef.xyz</code>
*
* @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
*/

View File

@ -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");
}
}