Skip to content

Commit 75eb184

Browse files
authored
[OpenAPI Generator] When deleting files, only consider specific packages (#529)
1 parent 9a17bc9 commit 75eb184

File tree

5 files changed

+43
-27
lines changed

5 files changed

+43
-27
lines changed

datamodel/openapi/openapi-generator-maven-plugin/src/main/java/com/sap/cloud/sdk/datamodel/openapi/generator/DataModelGeneratorMojo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public class DataModelGeneratorMojo extends AbstractMojo
107107
private Boolean sapCopyrightHeader;
108108

109109
/**
110-
* Defines whether to delete the output directory prior to the generation.
110+
* Defines whether to delete the generated files from output directory prior to the generation.
111111
*/
112112
@Parameter( property = "openapi.generate.deleteOutputDirectory", defaultValue = "false" )
113113
private boolean deleteOutputDirectory;

datamodel/openapi/openapi-generator/src/main/java/com/sap/cloud/sdk/datamodel/openapi/generator/DataModelGenerator.java

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111
import java.nio.file.Files;
1212
import java.nio.file.Paths;
1313
import java.util.List;
14+
import java.util.function.Predicate;
1415

1516
import javax.annotation.Nonnull;
1617

1718
import org.apache.commons.io.FileUtils;
19+
import org.apache.commons.io.function.IOConsumer;
1820
import org.openapitools.codegen.ClientOptInput;
1921
import org.openapitools.codegen.DefaultGenerator;
2022

@@ -107,15 +109,20 @@ private Try<GenerationResult> invokeCodeGeneration( @Nonnull final GenerationCon
107109
});
108110
}
109111

