forked from loafle/openapi-generator-original
Rework url handling (#236)
This commit is contained in:
@@ -175,7 +175,7 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
|
||||
}
|
||||
|
||||
URL url = URLPathUtils.getServerURL(openAPI);
|
||||
contextPath = config.escapeText(url == null ? "" : url.getPath());
|
||||
contextPath = config.escapeText(url.getPath());
|
||||
basePath = config.escapeText(URLPathUtils.getHost(openAPI));
|
||||
basePathWithoutHost = contextPath; // for backward compatibility
|
||||
}
|
||||
@@ -703,15 +703,13 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
|
||||
apis.put("apis", allOperations);
|
||||
|
||||
URL url = URLPathUtils.getServerURL(openAPI);
|
||||
if (url != null) {
|
||||
bundle.put("host", url.getHost());
|
||||
}
|
||||
|
||||
bundle.put("openAPI", openAPI);
|
||||
bundle.put("swagger", openAPI); // for backward compatibility
|
||||
bundle.put("basePath", basePath);
|
||||
bundle.put("basePathWithoutHost", basePathWithoutHost);
|
||||
bundle.put("scheme", URLPathUtils.getScheme(openAPI, config));
|
||||
bundle.put("scheme", URLPathUtils.getScheme(url, config));
|
||||
bundle.put("host", url.getHost());
|
||||
bundle.put("contextPath", contextPath);
|
||||
bundle.put("apiInfo", apis);
|
||||
bundle.put("models", allModels);
|
||||
|
||||
@@ -96,13 +96,8 @@ public abstract class AbstractJavaJAXRSServerCodegen extends AbstractJavaCodegen
|
||||
|
||||
if (!this.additionalProperties.containsKey("serverPort")) {
|
||||
URL url = URLPathUtils.getServerURL(openAPI);
|
||||
|
||||
Integer port = 8080; // Default value for a JEE Server
|
||||
if (url.getPort() != 0) {
|
||||
port = url.getPort();
|
||||
}
|
||||
|
||||
this.additionalProperties.put("serverPort", port);
|
||||
// 8080 is the default value for a JEE Server:
|
||||
this.additionalProperties.put("serverPort", URLPathUtils.getPort(url, 8080));
|
||||
}
|
||||
|
||||
if (openAPI.getPaths() != null) {
|
||||
|
||||
@@ -352,10 +352,7 @@ public class CSharpNancyFXServerCodegen extends AbstractCSharpCodegen {
|
||||
@Override
|
||||
public void preprocessOpenAPI(final OpenAPI openAPI) {
|
||||
URL url = URLPathUtils.getServerURL(openAPI);
|
||||
String path = "/";
|
||||
if (url != null) {
|
||||
path = url.getPath();
|
||||
}
|
||||
String path = URLPathUtils.getPath(url, "/");
|
||||
final String packageContextOption = (String) additionalProperties.get(PACKAGE_CONTEXT);
|
||||
additionalProperties.put("packageContext", packageContextOption == null ? sanitizeName(path) : packageContextOption);
|
||||
final Object basePathOption = additionalProperties.get(USE_BASE_PATH);
|
||||
|
||||
@@ -542,10 +542,8 @@ public class JavaPKMSTServerCodegen extends AbstractJavaCodegen {
|
||||
}
|
||||
|
||||
URL url = URLPathUtils.getServerURL(openAPI);
|
||||
String host = url.getHost();
|
||||
Integer port = url.getPort();
|
||||
|
||||
this.additionalProperties.put("serverPort", port);
|
||||
this.additionalProperties.put("serverPort", URLPathUtils.getPort(url, 8080));
|
||||
if (openAPI.getPaths() != null) {
|
||||
for (String pathname : openAPI.getPaths().keySet()) {
|
||||
PathItem path = openAPI.getPaths().get(pathname);
|
||||
|
||||
@@ -200,8 +200,7 @@ public class JavaVertXServerCodegen extends AbstractJavaCodegen {
|
||||
|
||||
// add server port from the swagger file, 8080 by default
|
||||
URL url = URLPathUtils.getServerURL(openAPI);
|
||||
Integer port = url.getPort();
|
||||
this.additionalProperties.put("serverPort", port);
|
||||
this.additionalProperties.put("serverPort", URLPathUtils.getPort(url, 8080));
|
||||
|
||||
// retrieve api version from swagger file, 1.0.0-SNAPSHOT by default
|
||||
if (openAPI.getInfo() != null && openAPI.getInfo().getVersion() != null) {
|
||||
|
||||
@@ -320,25 +320,9 @@ public class NodeJSServerCodegen extends DefaultCodegen implements CodegenConfig
|
||||
@Override
|
||||
public void preprocessOpenAPI(OpenAPI openAPI) {
|
||||
URL url = URLPathUtils.getServerURL(openAPI);
|
||||
String host = URLPathUtils.LOCAL_HOST;
|
||||
String port = defaultServerPort;
|
||||
String basePath = null;
|
||||
if (url != null) {
|
||||
port = String.valueOf(url.getPort());
|
||||
host = url.getHost();
|
||||
basePath = url.getPath();
|
||||
}
|
||||
|
||||
if (!StringUtils.isEmpty(host)) {
|
||||
String[] parts = host.split(":");
|
||||
if (parts.length > 1) {
|
||||
port = parts[1];
|
||||
}
|
||||
} else {
|
||||
// host is empty, default to https://localhost
|
||||
host = "http://localhost";
|
||||
LOGGER.warn("'host' in the specification is empty or undefined. Default to http://localhost.");
|
||||
}
|
||||
String host = URLPathUtils.getProtocolAndHost(url);
|
||||
String port = URLPathUtils.getPort(url, defaultServerPort) ;
|
||||
String basePath = url.getPath();
|
||||
|
||||
if (additionalProperties.containsKey(SERVER_PORT)) {
|
||||
port = additionalProperties.get(SERVER_PORT).toString();
|
||||
|
||||
@@ -242,7 +242,7 @@ public class RustServerCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
@Override
|
||||
public void preprocessOpenAPI(OpenAPI openAPI) {
|
||||
Info info = openAPI.getInfo();
|
||||
List versionComponents = new ArrayList(Arrays.asList(info.getVersion().split("[.]")));
|
||||
List<String> versionComponents = new ArrayList<>(Arrays.asList(info.getVersion().split("[.]")));
|
||||
if (versionComponents.size() < 1) {
|
||||
versionComponents.add("1");
|
||||
}
|
||||
@@ -253,7 +253,7 @@ public class RustServerCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
|
||||
URL url = URLPathUtils.getServerURL(openAPI);
|
||||
additionalProperties.put("serverHost", url.getHost());
|
||||
additionalProperties.put("serverPort", url.getPort());
|
||||
additionalProperties.put("serverPort", URLPathUtils.getPort(url, 80));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -413,11 +413,7 @@ public class SpringCodegen extends AbstractJavaCodegen
|
||||
}
|
||||
|
||||
URL url = URLPathUtils.getServerURL(openAPI);
|
||||
Integer port = 8080;
|
||||
if (url.getPort() != 0) {
|
||||
port = url.getPort();
|
||||
}
|
||||
this.additionalProperties.put("serverPort", port);
|
||||
this.additionalProperties.put("serverPort", URLPathUtils.getPort(url, 8080));
|
||||
|
||||
if (openAPI.getPaths() != null) {
|
||||
for (String pathname : openAPI.getPaths().keySet()) {
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
package org.openapitools.codegen.utils;
|
||||
|
||||
import org.openapitools.codegen.CodegenConfig;
|
||||
import io.swagger.v3.oas.models.OpenAPI;
|
||||
import io.swagger.v3.oas.models.servers.Server;
|
||||
|
||||
import org.openapitools.codegen.CodegenConfig;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@@ -34,8 +35,12 @@ public class URLPathUtils {
|
||||
}
|
||||
|
||||
public static String getScheme(OpenAPI openAPI, CodegenConfig config) {
|
||||
String scheme;
|
||||
URL url = getServerURL(openAPI);
|
||||
return getScheme(url, config);
|
||||
}
|
||||
|
||||
public static String getScheme(URL url, CodegenConfig config) {
|
||||
String scheme;
|
||||
if (url != null) {
|
||||
scheme = url.getProtocol();
|
||||
} else {
|
||||
@@ -47,6 +52,64 @@ public class URLPathUtils {
|
||||
return scheme;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return the port, example value <code>8080</code>
|
||||
* @param url
|
||||
* @param defaultPort if the port is not set
|
||||
* @return
|
||||
*/
|
||||
public static String getPort(URL url, int defaultPort) {
|
||||
return getPort(url, String.valueOf(defaultPort));
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the port, example value <code>8080</code>
|
||||
* @param url
|
||||
* @param defaultPort if the port is not set
|
||||
* @return
|
||||
*/
|
||||
public static String getPort(URL url, String defaultPort) {
|
||||
if (url == null || url.getPort() == -1) {
|
||||
return defaultPort;
|
||||
} else {
|
||||
return String.valueOf(url.getPort());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the path, example value <code>/abcdef/xyz</code>
|
||||
* @param url
|
||||
* @param defaultPath if the path is not empty
|
||||
* @return path
|
||||
*/
|
||||
public static String getPath(URL url, String defaultPath) {
|
||||
if (url == null || url.getPath() == null || url.getPath().isEmpty()) {
|
||||
return defaultPath;
|
||||
} else {
|
||||
return url.getPath();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the protocol and the host, example value <code>https://www.abcdef.xyz</code>
|
||||
* @param url
|
||||
* @return protocolAndHost
|
||||
*/
|
||||
public static String getProtocolAndHost(URL url) {
|
||||
if (url == null) {
|
||||
return LOCAL_HOST;
|
||||
} else {
|
||||
String protocol = (url.getProtocol() == null) ? "http" : url.getProtocol();
|
||||
return protocol + "://"+ url.getHost();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the first complete URL from the OpenAPI specification
|
||||
* @param openAPI
|
||||
* @return host
|
||||
*/
|
||||
public static String getHost(OpenAPI openAPI) {
|
||||
if (openAPI.getServers() != null && openAPI.getServers().size() > 0) {
|
||||
return sanitizeUrl(openAPI.getServers().get(0).getUrl());
|
||||
@@ -70,5 +133,4 @@ public class URLPathUtils {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package org.openapitools.codegen.utils;
|
||||
|
||||
import io.swagger.v3.oas.models.OpenAPI;
|
||||
import io.swagger.v3.oas.models.servers.Server;
|
||||
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import java.net.URL;
|
||||
|
||||
public class URLPathUtilsTest {
|
||||
|
||||
@Test
|
||||
public void testDefaultValues() throws Exception {
|
||||
OpenAPI openAPI = new OpenAPI();
|
||||
URL serverURL = URLPathUtils.getServerURL(openAPI);
|
||||
|
||||
Assert.assertEquals(serverURL.getHost(), "localhost");
|
||||
Assert.assertEquals(serverURL.getPort(), -1);
|
||||
Assert.assertEquals(serverURL.getPath(), "");
|
||||
Assert.assertEquals(URLPathUtils.getScheme(serverURL, null), "http");
|
||||
Assert.assertEquals(URLPathUtils.getPort(serverURL, 8080), "8080");
|
||||
Assert.assertEquals(URLPathUtils.getPort(serverURL, "8081"), "8081");
|
||||
Assert.assertEquals(URLPathUtils.getPath(serverURL, "/abc"), "/abc");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUrl() throws Exception {
|
||||
OpenAPI openAPI = new OpenAPI();
|
||||
openAPI.addServersItem(new Server().url("https://abcdef.xyz:9999/some/path"));
|
||||
URL serverURL = URLPathUtils.getServerURL(openAPI);
|
||||
|
||||
Assert.assertEquals(serverURL.getHost(), "abcdef.xyz");
|
||||
Assert.assertEquals(serverURL.getPort(), 9999);
|
||||
Assert.assertEquals(serverURL.getPath(), "/some/path");
|
||||
Assert.assertEquals(URLPathUtils.getScheme(serverURL, null), "https");
|
||||
Assert.assertEquals(URLPathUtils.getPort(serverURL, 8080), "9999");
|
||||
Assert.assertEquals(URLPathUtils.getPort(serverURL, "8081"), "9999");
|
||||
Assert.assertEquals(URLPathUtils.getPath(serverURL, "/abc"), "/some/path");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user