Skip to content

Commit d4a94fb

Browse files
committed
- DefaultGenerator now communicates failures by throwing exceptions
rather than setting a status flag in a public field - DefaultGenerator now decorates exceptions to record where a failure occured (which api, model or operation?) - CodeGenMojo now propagates this exception to maven to abort the build
1 parent 98ff2d2 commit d4a94fb

File tree

3 files changed

+134
-126
lines changed

3 files changed

+134
-126
lines changed

modules/swagger-codegen-maven-plugin/src/main/java/io/swagger/codegen/plugin/CodeGenMojo.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,16 @@ public void execute() throws MojoExecutionException {
162162

163163
ClientOptInput input = new ClientOptInput().opts(new ClientOpts()).swagger(swagger);
164164
input.setConfig(config);
165-
new DefaultGenerator().opts(input).generate();
165+
166+
try {
167+
new DefaultGenerator().opts(input).generate();
168+
} catch (Exception e) {
169+
// Maven logs exceptions thrown by plugins only if invoked with -e
170+
// I find it annoying to jump through hoops to get basic diagnostic information,
171+
// so let's log it in any case:
172+
getLog().error(e);
173+
throw new MojoExecutionException("Code generation failed. See above for the full exception.");
174+
}
166175

167176
if (addCompileSourceRoot) {
168177
project.addCompileSourceRoot(output.toString());

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

Lines changed: 124 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
import com.samskivert.mustache.Mustache;
77
import com.samskivert.mustache.Template;
8-
import io.swagger.codegen.languages.CodeGenStatus;
98
import io.swagger.models.ComposedModel;
109
import io.swagger.models.Contact;
1110
import io.swagger.models.Info;
@@ -47,8 +46,6 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
4746
protected ClientOptInput opts = null;
4847
protected Swagger swagger = null;
4948

50-
public CodeGenStatus status = CodeGenStatus.UNRUN;
51-
5249
@Override
5350
public Generator opts(ClientOptInput opts) {
5451
this.opts = opts;
@@ -69,76 +66,76 @@ public List<File> generate() {
6966
Json.prettyPrint(swagger);
7067
}
7168
List<File> files = new ArrayList<File>();
72-
try {
73-
config.processOpts();
74-
config.preprocessSwagger(swagger);
69+
config.processOpts();
70+
config.preprocessSwagger(swagger);
7571

76-
config.additionalProperties().put("generatedDate", DateTime.now().toString());
77-
config.additionalProperties().put("generatorClass", config.getClass().toString());
72+
config.additionalProperties().put("generatedDate", DateTime.now().toString());
73+
config.additionalProperties().put("generatorClass", config.getClass().toString());
7874

79-
if (swagger.getInfo() != null) {
80-
Info info = swagger.getInfo();
81-
if (info.getTitle() != null) {
82-
config.additionalProperties().put("appName", info.getTitle());
83-
}
84-
if (info.getVersion() != null) {
85-
config.additionalProperties().put("appVersion", info.getVersion());
86-
}
87-
if (info.getDescription() != null) {
88-
config.additionalProperties().put("appDescription",
89-
config.escapeText(info.getDescription()));
90-
}
91-
if (info.getContact() != null) {
92-
Contact contact = info.getContact();
93-
config.additionalProperties().put("infoUrl", contact.getUrl());
94-
if (contact.getEmail() != null) {
95-
config.additionalProperties().put("infoEmail", contact.getEmail());
96-
}
75+
if (swagger.getInfo() != null) {
76+
Info info = swagger.getInfo();
77+
if (info.getTitle() != null) {
78+
config.additionalProperties().put("appName", info.getTitle());
79+
}
80+
if (info.getVersion() != null) {
81+
config.additionalProperties().put("appVersion", info.getVersion());
82+
}
83+
if (info.getDescription() != null) {
84+
config.additionalProperties().put("appDescription",
85+
config.escapeText(info.getDescription()));
86+
}
87+
if (info.getContact() != null) {
88+
Contact contact = info.getContact();
89+
config.additionalProperties().put("infoUrl", contact.getUrl());
90+
if (contact.getEmail() != null) {
91+
config.additionalProperties().put("infoEmail", contact.getEmail());
9792
}
98-
if (info.getLicense() != null) {
99-
License license = info.getLicense();
100-
if (license.getName() != null) {
101-
config.additionalProperties().put("licenseInfo", license.getName());
102-
}
103-
if (license.getUrl() != null) {
104-
config.additionalProperties().put("licenseUrl", license.getUrl());
105-
}
93+
}
94+
if (info.getLicense() != null) {
95+
License license = info.getLicense();
96+
if (license.getName() != null) {
97+
config.additionalProperties().put("licenseInfo", license.getName());
10698
}
107-
if (info.getVersion() != null) {
108-
config.additionalProperties().put("version", info.getVersion());
99+
if (license.getUrl() != null) {
100+
config.additionalProperties().put("licenseUrl", license.getUrl());
109101
}
110102
}
111-
112-
StringBuilder hostBuilder = new StringBuilder();
113-
String scheme;
114-
if (swagger.getSchemes() != null && swagger.getSchemes().size() > 0) {
115-
scheme = swagger.getSchemes().get(0).toValue();
116-
} else {
117-
scheme = "https";
118-
}
119-
hostBuilder.append(scheme);
120-
hostBuilder.append("://");
121-
if (swagger.getHost() != null) {
122-
hostBuilder.append(swagger.getHost());
123-
} else {
124-
hostBuilder.append("localhost");
125-
}
126-
if (swagger.getBasePath() != null) {
127-
hostBuilder.append(swagger.getBasePath());
103+
if (info.getVersion() != null) {
104+
config.additionalProperties().put("version", info.getVersion());
128105
}
129-
String contextPath = swagger.getBasePath() == null ? "" : swagger.getBasePath();
130-
String basePath = hostBuilder.toString();
106+
}
107+
108+
StringBuilder hostBuilder = new StringBuilder();
109+
String scheme;
110+
if (swagger.getSchemes() != null && swagger.getSchemes().size() > 0) {
111+
scheme = swagger.getSchemes().get(0).toValue();
112+
} else {
113+
scheme = "https";
114+
}
115+
hostBuilder.append(scheme);
116+
hostBuilder.append("://");
117+
if (swagger.getHost() != null) {
118+
hostBuilder.append(swagger.getHost());
119+
} else {
120+
hostBuilder.append("localhost");
121+
}
122+
if (swagger.getBasePath() != null) {
123+
hostBuilder.append(swagger.getBasePath());
124+
}
125+
String contextPath = swagger.getBasePath() == null ? "" : swagger.getBasePath();
126+
String basePath = hostBuilder.toString();
131127

132128

133-
List<Object> allOperations = new ArrayList<Object>();
134-
List<Object> allModels = new ArrayList<Object>();
129+
List<Object> allOperations = new ArrayList<Object>();
130+
List<Object> allModels = new ArrayList<Object>();
135131

136-
// models
137-
Map<String, Model> definitions = swagger.getDefinitions();
138-
if (definitions != null) {
139-
List<String> sortedModelKeys = sortModelsByInheritance(definitions);
132+
// models
133+
Map<String, Model> definitions = swagger.getDefinitions();
134+
if (definitions != null) {
135+
List<String> sortedModelKeys = sortModelsByInheritance(definitions);
140136

141-
for (String name : sortedModelKeys) {
137+
for (String name : sortedModelKeys) {
138+
try {
142139

143140
//dont generate models that have an import mapping
144141
if(config.importMapping().containsKey(name)) {
@@ -173,16 +170,20 @@ public Reader getTemplate(String name) {
173170
writeToFile(filename, tmpl.execute(models));
174171
files.add(new File(filename));
175172
}
173+
} catch (Exception e) {
174+
throw new RuntimeException("Could not generate model '" + name + "'", e);
176175
}
177176
}
178-
if (System.getProperty("debugModels") != null) {
179-
System.out.println("############ Model info ############");
180-
Json.prettyPrint(allModels);
181-
}
177+
}
178+
if (System.getProperty("debugModels") != null) {
179+
System.out.println("############ Model info ############");
180+
Json.prettyPrint(allModels);
181+
}
182182

183-
// apis
184-
Map<String, List<CodegenOperation>> paths = processPaths(swagger.getPaths());
185-
for (String tag : paths.keySet()) {
183+
// apis
184+
Map<String, List<CodegenOperation>> paths = processPaths(swagger.getPaths());
185+
for (String tag : paths.keySet()) {
186+
try {
186187
List<CodegenOperation> ops = paths.get(tag);
187188
Map<String, Object> operation = processOperations(config, tag, ops);
188189

@@ -207,7 +208,6 @@ public Reader getTemplate(String name) {
207208
}
208209

209210
for (String templateName : config.apiTemplateFiles().keySet()) {
210-
211211
String filename = config.apiFilename(templateName, tag);
212212
if (!config.shouldOverwrite(filename) && new File(filename).exists()) {
213213
continue;
@@ -228,51 +228,55 @@ public Reader getTemplate(String name) {
228228
writeToFile(filename, tmpl.execute(operation));
229229
files.add(new File(filename));
230230
}
231-
}
232-
if (System.getProperty("debugOperations") != null) {
233-
System.out.println("############ Operation info ############");
234-
Json.prettyPrint(allOperations);
235-
}
231+
} catch (Exception e) {
232+
throw new RuntimeException("Could not generate api file for '" + tag + "'", e);
233+
}
234+
}
235+
if (System.getProperty("debugOperations") != null) {
236+
System.out.println("############ Operation info ############");
237+
Json.prettyPrint(allOperations);
238+
}
236239

237-
// supporting files
238-
Map<String, Object> bundle = new HashMap<String, Object>();
239-
bundle.putAll(config.additionalProperties());
240-
bundle.put("apiPackage", config.apiPackage());
240+
// supporting files
241+
Map<String, Object> bundle = new HashMap<String, Object>();
242+
bundle.putAll(config.additionalProperties());
243+
bundle.put("apiPackage", config.apiPackage());
241244

242-
Map<String, Object> apis = new HashMap<String, Object>();
243-
apis.put("apis", allOperations);
244-
if (swagger.getHost() != null) {
245-
bundle.put("host", swagger.getHost());
246-
}
247-
bundle.put("basePath", basePath);
248-
bundle.put("scheme", scheme);
249-
bundle.put("contextPath", contextPath);
250-
bundle.put("apiInfo", apis);
251-
bundle.put("models", allModels);
252-
bundle.put("apiFolder", config.apiPackage().replace('.', File.separatorChar));
253-
bundle.put("modelPackage", config.modelPackage());
254-
List<CodegenSecurity> authMethods = config.fromSecurity(swagger.getSecurityDefinitions());
255-
if (authMethods != null && !authMethods.isEmpty()) {
256-
bundle.put("authMethods", authMethods);
257-
bundle.put("hasAuthMethods", true);
258-
}
259-
if (swagger.getExternalDocs() != null) {
260-
bundle.put("externalDocs", swagger.getExternalDocs());
261-
}
262-
for (int i = 0; i < allModels.size() - 1; i++) {
263-
HashMap<String, CodegenModel> cm = (HashMap<String, CodegenModel>) allModels.get(i);
264-
CodegenModel m = cm.get("model");
265-
m.hasMoreModels = true;
266-
}
245+
Map<String, Object> apis = new HashMap<String, Object>();
246+
apis.put("apis", allOperations);
247+
if (swagger.getHost() != null) {
248+
bundle.put("host", swagger.getHost());
249+
}
250+
bundle.put("basePath", basePath);
251+
bundle.put("scheme", scheme);
252+
bundle.put("contextPath", contextPath);
253+
bundle.put("apiInfo", apis);
254+
bundle.put("models", allModels);
255+
bundle.put("apiFolder", config.apiPackage().replace('.', File.separatorChar));
256+
bundle.put("modelPackage", config.modelPackage());
257+
List<CodegenSecurity> authMethods = config.fromSecurity(swagger.getSecurityDefinitions());
258+
if (authMethods != null && !authMethods.isEmpty()) {
259+
bundle.put("authMethods", authMethods);
260+
bundle.put("hasAuthMethods", true);
261+
}
262+
if (swagger.getExternalDocs() != null) {
263+
bundle.put("externalDocs", swagger.getExternalDocs());
264+
}
265+
for (int i = 0; i < allModels.size() - 1; i++) {
266+
HashMap<String, CodegenModel> cm = (HashMap<String, CodegenModel>) allModels.get(i);
267+
CodegenModel m = cm.get("model");
268+
m.hasMoreModels = true;
269+
}
267270

268-
config.postProcessSupportingFileData(bundle);
271+
config.postProcessSupportingFileData(bundle);
269272

270-
if (System.getProperty("debugSupportingFiles") != null) {
271-
System.out.println("############ Supporting file info ############");
272-
Json.prettyPrint(bundle);
273-
}
273+
if (System.getProperty("debugSupportingFiles") != null) {
274+
System.out.println("############ Supporting file info ############");
275+
Json.prettyPrint(bundle);
276+
}
274277

275-
for (SupportingFile support : config.supportingFiles()) {
278+
for (SupportingFile support : config.supportingFiles()) {
279+
try {
276280
String outputFolder = config.outputFolder();
277281
if (isNotEmpty(support.folder)) {
278282
outputFolder += File.separator + support.folder;
@@ -329,14 +333,13 @@ public Reader getTemplate(String name) {
329333

330334
files.add(outputFile);
331335
}
336+
} catch (Exception e) {
337+
throw new RuntimeException("Could not generate supporting file '" + support + "'", e);
332338
}
339+
}
333340

334-
config.processSwagger(swagger);
341+
config.processSwagger(swagger);
335342

336-
status = CodeGenStatus.SUCCESSFUL;
337-
} catch (Exception e) {
338-
status = CodeGenStatus.FAILED;
339-
}
340343
return files;
341344
}
342345

@@ -508,11 +511,12 @@ public void processOperation(String resourcePath, String httpMethod, Operation o
508511
}
509512
}
510513
catch (Exception ex) {
511-
LOGGER.error("Error while trying to get Config from Operation for tag(" + tag + ")\n" //
512-
+ "\tResource: " + httpMethod + " " + resourcePath + "\n"//
513-
+ "\tOperation:" + operation + "\n" //
514-
+ "\tDefinitions: " + swagger.getDefinitions() + "\n");
515-
ex.printStackTrace();
514+
String msg = "Could not process operation:\n" //
515+
+ " Tag: " + tag + "\n"//
516+
+ " Operation: " + operation.getOperationId() + "\n" //
517+
+ " Resource: " + httpMethod + " " + resourcePath + "\n"//
518+
+ " Definitions: " + swagger.getDefinitions();
519+
throw new RuntimeException(msg, ex);
516520
}
517521
}
518522
}

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

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)