110-
private void cleanOutputDirectoryIfRequested( final GenerationConfiguration generationConfiguration )
112+
private void cleanOutputDirectoryIfRequested( final GenerationConfiguration configuration )
111113
throws IOException
112114
{
113-
final File outputDirectory = FileUtils.getFile(generationConfiguration.getOutputDirectory());
114-
if( generationConfiguration.deleteOutputDirectory()
115-
&& outputDirectory.exists()
116-
&& outputDirectory.isDirectory() ) {
117-
log.info("Deleting output directory \"{}\".", outputDirectory.getAbsolutePath());
118-
FileUtils.cleanDirectory(outputDirectory);
115+
final File outputDirectory = FileUtils.getFile(configuration.getOutputDirectory());
116+
if( configuration.deleteOutputDirectory() && outputDirectory.exists() && outputDirectory.isDirectory() ) {
117+
log.info("Cleaning generated folders in output directory \"{}\".", outputDirectory.getAbsolutePath());
118+
119+
for( final var pckg : List.of(configuration.getModelPackage(), configuration.getApiPackage()) ) {
120+
final var file = outputDirectory.toPath().resolve(pckg.replace(".", File.separator)).toFile();
121+
if( file.exists() && file.isDirectory() ) {
122+
log.info("Deleting files from directory \"{}\".", file);
123+
IOConsumer.forAll(FileUtils::forceDelete, file);
124+
}
125+
}
119126
}
120127
}
121128

@@ -149,17 +156,16 @@ private void assertRequiredFieldsAreFilled( final GenerationConfiguration config
149156
if( configuration.getInputSpec() == null || configuration.getInputSpec().isEmpty() ) {
150157
throw new IllegalArgumentException("Input file path is null or empty.");
151158
}
152-
153-
if( configuration.getApiPackage() == null || configuration.getApiPackage().isEmpty() ) {
154-
throw new IllegalArgumentException("API package is null or empty.");
159+
if( configuration.getOutputDirectory() == null || configuration.getOutputDirectory().isEmpty() ) {
160+
throw new IllegalArgumentException("Output directory is null or empty.");
155161
}
156162

157-
if( configuration.getModelPackage() == null || configuration.getModelPackage().isEmpty() ) {
158-
throw new IllegalArgumentException("Model package is null or empty.");
163+
final Predicate<String> goodPackage = p -> !p.isEmpty() && !p.startsWith(".") && !p.contains(File.separator);
164+
if( configuration.getApiPackage() == null || !goodPackage.test(configuration.getApiPackage()) ) {
165+
throw new IllegalArgumentException("API package is null or empty or invalid.");
159166
}
160-
161-
if( configuration.getOutputDirectory() == null || configuration.getOutputDirectory().isEmpty() ) {
162-
throw new IllegalArgumentException("Output directory is null or empty.");
167+
if( configuration.getModelPackage() == null || !goodPackage.test(configuration.getModelPackage()) ) {
168+
throw new IllegalArgumentException("Model package is null or empty or invalid.");
163169
}
164170
}
165171

datamodel/openapi/openapi-generator/src/main/java/com/sap/cloud/sdk/datamodel/openapi/generator/model/GenerationConfiguration.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ public boolean useSapCopyrightHeader()
6666
}
6767

6868
/**
69-
* Indicates whether to delete the output directory prior to the generation.
69+
* Indicates whether to delete the generated files from output directory prior to the generation.
7070
*
71-
* @return {@code true} if the output directory should be deleted before generating the OpenAPI client,
72-
* {@code false} otherwise.
71+
* @return {@code true} if the generated files should be deleted from output directory before generating the OpenAPI
72+
* client, {@code false} otherwise.
7373
*/
7474
public boolean deleteOutputDirectory()
7575
{

datamodel/openapi/openapi-generator/src/test/java/com/sap/cloud/sdk/datamodel/openapi/generator/DataModelGeneratorUnitTest.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -306,11 +306,16 @@ void testExceptionIfTemplatesUnavailable()
306306
@SneakyThrows
307307
void testCleanOutputDirectory()
308308
{
309-
final File existingFile =
310-
Files
311-
.createTempFile(outputDirectory, "dummyFile", DataModelGeneratorUnitTest.class.getSimpleName())
312-
.toFile();
313-
assertThat(existingFile.exists()).isTrue();
309+
final Path modelDir = Files.createDirectory(outputDirectory.resolve("model"));
310+
final Path apiDir = Files.createDirectory(outputDirectory.resolve("api"));
311+
312+
final File modelFile = Files.createFile(modelDir.resolve("mymodel.java")).toFile();
313+
final File apiFile = Files.createFile(apiDir.resolve("myapi.java")).toFile();
314+
final File ignoreFile = Files.createFile(outputDirectory.resolve(".ignore")).toFile();
315+
316+
assertThat(modelFile.exists()).isTrue();
317+
assertThat(apiFile.exists()).isTrue();
318+
assertThat(ignoreFile.exists()).isTrue();
314319

315320
final GenerationConfiguration configuration =
316321
GenerationConfiguration
@@ -326,8 +331,11 @@ void testCleanOutputDirectory()
326331

327332
assertThat(generationResult.isSuccess()).isTrue();
328333

329-
// assert that the file was deleted
330-
assertThat(existingFile.exists()).isFalse();
334+
// assert that the java files are deleted
335+
assertThat(modelFile.exists()).isFalse();
336+
assertThat(apiFile.exists()).isFalse();
337+
// assert that the ignore file remains
338+
assertThat(ignoreFile.exists()).isTrue();
331339
}
332340

333341
@Test

release_notes.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616

1717
### 📈 Improvements
1818

19-
-
19+
- \[OpenAPI Generator\] Setting the Maven plugin configuration property `openapi.generate.deleteOutputDirectory` to `true` will no longer result in deletion of all files from the `outputDirectory` prior to generation.
20+
Instead, only the `apiPackage`- and `apiPackage`-related directories will be cleaned.
21+
This reduces the risk of deleting files unexpectedly and allows for reusing the same `outputDirectory` for multiple generator plugin invocations.
2022

2123
### 🐛 Fixed Issues
2224

0 commit comments

Comments
 (0)