Skip to content

Commit 2ebda09

Browse files
authored
feat: add support for minimal update option in maven plugin (#21872)
1 parent fde0171 commit 2ebda09

File tree

3 files changed

+119
-1
lines changed

3 files changed

+119
-1
lines changed

modules/openapi-generator-maven-plugin/src/main/java/org/openapitools/codegen/plugin/CodeGenMojo.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,12 @@ public class CodeGenMojo extends AbstractMojo {
321321
@Parameter(name = "generateAliasAsModel", property = "openapi.generator.maven.plugin.generateAliasAsModel")
322322
private Boolean generateAliasAsModel;
323323

324+
/**
325+
* Only write output files that have changed.
326+
*/
327+
@Parameter(name = "minimalUpdate", property = "openapi.generator.maven.plugin.minimalUpdate")
328+
private Boolean minimalUpdate;
329+
324330
/**
325331
* A map of language-specific parameters as passed with the -c option to the command line
326332
*/
@@ -698,6 +704,10 @@ public void execute() throws MojoExecutionException {
698704
configurator.setGenerateAliasAsModel(generateAliasAsModel);
699705
}
700706

707+
if (minimalUpdate != null) {
708+
configurator.setEnableMinimalUpdate(minimalUpdate);
709+
}
710+
701711
if (isNotEmpty(generatorName)) {
702712
configurator.setGeneratorName(generatorName);
703713
} else {

modules/openapi-generator-maven-plugin/src/test/java/org/openapitools/codegen/plugin/CodeGenMojoTest.java

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import java.nio.file.Path;
3939
import java.nio.file.StandardOpenOption;
4040
import java.util.Comparator;
41+
import java.util.HashMap;
4142
import java.util.List;
4243
import java.util.Map;
4344
import java.util.stream.Collectors;
@@ -88,6 +89,17 @@ private void testCommonConfiguration(String profile) throws Exception {
8889
assertEquals("joda", configOptions.get("dateLibrary"));
8990
}
9091

92+
public void testMinimalUpdateConfiguration() throws Exception {
93+
// GIVEN
94+
CodeGenMojo mojo = loadMojo(newTempFolder(), "src/test/resources/minimal-update", null);
95+
96+
// WHEN
97+
mojo.execute();
98+
99+
// THEN
100+
assertEquals(Boolean.TRUE, getVariableValueFromObject(mojo, "minimalUpdate"));
101+
}
102+
91103
public void testHashGenerationFileContainsExecutionId() throws Exception {
92104
// GIVEN
93105
final Path tempDir = newTempFolder();
@@ -136,6 +148,50 @@ public void testSkipRegenerationForClasspathSpecFileNoChange() throws Exception
136148
assertFalse("src directory should not have been regenerated", Files.exists(generatedDir.resolve("src")));
137149
}
138150

151+
public void testMinimalUpdate() throws Exception {
152+
//GIVEN
153+
/* Set up the mojo */
154+
final Path tempDir = newTempFolder();
155+
final CodeGenMojo mojo = loadMojo(tempDir, "src/test/resources/minimal-update", null, "executionId");
156+
157+
/* Perform an initial generation */
158+
mojo.execute();
159+
160+
/* Collect last modified times of generated files */
161+
final Path generatedDir = tempDir.resolve("target/generated-sources/minimal-update");
162+
assertTrue("Generated directory should exist", Files.exists(generatedDir));
163+
164+
Map<Path, Long> lastModifiedTimes = new HashMap<>();
165+
try (Stream<Path> files = Files.walk(generatedDir)) {
166+
files
167+
.filter(Files::isRegularFile)
168+
.filter(path -> !path.getFileName().toString().endsWith(".sha256"))
169+
.filter(path -> !path.getFileName().toString().equals("FILES"))
170+
.forEach(file -> {
171+
try {
172+
lastModifiedTimes.put(file, Files.getLastModifiedTime(file).toMillis());
173+
} catch (IOException e) {
174+
throw new RuntimeException(e);
175+
}
176+
});
177+
}
178+
assertTrue("Should have recorded last modified times for more than 3 files", lastModifiedTimes.size() > 3);
179+
180+
// WHEN
181+
/* Execute the mojo again */
182+
mojo.execute();
183+
184+
// THEN
185+
/* Verify that file modification times haven't changed (files weren't touched) */
186+
for (Map.Entry<Path, Long> entry : lastModifiedTimes.entrySet()) {
187+
Path file = entry.getKey();
188+
Long originalTime = entry.getValue();
189+
Long currentTime = Files.getLastModifiedTime(file).toMillis();
190+
assertEquals("File " + file + " should not have been modified (minimal update should skip unchanged files)",
191+
originalTime, currentTime);
192+
}
193+
}
194+
139195
/**
140196
* For a Pom file which refers to an input file which will be on the classpath, as opposed to a file path,
141197
* test that the generated source is regenerated when the hash has changed.
@@ -242,7 +298,7 @@ public void test_skipIfSpecIsUnchanged_recognizesUpdatesInExternalReferencedFile
242298
final Path generatedDir = tempDir.resolve("target/generated-sources/issue-16489");
243299
final Path hashFile = generatedDir.resolve(".openapi-generator/petstore.yaml-default.sha256");
244300
final CodeGenMojo mojo = loadMojo(tempDir, "src/test/resources/issue-16489", null);
245-
mojo.execute(); // Perform an initial generation
301+
mojo.execute(); // Perform an initial generation
246302
var currentHash = Files.readString(hashFile); // read hash
247303
FileUtils.deleteDirectory(generatedDir.resolve("src").toFile()); // Remove the generated source
248304
Files.writeString( // change schema definition in external file
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<!--
2+
~ Copyright 2020, 2021 OpenAPI-Generator Contributors (https://openapi-generator.tech)
3+
~
4+
~ Licensed under the Apache License, Version 2.0 (the "License");
5+
~ you may not use this file except in compliance with the License.
6+
~ You may obtain a copy of the License at
7+
~
8+
~ http://www.apache.org/licenses/LICENSE-2.0
9+
~
10+
~ Unless required by applicable law or agreed to in writing, software
11+
~ distributed under the License is distributed on an "AS IS" BASIS,
12+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
~ See the License for the specific language governing permissions and
14+
~ limitations under the License.
15+
-->
16+
17+
<project>
18+
<modelVersion>4.0.0</modelVersion>
19+
<groupId>minimal.update.test</groupId>
20+
<artifactId>minimal-update-test</artifactId>
21+
<packaging>jar</packaging>
22+
<version>1.0.0-SNAPSHOT</version>
23+
<name>OpenAPI Generator Minimal Update Test</name>
24+
<url>https://openapi-generator.tech/</url>
25+
<build>
26+
<finalName>minimal-update-test</finalName>
27+
<plugins>
28+
<plugin>
29+
<groupId>org.openapitools</groupId>
30+
<artifactId>openapi-generator-maven-plugin</artifactId>
31+
<configuration>
32+
<inputSpec>petstore-on-classpath.yaml</inputSpec>
33+
<generatorName>spring</generatorName>
34+
<output>${basedir}/target/generated-sources/minimal-update</output>
35+
<minimalUpdate>true</minimalUpdate>
36+
<configOptions>
37+
<hideGenerationTimestamp>true</hideGenerationTimestamp>
38+
</configOptions>
39+
</configuration>
40+
<executions>
41+
<execution>
42+
<id>executionId</id>
43+
<phase>generate-sources</phase>
44+
<goals>
45+
<goal>generate</goal>
46+
</goals>
47+
</execution>
48+
</executions>
49+
</plugin>
50+
</plugins>
51+
</build>
52+
</project>

0 commit comments

Comments
 (0)