29
29
import java .io .InputStream ;
30
30
import java .io .OutputStream ;
31
31
import java .io .Reader ;
32
- import java .util .ArrayList ;
33
- import java .util .Collections ;
34
- import java .util .Comparator ;
35
- import java .util .HashMap ;
36
- import java .util .HashSet ;
37
- import java .util .LinkedHashMap ;
38
- import java .util .LinkedHashSet ;
39
- import java .util .List ;
40
- import java .util .Map ;
41
- import java .util .Set ;
32
+ import java .util .*;
42
33
43
34
public class DefaultGenerator extends AbstractGenerator implements Generator {
44
35
Logger LOGGER = LoggerFactory .getLogger (DefaultGenerator .class );
@@ -60,6 +51,22 @@ public Generator opts(ClientOptInput opts) {
60
51
61
52
@ Override
62
53
public List <File > generate () {
54
+ boolean generateApis = true ;
55
+ boolean generateModels = true ;
56
+ boolean generateSupportingFiles = true ;
57
+
58
+ Set <String > modelsToGenerate = null ;
59
+
60
+ // allows generating only models by specifying a CSV of models to generate, or empty for all
61
+ if (System .getProperty ("models" ) != null ) {
62
+ generateApis = false ;
63
+ generateSupportingFiles = false ;
64
+ String modelNames = System .getProperty ("models" );
65
+ if (!modelNames .isEmpty ()) {
66
+ modelsToGenerate = new HashSet <String >(Arrays .asList (modelNames .split ("," )));
67
+ }
68
+ }
69
+
63
70
if (swagger == null || config == null ) {
64
71
throw new RuntimeException ("missing swagger input or config!" );
65
72
}
@@ -139,28 +146,97 @@ public List<File> generate() {
139
146
if (definitions != null ) {
140
147
List <String > sortedModelKeys = sortModelsByInheritance (definitions );
141
148
142
- for (String name : sortedModelKeys ) {
143
- try {
144
-
145
- //dont generate models that have an import mapping
146
- if (config .importMapping ().containsKey (name )) {
147
- continue ;
149
+ if (generateModels ) {
150
+ if (modelsToGenerate != null ) {
151
+ List <String > updatedKeys = new ArrayList <String >();
152
+ for (String m : sortedModelKeys ) {
153
+ if (modelsToGenerate .contains (m )) {
154
+ updatedKeys .add (m );
155
+ }
148
156
}
157
+ sortedModelKeys = updatedKeys ;
158
+ }
149
159
150
- Model model = definitions .get (name );
151
- Map <String , Model > modelMap = new HashMap <String , Model >();
152
- modelMap .put (name , model );
153
- Map <String , Object > models = processModels (config , modelMap , definitions );
154
- models .putAll (config .additionalProperties ());
160
+ for (String name : sortedModelKeys ) {
161
+ try {
162
+ //don't generate models that have an import mapping
163
+ if (config .importMapping ().containsKey (name )) {
164
+ continue ;
165
+ }
155
166
156
- allModels .add (((List <Object >) models .get ("models" )).get (0 ));
167
+ Model model = definitions .get (name );
168
+ Map <String , Model > modelMap = new HashMap <String , Model >();
169
+ modelMap .put (name , model );
170
+ Map <String , Object > models = processModels (config , modelMap , definitions );
171
+ models .putAll (config .additionalProperties ());
157
172
158
- for (String templateName : config .modelTemplateFiles ().keySet ()) {
159
- String suffix = config .modelTemplateFiles ().get (templateName );
160
- String filename = config .modelFileFolder () + File .separator + config .toModelFilename (name ) + suffix ;
161
- if (!config .shouldOverwrite (filename )) {
173
+ allModels .add (((List <Object >) models .get ("models" )).get (0 ));
174
+
175
+ for (String templateName : config .modelTemplateFiles ().keySet ()) {
176
+ String suffix = config .modelTemplateFiles ().get (templateName );
177
+ String filename = config .modelFileFolder () + File .separator + config .toModelFilename (name ) + suffix ;
178
+ if (!config .shouldOverwrite (filename )) {
179
+ continue ;
180
+ }
181
+ String templateFile = getFullTemplateFile (config , templateName );
182
+ String template = readTemplate (templateFile );
183
+ Template tmpl = Mustache .compiler ()
184
+ .withLoader (new Mustache .TemplateLoader () {
185
+ @ Override
186
+ public Reader getTemplate (String name ) {
187
+ return getTemplateReader (config .templateDir () + File .separator + name + ".mustache" );
188
+ }
189
+ })
190
+ .defaultValue ("" )
191
+ .compile (template );
192
+ writeToFile (filename , tmpl .execute (models ));
193
+ files .add (new File (filename ));
194
+ }
195
+ } catch (Exception e ) {
196
+ throw new RuntimeException ("Could not generate model '" + name + "'" , e );
197
+ }
198
+ }
199
+ }
200
+ }
201
+ if (System .getProperty ("debugModels" ) != null ) {
202
+ System .out .println ("############ Model info ############" );
203
+ Json .prettyPrint (allModels );
204
+ }
205
+
206
+ // apis
207
+ Map <String , List <CodegenOperation >> paths = processPaths (swagger .getPaths ());
208
+ if (generateApis ) {
209
+ for (String tag : paths .keySet ()) {
210
+ try {
211
+ List <CodegenOperation > ops = paths .get (tag );
212
+ Map <String , Object > operation = processOperations (config , tag , ops );
213
+
214
+ operation .put ("basePath" , basePath );
215
+ operation .put ("contextPath" , contextPath );
216
+ operation .put ("baseName" , tag );
217
+ operation .put ("modelPackage" , config .modelPackage ());
218
+ operation .putAll (config .additionalProperties ());
219
+ operation .put ("classname" , config .toApiName (tag ));
220
+ operation .put ("classVarName" , config .toApiVarName (tag ));
221
+ operation .put ("importPath" , config .toApiImport (tag ));
222
+
223
+ processMimeTypes (swagger .getConsumes (), operation , "consumes" );
224
+ processMimeTypes (swagger .getProduces (), operation , "produces" );
225
+
226
+ allOperations .add (new HashMap <String , Object >(operation ));
227
+ for (int i = 0 ; i < allOperations .size (); i ++) {
228
+ Map <String , Object > oo = (Map <String , Object >) allOperations .get (i );
229
+ if (i < (allOperations .size () - 1 )) {
230
+ oo .put ("hasMore" , "true" );
231
+ }
232
+ }
233
+
234
+ for (String templateName : config .apiTemplateFiles ().keySet ()) {
235
+ String filename = config .apiFilename (templateName , tag );
236
+ if (!config .shouldOverwrite (filename ) && new File (filename ).exists ()) {
162
237
continue ;
163
238
}
239
+
164
240
String templateFile = getFullTemplateFile (config , templateName );
165
241
String template = readTemplate (templateFile );
166
242
Template tmpl = Mustache .compiler ()
@@ -172,71 +248,15 @@ public Reader getTemplate(String name) {
172
248
})
173
249
.defaultValue ("" )
174
250
.compile (template );
175
- writeToFile (filename , tmpl .execute (models ));
251
+
252
+ writeToFile (filename , tmpl .execute (operation ));
176
253
files .add (new File (filename ));
177
254
}
178
255
} catch (Exception e ) {
179
- throw new RuntimeException ("Could not generate model '" + name + "'" , e );
256
+ throw new RuntimeException ("Could not generate api file for '" + tag + "'" , e );
180
257
}
181
258
}
182
259
}
183
- if (System .getProperty ("debugModels" ) != null ) {
184
- System .out .println ("############ Model info ############" );
185
- Json .prettyPrint (allModels );
186
- }
187
-
188
- // apis
189
- Map <String , List <CodegenOperation >> paths = processPaths (swagger .getPaths ());
190
- for (String tag : paths .keySet ()) {
191
- try {
192
- List <CodegenOperation > ops = paths .get (tag );
193
- Map <String , Object > operation = processOperations (config , tag , ops );
194
-
195
- operation .put ("basePath" , basePath );
196
- operation .put ("contextPath" , contextPath );
197
- operation .put ("baseName" , tag );
198
- operation .put ("modelPackage" , config .modelPackage ());
199
- operation .putAll (config .additionalProperties ());
200
- operation .put ("classname" , config .toApiName (tag ));
201
- operation .put ("classVarName" , config .toApiVarName (tag ));
202
- operation .put ("importPath" , config .toApiImport (tag ));
203
-
204
- processMimeTypes (swagger .getConsumes (), operation , "consumes" );
205
- processMimeTypes (swagger .getProduces (), operation , "produces" );
206
-
207
- allOperations .add (new HashMap <String , Object >(operation ));
208
- for (int i = 0 ; i < allOperations .size (); i ++) {
209
- Map <String , Object > oo = (Map <String , Object >) allOperations .get (i );
210
- if (i < (allOperations .size () - 1 )) {
211
- oo .put ("hasMore" , "true" );
212
- }
213
- }
214
-
215
- for (String templateName : config .apiTemplateFiles ().keySet ()) {
216
- String filename = config .apiFilename (templateName , tag );
217
- if (!config .shouldOverwrite (filename ) && new File (filename ).exists ()) {
218
- continue ;
219
- }
220
-
221
- String templateFile = getFullTemplateFile (config , templateName );
222
- String template = readTemplate (templateFile );
223
- Template tmpl = Mustache .compiler ()
224
- .withLoader (new Mustache .TemplateLoader () {
225
- @ Override
226
- public Reader getTemplate (String name ) {
227
- return getTemplateReader (config .templateDir () + File .separator + name + ".mustache" );
228
- }
229
- })
230
- .defaultValue ("" )
231
- .compile (template );
232
-
233
- writeToFile (filename , tmpl .execute (operation ));
234
- files .add (new File (filename ));
235
- }
236
- } catch (Exception e ) {
237
- throw new RuntimeException ("Could not generate api file for '" + tag + "'" , e );
238
- }
239
- }
240
260
if (System .getProperty ("debugOperations" ) != null ) {
241
261
System .out .println ("############ Operation info ############" );
242
262
Json .prettyPrint (allOperations );
@@ -280,66 +300,68 @@ public Reader getTemplate(String name) {
280
300
Json .prettyPrint (bundle );
281
301
}
282
302
283
- for (SupportingFile support : config .supportingFiles ()) {
284
- try {
285
- String outputFolder = config .outputFolder ();
286
- if (isNotEmpty (support .folder )) {
287
- outputFolder += File .separator + support .folder ;
288
- }
289
- File of = new File (outputFolder );
290
- if (!of .isDirectory ()) {
291
- of .mkdirs ();
292
- }
293
- String outputFilename = outputFolder + File .separator + support .destinationFilename ;
294
- if (!config .shouldOverwrite (outputFilename )) {
295
- continue ;
296
- }
297
-
298
- String templateFile = getFullTemplateFile (config , support .templateFile );
303
+ if (generateSupportingFiles ) {
304
+ for (SupportingFile support : config .supportingFiles ()) {
305
+ try {
306
+ String outputFolder = config .outputFolder ();
307
+ if (isNotEmpty (support .folder )) {
308
+ outputFolder += File .separator + support .folder ;
309
+ }
310
+ File of = new File (outputFolder );
311
+ if (!of .isDirectory ()) {
312
+ of .mkdirs ();
313
+ }
314
+ String outputFilename = outputFolder + File .separator + support .destinationFilename ;
315
+ if (!config .shouldOverwrite (outputFilename )) {
316
+ continue ;
317
+ }
299
318
300
- if (templateFile .endsWith ("mustache" )) {
301
- String template = readTemplate (templateFile );
302
- Template tmpl = Mustache .compiler ()
303
- .withLoader (new Mustache .TemplateLoader () {
304
- @ Override
305
- public Reader getTemplate (String name ) {
306
- return getTemplateReader (config .templateDir () + File .separator + name + ".mustache" );
307
- }
308
- })
309
- .defaultValue ("" )
310
- .compile (template );
319
+ String templateFile = getFullTemplateFile (config , support .templateFile );
311
320
312
- writeToFile (outputFilename , tmpl .execute (bundle ));
313
- files .add (new File (outputFilename ));
314
- } else {
315
- InputStream in = null ;
321
+ if (templateFile .endsWith ("mustache" )) {
322
+ String template = readTemplate (templateFile );
323
+ Template tmpl = Mustache .compiler ()
324
+ .withLoader (new Mustache .TemplateLoader () {
325
+ @ Override
326
+ public Reader getTemplate (String name ) {
327
+ return getTemplateReader (config .templateDir () + File .separator + name + ".mustache" );
328
+ }
329
+ })
330
+ .defaultValue ("" )
331
+ .compile (template );
316
332
317
- try {
318
- in = new FileInputStream (templateFile );
319
- } catch (Exception e ) {
320
- // continue
321
- }
322
- if (in == null ) {
323
- in = this .getClass ().getClassLoader ().getResourceAsStream (getCPResourcePath (templateFile ));
324
- }
325
- File outputFile = new File (outputFilename );
326
- OutputStream out = new FileOutputStream (outputFile , false );
327
- if (in != null && out != null ) {
328
- System .out .println ("writing file " + outputFile );
329
- IOUtils .copy (in , out );
333
+ writeToFile (outputFilename , tmpl .execute (bundle ));
334
+ files .add (new File (outputFilename ));
330
335
} else {
336
+ InputStream in = null ;
337
+
338
+ try {
339
+ in = new FileInputStream (templateFile );
340
+ } catch (Exception e ) {
341
+ // continue
342
+ }
331
343
if (in == null ) {
332
- System . out . println ( "can't open " + templateFile + " for input" );
344
+ in = this . getClass (). getClassLoader (). getResourceAsStream ( getCPResourcePath ( templateFile ) );
333
345
}
334
- if (out == null ) {
335
- System .out .println ("can't open " + outputFile + " for output" );
346
+ File outputFile = new File (outputFilename );
347
+ OutputStream out = new FileOutputStream (outputFile , false );
348
+ if (in != null && out != null ) {
349
+ System .out .println ("writing file " + outputFile );
350
+ IOUtils .copy (in , out );
351
+ } else {
352
+ if (in == null ) {
353
+ System .out .println ("can't open " + templateFile + " for input" );
354
+ }
355
+ if (out == null ) {
356
+ System .out .println ("can't open " + outputFile + " for output" );
357
+ }
336
358
}
337
- }
338
359
339
- files .add (outputFile );
360
+ files .add (outputFile );
361
+ }
362
+ } catch (Exception e ) {
363
+ throw new RuntimeException ("Could not generate supporting file '" + support + "'" , e );
340
364
}
341
- } catch (Exception e ) {
342
- throw new RuntimeException ("Could not generate supporting file '" + support + "'" , e );
343
365
}
344
366
}
345
367
0 commit comments