25
25
import io .swagger .v3 .oas .models .servers .Server ;
26
26
import io .swagger .v3 .oas .models .tags .Tag ;
27
27
28
+ import org .apache .commons .io .FileUtils ;
28
29
import org .openapitools .codegen .api .TemplatePathLocator ;
29
30
import org .openapitools .codegen .ignore .CodegenIgnoreProcessor ;
30
31
import org .openapitools .codegen .model .ModelMap ;
@@ -740,6 +741,7 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
740
741
* this means that the generated client does not use these models
741
742
* because they are not used we do not write them
742
743
* - fix the model imports, go from model name to the full import string with toModelImport + globalImportFixer
744
+ * Also cleans the test folder if test cases exist and the testFolder is set because the tests are autogenerated
743
745
*
744
746
* @param objs a map going from the model name to a object hoding the model info
745
747
* @return the updated objs
@@ -748,26 +750,43 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
748
750
public Map <String , ModelsMap > postProcessAllModels (Map <String , ModelsMap > objs ) {
749
751
super .postProcessAllModels (objs );
750
752
753
+ boolean anyModelContainsTestCases = false ;
751
754
Map <String , Schema > allDefinitions = ModelUtils .getSchemas (this .openAPI );
752
755
for (String schemaName : allDefinitions .keySet ()) {
753
756
Schema refSchema = new Schema ().$ref ("#/components/schemas/" + schemaName );
754
757
Schema unaliasedSchema = unaliasSchema (refSchema , schemaMapping );
755
758
String modelName = toModelName (schemaName );
756
759
if (unaliasedSchema .get$ref () == null ) {
757
760
continue ;
758
- } else {
759
- ModelsMap objModel = objs .get (modelName );
760
- if (objModel != null ) { // to avoid form parameter's models that are not generated (skipFormModel=true)
761
- for (ModelMap model : objModel .getModels ()) {
762
- CodegenModel cm = model .getModel ();
763
- String [] importModelNames = cm .imports .toArray (new String [0 ]);
764
- cm .imports .clear ();
765
- for (String importModelName : importModelNames ) {
766
- cm .imports .add (toModelImport (importModelName ));
767
- }
768
- }
761
+ }
762
+ ModelsMap objModel = objs .get (modelName );
763
+ if (objModel == null ) {
764
+ // to avoid form parameter's models that are not generated (skipFormModel=true)
765
+ continue ;
766
+ }
767
+ for (ModelMap model : objModel .getModels ()) {
768
+ CodegenModel cm = model .getModel ();
769
+ if (cm .testCases != null && !cm .testCases .isEmpty ()) {
770
+ anyModelContainsTestCases = true ;
769
771
}
772
+ String [] importModelNames = cm .imports .toArray (new String [0 ]);
773
+ cm .imports .clear ();
774
+ for (String importModelName : importModelNames ) {
775
+ cm .imports .add (toModelImport (importModelName ));
776
+ }
777
+ }
778
+ }
779
+ boolean testFolderSet = testFolder != null ;
780
+ if (testFolderSet && anyModelContainsTestCases ) {
781
+ // delete the test folder because tests there will be autogenerated
782
+ String modelTestFolder = modelTestFileFolder ();
783
+ File testDirectory = new File (modelTestFolder );
784
+ try {
785
+ FileUtils .cleanDirectory (testDirectory );
786
+ } catch (IOException e ) {
787
+ LOGGER .info ("Unable to delete the test folder because of exception=" + e .toString ());
770
788
}
789
+
771
790
}
772
791
773
792
return objs ;
0 commit comments