20
20
import io .swagger .util .Json ;
21
21
import org .apache .commons .io .IOUtils ;
22
22
import org .joda .time .DateTime ;
23
+ import org .slf4j .Logger ;
24
+ import org .slf4j .LoggerFactory ;
23
25
24
26
import java .io .File ;
25
27
import java .io .FileInputStream ;
39
41
import java .util .Set ;
40
42
41
43
public class DefaultGenerator extends AbstractGenerator implements Generator {
44
+ Logger LOGGER = LoggerFactory .getLogger (DefaultGenerator .class );
45
+
42
46
protected CodegenConfig config ;
43
47
protected ClientOptInput opts = null ;
44
48
protected Swagger swagger = null ;
45
49
46
50
public CodeGenStatus status = CodeGenStatus .UNRUN ;
47
51
52
+ @ Override
48
53
public Generator opts (ClientOptInput opts ) {
49
54
this .opts = opts ;
50
55
@@ -55,6 +60,7 @@ public Generator opts(ClientOptInput opts) {
55
60
return this ;
56
61
}
57
62
63
+ @ Override
58
64
public List <File > generate () {
59
65
if (swagger == null || config == null ) {
60
66
throw new RuntimeException ("missing swagger input or config!" );
@@ -150,6 +156,7 @@ public List<File> generate() {
150
156
String template = readTemplate (templateFile );
151
157
Template tmpl = Mustache .compiler ()
152
158
.withLoader (new Mustache .TemplateLoader () {
159
+ @ Override
153
160
public Reader getTemplate (String name ) {
154
161
return getTemplateReader (config .templateDir () + File .separator + name + ".mustache" );
155
162
}
@@ -203,6 +210,7 @@ public Reader getTemplate(String name) {
203
210
String template = readTemplate (templateFile );
204
211
Template tmpl = Mustache .compiler ()
205
212
.withLoader (new Mustache .TemplateLoader () {
213
+ @ Override
206
214
public Reader getTemplate (String name ) {
207
215
return getTemplateReader (config .templateDir () + File .separator + name + ".mustache" );
208
216
}
@@ -277,6 +285,7 @@ public Reader getTemplate(String name) {
277
285
String template = readTemplate (templateFile );
278
286
Template tmpl = Mustache .compiler ()
279
287
.withLoader (new Mustache .TemplateLoader () {
288
+ @ Override
280
289
public Reader getTemplate (String name ) {
281
290
return getTemplateReader (config .templateDir () + File .separator + name + ".mustache" );
282
291
}
@@ -416,6 +425,9 @@ public SecuritySchemeDefinition fromSecurity(String name) {
416
425
417
426
public void processOperation (String resourcePath , String httpMethod , Operation operation , Map <String , List <CodegenOperation >> operations , Path path ) {
418
427
if (operation != null ) {
428
+ if (System .getProperty ("debugOperations" ) != null ) {
429
+ LOGGER .debug ("processOperation: resourcePath= " + resourcePath + "\t ;" + httpMethod + " " + operation + "\n " );
430
+ }
419
431
List <String > tags = operation .getTags ();
420
432
if (tags == null ) {
421
433
tags = new ArrayList <String >();
@@ -445,44 +457,54 @@ public void processOperation(String resourcePath, String httpMethod, Operation o
445
457
}
446
458
447
459
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 ) {
461
469
continue ;
462
470
}
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 );
482
500
}
483
501
}
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
+ + "\t Resource: " + httpMethod + " " + resourcePath + "\n " //
505
+ + "\t Operation:" + operation + "\n " //
506
+ + "\t Definitions: " + swagger .getDefinitions () + "\n " );
507
+ ex .printStackTrace ();
486
508
}
487
509
}
488
510
}
0 commit comments