mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2026-03-19 03:09:15 +00:00
Handle variables in server declaration (#614)
This commit is contained in:
@@ -19,6 +19,8 @@ package org.openapitools.codegen.utils;
|
||||
|
||||
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.openapitools.codegen.CodegenConfig;
|
||||
import org.slf4j.Logger;
|
||||
@@ -26,12 +28,17 @@ import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class URLPathUtils {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(URLPathUtils.class);
|
||||
public static final String LOCAL_HOST = "http://localhost";
|
||||
public static final Pattern VARIABLE_PATTERN = Pattern.compile("\\{([^\\}]+)\\}");
|
||||
|
||||
public static URL getServerURL(OpenAPI openAPI) {
|
||||
final List<Server> servers = openAPI.getServers();
|
||||
@@ -40,8 +47,40 @@ public class URLPathUtils {
|
||||
return getDefaultUrl();
|
||||
}
|
||||
// TODO need a way to obtain all server URLs
|
||||
final Server server = servers.get(0);
|
||||
String url = sanitizeUrl(server.getUrl());
|
||||
return getServerURL(servers.get(0));
|
||||
}
|
||||
|
||||
static URL getServerURL(final Server server) {
|
||||
String url = server.getUrl();
|
||||
ServerVariables variables = server.getVariables();
|
||||
if(variables == null) {
|
||||
variables = new ServerVariables();
|
||||
}
|
||||
Set<String> replacedVariables = new HashSet<>();
|
||||
Matcher matcher = VARIABLE_PATTERN.matcher(url);
|
||||
while(matcher.find()) {
|
||||
if(!replacedVariables.contains(matcher.group())) {
|
||||
ServerVariable variable = variables.get(matcher.group(1));
|
||||
String replacement;
|
||||
if(variable != null) {
|
||||
if(variable.getDefault() != null) {
|
||||
replacement = variable.getDefault();
|
||||
} 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());
|
||||
replacement = "";
|
||||
}
|
||||
} else {
|
||||
LOGGER.warn("No variable '{}' found in server definition '{}', default to empty string.", matcher.group(1), server.getUrl());
|
||||
replacement = "";
|
||||
}
|
||||
url = url.replace(matcher.group(), replacement);
|
||||
replacedVariables.add(matcher.group());
|
||||
matcher = VARIABLE_PATTERN.matcher(url);
|
||||
}
|
||||
}
|
||||
url = sanitizeUrl(url);
|
||||
|
||||
try {
|
||||
return new URL(url);
|
||||
|
||||
Reference in New Issue
Block a user