forked from loafle/openapi-generator-original
switch Generate to use CodegenConfigurator
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package io.swagger.codegen;
|
||||
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import io.swagger.codegen.auth.AuthParser;
|
||||
import io.swagger.models.Swagger;
|
||||
import io.swagger.models.auth.AuthorizationValue;
|
||||
|
||||
@@ -32,47 +33,23 @@ public class ClientOptInput {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public ClientOptInput auth(String urlEncodedAuthString) {
|
||||
this.setAuth(urlEncodedAuthString);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public String getAuth() {
|
||||
if (auths != null) {
|
||||
StringBuilder b = new StringBuilder();
|
||||
for (AuthorizationValue v : auths) {
|
||||
try {
|
||||
if (b.toString().length() > 0) {
|
||||
b.append(",");
|
||||
}
|
||||
b.append(URLEncoder.encode(v.getKeyName(), "UTF-8"))
|
||||
.append(":")
|
||||
.append(URLEncoder.encode(v.getValue(), "UTF-8"));
|
||||
} catch (Exception e) {
|
||||
// continue
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return b.toString();
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
return AuthParser.reconstruct(auths);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public void setAuth(String urlEncodedAuthString) {
|
||||
List<AuthorizationValue> auths = new ArrayList<AuthorizationValue>();
|
||||
if (isNotEmpty(urlEncodedAuthString)) {
|
||||
String[] parts = urlEncodedAuthString.split(",");
|
||||
for (String part : parts) {
|
||||
String[] kvPair = part.split(":");
|
||||
if (kvPair.length == 2) {
|
||||
auths.add(new AuthorizationValue(URLDecoder.decode(kvPair[0]), URLDecoder.decode(kvPair[1]), "header"));
|
||||
}
|
||||
}
|
||||
}
|
||||
this.auths = auths;
|
||||
this.auths = AuthParser.parse(urlEncodedAuthString);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public List<AuthorizationValue> getAuthorizationValues() {
|
||||
return auths;
|
||||
}
|
||||
|
||||
@@ -33,5 +33,8 @@ public class CodegenConstants {
|
||||
|
||||
public static final String SERIALIZABLE_MODEL = "serializableModel";
|
||||
public static final String SERIALIZABLE_MODEL_DESC = "boolean - toggle \"implements Serializable\" for generated models";
|
||||
|
||||
public static final String LIBRARY = "library";
|
||||
public static final String LIBRARY_DESC = "library template (sub-template)";
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
package io.swagger.codegen.auth;
|
||||
|
||||
import io.swagger.models.auth.AuthorizationValue;
|
||||
|
||||
import java.net.URLDecoder;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.apache.commons.lang3.StringUtils.isNotEmpty;
|
||||
|
||||
public class AuthParser {
|
||||
|
||||
public static List<AuthorizationValue> parse(String urlEncodedAuthStr) {
|
||||
List<AuthorizationValue> auths = new ArrayList<AuthorizationValue>();
|
||||
if (isNotEmpty(urlEncodedAuthStr)) {
|
||||
String[] parts = urlEncodedAuthStr.split(",");
|
||||
for (String part : parts) {
|
||||
String[] kvPair = part.split(":");
|
||||
if (kvPair.length == 2) {
|
||||
auths.add(new AuthorizationValue(URLDecoder.decode(kvPair[0]), URLDecoder.decode(kvPair[1]), "header"));
|
||||
}
|
||||
}
|
||||
}
|
||||
return auths;
|
||||
}
|
||||
|
||||
public static String reconstruct(List<AuthorizationValue> authorizationValueList) {
|
||||
if (authorizationValueList != null) {
|
||||
StringBuilder b = new StringBuilder();
|
||||
for (AuthorizationValue v : authorizationValueList) {
|
||||
try {
|
||||
if (b.toString().length() > 0) {
|
||||
b.append(",");
|
||||
}
|
||||
b.append(URLEncoder.encode(v.getKeyName(), "UTF-8"))
|
||||
.append(":")
|
||||
.append(URLEncoder.encode(v.getValue(), "UTF-8"));
|
||||
} catch (Exception e) {
|
||||
// continue
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return b.toString();
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,20 +1,28 @@
|
||||
package io.swagger.codegen.config;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonAnyGetter;
|
||||
import com.fasterxml.jackson.annotation.JsonAnySetter;
|
||||
import io.swagger.codegen.CliOption;
|
||||
import io.swagger.codegen.ClientOptInput;
|
||||
import io.swagger.codegen.ClientOpts;
|
||||
import io.swagger.codegen.CodegenConfig;
|
||||
import io.swagger.codegen.CodegenConfigLoader;
|
||||
import io.swagger.codegen.CodegenConstants;
|
||||
import io.swagger.codegen.auth.AuthParser;
|
||||
import io.swagger.models.Swagger;
|
||||
import io.swagger.models.auth.AuthorizationValue;
|
||||
import io.swagger.parser.SwaggerParser;
|
||||
import io.swagger.util.Json;
|
||||
import org.apache.commons.lang3.Validate;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
@@ -34,7 +42,7 @@ public class CodegenConfigurator {
|
||||
private String inputSpec;
|
||||
private String outputDir;
|
||||
private boolean verbose = false;
|
||||
private boolean skipOverwrite;
|
||||
private boolean skipOverwrite = false;
|
||||
private String templateDir;
|
||||
private String auth;
|
||||
private String apiPackage;
|
||||
@@ -43,6 +51,7 @@ public class CodegenConfigurator {
|
||||
private String groupId;
|
||||
private String artifactId;
|
||||
private String artifactVersion;
|
||||
private String library;
|
||||
private Map<String, String> systemProperties = new HashMap<String, String>();
|
||||
private Map<String, String> instantiationTypes = new HashMap<String, String>();
|
||||
private Map<String, String> typeMappings = new HashMap<String, String>();
|
||||
@@ -50,6 +59,8 @@ public class CodegenConfigurator {
|
||||
private Map<String, String> importMappings = new HashMap<String, String>();
|
||||
private Set<String> languageSpecificPrimitives = new HashSet<String>();
|
||||
|
||||
private final Map<String, String> dynamicProperties = new HashMap<String, String>(); //the map that holds the JsonAnySetter/JsonAnyGetter values
|
||||
|
||||
public CodegenConfigurator() {
|
||||
this.setOutputDir(".");
|
||||
}
|
||||
@@ -255,6 +266,15 @@ public class CodegenConfigurator {
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getLibrary() {
|
||||
return library;
|
||||
}
|
||||
|
||||
public CodegenConfigurator setLibrary(String library) {
|
||||
this.library = library;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ClientOptInput toClientOptInput() {
|
||||
|
||||
Validate.notEmpty(lang, "language must be specified");
|
||||
@@ -281,7 +301,8 @@ public class CodegenConfigurator {
|
||||
checkAndSetAdditionalProperty(artifactVersion, CodegenConstants.ARTIFACT_VERSION);
|
||||
checkAndSetAdditionalProperty(templateDir, toAbsolutePathStr(templateDir), CodegenConstants.TEMPLATE_DIR);
|
||||
|
||||
final String library = additionalProperties.remove(CodegenConstants.LIBRARY);
|
||||
handleDynamicProperties(config);
|
||||
|
||||
if (isNotEmpty(library)) {
|
||||
config.setLibrary(library);
|
||||
}
|
||||
@@ -289,16 +310,37 @@ public class CodegenConfigurator {
|
||||
config.additionalProperties().putAll(additionalProperties);
|
||||
|
||||
ClientOptInput input = new ClientOptInput()
|
||||
.config(config)
|
||||
.auth(auth);
|
||||
.config(config);
|
||||
|
||||
Swagger swagger = new SwaggerParser().read(inputSpec, input.getAuthorizationValues(), true);
|
||||
final List<AuthorizationValue> authorizationValues = AuthParser.parse(auth);
|
||||
|
||||
Swagger swagger = new SwaggerParser().read(inputSpec, authorizationValues, true);
|
||||
|
||||
input.opts(new ClientOpts())
|
||||
.swagger(swagger);
|
||||
|
||||
return input;
|
||||
}
|
||||
|
||||
@JsonAnySetter
|
||||
public void addDynamicProperty(String name, Object value) {
|
||||
if (value instanceof String) {
|
||||
dynamicProperties.put(name, (String) value);
|
||||
}
|
||||
}
|
||||
|
||||
@JsonAnyGetter
|
||||
public Map<String, String> getDynamicProperties() {
|
||||
return dynamicProperties;
|
||||
}
|
||||
|
||||
private void handleDynamicProperties(CodegenConfig codegenConfig) {
|
||||
for (CliOption langCliOption : codegenConfig.cliOptions()) {
|
||||
String opt = langCliOption.getOpt();
|
||||
if (dynamicProperties.containsKey(opt)) {
|
||||
codegenConfig.additionalProperties().put(opt, dynamicProperties.get(opt));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void setVerboseFlags() {
|
||||
@@ -342,4 +384,17 @@ public class CodegenConfigurator {
|
||||
}
|
||||
}
|
||||
|
||||
public static CodegenConfigurator fromFile(String configFile) {
|
||||
|
||||
if(isNotEmpty(configFile)) {
|
||||
try {
|
||||
CodegenConfigurator result = Json.mapper().readValue(new File(configFile), CodegenConfigurator.class);
|
||||
return result;
|
||||
} catch (IOException e) {
|
||||
LOG.error("Unable to deserialize config file: " + configFile, e);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user