Skip to content

Commit

Permalink
[OpenAPI Generator] When deleting files, only consider specific packa…
Browse files Browse the repository at this point in the history
…ges (#529)
  • Loading branch information
newtork authored Aug 9, 2024
1 parent 9a17bc9 commit 75eb184
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public class DataModelGeneratorMojo extends AbstractMojo
private Boolean sapCopyrightHeader;

/**
* Defines whether to delete the output directory prior to the generation.
* Defines whether to delete the generated files from output directory prior to the generation.
*/
@Parameter( property = "openapi.generate.deleteOutputDirectory", defaultValue = "false" )
private boolean deleteOutputDirectory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.function.Predicate;

import javax.annotation.Nonnull;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.function.IOConsumer;
import org.openapitools.codegen.ClientOptInput;
import org.openapitools.codegen.DefaultGenerator;

Expand Down Expand Up @@ -107,15 +109,20 @@ private Try<GenerationResult> invokeCodeGeneration( @Nonnull final GenerationCon
});
}

private void cleanOutputDirectoryIfRequested( final GenerationConfiguration generationConfiguration )
private void cleanOutputDirectoryIfRequested( final GenerationConfiguration configuration )
throws IOException
{
final File outputDirectory = FileUtils.getFile(generationConfiguration.getOutputDirectory());
if( generationConfiguration.deleteOutputDirectory()
&& outputDirectory.exists()
&& outputDirectory.isDirectory() ) {
log.info("Deleting output directory \"{}\".", outputDirectory.getAbsolutePath());
FileUtils.cleanDirectory(outputDirectory);
final File outputDirectory = FileUtils.getFile(configuration.getOutputDirectory());
if( configuration.deleteOutputDirectory() && outputDirectory.exists() && outputDirectory.isDirectory() ) {
log.info("Cleaning generated folders in output directory \"{}\".", outputDirectory.getAbsolutePath());

for( final var pckg : List.of(configuration.getModelPackage(), configuration.getApiPackage()) ) {
final var file = outputDirectory.toPath().resolve(pckg.replace(".", File.separator)).toFile();
if( file.exists() && file.isDirectory() ) {
log.info("Deleting files from directory \"{}\".", file);
IOConsumer.forAll(FileUtils::forceDelete, file);
}
}
}
}

Expand Down Expand Up @@ -149,17 +156,16 @@ private void assertRequiredFieldsAreFilled( final GenerationConfiguration config
if( configuration.getInputSpec() == null || configuration.getInputSpec().isEmpty() ) {
throw new IllegalArgumentException("Input file path is null or empty.");
}

if( configuration.getApiPackage() == null || configuration.getApiPackage().isEmpty() ) {
throw new IllegalArgumentException("API package is null or empty.");
if( configuration.getOutputDirectory() == null || configuration.getOutputDirectory().isEmpty() ) {
throw new IllegalArgumentException("Output directory is null or empty.");
}

if( configuration.getModelPackage() == null || configuration.getModelPackage().isEmpty() ) {
throw new IllegalArgumentException("Model package is null or empty.");
final Predicate<String> goodPackage = p -> !p.isEmpty() && !p.startsWith(".") && !p.contains(File.separator);
if( configuration.getApiPackage() == null || !goodPackage.test(configuration.getApiPackage()) ) {
throw new IllegalArgumentException("API package is null or empty or invalid.");
}

if( configuration.getOutputDirectory() == null || configuration.getOutputDirectory().isEmpty() ) {
throw new IllegalArgumentException("Output directory is null or empty.");
if( configuration.getModelPackage() == null || !goodPackage.test(configuration.getModelPackage()) ) {
throw new IllegalArgumentException("Model package is null or empty or invalid.");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ public boolean useSapCopyrightHeader()
}

/**
* Indicates whether to delete the output directory prior to the generation.
* Indicates whether to delete the generated files from output directory prior to the generation.
*
* @return {@code true} if the output directory should be deleted before generating the OpenAPI client,
* {@code false} otherwise.
* @return {@code true} if the generated files should be deleted from output directory before generating the OpenAPI
* client, {@code false} otherwise.
*/
public boolean deleteOutputDirectory()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,11 +306,16 @@ void testExceptionIfTemplatesUnavailable()
@SneakyThrows
void testCleanOutputDirectory()
{
final File existingFile =
Files
.createTempFile(outputDirectory, "dummyFile", DataModelGeneratorUnitTest.class.getSimpleName())
.toFile();
assertThat(existingFile.exists()).isTrue();
final Path modelDir = Files.createDirectory(outputDirectory.resolve("model"));
final Path apiDir = Files.createDirectory(outputDirectory.resolve("api"));

final File modelFile = Files.createFile(modelDir.resolve("mymodel.java")).toFile();
final File apiFile = Files.createFile(apiDir.resolve("myapi.java")).toFile();
final File ignoreFile = Files.createFile(outputDirectory.resolve(".ignore")).toFile();

assertThat(modelFile.exists()).isTrue();
assertThat(apiFile.exists()).isTrue();
assertThat(ignoreFile.exists()).isTrue();

final GenerationConfiguration configuration =
GenerationConfiguration
Expand All @@ -326,8 +331,11 @@ void testCleanOutputDirectory()

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

// assert that the file was deleted
assertThat(existingFile.exists()).isFalse();
// assert that the java files are deleted
assertThat(modelFile.exists()).isFalse();
assertThat(apiFile.exists()).isFalse();
// assert that the ignore file remains
assertThat(ignoreFile.exists()).isTrue();
}

@Test
Expand Down
4 changes: 3 additions & 1 deletion release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

### 📈 Improvements

-
- \[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.
Instead, only the `apiPackage`- and `apiPackage`-related directories will be cleaned.
This reduces the risk of deleting files unexpectedly and allows for reusing the same `outputDirectory` for multiple generator plugin invocations.

### 🐛 Fixed Issues

Expand Down

0 comments on commit 75eb184

Please sign in to comment.