19
19
import java .util .LinkedHashSet ;
20
20
import java .util .List ;
21
21
import java .util .Map ;
22
+ import java .util .Optional ;
22
23
import java .util .Set ;
23
24
import java .util .TreeMap ;
24
25
import java .util .TreeSet ;
@@ -174,10 +175,15 @@ private void configureGeneratorProperties() {
174
175
}
175
176
// model/api tests and documentation options rely on parent generate options (api or model) and no other options.
176
177
// They default to true in all scenarios and can only be marked false explicitly
177
- generateModelTests = System .getProperty (CodegenConstants .MODEL_TESTS ) != null ? Boolean .valueOf (System .getProperty (CodegenConstants .MODEL_TESTS )) : getGeneratorPropertyDefaultSwitch (CodegenConstants .MODEL_TESTS , true );
178
- generateModelDocumentation = System .getProperty (CodegenConstants .MODEL_DOCS ) != null ? Boolean .valueOf (System .getProperty (CodegenConstants .MODEL_DOCS )) : getGeneratorPropertyDefaultSwitch (CodegenConstants .MODEL_DOCS , true );
179
- generateApiTests = System .getProperty (CodegenConstants .API_TESTS ) != null ? Boolean .valueOf (System .getProperty (CodegenConstants .API_TESTS )) : getGeneratorPropertyDefaultSwitch (CodegenConstants .API_TESTS , true );
180
- generateApiDocumentation = System .getProperty (CodegenConstants .API_DOCS ) != null ? Boolean .valueOf (System .getProperty (CodegenConstants .API_DOCS )) : getGeneratorPropertyDefaultSwitch (CodegenConstants .API_DOCS , true );
178
+ final Boolean generateModelTestsOption = getCustomOptionBooleanValue (CodegenConstants .MODEL_TESTS_OPTION );
179
+ final Boolean generateModelDocsOption = getCustomOptionBooleanValue (CodegenConstants .MODEL_DOCS_OPTION );
180
+ final Boolean generateAPITestsOption = getCustomOptionBooleanValue (CodegenConstants .API_TESTS_OPTION );
181
+ final Boolean generateAPIDocsOption = getCustomOptionBooleanValue (CodegenConstants .API_DOCS_OPTION );
182
+
183
+ generateModelTests = generateModelTestsOption != null ? generateModelTestsOption : getGeneratorPropertyDefaultSwitch (CodegenConstants .MODEL_TESTS , true );
184
+ generateModelDocumentation = generateModelDocsOption != null ? generateModelDocsOption : getGeneratorPropertyDefaultSwitch (CodegenConstants .MODEL_DOCS , true );
185
+ generateApiTests = generateAPITestsOption != null ? generateAPITestsOption : getGeneratorPropertyDefaultSwitch (CodegenConstants .API_TESTS , true );
186
+ generateApiDocumentation = generateAPIDocsOption != null ? generateAPIDocsOption : getGeneratorPropertyDefaultSwitch (CodegenConstants .API_DOCS , true );
181
187
182
188
// Additional properties added for tests to exclude references in project related files
183
189
config .additionalProperties ().put (CodegenConstants .GENERATE_API_TESTS , generateApiTests );
@@ -317,12 +323,12 @@ private void generateModels(List<File> files, List<Object> allModels) {
317
323
String modelNames = System .getProperty ("models" );
318
324
Set <String > modelsToGenerate = null ;
319
325
if (modelNames != null && !modelNames .isEmpty ()) {
320
- modelsToGenerate = new HashSet <String >(Arrays .asList (modelNames .split ("," )));
326
+ modelsToGenerate = new HashSet <>(Arrays .asList (modelNames .split ("," )));
321
327
}
322
328
323
329
Set <String > modelKeys = schemas .keySet ();
324
330
if (modelsToGenerate != null && !modelsToGenerate .isEmpty ()) {
325
- Set <String > updatedKeys = new HashSet <String >();
331
+ Set <String > updatedKeys = new HashSet <>();
326
332
for (String m : modelKeys ) {
327
333
if (modelsToGenerate .contains (m )) {
328
334
updatedKeys .add (m );
@@ -332,7 +338,7 @@ private void generateModels(List<File> files, List<Object> allModels) {
332
338
}
333
339
334
340
// store all processed models
335
- Map <String ,Object > allProcessedModels = new TreeMap <String , Object >(new Comparator <String >() {
341
+ Map <String ,Object > allProcessedModels = new TreeMap <>(new Comparator <String >() {
336
342
@ Override
337
343
public int compare (String o1 , String o2 ) {
338
344
return ObjectUtils .compare (config .toModelName (o1 ), config .toModelName (o2 ));
@@ -422,7 +428,7 @@ private void generateApis(List<File> files, List<Object> allOperations, List<Obj
422
428
apisToGenerate = new HashSet <String >(Arrays .asList (apiNames .split ("," )));
423
429
}
424
430
if (apisToGenerate != null && !apisToGenerate .isEmpty ()) {
425
- Map <String , List <CodegenOperation >> updatedPaths = new TreeMap <String , List < CodegenOperation > >();
431
+ Map <String , List <CodegenOperation >> updatedPaths = new TreeMap <>();
426
432
for (String m : paths .keySet ()) {
427
433
if (apisToGenerate .contains (m )) {
428
434
updatedPaths .put (m , paths .get (m ));
@@ -463,7 +469,7 @@ public int compare(CodegenOperation one, CodegenOperation another) {
463
469
}
464
470
operation .put ("sortParamsByRequiredFlag" , sortParamsByRequiredFlag );
465
471
466
- allOperations .add (new HashMap <String , Object >(operation ));
472
+ allOperations .add (new HashMap <>(operation ));
467
473
for (int i = 0 ; i < allOperations .size (); i ++) {
468
474
Map <String , Object > oo = (Map <String , Object >) allOperations .get (i );
469
475
if (i < (allOperations .size () - 1 )) {
@@ -536,7 +542,7 @@ private void generateSupportingFiles(List<File> files, Map<String, Object> bundl
536
542
Set <String > supportingFilesToGenerate = null ;
537
543
String supportingFiles = System .getProperty (CodegenConstants .SUPPORTING_FILES );
538
544
if (supportingFiles != null && !supportingFiles .isEmpty ()) {
539
- supportingFilesToGenerate = new HashSet <String >(Arrays .asList (supportingFiles .split ("," )));
545
+ supportingFilesToGenerate = new HashSet <>(Arrays .asList (supportingFiles .split ("," )));
540
546
}
541
547
542
548
for (SupportingFile support : config .supportingFiles ()) {
@@ -658,11 +664,11 @@ private void generateSupportingFiles(List<File> files, Map<String, Object> bundl
658
664
659
665
private Map <String , Object > buildSupportFileBundle (List <Object > allOperations , List <Object > allModels ) {
660
666
661
- Map <String , Object > bundle = new HashMap <String , Object >();
667
+ Map <String , Object > bundle = new HashMap <>();
662
668
bundle .putAll (config .additionalProperties ());
663
669
bundle .put ("apiPackage" , config .apiPackage ());
664
670
665
- Map <String , Object > apis = new HashMap <String , Object >();
671
+ Map <String , Object > apis = new HashMap <>();
666
672
apis .put ("apis" , allOperations );
667
673
668
674
URL url = URLPathUtil .getServerURL (openAPI );
@@ -717,12 +723,12 @@ public List<File> generate() {
717
723
configureGeneratorProperties ();
718
724
configureSwaggerInfo ();
719
725
720
- List <File > files = new ArrayList <File >();
726
+ List <File > files = new ArrayList <>();
721
727
// models
722
- List <Object > allModels = new ArrayList <Object >();
728
+ List <Object > allModels = new ArrayList <>();
723
729
generateModels (files , allModels );
724
730
// apis
725
- List <Object > allOperations = new ArrayList <Object >();
731
+ List <Object > allOperations = new ArrayList <>();
726
732
generateApis (files , allOperations , allModels );
727
733
728
734
// supporting files
@@ -750,10 +756,10 @@ private static void processMimeTypes(List<String> mimeTypeList, Map<String, Obje
750
756
if (mimeTypeList == null || mimeTypeList .isEmpty ()){
751
757
return ;
752
758
}
753
- List <Map <String , String >> c = new ArrayList <Map < String , String > >();
759
+ List <Map <String , String >> c = new ArrayList <>();
754
760
int count = 0 ;
755
761
for (String key : mimeTypeList ) {
756
- Map <String , String > mediaType = new HashMap <String , String >();
762
+ Map <String , String > mediaType = new HashMap <>();
757
763
mediaType .put ("mediaType" , key );
758
764
count += 1 ;
759
765
if (count < mimeTypeList .size ()) {
@@ -770,7 +776,7 @@ private static void processMimeTypes(List<String> mimeTypeList, Map<String, Obje
770
776
}
771
777
772
778
public Map <String , List <CodegenOperation >> processPaths (Paths paths ) {
773
- Map <String , List <CodegenOperation >> ops = new TreeMap <String , List < CodegenOperation > >();
779
+ Map <String , List <CodegenOperation >> ops = new TreeMap <>();
774
780
for (String resourcePath : paths .keySet ()) {
775
781
PathItem path = paths .get (resourcePath );
776
782
processOperation (resourcePath , "get" , path .getGet (), ops , path );
@@ -827,7 +833,7 @@ private void processOperation(String resourcePath, String httpMethod, Operation
827
833
per the swagger 2.0 spec "A unique parameter is defined by a combination of a name and location"
828
834
i'm assuming "location" == "in"
829
835
*/
830
- Set <String > operationParameters = new HashSet <String >();
836
+ Set <String > operationParameters = new HashSet <>();
831
837
if (operation .getParameters () != null ) {
832
838
for (Parameter parameter : operation .getParameters ()) {
833
839
operationParameters .add (generateParameterId (parameter ));
@@ -886,13 +892,13 @@ private static String generateParameterId(Parameter parameter) {
886
892
887
893
888
894
private Map <String , Object > processOperations (CodegenConfig config , String tag , List <CodegenOperation > ops , List <Object > allModels ) {
889
- Map <String , Object > operations = new HashMap <String , Object >();
890
- Map <String , Object > objs = new HashMap <String , Object >();
895
+ Map <String , Object > operations = new HashMap <>();
896
+ Map <String , Object > objs = new HashMap <>();
891
897
objs .put ("classname" , config .toApiName (tag ));
892
898
objs .put ("pathPrefix" , config .toApiVarName (tag ));
893
899
894
900
// check for operationId uniqueness
895
- Set <String > opIds = new HashSet <String >();
901
+ Set <String > opIds = new HashSet <>();
896
902
int counter = 0 ;
897
903
for (CodegenOperation op : ops ) {
898
904
String opId = op .nickname ;
@@ -908,14 +914,14 @@ private Map<String, Object> processOperations(CodegenConfig config, String tag,
908
914
operations .put ("package" , config .apiPackage ());
909
915
910
916
911
- Set <String > allImports = new TreeSet <String >();
917
+ Set <String > allImports = new TreeSet <>();
912
918
for (CodegenOperation op : ops ) {
913
919
allImports .addAll (op .imports );
914
920
}
915
921
916
- List <Map <String , String >> imports = new ArrayList <Map < String , String > >();
922
+ List <Map <String , String >> imports = new ArrayList <>();
917
923
for (String nextImport : allImports ) {
918
- Map <String , String > im = new LinkedHashMap <String , String >();
924
+ Map <String , String > im = new LinkedHashMap <>();
919
925
String mapping = config .importMapping ().get (nextImport );
920
926
if (mapping == null ) {
921
927
mapping = config .toModelImport (nextImport );
@@ -962,7 +968,7 @@ private Map<String, Object> processModels(CodegenConfig config, Map<String, Sche
962
968
allImports .addAll (cm .imports );
963
969
}
964
970
objs .put ("models" , models );
965
- Set <String > importSet = new TreeSet <String >();
971
+ Set <String > importSet = new TreeSet <>();
966
972
for (String nextImport : allImports ) {
967
973
String mapping = config .importMapping ().get (nextImport );
968
974
if (mapping == null ) {
@@ -977,9 +983,9 @@ private Map<String, Object> processModels(CodegenConfig config, Map<String, Sche
977
983
importSet .add (mapping );
978
984
}
979
985
}
980
- List <Map <String , String >> imports = new ArrayList <Map < String , String > >();
986
+ List <Map <String , String >> imports = new ArrayList <>();
981
987
for (String s : importSet ) {
982
- Map <String , String > item = new HashMap <String , String >();
988
+ Map <String , String > item = new HashMap <>();
983
989
item .put ("import" , s );
984
990
imports .add (item );
985
991
}
@@ -1020,4 +1026,19 @@ private boolean isJavaCodegen(String name) {
1020
1026
return name .equalsIgnoreCase ("java" )
1021
1027
|| name .equalsIgnoreCase ("inflector" );
1022
1028
}
1029
+
1030
+ private Boolean getCustomOptionBooleanValue (String option ) {
1031
+ List <CodegenArgument > languageArguments = config .getLanguageArguments ();
1032
+ if (languageArguments == null || languageArguments .isEmpty ()) {
1033
+ return null ;
1034
+ }
1035
+ Optional <CodegenArgument > optionalCodegenArgument = languageArguments .stream ()
1036
+ .filter (argument -> option .equalsIgnoreCase (argument .getOption ()))
1037
+ .findFirst ();
1038
+
1039
+ if (!optionalCodegenArgument .isPresent ()) {
1040
+ return null ;
1041
+ }
1042
+ return Boolean .valueOf (optionalCodegenArgument .get ().getValue ());
1043
+ }
1023
1044
}
0 commit comments