5
5
6
6
import com .samskivert .mustache .Mustache ;
7
7
import com .samskivert .mustache .Template ;
8
- import io .swagger .codegen .languages .CodeGenStatus ;
9
8
import io .swagger .models .ComposedModel ;
10
9
import io .swagger .models .Contact ;
11
10
import io .swagger .models .Info ;
@@ -47,8 +46,6 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
47
46
protected ClientOptInput opts = null ;
48
47
protected Swagger swagger = null ;
49
48
50
- public CodeGenStatus status = CodeGenStatus .UNRUN ;
51
-
52
49
@ Override
53
50
public Generator opts (ClientOptInput opts ) {
54
51
this .opts = opts ;
@@ -69,76 +66,76 @@ public List<File> generate() {
69
66
Json .prettyPrint (swagger );
70
67
}
71
68
List <File > files = new ArrayList <File >();
72
- try {
73
- config .processOpts ();
74
- config .preprocessSwagger (swagger );
69
+ config .processOpts ();
70
+ config .preprocessSwagger (swagger );
75
71
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 ());
78
74
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 ());
97
92
}
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 ());
106
98
}
107
- if (info . getVersion () != null ) {
108
- config .additionalProperties ().put ("version " , info . getVersion ());
99
+ if (license . getUrl () != null ) {
100
+ config .additionalProperties ().put ("licenseUrl " , license . getUrl ());
109
101
}
110
102
}
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 ());
128
105
}
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 ();
131
127
132
128
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 >();
135
131
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 );
140
136
141
- for (String name : sortedModelKeys ) {
137
+ for (String name : sortedModelKeys ) {
138
+ try {
142
139
143
140
//dont generate models that have an import mapping
144
141
if (config .importMapping ().containsKey (name )) {
@@ -173,16 +170,20 @@ public Reader getTemplate(String name) {
173
170
writeToFile (filename , tmpl .execute (models ));
174
171
files .add (new File (filename ));
175
172
}
173
+ } catch (Exception e ) {
174
+ throw new RuntimeException ("Could not generate model '" + name + "'" , e );
176
175
}
177
176
}
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
+ }
182
182
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 {
186
187
List <CodegenOperation > ops = paths .get (tag );
187
188
Map <String , Object > operation = processOperations (config , tag , ops );
188
189
@@ -207,7 +208,6 @@ public Reader getTemplate(String name) {
207
208
}
208
209
209
210
for (String templateName : config .apiTemplateFiles ().keySet ()) {
210
-
211
211
String filename = config .apiFilename (templateName , tag );
212
212
if (!config .shouldOverwrite (filename ) && new File (filename ).exists ()) {
213
213
continue ;
@@ -228,51 +228,55 @@ public Reader getTemplate(String name) {
228
228
writeToFile (filename , tmpl .execute (operation ));
229
229
files .add (new File (filename ));
230
230
}
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
+ }
236
239
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 ());
241
244
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
+ }
267
270
268
- config .postProcessSupportingFileData (bundle );
271
+ config .postProcessSupportingFileData (bundle );
269
272
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
+ }
274
277
275
- for (SupportingFile support : config .supportingFiles ()) {
278
+ for (SupportingFile support : config .supportingFiles ()) {
279
+ try {
276
280
String outputFolder = config .outputFolder ();
277
281
if (isNotEmpty (support .folder )) {
278
282
outputFolder += File .separator + support .folder ;
@@ -329,14 +333,13 @@ public Reader getTemplate(String name) {
329
333
330
334
files .add (outputFile );
331
335
}
336
+ } catch (Exception e ) {
337
+ throw new RuntimeException ("Could not generate supporting file '" + support + "'" , e );
332
338
}
339
+ }
333
340
334
- config .processSwagger (swagger );
341
+ config .processSwagger (swagger );
335
342
336
- status = CodeGenStatus .SUCCESSFUL ;
337
- } catch (Exception e ) {
338
- status = CodeGenStatus .FAILED ;
339
- }
340
343
return files ;
341
344
}
342
345
@@ -508,11 +511,12 @@ public void processOperation(String resourcePath, String httpMethod, Operation o
508
511
}
509
512
}
510
513
catch (Exception ex ) {
511
- LOGGER .error ("Error while trying to get Config from Operation for tag(" + tag + ")\n " //
512
- + "\t Resource: " + httpMethod + " " + resourcePath + "\n " //
513
- + "\t Operation:" + operation + "\n " //
514
- + "\t Definitions: " + 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 );
516
520
}
517
521
}
518
522
}
0 commit comments