added some useful log/output messages and exception handling

This commit is contained in:
F481 2015-08-24 14:04:50 +02:00
parent 3f8dbf416d
commit 0261121d78
4 changed files with 94 additions and 42 deletions

View File

@ -78,17 +78,19 @@ public class Generate implements Runnable {
*/ */
private static CodegenConfig forName(String name) { private static CodegenConfig forName(String name) {
ServiceLoader<CodegenConfig> loader = load(CodegenConfig.class); ServiceLoader<CodegenConfig> loader = load(CodegenConfig.class);
String available = "";
for (CodegenConfig config : loader) { for (CodegenConfig config : loader) {
if (config.getName().equals(name)) { if (config.getName().equals(name)) {
return config; return config;
} }
available = available + config.getName() + "\n";
} }
// else try to load directly // else try to load directly
try { try {
return (CodegenConfig) Class.forName(name).newInstance(); return (CodegenConfig) Class.forName(name).newInstance();
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException("Can't load config class with name ".concat(name), e); throw new RuntimeException("Can't load config class with name ".concat(name) + "Available: "+available, e);
} }
} }

View File

@ -391,7 +391,13 @@ public class DefaultCodegen {
public String toInstantiationType(Property p) { public String toInstantiationType(Property p) {
if (p instanceof MapProperty) { if (p instanceof MapProperty) {
MapProperty ap = (MapProperty) p; MapProperty ap = (MapProperty) p;
String inner = getSwaggerType(ap.getAdditionalProperties()); Property additionalProperties2 = ap.getAdditionalProperties();
String type = additionalProperties2.getType();
if (null == type) {
LOGGER.error("No Type defined for Additional Property " + additionalProperties2 + "\n" //
+ "\tIn Property: " + p);
}
String inner = getSwaggerType(additionalProperties2);
return instantiationTypes.get("map") + "<String, " + inner + ">"; return instantiationTypes.get("map") + "<String, " + inner + ">";
} else if (p instanceof ArrayProperty) { } else if (p instanceof ArrayProperty) {
ArrayProperty ap = (ArrayProperty) p; ArrayProperty ap = (ArrayProperty) p;
@ -763,7 +769,7 @@ public class DefaultCodegen {
} }
} }
operationId = builder.toString(); operationId = builder.toString();
LOGGER.warn("generated operationId " + operationId); LOGGER.info("generated operationId " + operationId + "\tfor Path: " + httpMethod + " " + path);
} }
operationId = removeNonNameElementToCamelCase(operationId); operationId = removeNonNameElementToCamelCase(operationId);
op.path = path; op.path = path;
@ -1002,6 +1008,10 @@ public class DefaultCodegen {
} }
p.jsonSchema = Json.pretty(param); p.jsonSchema = Json.pretty(param);
if (System.getProperty("debugParser") != null) {
LOGGER.info("working on Parameter " + param);
}
// move the defaultValue for headers, forms and params // move the defaultValue for headers, forms and params
if (param instanceof QueryParameter) { if (param instanceof QueryParameter) {
p.defaultValue = ((QueryParameter) param).getDefaultValue(); p.defaultValue = ((QueryParameter) param).getDefaultValue();
@ -1015,7 +1025,11 @@ public class DefaultCodegen {
SerializableParameter qp = (SerializableParameter) param; SerializableParameter qp = (SerializableParameter) param;
Property property = null; Property property = null;
String collectionFormat = null; String collectionFormat = null;
if ("array".equals(qp.getType())) { String type = qp.getType();
if (null == type) {
LOGGER.warn("Type is NULL for Serializable Parameter: " + param);
}
if ("array".equals(type)) {
Property inner = qp.getItems(); Property inner = qp.getItems();
if (inner == null) { if (inner == null) {
LOGGER.warn("warning! No inner type supplied for array parameter \"" + qp.getName() + "\", using String"); LOGGER.warn("warning! No inner type supplied for array parameter \"" + qp.getName() + "\", using String");
@ -1027,7 +1041,7 @@ public class DefaultCodegen {
p.baseType = pr.datatype; p.baseType = pr.datatype;
p.isContainer = true; p.isContainer = true;
imports.add(pr.baseType); imports.add(pr.baseType);
} else if ("object".equals(qp.getType())) { } else if ("object".equals(type)) {
Property inner = qp.getItems(); Property inner = qp.getItems();
if (inner == null) { if (inner == null) {
LOGGER.warn("warning! No inner type supplied for map parameter \"" + qp.getName() + "\", using String"); LOGGER.warn("warning! No inner type supplied for map parameter \"" + qp.getName() + "\", using String");
@ -1040,12 +1054,13 @@ public class DefaultCodegen {
imports.add(pr.baseType); imports.add(pr.baseType);
} else { } else {
Map<PropertyId, Object> args = new HashMap<PropertyId, Object>(); Map<PropertyId, Object> args = new HashMap<PropertyId, Object>();
String format = qp.getFormat();
args.put(PropertyId.ENUM, qp.getEnum()); args.put(PropertyId.ENUM, qp.getEnum());
property = PropertyBuilder.build(qp.getType(), qp.getFormat(), args); property = PropertyBuilder.build(type, format, args);
} }
if (property == null) { if (property == null) {
LOGGER.warn("warning! Property type \"" + qp.getType() + "\" not found for parameter \"" + param.getName() + "\", using String"); LOGGER.warn("warning! Property type \"" + type + "\" not found for parameter \"" + param.getName() + "\", using String");
property = new StringProperty().description("//TODO automatically added by swagger-codegen. Type was " + qp.getType() + " but not supported"); property = new StringProperty().description("//TODO automatically added by swagger-codegen. Type was " + type + " but not supported");
} }
property.setRequired(param.getRequired()); property.setRequired(param.getRequired());
CodegenProperty model = fromProperty(qp.getName(), property); CodegenProperty model = fromProperty(qp.getName(), property);
@ -1060,6 +1075,10 @@ public class DefaultCodegen {
imports.add(model.complexType); imports.add(model.complexType);
} }
} else { } else {
if (!(param instanceof BodyParameter)) {
LOGGER.error("Cannot use Parameter " + param + " as Body Parameter");
}
BodyParameter bp = (BodyParameter) param; BodyParameter bp = (BodyParameter) param;
Model model = bp.getSchema(); Model model = bp.getSchema();

View File

@ -20,6 +20,8 @@ import io.swagger.models.parameters.Parameter;
import io.swagger.util.Json; import io.swagger.util.Json;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.joda.time.DateTime; import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
@ -39,12 +41,15 @@ import java.util.Map;
import java.util.Set; import java.util.Set;
public class DefaultGenerator extends AbstractGenerator implements Generator { public class DefaultGenerator extends AbstractGenerator implements Generator {
Logger LOGGER = LoggerFactory.getLogger(DefaultGenerator.class);
protected CodegenConfig config; protected CodegenConfig config;
protected ClientOptInput opts = null; protected ClientOptInput opts = null;
protected Swagger swagger = null; protected Swagger swagger = null;
public CodeGenStatus status = CodeGenStatus.UNRUN; public CodeGenStatus status = CodeGenStatus.UNRUN;
@Override
public Generator opts(ClientOptInput opts) { public Generator opts(ClientOptInput opts) {
this.opts = opts; this.opts = opts;
@ -55,6 +60,7 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
return this; return this;
} }
@Override
public List<File> generate() { public List<File> generate() {
if (swagger == null || config == null) { if (swagger == null || config == null) {
throw new RuntimeException("missing swagger input or config!"); throw new RuntimeException("missing swagger input or config!");
@ -150,6 +156,7 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
String template = readTemplate(templateFile); String template = readTemplate(templateFile);
Template tmpl = Mustache.compiler() Template tmpl = Mustache.compiler()
.withLoader(new Mustache.TemplateLoader() { .withLoader(new Mustache.TemplateLoader() {
@Override
public Reader getTemplate(String name) { public Reader getTemplate(String name) {
return getTemplateReader(config.templateDir() + File.separator + name + ".mustache"); return getTemplateReader(config.templateDir() + File.separator + name + ".mustache");
} }
@ -203,6 +210,7 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
String template = readTemplate(templateFile); String template = readTemplate(templateFile);
Template tmpl = Mustache.compiler() Template tmpl = Mustache.compiler()
.withLoader(new Mustache.TemplateLoader() { .withLoader(new Mustache.TemplateLoader() {
@Override
public Reader getTemplate(String name) { public Reader getTemplate(String name) {
return getTemplateReader(config.templateDir() + File.separator + name + ".mustache"); return getTemplateReader(config.templateDir() + File.separator + name + ".mustache");
} }
@ -277,6 +285,7 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
String template = readTemplate(templateFile); String template = readTemplate(templateFile);
Template tmpl = Mustache.compiler() Template tmpl = Mustache.compiler()
.withLoader(new Mustache.TemplateLoader() { .withLoader(new Mustache.TemplateLoader() {
@Override
public Reader getTemplate(String name) { public Reader getTemplate(String name) {
return getTemplateReader(config.templateDir() + File.separator + name + ".mustache"); return getTemplateReader(config.templateDir() + File.separator + name + ".mustache");
} }
@ -416,6 +425,7 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
public void processOperation(String resourcePath, String httpMethod, Operation operation, Map<String, List<CodegenOperation>> operations, Path path) { public void processOperation(String resourcePath, String httpMethod, Operation operation, Map<String, List<CodegenOperation>> operations, Path path) {
if (operation != null) { if (operation != null) {
LOGGER.info("processOperation: resourcePath= " + resourcePath + "\t;" + httpMethod + " " + operation + "\n");
List<String> tags = operation.getTags(); List<String> tags = operation.getTags();
if (tags == null) { if (tags == null) {
tags = new ArrayList<String>(); tags = new ArrayList<String>();
@ -445,44 +455,54 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
} }
for (String tag : tags) { for (String tag : tags) {
CodegenOperation co = config.fromOperation(resourcePath, httpMethod, operation, swagger.getDefinitions()); CodegenOperation co = null;
co.tags = new ArrayList<String>(); try {
co.tags.add(sanitizeTag(tag)); co = config.fromOperation(resourcePath, httpMethod, operation, swagger.getDefinitions());
config.addOperationToGroup(sanitizeTag(tag), resourcePath, operation, co, operations); co.tags = new ArrayList<String>();
co.tags.add(sanitizeTag(tag));
config.addOperationToGroup(sanitizeTag(tag), resourcePath, operation, co, operations);
List<Map<String, List<String>>> securities = operation.getSecurity(); List<Map<String, List<String>>> securities = operation.getSecurity();
if (securities == null) { if (securities == null) {
continue;
}
Map<String, SecuritySchemeDefinition> authMethods = new HashMap<String, SecuritySchemeDefinition>();
for (Map<String, List<String>> security : securities) {
if (security.size() != 1) {
//Not sure what to do
continue; continue;
} }
String securityName = security.keySet().iterator().next(); Map<String, SecuritySchemeDefinition> authMethods = new HashMap<String, SecuritySchemeDefinition>();
SecuritySchemeDefinition securityDefinition = fromSecurity(securityName); for (Map<String, List<String>> security : securities) {
if (securityDefinition != null) { if (security.size() != 1) {
if(securityDefinition instanceof OAuth2Definition) { //Not sure what to do
OAuth2Definition oauth2Definition = (OAuth2Definition) securityDefinition; continue;
OAuth2Definition oauth2Operation = new OAuth2Definition(); }
oauth2Operation.setType(oauth2Definition.getType()); String securityName = security.keySet().iterator().next();
oauth2Operation.setAuthorizationUrl(oauth2Definition.getAuthorizationUrl()); SecuritySchemeDefinition securityDefinition = fromSecurity(securityName);
oauth2Operation.setFlow(oauth2Definition.getFlow()); if (securityDefinition != null) {
oauth2Operation.setTokenUrl(oauth2Definition.getTokenUrl()); if(securityDefinition instanceof OAuth2Definition) {
for (String scope : security.values().iterator().next()) { OAuth2Definition oauth2Definition = (OAuth2Definition) securityDefinition;
if (oauth2Definition.getScopes().containsKey(scope)) { OAuth2Definition oauth2Operation = new OAuth2Definition();
oauth2Operation.addScope(scope, oauth2Definition.getScopes().get(scope)); oauth2Operation.setType(oauth2Definition.getType());
} oauth2Operation.setAuthorizationUrl(oauth2Definition.getAuthorizationUrl());
} oauth2Operation.setFlow(oauth2Definition.getFlow());
authMethods.put(securityName, oauth2Operation); oauth2Operation.setTokenUrl(oauth2Definition.getTokenUrl());
} else { for (String scope : security.values().iterator().next()) {
authMethods.put(securityName, securityDefinition); if (oauth2Definition.getScopes().containsKey(scope)) {
} oauth2Operation.addScope(scope, oauth2Definition.getScopes().get(scope));
}
}
authMethods.put(securityName, oauth2Operation);
} else {
authMethods.put(securityName, securityDefinition);
}
}
}
if (!authMethods.isEmpty()) {
co.authMethods = config.fromSecurity(authMethods);
}
catch (Exception ex) {
LOGGER.error("Error while trying to get Config from Operation for tag(" + tag + ")\n" //
+ "\tResource: " + httpMethod + " " + resourcePath + "\n"//
+ "\tOperation:" + operation + "\n" //
+ "\tDefinitions: " + swagger.getDefinitions() + "\n");
ex.printStackTrace();
} }
}
if (!authMethods.isEmpty()) {
co.authMethods = config.fromSecurity(authMethods);
} }
} }
} }

View File

@ -27,8 +27,12 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class JavaClientCodegen extends DefaultCodegen implements CodegenConfig { public class JavaClientCodegen extends DefaultCodegen implements CodegenConfig {
private static final Logger LOGGER = LoggerFactory.getLogger(JavaClientCodegen.class);
protected String invokerPackage = "io.swagger.client"; protected String invokerPackage = "io.swagger.client";
protected String groupId = "io.swagger"; protected String groupId = "io.swagger";
protected String artifactId = "swagger-java-client"; protected String artifactId = "swagger-java-client";
@ -85,14 +89,17 @@ public class JavaClientCodegen extends DefaultCodegen implements CodegenConfig {
cliOptions.add(buildLibraryCliOption(supportedLibraries)); cliOptions.add(buildLibraryCliOption(supportedLibraries));
} }
@Override
public CodegenType getTag() { public CodegenType getTag() {
return CodegenType.CLIENT; return CodegenType.CLIENT;
} }
@Override
public String getName() { public String getName() {
return "java"; return "java";
} }
@Override
public String getHelp() { public String getHelp() {
return "Generates a Java client library."; return "Generates a Java client library.";
} }
@ -194,6 +201,7 @@ public class JavaClientCodegen extends DefaultCodegen implements CodegenConfig {
return outputFolder + "/" + sourceFolder + "/" + apiPackage().replace('.', File.separatorChar); return outputFolder + "/" + sourceFolder + "/" + apiPackage().replace('.', File.separatorChar);
} }
@Override
public String modelFileFolder() { public String modelFileFolder() {
return outputFolder + "/" + sourceFolder + "/" + modelPackage().replace('.', File.separatorChar); return outputFolder + "/" + sourceFolder + "/" + modelPackage().replace('.', File.separatorChar);
} }
@ -287,6 +295,9 @@ public class JavaClientCodegen extends DefaultCodegen implements CodegenConfig {
} else { } else {
type = swaggerType; type = swaggerType;
} }
if (null == type) {
LOGGER.error("No Type defined for Property " + p);
}
return toModelName(type); return toModelName(type);
} }