forked from loafle/openapi-generator-original
parent
c88174eafd
commit
2c26fd3a89
@ -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.Server;
|
||||||
import io.swagger.v3.oas.models.servers.ServerVariable;
|
import io.swagger.v3.oas.models.servers.ServerVariable;
|
||||||
import io.swagger.v3.oas.models.servers.ServerVariables;
|
import io.swagger.v3.oas.models.servers.ServerVariables;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.openapitools.codegen.CodegenConfig;
|
import org.openapitools.codegen.CodegenConfig;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
@ -55,6 +56,20 @@ public class URLPathUtils {
|
|||||||
if (variables == null) {
|
if (variables == null) {
|
||||||
variables = new ServerVariables();
|
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<>();
|
Set<String> replacedVariables = new HashSet<>();
|
||||||
Matcher matcher = VARIABLE_PATTERN.matcher(url);
|
Matcher matcher = VARIABLE_PATTERN.matcher(url);
|
||||||
while (matcher.find()) {
|
while (matcher.find()) {
|
||||||
@ -79,14 +94,7 @@ public class URLPathUtils {
|
|||||||
matcher = VARIABLE_PATTERN.matcher(url);
|
matcher = VARIABLE_PATTERN.matcher(url);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
url = sanitizeUrl(url);
|
return url;
|
||||||
|
|
||||||
try {
|
|
||||||
return new URL(url);
|
|
||||||
} catch (MalformedURLException e) {
|
|
||||||
LOGGER.warn("Not valid URL: {}. Default to {}.", server.getUrl(), LOCAL_HOST);
|
|
||||||
return getDefaultUrl();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getScheme(OpenAPI openAPI, CodegenConfig config) {
|
public static String getScheme(OpenAPI openAPI, CodegenConfig config) {
|
||||||
@ -110,6 +118,7 @@ public class URLPathUtils {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the port, example value <code>8080</code>
|
* Return the port, example value <code>8080</code>
|
||||||
|
*
|
||||||
* @param url server url
|
* @param url server url
|
||||||
* @param defaultPort if the port is not set
|
* @param defaultPort if the port is not set
|
||||||
* @return port
|
* @return port
|
||||||
@ -120,6 +129,7 @@ public class URLPathUtils {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the port, example value <code>8080</code>
|
* Return the port, example value <code>8080</code>
|
||||||
|
*
|
||||||
* @param url server url
|
* @param url server url
|
||||||
* @param defaultPort if the port is not set
|
* @param defaultPort if the port is not set
|
||||||
* @return port
|
* @return port
|
||||||
@ -134,6 +144,7 @@ public class URLPathUtils {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the path, example value <code>/abcdef/xyz</code>
|
* 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
|
* @param defaultPath if the path is not empty
|
||||||
* @return path
|
* @return path
|
||||||
@ -148,6 +159,7 @@ public class URLPathUtils {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the protocol and the host, example value <code>https://www.abcdef.xyz</code>
|
* Get the protocol and the host, example value <code>https://www.abcdef.xyz</code>
|
||||||
|
*
|
||||||
* @param url server url
|
* @param url server url
|
||||||
* @return protocolAndHost
|
* @return protocolAndHost
|
||||||
*/
|
*/
|
||||||
@ -162,6 +174,7 @@ public class URLPathUtils {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the first complete URL from the OpenAPI specification
|
* Return the first complete URL from the OpenAPI specification
|
||||||
|
*
|
||||||
* @param openAPI current OpenAPI specification
|
* @param openAPI current OpenAPI specification
|
||||||
* @return host
|
* @return host
|
||||||
*/
|
*/
|
||||||
|
@ -30,7 +30,7 @@ import java.util.Arrays;
|
|||||||
public class URLPathUtilsTest {
|
public class URLPathUtilsTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDefaultValues() throws Exception {
|
public void testDefaultValues() {
|
||||||
OpenAPI openAPI = new OpenAPI();
|
OpenAPI openAPI = new OpenAPI();
|
||||||
URL serverURL = URLPathUtils.getServerURL(openAPI);
|
URL serverURL = URLPathUtils.getServerURL(openAPI);
|
||||||
|
|
||||||
@ -44,7 +44,7 @@ public class URLPathUtilsTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testUrl() throws Exception {
|
public void testUrl() {
|
||||||
OpenAPI openAPI = new OpenAPI();
|
OpenAPI openAPI = new OpenAPI();
|
||||||
openAPI.addServersItem(new Server().url("https://abcdef.xyz:9999/some/path"));
|
openAPI.addServersItem(new Server().url("https://abcdef.xyz:9999/some/path"));
|
||||||
URL serverURL = URLPathUtils.getServerURL(openAPI);
|
URL serverURL = URLPathUtils.getServerURL(openAPI);
|
||||||
@ -59,7 +59,7 @@ public class URLPathUtilsTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSanitizeUrl() throws Exception {
|
public void testSanitizeUrl() {
|
||||||
String[][] testData = {
|
String[][] testData = {
|
||||||
{ "https://abc1.xyz:9999/some/path", "https://abc1.xyz:9999/some/path" },
|
{ "https://abc1.xyz:9999/some/path", "https://abc1.xyz:9999/some/path" },
|
||||||
{ "HTTPS://abc2.xyz:9999/some/path", "https://abc2.xyz:9999/some/path" },
|
{ "HTTPS://abc2.xyz:9999/some/path", "https://abc2.xyz:9999/some/path" },
|
||||||
@ -79,7 +79,7 @@ public class URLPathUtilsTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@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")));
|
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/");
|
Assert.assertEquals(URLPathUtils.getServerURL(s1).toString(), "http://localhost:8080/");
|
||||||
|
|
||||||
@ -113,4 +113,12 @@ public class URLPathUtilsTest {
|
|||||||
.addServerVariable("user", new ServerVariable()._default("{user}")));
|
.addServerVariable("user", new ServerVariable()._default("{user}")));
|
||||||
Assert.assertEquals(URLPathUtils.getServerURL(s9).toString(), "https://{user}.example.com/v1");
|
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");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user