Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,12 @@ public class CodeGenMojo extends AbstractMojo {
@Parameter(name = "generateAliasAsModel", property = "openapi.generator.maven.plugin.generateAliasAsModel")
private Boolean generateAliasAsModel;

/**
* Only write output files that have changed.
*/
@Parameter(name = "minimalUpdate", property = "openapi.generator.maven.plugin.minimalUpdate")
private Boolean minimalUpdate;

/**
* A map of language-specific parameters as passed with the -c option to the command line
*/
Expand Down Expand Up @@ -698,6 +704,10 @@ public void execute() throws MojoExecutionException {
configurator.setGenerateAliasAsModel(generateAliasAsModel);
}

if (minimalUpdate != null) {
configurator.setEnableMinimalUpdate(minimalUpdate);
}

if (isNotEmpty(generatorName)) {
configurator.setGeneratorName(generatorName);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -88,6 +89,17 @@ private void testCommonConfiguration(String profile) throws Exception {
assertEquals("joda", configOptions.get("dateLibrary"));
}

public void testMinimalUpdateConfiguration() throws Exception {
// GIVEN
CodeGenMojo mojo = loadMojo(newTempFolder(), "src/test/resources/minimal-update", null);

// WHEN
mojo.execute();

// THEN
assertEquals(Boolean.TRUE, getVariableValueFromObject(mojo, "minimalUpdate"));
}

public void testHashGenerationFileContainsExecutionId() throws Exception {
// GIVEN
final Path tempDir = newTempFolder();
Expand Down Expand Up @@ -136,6 +148,50 @@ public void testSkipRegenerationForClasspathSpecFileNoChange() throws Exception
assertFalse("src directory should not have been regenerated", Files.exists(generatedDir.resolve("src")));
}

public void testMinimalUpdate() throws Exception {
//GIVEN
/* Set up the mojo */
final Path tempDir = newTempFolder();
final CodeGenMojo mojo = loadMojo(tempDir, "src/test/resources/minimal-update", null, "executionId");

/* Perform an initial generation */
mojo.execute();

/* Collect last modified times of generated files */
final Path generatedDir = tempDir.resolve("target/generated-sources/minimal-update");
assertTrue("Generated directory should exist", Files.exists(generatedDir));

Map<Path, Long> lastModifiedTimes = new HashMap<>();
try (Stream<Path> files = Files.walk(generatedDir)) {
files
.filter(Files::isRegularFile)
.filter(path -> !path.getFileName().toString().endsWith(".sha256"))
.filter(path -> !path.getFileName().toString().equals("FILES"))
.forEach(file -> {
try {
lastModifiedTimes.put(file, Files.getLastModifiedTime(file).toMillis());
} catch (IOException e) {
throw new RuntimeException(e);
}
});
}
assertTrue("Should have recorded last modified times for more than 3 files", lastModifiedTimes.size() > 3);

// WHEN
/* Execute the mojo again */
mojo.execute();

// THEN
/* Verify that file modification times haven't changed (files weren't touched) */
for (Map.Entry<Path, Long> entry : lastModifiedTimes.entrySet()) {
Path file = entry.getKey();
Long originalTime = entry.getValue();
Long currentTime = Files.getLastModifiedTime(file).toMillis();
assertEquals("File " + file + " should not have been modified (minimal update should skip unchanged files)",
originalTime, currentTime);
}
}

/**
* For a Pom file which refers to an input file which will be on the classpath, as opposed to a file path,
* test that the generated source is regenerated when the hash has changed.
Expand Down Expand Up @@ -242,7 +298,7 @@ public void test_skipIfSpecIsUnchanged_recognizesUpdatesInExternalReferencedFile
final Path generatedDir = tempDir.resolve("target/generated-sources/issue-16489");
final Path hashFile = generatedDir.resolve(".openapi-generator/petstore.yaml-default.sha256");
final CodeGenMojo mojo = loadMojo(tempDir, "src/test/resources/issue-16489", null);
mojo.execute(); // Perform an initial generation
mojo.execute(); // Perform an initial generation
var currentHash = Files.readString(hashFile); // read hash
FileUtils.deleteDirectory(generatedDir.resolve("src").toFile()); // Remove the generated source
Files.writeString( // change schema definition in external file
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<!--
~ Copyright 2020, 2021 OpenAPI-Generator Contributors (https://openapi-generator.tech)
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<project>
<modelVersion>4.0.0</modelVersion>
<groupId>minimal.update.test</groupId>
<artifactId>minimal-update-test</artifactId>
<packaging>jar</packaging>
<version>1.0.0-SNAPSHOT</version>
<name>OpenAPI Generator Minimal Update Test</name>
<url>https://openapi-generator.tech/</url>
<build>
<finalName>minimal-update-test</finalName>
<plugins>
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<configuration>
<inputSpec>petstore-on-classpath.yaml</inputSpec>
<generatorName>spring</generatorName>
<output>${basedir}/target/generated-sources/minimal-update</output>
<minimalUpdate>true</minimalUpdate>
<configOptions>
<hideGenerationTimestamp>true</hideGenerationTimestamp>
</configOptions>
</configuration>
<executions>
<execution>
<id>executionId</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Loading