forked from loafle/openapi-generator-original
@@ -135,9 +135,21 @@ public class URLPathUtils {
|
||||
}
|
||||
|
||||
private static String sanitizeUrl(String url) {
|
||||
if (url.startsWith("/")) {
|
||||
LOGGER.warn("'host' not defined in the spec (2.0). Default to " + LOCAL_HOST);
|
||||
if (url.startsWith("//")) {
|
||||
url = "http:" + url;
|
||||
LOGGER.warn("'scheme' not defined in the spec (2.0). Default to [http] for server URL [{}]", url);
|
||||
} else if (url.startsWith("/")) {
|
||||
url = LOCAL_HOST + url;
|
||||
LOGGER.warn("'host' not defined in the spec (2.0). Default to [{}] for server URL [{}]", LOCAL_HOST, url);
|
||||
} else if (!url.matches("[a-zA-Z][0-9a-zA-Z.+\\-]+://.+")) {
|
||||
// Add http scheme for urls without a scheme.
|
||||
// 2.0 spec is restricted to the following schemes: "http", "https", "ws", "wss"
|
||||
// 3.0 spec does not have an enumerated list of schemes
|
||||
// This regex attempts to capture all schemes in IANA example schemes which
|
||||
// can have alpha-numeric characters and [.+-]. Examples are here:
|
||||
// https://www.iana.org/assignments/uri-schemes/uri-schemes.xhtml
|
||||
url = "http://" + url;
|
||||
LOGGER.warn("'scheme' not defined in the spec (2.0). Default to [http] for server URL [{}]", url);
|
||||
}
|
||||
|
||||
return url;
|
||||
|
||||
@@ -55,4 +55,24 @@ public class URLPathUtilsTest {
|
||||
Assert.assertEquals(URLPathUtils.getPort(serverURL, "8081"), "9999");
|
||||
Assert.assertEquals(URLPathUtils.getPath(serverURL, "/abc"), "/some/path");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSanitizeUrl() throws Exception {
|
||||
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" },
|
||||
{ "http://abc3.xyz:9999/some/path", "http://abc3.xyz:9999/some/path" },
|
||||
{ "HTTP://abc4.xyz:9999/some/path", "http://abc4.xyz:9999/some/path" },
|
||||
{ "//abc5.xyz:9999/some/path", "http://abc5.xyz:9999/some/path" },
|
||||
{ "abc6.xyz:9999/some/path", "http://abc6.xyz:9999/some/path" },
|
||||
{ "localhost:9000/api", "http://localhost:9000/api" },
|
||||
{ "/some/path", "http://localhost/some/path" } };
|
||||
|
||||
for (String[] t:testData) {
|
||||
OpenAPI openAPI = new OpenAPI();
|
||||
openAPI.addServersItem(new Server().url(t[0]));
|
||||
|
||||
Assert.assertEquals(URLPathUtils.getServerURL(openAPI).toString(), t[1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user