Added server variable support (#816)

* Added server variable support

* Replaced tabs with 4 spaces
This commit is contained in:
Tino Fuhrmann 2018-08-19 11:45:55 +02:00 committed by William Cheng
parent d593988e5d
commit 3b9de3baa3
6 changed files with 69 additions and 1 deletions

View File

@ -23,6 +23,8 @@ import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.security.SecurityScheme;
import io.swagger.v3.oas.models.servers.Server;
import io.swagger.v3.oas.models.servers.ServerVariable;
import java.util.List;
import java.util.Map;
@ -113,6 +115,10 @@ public interface CodegenConfig {
List<CodegenSecurity> fromSecurity(Map<String, SecurityScheme> schemas);
List<CodegenServer> fromServers(List<Server> servers);
List<CodegenServerVariable> fromServerVariables(Map<String, ServerVariable> variables);
Set<String> defaultIncludes();
Map<String, String> typeMapping();

View File

@ -0,0 +1,9 @@
package org.openapitools.codegen;
import java.util.List;
public class CodegenServer {
public String url;
public String description;
public List<CodegenServerVariable> variables;
}

View File

@ -0,0 +1,10 @@
package org.openapitools.codegen;
import java.util.List;
public class CodegenServerVariable {
public String name;
public String defaultValue;
public String description;
public List<String> enumValues;
}

View File

@ -43,6 +43,8 @@ import io.swagger.v3.oas.models.responses.ApiResponses;
import io.swagger.v3.oas.models.security.OAuthFlow;
import io.swagger.v3.oas.models.security.OAuthFlows;
import io.swagger.v3.oas.models.security.SecurityScheme;
import io.swagger.v3.oas.models.servers.Server;
import io.swagger.v3.oas.models.servers.ServerVariable;
import io.swagger.v3.parser.util.SchemaTypeUtil;
import org.apache.commons.lang3.ObjectUtils;
@ -64,6 +66,7 @@ import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
@ -4588,4 +4591,38 @@ public class DefaultCodegen implements CodegenConfig {
public boolean isDataTypeString(String dataType) {
return "String".equals(dataType);
}
@Override
public List<CodegenServer> fromServers(List<Server> servers) {
if (servers == null) {
return Collections.emptyList();
}
List<CodegenServer> codegenServers = new LinkedList<>();
for (Server server: servers) {
CodegenServer cs = new CodegenServer();
cs.description = server.getDescription();
cs.url = server.getUrl();
cs.variables = this.fromServerVariables(server.getVariables());
codegenServers.add(cs);
}
return codegenServers;
}
@Override
public List<CodegenServerVariable> fromServerVariables(Map<String, ServerVariable> variables) {
if (variables == null) {
return Collections.emptyList();
}
List<CodegenServerVariable> codegenServerVariables = new LinkedList<>();
for (Entry<String, ServerVariable> variableEntry: variables.entrySet()) {
CodegenServerVariable codegenServerVariable = new CodegenServerVariable();
ServerVariable variable = variableEntry.getValue();
codegenServerVariable.defaultValue = variable.getDefault();
codegenServerVariable.description = variable.getDescription();
codegenServerVariable.enumValues = variable.getEnum();
codegenServerVariable.name = variableEntry.getKey();
codegenServerVariables.add(codegenServerVariable);
}
return codegenServerVariables;
}
}

View File

@ -762,6 +762,12 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
bundle.put("authMethods", authMethods);
bundle.put("hasAuthMethods", true);
}
List<CodegenServer> servers = config.fromServers(openAPI.getServers());
if (servers != null && !servers.isEmpty()) {
bundle.put("servers", servers);
bundle.put("hasServers", true);
}
if (openAPI.getExternalDocs() != null) {
bundle.put("externalDocs", openAPI.getExternalDocs());

View File

@ -50,7 +50,7 @@ public class URLPathUtils {
return getServerURL(servers.get(0));
}
static URL getServerURL(final Server server) {
public static URL getServerURL(final Server server) {
String url = server.getUrl();
ServerVariables variables = server.getVariables();
if(variables == null) {