From 5a28ee2ba5df6f6327e68afd458375090cab5abd Mon Sep 17 00:00:00 2001 From: jpfinne Date: Thu, 30 Oct 2025 09:30:06 +0100 Subject: [PATCH 1/5] configurationFile with generatorName skipIfConfigurationFileMissing --- .../codegen/plugin/CodeGenMojo.java | 58 ++++++++++++++++--- .../codegen/plugin/CodeGenMojoTest.java | 25 ++++++++ .../configuration-file-missing/pom.xml | 47 +++++++++++++++ .../resources/configuration-file/config.yaml | 3 + .../test/resources/configuration-file/pom.xml | 47 +++++++++++++++ 5 files changed, 172 insertions(+), 8 deletions(-) create mode 100644 modules/openapi-generator-maven-plugin/src/test/resources/configuration-file-missing/pom.xml create mode 100644 modules/openapi-generator-maven-plugin/src/test/resources/configuration-file/config.yaml create mode 100644 modules/openapi-generator-maven-plugin/src/test/resources/configuration-file/pom.xml diff --git a/modules/openapi-generator-maven-plugin/src/main/java/org/openapitools/codegen/plugin/CodeGenMojo.java b/modules/openapi-generator-maven-plugin/src/main/java/org/openapitools/codegen/plugin/CodeGenMojo.java index 443f5b52b482..7114b8f37488 100644 --- a/modules/openapi-generator-maven-plugin/src/main/java/org/openapitools/codegen/plugin/CodeGenMojo.java +++ b/modules/openapi-generator-maven-plugin/src/main/java/org/openapitools/codegen/plugin/CodeGenMojo.java @@ -51,6 +51,7 @@ import java.io.File; import java.io.IOException; +import java.lang.reflect.Field; import java.net.MalformedURLException; import java.net.URI; import java.net.URISyntaxException; @@ -195,6 +196,12 @@ public class CodeGenMojo extends AbstractMojo { @Parameter(name = "configurationFile", property = "openapi.generator.maven.plugin.configurationFile") private String configurationFile; + /** + * Skip the execution if the source file is older than the output folder. + */ + @Parameter(name = "skipIfConfigurationFileMissing", property = "codegen.skipIfConfigurationFileMissing", defaultValue = "false") + private Boolean skipIfConfigurationFileMissing; + /** * Specifies if the existing files should be overwritten during the generation. */ @@ -563,6 +570,25 @@ public class CodeGenMojo extends AbstractMojo { @Override public void execute() throws MojoExecutionException { + if (Boolean.TRUE.equals(skipIfConfigurationFileMissing) && isNotEmpty(configurationFile)) { + if (!new File(configurationFile).exists()) { + getLog().info("Code generation is skipped because configuration file [" + configurationFile + "] is missing"); + return; + } + } + + // attempt to read from config file + CodegenConfigurator configurator = CodegenConfigurator.fromFile(configurationFile); + + // if a config file wasn't specified, or we were unable to read it + if (configurator == null) { + configurator = new CodegenConfigurator(); + } else { + // retrieve mandatory fields from the configurationFile if not defined in the pom.xml + this.generatorName = fromConfigurator(configurator, "generatorName", String.class, generatorName); + this.inputSpec = fromConfigurator(configurator, "inputSpec", String.class, inputSpec); + } + if (StringUtils.isBlank(inputSpec) && StringUtils.isBlank(inputSpecRootDirectory)) { LOGGER.error("inputSpec or inputSpecRootDirectory must be specified"); throw new MojoExecutionException("inputSpec or inputSpecRootDirectory must be specified"); @@ -638,14 +664,6 @@ public void execute() throws MojoExecutionException { } } - // attempt to read from config file - CodegenConfigurator configurator = CodegenConfigurator.fromFile(configurationFile); - - // if a config file wasn't specified, or we were unable to read it - if (configurator == null) { - configurator = new CodegenConfigurator(); - } - configurator.setVerbose(verbose); if (skipOverwrite != null) { @@ -1032,6 +1050,30 @@ public void execute() throws MojoExecutionException { } } + /** + * Access private fields of the CodegenConfigurator class. + * + * @param configurator the CodegenConfigurator + * @param fieldName name of the field + * @param clazz type of the field + * @param defaultValue default if configuration.fieldName is null + * @return the value of configuration.fieldName if defaultValue is null + */ + private T fromConfigurator(CodegenConfigurator configurator, String fieldName, Class clazz, T defaultValue) { + if (defaultValue != null) { + // keep backward compatibilty, the value in the pom.xml has precedence over the value in the config file. + return defaultValue; + } + try { + Field field = CodegenConfigurator.class.getDeclaredField(fieldName); + field.setAccessible(true); + T value = (T)field.get(configurator); + return value == null? defaultValue : value; + } catch (Exception e) { + throw new RuntimeException("Failed to read " + fieldName + " from configuration file.", e); + } + } + /** * Calculate an SHA256 hash for the openapi specification. * If the specification is hosted on a remote resource it is downloaded first. diff --git a/modules/openapi-generator-maven-plugin/src/test/java/org/openapitools/codegen/plugin/CodeGenMojoTest.java b/modules/openapi-generator-maven-plugin/src/test/java/org/openapitools/codegen/plugin/CodeGenMojoTest.java index d08c88b8fac6..3899257d0e64 100644 --- a/modules/openapi-generator-maven-plugin/src/test/java/org/openapitools/codegen/plugin/CodeGenMojoTest.java +++ b/modules/openapi-generator-maven-plugin/src/test/java/org/openapitools/codegen/plugin/CodeGenMojoTest.java @@ -315,6 +315,31 @@ public void test_skipIfSpecIsUnchanged_recognizesUpdatesInExternalReferencedFile assertTrue("Src directory should have been regenerated", Files.exists(generatedDir.resolve("src"))); } + public void testConfigurationFile() throws Exception { + // GIVEN + CodeGenMojo mojo = loadMojo(newTempFolder(), "src/test/resources/configuration-file", null); + + // WHEN + mojo.execute(); + + // THEN + assertEquals("spring", getVariableValueFromObject(mojo, "generatorName")); + } + + public void testSkipConfigurationFileIfMissing() throws Exception { + // GIVEN + final Path tempDir = newTempFolder(); + final Path generatedDir = tempDir.resolve("target/generated-sources/configuration-file-missing"); + CodeGenMojo mojo = loadMojo(tempDir, "src/test/resources/configuration-file-missing", null); + + // WHEN + mojo.execute(); + + // THEN + assertFalse("Src directory should not be present", Files.exists(generatedDir.resolve("src"))); + } + + protected CodeGenMojo loadMojo(Path temporaryFolder, String projectRoot, String profile) throws Exception { return loadMojo(temporaryFolder, projectRoot, profile, "default"); } diff --git a/modules/openapi-generator-maven-plugin/src/test/resources/configuration-file-missing/pom.xml b/modules/openapi-generator-maven-plugin/src/test/resources/configuration-file-missing/pom.xml new file mode 100644 index 000000000000..e8d44fc9a83b --- /dev/null +++ b/modules/openapi-generator-maven-plugin/src/test/resources/configuration-file-missing/pom.xml @@ -0,0 +1,47 @@ + + + + 4.0.0 + minimal.update.test + minimal-update-test + jar + 1.0.0-SNAPSHOT + OpenAPI Generator Minimal Update Test + https://openapi-generator.tech/ + + minimal-update-test + + + org.openapitools + openapi-generator-maven-plugin + + ${basedir}/config.yaml + true + + + + executionId + generate-sources + + generate + + + + + + + diff --git a/modules/openapi-generator-maven-plugin/src/test/resources/configuration-file/config.yaml b/modules/openapi-generator-maven-plugin/src/test/resources/configuration-file/config.yaml new file mode 100644 index 000000000000..517c0eaad107 --- /dev/null +++ b/modules/openapi-generator-maven-plugin/src/test/resources/configuration-file/config.yaml @@ -0,0 +1,3 @@ +inputSpec: petstore-on-classpath.yaml +generatorName: spring +output: ./target/generated-sources/minimal-update< diff --git a/modules/openapi-generator-maven-plugin/src/test/resources/configuration-file/pom.xml b/modules/openapi-generator-maven-plugin/src/test/resources/configuration-file/pom.xml new file mode 100644 index 000000000000..e8d44fc9a83b --- /dev/null +++ b/modules/openapi-generator-maven-plugin/src/test/resources/configuration-file/pom.xml @@ -0,0 +1,47 @@ + + + + 4.0.0 + minimal.update.test + minimal-update-test + jar + 1.0.0-SNAPSHOT + OpenAPI Generator Minimal Update Test + https://openapi-generator.tech/ + + minimal-update-test + + + org.openapitools + openapi-generator-maven-plugin + + ${basedir}/config.yaml + true + + + + executionId + generate-sources + + generate + + + + + + + From ba9d8132c03720777744dba5463257d6a56629d5 Mon Sep 17 00:00:00 2001 From: jpfinne Date: Thu, 30 Oct 2025 10:02:22 +0100 Subject: [PATCH 2/5] Add getters for simplicity --- .../codegen/plugin/CodeGenMojo.java | 23 +++++-------------- .../codegen/config/CodegenConfigurator.java | 8 +++++++ 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/modules/openapi-generator-maven-plugin/src/main/java/org/openapitools/codegen/plugin/CodeGenMojo.java b/modules/openapi-generator-maven-plugin/src/main/java/org/openapitools/codegen/plugin/CodeGenMojo.java index 7114b8f37488..9f9aabb799ad 100644 --- a/modules/openapi-generator-maven-plugin/src/main/java/org/openapitools/codegen/plugin/CodeGenMojo.java +++ b/modules/openapi-generator-maven-plugin/src/main/java/org/openapitools/codegen/plugin/CodeGenMojo.java @@ -585,8 +585,8 @@ public void execute() throws MojoExecutionException { configurator = new CodegenConfigurator(); } else { // retrieve mandatory fields from the configurationFile if not defined in the pom.xml - this.generatorName = fromConfigurator(configurator, "generatorName", String.class, generatorName); - this.inputSpec = fromConfigurator(configurator, "inputSpec", String.class, inputSpec); + this.generatorName = fromConfigurator(configurator.getGeneratorName(), generatorName); + this.inputSpec = fromConfigurator(configurator.getInputSpec(), inputSpec); } if (StringUtils.isBlank(inputSpec) && StringUtils.isBlank(inputSpecRootDirectory)) { @@ -1051,27 +1051,16 @@ public void execute() throws MojoExecutionException { } /** - * Access private fields of the CodegenConfigurator class. + * Use the configurator value is not defined in the pom.xml * - * @param configurator the CodegenConfigurator - * @param fieldName name of the field - * @param clazz type of the field - * @param defaultValue default if configuration.fieldName is null - * @return the value of configuration.fieldName if defaultValue is null + * @param defaultValue default value taking precedence */ - private T fromConfigurator(CodegenConfigurator configurator, String fieldName, Class clazz, T defaultValue) { + private T fromConfigurator(T value, T defaultValue) { if (defaultValue != null) { // keep backward compatibilty, the value in the pom.xml has precedence over the value in the config file. return defaultValue; } - try { - Field field = CodegenConfigurator.class.getDeclaredField(fieldName); - field.setAccessible(true); - T value = (T)field.get(configurator); - return value == null? defaultValue : value; - } catch (Exception e) { - throw new RuntimeException("Failed to read " + fieldName + " from configuration file.", e); - } + return value; } /** diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/config/CodegenConfigurator.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/config/CodegenConfigurator.java index a3597fa37f2e..c1c8b6027eda 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/config/CodegenConfigurator.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/config/CodegenConfigurator.java @@ -811,4 +811,12 @@ public ClientOptInput toClientOptInput() { return input.openAPI((OpenAPI) context.getSpecDocument()); } + + public String getGeneratorName() { + return generatorName; + } + + public String getInputSpec() { + return inputSpec; + } } From 02a83e3b28d963a3bd50f8acd7729a0a32370019 Mon Sep 17 00:00:00 2001 From: jpfinne Date: Thu, 30 Oct 2025 12:59:30 +0100 Subject: [PATCH 3/5] Improve config in pom.xml --- .../src/test/resources/configuration-file-missing/pom.xml | 6 +++--- .../src/test/resources/configuration-file/pom.xml | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/modules/openapi-generator-maven-plugin/src/test/resources/configuration-file-missing/pom.xml b/modules/openapi-generator-maven-plugin/src/test/resources/configuration-file-missing/pom.xml index e8d44fc9a83b..716ba128b998 100644 --- a/modules/openapi-generator-maven-plugin/src/test/resources/configuration-file-missing/pom.xml +++ b/modules/openapi-generator-maven-plugin/src/test/resources/configuration-file-missing/pom.xml @@ -16,14 +16,14 @@ 4.0.0 - minimal.update.test - minimal-update-test + config.file.test + not-available jar 1.0.0-SNAPSHOT OpenAPI Generator Minimal Update Test https://openapi-generator.tech/ - minimal-update-test + No generation org.openapitools diff --git a/modules/openapi-generator-maven-plugin/src/test/resources/configuration-file/pom.xml b/modules/openapi-generator-maven-plugin/src/test/resources/configuration-file/pom.xml index e8d44fc9a83b..4feec261020c 100644 --- a/modules/openapi-generator-maven-plugin/src/test/resources/configuration-file/pom.xml +++ b/modules/openapi-generator-maven-plugin/src/test/resources/configuration-file/pom.xml @@ -16,14 +16,14 @@ 4.0.0 - minimal.update.test - minimal-update-test + config.file.test + available jar 1.0.0-SNAPSHOT - OpenAPI Generator Minimal Update Test + ConfigFile https://openapi-generator.tech/ - minimal-update-test + configuration file test org.openapitools From ee726834ce0d924bbce8fe138ffcec1172ca2413 Mon Sep 17 00:00:00 2001 From: jpfinne Date: Thu, 30 Oct 2025 13:17:33 +0100 Subject: [PATCH 4/5] fix typo in configuration file --- .../src/test/resources/configuration-file/config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/openapi-generator-maven-plugin/src/test/resources/configuration-file/config.yaml b/modules/openapi-generator-maven-plugin/src/test/resources/configuration-file/config.yaml index 517c0eaad107..f0dd0bfdbb80 100644 --- a/modules/openapi-generator-maven-plugin/src/test/resources/configuration-file/config.yaml +++ b/modules/openapi-generator-maven-plugin/src/test/resources/configuration-file/config.yaml @@ -1,3 +1,3 @@ inputSpec: petstore-on-classpath.yaml generatorName: spring -output: ./target/generated-sources/minimal-update< +output: ./target/generated-sources/config From 5b119e076ce7be3231f0836798d120ef101ccc89 Mon Sep 17 00:00:00 2001 From: jpfinne Date: Thu, 13 Nov 2025 11:38:48 +0100 Subject: [PATCH 5/5] Fix description of skipIfConfigurationFileMissing --- .../main/java/org/openapitools/codegen/plugin/CodeGenMojo.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/openapi-generator-maven-plugin/src/main/java/org/openapitools/codegen/plugin/CodeGenMojo.java b/modules/openapi-generator-maven-plugin/src/main/java/org/openapitools/codegen/plugin/CodeGenMojo.java index 9f9aabb799ad..cc0088e52663 100644 --- a/modules/openapi-generator-maven-plugin/src/main/java/org/openapitools/codegen/plugin/CodeGenMojo.java +++ b/modules/openapi-generator-maven-plugin/src/main/java/org/openapitools/codegen/plugin/CodeGenMojo.java @@ -197,7 +197,7 @@ public class CodeGenMojo extends AbstractMojo { private String configurationFile; /** - * Skip the execution if the source file is older than the output folder. + * Skip the execution if the configuration file does not exist. */ @Parameter(name = "skipIfConfigurationFileMissing", property = "codegen.skipIfConfigurationFileMissing", defaultValue = "false") private Boolean skipIfConfigurationFileMissing;