Skip to content

Commit 84ac06a

Browse files
authored
Fixes test folder cleaning in python-exp only (#12825)
* Adds test folder file removal in python-exp only * Samples regnerated * Removes unused code, tweaks unit test sample to test PR change * Reverts spec change * FIxes javadoc
1 parent 574f6f3 commit 84ac06a

File tree

2 files changed

+30
-15
lines changed

2 files changed

+30
-15
lines changed

bin/generate-samples.sh

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,7 @@ if [[ ${#files[@]} -eq 1 && "${files[0]}" != *'*'* ]]; then
5353
java ${JAVA_OPTS} -jar "$executable" generate -c ${files[0]} ${args[@]}
5454
else
5555
echo "Please press CTRL+C to stop or the script will continue in 5 seconds."
56-
5756
sleep 5
58-
# delete the 3_0_3 python-experimental tests because they are autogenerated our tooling needs to see differences
59-
rm -rf "${root}/samples/openapi3/client/3_0_3_unit_test/python-experimental/test"
60-
6157
if [ ${#files[@]} -eq 0 ]; then
6258
files=("${root}"/bin/configs/*.yaml)
6359
fi

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonExperimentalClientCodegen.java

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import io.swagger.v3.oas.models.servers.Server;
2626
import io.swagger.v3.oas.models.tags.Tag;
2727

28+
import org.apache.commons.io.FileUtils;
2829
import org.openapitools.codegen.api.TemplatePathLocator;
2930
import org.openapitools.codegen.ignore.CodegenIgnoreProcessor;
3031
import org.openapitools.codegen.model.ModelMap;
@@ -740,6 +741,7 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
740741
* this means that the generated client does not use these models
741742
* because they are not used we do not write them
742743
* - 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
743745
*
744746
* @param objs a map going from the model name to a object hoding the model info
745747
* @return the updated objs
@@ -748,26 +750,43 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
748750
public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs) {
749751
super.postProcessAllModels(objs);
750752

753+
boolean anyModelContainsTestCases = false;
751754
Map<String, Schema> allDefinitions = ModelUtils.getSchemas(this.openAPI);
752755
for (String schemaName : allDefinitions.keySet()) {
753756
Schema refSchema = new Schema().$ref("#/components/schemas/" + schemaName);
754757
Schema unaliasedSchema = unaliasSchema(refSchema, schemaMapping);
755758
String modelName = toModelName(schemaName);
756759
if (unaliasedSchema.get$ref() == null) {
757760
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;
769771
}
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());
770788
}
789+
771790
}
772791

773792
return objs;

0 commit comments

Comments
 (0)