Skip to content

Commit 84346c0

Browse files
committed
Merge pull request #1122 from F481/master
added some useful log/output messages and exception handling
2 parents 9e5de7d + fc4fce3 commit 84346c0

File tree

4 files changed

+96
-42
lines changed

4 files changed

+96
-42
lines changed

modules/swagger-codegen-cli/src/main/java/io/swagger/codegen/cmd/Generate.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,17 +78,19 @@ public class Generate implements Runnable {
7878
*/
7979
private static CodegenConfig forName(String name) {
8080
ServiceLoader<CodegenConfig> loader = load(CodegenConfig.class);
81+
String available = "";
8182
for (CodegenConfig config : loader) {
8283
if (config.getName().equals(name)) {
8384
return config;
8485
}
86+
available = available + config.getName() + "\n";
8587
}
8688

8789
// else try to load directly
8890
try {
8991
return (CodegenConfig) Class.forName(name).newInstance();
9092
} catch (Exception e) {
91-
throw new RuntimeException("Can't load config class with name ".concat(name), e);
93+
throw new RuntimeException("Can't load config class with name ".concat(name) + "Available: "+available, e);
9294
}
9395
}
9496

modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,13 @@ public String generateExamplePath(String path, Operation operation) {
394394
public String toInstantiationType(Property p) {
395395
if (p instanceof MapProperty) {
396396
MapProperty ap = (MapProperty) p;
397-
String inner = getSwaggerType(ap.getAdditionalProperties());
397+
Property additionalProperties2 = ap.getAdditionalProperties();
398+
String type = additionalProperties2.getType();
399+
if (null == type) {
400+
LOGGER.error("No Type defined for Additional Property " + additionalProperties2 + "\n" //
401+
+ "\tIn Property: " + p);
402+
}
403+
String inner = getSwaggerType(additionalProperties2);
398404
return instantiationTypes.get("map") + "<String, " + inner + ">";
399405
} else if (p instanceof ArrayProperty) {
400406
ArrayProperty ap = (ArrayProperty) p;
@@ -769,7 +775,7 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation
769775
}
770776
}
771777
operationId = builder.toString();
772-
LOGGER.warn("generated operationId " + operationId);
778+
LOGGER.info("generated operationId " + operationId + "\tfor Path: " + httpMethod + " " + path);
773779
}
774780
operationId = removeNonNameElementToCamelCase(operationId);
775781
op.path = path;
@@ -1009,6 +1015,10 @@ public CodegenParameter fromParameter(Parameter param, Set<String> imports) {
10091015
}
10101016
p.jsonSchema = Json.pretty(param);
10111017

1018+
if (System.getProperty("debugParser") != null) {
1019+
LOGGER.info("working on Parameter " + param);
1020+
}
1021+
10121022
// move the defaultValue for headers, forms and params
10131023
if (param instanceof QueryParameter) {
10141024
p.defaultValue = ((QueryParameter) param).getDefaultValue();
@@ -1022,7 +1032,11 @@ public CodegenParameter fromParameter(Parameter param, Set<String> imports) {
10221032
SerializableParameter qp = (SerializableParameter) param;
10231033
Property property = null;
10241034
String collectionFormat = null;
1025-
if ("array".equals(qp.getType())) {
1035+
String type = qp.getType();
1036+
if (null == type) {
1037+
LOGGER.warn("Type is NULL for Serializable Parameter: " + param);
1038+
}
1039+
if ("array".equals(type)) {
10261040
Property inner = qp.getItems();
10271041
if (inner == null) {
10281042
LOGGER.warn("warning! No inner type supplied for array parameter \"" + qp.getName() + "\", using String");
@@ -1034,7 +1048,7 @@ public CodegenParameter fromParameter(Parameter param, Set<String> imports) {
10341048
p.baseType = pr.datatype;
10351049
p.isContainer = true;
10361050
imports.add(pr.baseType);
1037-
} else if ("object".equals(qp.getType())) {
1051+
} else if ("object".equals(type)) {
10381052
Property inner = qp.getItems();
10391053
if (inner == null) {
10401054
LOGGER.warn("warning! No inner type supplied for map parameter \"" + qp.getName() + "\", using String");
@@ -1047,12 +1061,13 @@ public CodegenParameter fromParameter(Parameter param, Set<String> imports) {
10471061
imports.add(pr.baseType);
10481062
} else {
10491063
Map<PropertyId, Object> args = new HashMap<PropertyId, Object>();
1064+
String format = qp.getFormat();
10501065
args.put(PropertyId.ENUM, qp.getEnum());
1051-
property = PropertyBuilder.build(qp.getType(), qp.getFormat(), args);
1066+
property = PropertyBuilder.build(type, format, args);
10521067
}
10531068
if (property == null) {
1054-
LOGGER.warn("warning! Property type \"" + qp.getType() + "\" not found for parameter \"" + param.getName() + "\", using String");
1055-
property = new StringProperty().description("//TODO automatically added by swagger-codegen. Type was " + qp.getType() + " but not supported");
1069+
LOGGER.warn("warning! Property type \"" + type + "\" not found for parameter \"" + param.getName() + "\", using String");
1070+
property = new StringProperty().description("//TODO automatically added by swagger-codegen. Type was " + type + " but not supported");
10561071
}
10571072
property.setRequired(param.getRequired());
10581073
CodegenProperty model = fromProperty(qp.getName(), property);
@@ -1067,6 +1082,10 @@ public CodegenParameter fromParameter(Parameter param, Set<String> imports) {
10671082
imports.add(model.complexType);
10681083
}
10691084
} else {
1085+
if (!(param instanceof BodyParameter)) {
1086+
LOGGER.error("Cannot use Parameter " + param + " as Body Parameter");
1087+
}
1088+
10701089
BodyParameter bp = (BodyParameter) param;
10711090
Model model = bp.getSchema();
10721091

modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultGenerator.java

Lines changed: 56 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import io.swagger.util.Json;
2121
import org.apache.commons.io.IOUtils;
2222
import org.joda.time.DateTime;
23+
import org.slf4j.Logger;
24+
import org.slf4j.LoggerFactory;
2325

2426
import java.io.File;
2527
import java.io.FileInputStream;
@@ -39,12 +41,15 @@
3941
import java.util.Set;
4042

4143
public class DefaultGenerator extends AbstractGenerator implements Generator {
44+
Logger LOGGER = LoggerFactory.getLogger(DefaultGenerator.class);
45+
4246
protected CodegenConfig config;
4347
protected ClientOptInput opts = null;
4448
protected Swagger swagger = null;
4549

4650
public CodeGenStatus status = CodeGenStatus.UNRUN;
4751

52+
@Override
4853
public Generator opts(ClientOptInput opts) {
4954
this.opts = opts;
5055

@@ -55,6 +60,7 @@ public Generator opts(ClientOptInput opts) {
5560
return this;
5661
}
5762

63+
@Override
5864
public List<File> generate() {
5965
if (swagger == null || config == null) {
6066
throw new RuntimeException("missing swagger input or config!");
@@ -150,6 +156,7 @@ public List<File> generate() {
150156
String template = readTemplate(templateFile);
151157
Template tmpl = Mustache.compiler()
152158
.withLoader(new Mustache.TemplateLoader() {
159+
@Override
153160
public Reader getTemplate(String name) {
154161
return getTemplateReader(config.templateDir() + File.separator + name + ".mustache");
155162
}
@@ -203,6 +210,7 @@ public Reader getTemplate(String name) {
203210
String template = readTemplate(templateFile);
204211
Template tmpl = Mustache.compiler()
205212
.withLoader(new Mustache.TemplateLoader() {
213+
@Override
206214
public Reader getTemplate(String name) {
207215
return getTemplateReader(config.templateDir() + File.separator + name + ".mustache");
208216
}
@@ -277,6 +285,7 @@ public Reader getTemplate(String name) {
277285
String template = readTemplate(templateFile);
278286
Template tmpl = Mustache.compiler()
279287
.withLoader(new Mustache.TemplateLoader() {
288+
@Override
280289
public Reader getTemplate(String name) {
281290
return getTemplateReader(config.templateDir() + File.separator + name + ".mustache");
282291
}
@@ -416,6 +425,9 @@ public SecuritySchemeDefinition fromSecurity(String name) {
416425

417426
public void processOperation(String resourcePath, String httpMethod, Operation operation, Map<String, List<CodegenOperation>> operations, Path path) {
418427
if (operation != null) {
428+
if (System.getProperty("debugOperations") != null) {
429+
LOGGER.debug("processOperation: resourcePath= " + resourcePath + "\t;" + httpMethod + " " + operation + "\n");
430+
}
419431
List<String> tags = operation.getTags();
420432
if (tags == null) {
421433
tags = new ArrayList<String>();
@@ -445,44 +457,54 @@ public void processOperation(String resourcePath, String httpMethod, Operation o
445457
}
446458

447459
for (String tag : tags) {
448-
CodegenOperation co = config.fromOperation(resourcePath, httpMethod, operation, swagger.getDefinitions());
449-
co.tags = new ArrayList<String>();
450-
co.tags.add(sanitizeTag(tag));
451-
config.addOperationToGroup(sanitizeTag(tag), resourcePath, operation, co, operations);
452-
453-
List<Map<String, List<String>>> securities = operation.getSecurity();
454-
if (securities == null) {
455-
continue;
456-
}
457-
Map<String, SecuritySchemeDefinition> authMethods = new HashMap<String, SecuritySchemeDefinition>();
458-
for (Map<String, List<String>> security : securities) {
459-
if (security.size() != 1) {
460-
//Not sure what to do
460+
CodegenOperation co = null;
461+
try {
462+
co = config.fromOperation(resourcePath, httpMethod, operation, swagger.getDefinitions());
463+
co.tags = new ArrayList<String>();
464+
co.tags.add(sanitizeTag(tag));
465+
config.addOperationToGroup(sanitizeTag(tag), resourcePath, operation, co, operations);
466+
467+
List<Map<String, List<String>>> securities = operation.getSecurity();
468+
if (securities == null) {
461469
continue;
462470
}
463-
String securityName = security.keySet().iterator().next();
464-
SecuritySchemeDefinition securityDefinition = fromSecurity(securityName);
465-
if (securityDefinition != null) {
466-
if(securityDefinition instanceof OAuth2Definition) {
467-
OAuth2Definition oauth2Definition = (OAuth2Definition) securityDefinition;
468-
OAuth2Definition oauth2Operation = new OAuth2Definition();
469-
oauth2Operation.setType(oauth2Definition.getType());
470-
oauth2Operation.setAuthorizationUrl(oauth2Definition.getAuthorizationUrl());
471-
oauth2Operation.setFlow(oauth2Definition.getFlow());
472-
oauth2Operation.setTokenUrl(oauth2Definition.getTokenUrl());
473-
for (String scope : security.values().iterator().next()) {
474-
if (oauth2Definition.getScopes().containsKey(scope)) {
475-
oauth2Operation.addScope(scope, oauth2Definition.getScopes().get(scope));
476-
}
477-
}
478-
authMethods.put(securityName, oauth2Operation);
479-
} else {
480-
authMethods.put(securityName, securityDefinition);
481-
}
471+
Map<String, SecuritySchemeDefinition> authMethods = new HashMap<String, SecuritySchemeDefinition>();
472+
for (Map<String, List<String>> security : securities) {
473+
if (security.size() != 1) {
474+
//Not sure what to do
475+
continue;
476+
}
477+
String securityName = security.keySet().iterator().next();
478+
SecuritySchemeDefinition securityDefinition = fromSecurity(securityName);
479+
if (securityDefinition != null) {
480+
if(securityDefinition instanceof OAuth2Definition) {
481+
OAuth2Definition oauth2Definition = (OAuth2Definition) securityDefinition;
482+
OAuth2Definition oauth2Operation = new OAuth2Definition();
483+
oauth2Operation.setType(oauth2Definition.getType());
484+
oauth2Operation.setAuthorizationUrl(oauth2Definition.getAuthorizationUrl());
485+
oauth2Operation.setFlow(oauth2Definition.getFlow());
486+
oauth2Operation.setTokenUrl(oauth2Definition.getTokenUrl());
487+
for (String scope : security.values().iterator().next()) {
488+
if (oauth2Definition.getScopes().containsKey(scope)) {
489+
oauth2Operation.addScope(scope, oauth2Definition.getScopes().get(scope));
490+
}
491+
}
492+
authMethods.put(securityName, oauth2Operation);
493+
} else {
494+
authMethods.put(securityName, securityDefinition);
495+
}
496+
}
497+
}
498+
if (!authMethods.isEmpty()) {
499+
co.authMethods = config.fromSecurity(authMethods);
482500
}
483501
}
484-
if (!authMethods.isEmpty()) {
485-
co.authMethods = config.fromSecurity(authMethods);
502+
catch (Exception ex) {
503+
LOGGER.error("Error while trying to get Config from Operation for tag(" + tag + ")\n" //
504+
+ "\tResource: " + httpMethod + " " + resourcePath + "\n"//
505+
+ "\tOperation:" + operation + "\n" //
506+
+ "\tDefinitions: " + swagger.getDefinitions() + "\n");
507+
ex.printStackTrace();
486508
}
487509
}
488510
}

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/JavaClientCodegen.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,12 @@
2727
import java.util.Map;
2828

2929
import org.apache.commons.lang.StringUtils;
30+
import org.slf4j.Logger;
31+
import org.slf4j.LoggerFactory;
3032

3133
public class JavaClientCodegen extends DefaultCodegen implements CodegenConfig {
34+
private static final Logger LOGGER = LoggerFactory.getLogger(JavaClientCodegen.class);
35+
3236
protected String invokerPackage = "io.swagger.client";
3337
protected String groupId = "io.swagger";
3438
protected String artifactId = "swagger-java-client";
@@ -86,14 +90,17 @@ public JavaClientCodegen() {
8690
cliOptions.add(buildLibraryCliOption(supportedLibraries));
8791
}
8892

93+
@Override
8994
public CodegenType getTag() {
9095
return CodegenType.CLIENT;
9196
}
9297

98+
@Override
9399
public String getName() {
94100
return "java";
95101
}
96102

103+
@Override
97104
public String getHelp() {
98105
return "Generates a Java client library.";
99106
}
@@ -195,6 +202,7 @@ public String apiFileFolder() {
195202
return outputFolder + "/" + sourceFolder + "/" + apiPackage().replace('.', File.separatorChar);
196203
}
197204

205+
@Override
198206
public String modelFileFolder() {
199207
return outputFolder + "/" + sourceFolder + "/" + modelPackage().replace('.', File.separatorChar);
200208
}
@@ -288,6 +296,9 @@ public String getSwaggerType(Property p) {
288296
} else {
289297
type = swaggerType;
290298
}
299+
if (null == type) {
300+
LOGGER.error("No Type defined for Property " + p);
301+
}
291302
return toModelName(type);
292303
}
293304

0 commit comments

Comments
 (0)