feat: add support for minimal update option in maven plugin (#21872)

This commit is contained in:
Julius 2025-09-03 19:00:00 +02:00 committed by GitHub
parent fde017150e
commit 2ebda09b9f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 119 additions and 1 deletions

View File

@ -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
*/
@ -698,6 +704,10 @@ public class CodeGenMojo extends AbstractMojo {
configurator.setGenerateAliasAsModel(generateAliasAsModel);
}
if (minimalUpdate != null) {
configurator.setEnableMinimalUpdate(minimalUpdate);
}
if (isNotEmpty(generatorName)) {
configurator.setGeneratorName(generatorName);
} else {

View File

@ -38,6 +38,7 @@ import java.nio.file.Files;
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;
@ -88,6 +89,17 @@ public class CodeGenMojoTest extends BaseTestCase {
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();
@ -136,6 +148,50 @@ public class CodeGenMojoTest extends BaseTestCase {
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.
@ -242,7 +298,7 @@ public class CodeGenMojoTest extends BaseTestCase {
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

View File

@ -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>