diff --git a/modules/openapi-generator-gradle-plugin/README.adoc b/modules/openapi-generator-gradle-plugin/README.adoc index b55c5e8565a..a59450bc03d 100644 --- a/modules/openapi-generator-gradle-plugin/README.adoc +++ b/modules/openapi-generator-gradle-plugin/README.adoc @@ -418,6 +418,12 @@ apply plugin: 'org.openapi.generator' |Boolean |false |Defines whether the output directory should be cleaned up before generating the output. + +|dryRun +|Boolean +|false +|Defines whether the generator should run in dry-run mode. In dry-run mode no files are written and a summary about +file states is output. |=== [NOTE] diff --git a/modules/openapi-generator-gradle-plugin/src/main/kotlin/org/openapitools/generator/gradle/plugin/OpenApiGeneratorPlugin.kt b/modules/openapi-generator-gradle-plugin/src/main/kotlin/org/openapitools/generator/gradle/plugin/OpenApiGeneratorPlugin.kt index 31fcd24584c..08f92166d31 100644 --- a/modules/openapi-generator-gradle-plugin/src/main/kotlin/org/openapitools/generator/gradle/plugin/OpenApiGeneratorPlugin.kt +++ b/modules/openapi-generator-gradle-plugin/src/main/kotlin/org/openapitools/generator/gradle/plugin/OpenApiGeneratorPlugin.kt @@ -149,6 +149,7 @@ class OpenApiGeneratorPlugin : Plugin { generateAliasAsModel.set(generate.generateAliasAsModel) engine.set(generate.engine) cleanupOutput.set(generate.cleanupOutput) + dryRun.set(generate.dryRun) } } } diff --git a/modules/openapi-generator-gradle-plugin/src/main/kotlin/org/openapitools/generator/gradle/plugin/extensions/OpenApiGeneratorGenerateExtension.kt b/modules/openapi-generator-gradle-plugin/src/main/kotlin/org/openapitools/generator/gradle/plugin/extensions/OpenApiGeneratorGenerateExtension.kt index d93a03ba008..0bee2e89881 100644 --- a/modules/openapi-generator-gradle-plugin/src/main/kotlin/org/openapitools/generator/gradle/plugin/extensions/OpenApiGeneratorGenerateExtension.kt +++ b/modules/openapi-generator-gradle-plugin/src/main/kotlin/org/openapitools/generator/gradle/plugin/extensions/OpenApiGeneratorGenerateExtension.kt @@ -359,6 +359,11 @@ open class OpenApiGeneratorGenerateExtension(project: Project) { */ val cleanupOutput = project.objects.property() + /** + * Defines whether the generator should run in dry-run mode. + */ + val dryRun = project.objects.property() + init { applyDefaults() } @@ -381,5 +386,6 @@ open class OpenApiGeneratorGenerateExtension(project: Project) { skipValidateSpec.set(false) generateAliasAsModel.set(false) cleanupOutput.set(false) + dryRun.set(false) } } diff --git a/modules/openapi-generator-gradle-plugin/src/main/kotlin/org/openapitools/generator/gradle/plugin/tasks/GenerateTask.kt b/modules/openapi-generator-gradle-plugin/src/main/kotlin/org/openapitools/generator/gradle/plugin/tasks/GenerateTask.kt index 0dd57755ad5..aef0326d031 100644 --- a/modules/openapi-generator-gradle-plugin/src/main/kotlin/org/openapitools/generator/gradle/plugin/tasks/GenerateTask.kt +++ b/modules/openapi-generator-gradle-plugin/src/main/kotlin/org/openapitools/generator/gradle/plugin/tasks/GenerateTask.kt @@ -521,6 +521,13 @@ open class GenerateTask : DefaultTask() { @Input val cleanupOutput = project.objects.property() + /** + * Defines whether the generator should run in dry-run mode. + */ + @Optional + @Input + val dryRun = project.objects.property() + private fun Property.ifNotEmpty(block: Property.(T) -> Unit) { if (isPresent) { val item: T? = get() @@ -822,6 +829,11 @@ open class GenerateTask : DefaultTask() { } } + var dryRunSetting = false + dryRun.ifNotEmpty { setting -> + dryRunSetting = setting + } + val clientOptInput = configurator.toClientOptInput() val codegenConfig = clientOptInput.config @@ -838,7 +850,7 @@ open class GenerateTask : DefaultTask() { val out = services.get(StyledTextOutputFactory::class.java).create("openapi") out.withStyle(StyledTextOutput.Style.Success) - DefaultGenerator().opts(clientOptInput).generate() + DefaultGenerator(dryRunSetting).opts(clientOptInput).generate() out.println("Successfully generated code to ${outputDir.get()}") } catch (e: RuntimeException) { diff --git a/modules/openapi-generator-gradle-plugin/src/test/kotlin/GenerateTaskDslTest.kt b/modules/openapi-generator-gradle-plugin/src/test/kotlin/GenerateTaskDslTest.kt index d819025e2c3..a4f288d34a6 100644 --- a/modules/openapi-generator-gradle-plugin/src/test/kotlin/GenerateTaskDslTest.kt +++ b/modules/openapi-generator-gradle-plugin/src/test/kotlin/GenerateTaskDslTest.kt @@ -458,4 +458,45 @@ class GenerateTaskDslTest : TestBase() { assertEquals(TaskOutcome.FAILED, result.task(":openApiGenerate")?.outcome, "Expected a failed run, but found ${result.task(":openApiGenerate")?.outcome}") } + + @Test + fun `openapiGenerate should set dryRun flag`() { + // Arrange + val projectFiles = mapOf( + "spec.yaml" to javaClass.classLoader.getResourceAsStream("specs/petstore-v3.0.yaml") + ) + withProject( + """ + plugins { + id 'org.openapi.generator' + } + openApiGenerate { + generatorName = "kotlin" + inputSpec = file("spec.yaml").absolutePath + outputDir = file("build/kotlin").absolutePath + apiPackage = "org.openapitools.example.api" + invokerPackage = "org.openapitools.example.invoker" + modelPackage = "org.openapitools.example.model" + configOptions = [ + dateLibrary: "java8" + ] + dryRun = true + } + """.trimIndent(), + projectFiles + ) + + // Act + val result = GradleRunner.create() + .withProjectDir(temp) + .withArguments("openApiGenerate") + .withPluginClasspath() + .build() + + // Assert + assertTrue( + result.output.contains("Dry Run Results:"), + "Dry run results message is missing." + ) + } } diff --git a/modules/openapi-generator-maven-plugin/README.md b/modules/openapi-generator-maven-plugin/README.md index e90392b9507..4a0f9293398 100644 --- a/modules/openapi-generator-maven-plugin/README.md +++ b/modules/openapi-generator-maven-plugin/README.md @@ -107,6 +107,7 @@ mvn clean compile | `skipIfSpecIsUnchanged` | `codegen.skipIfSpecIsUnchanged` | Skip the execution if the source file is older than the output folder (`false` by default. Can also be set globally through the `codegen.skipIfSpecIsUnchanged` property) | `addCompileSourceRoot` | `openapi.generator.maven.plugin.addCompileSourceRoot` | Add the output directory to the project as a source root, so that the generated java types are compiled and included in the project artifact (`true` by default). Mutually exclusive with `addTestCompileSourceRoot`. | `addTestCompileSourceRoot` | `openapi.generator.maven.plugin.addTestCompileSourceRoot` | Add the output directory to the project as a test source root, so that the generated java types are compiled only for the test classpath of the project (`false` by default). Mutually exclusive with `addCompileSourceRoot`. +| `dryRun` | `openapi.generator.maven.plugin.dryRun` | Defines whether the generator should run in dry-run mode. In dry-run mode no files are written and a summary about file states is output ( `false` by default). | `environmentVariables` | N/A | deprecated. Use globalProperties instead. | `globalProperties` | N/A | A **map** of items conceptually similar to "environment variables" or "system properties". These are available to all aspects of the generation flow. See [Global Properties](https://openapi-generator.tech/docs/globals/) for list of available properties. | `configHelp` | `codegen.configHelp` | dumps the configuration help for the specified library (generates no sources) 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 191275884da..f6b31f8f610 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 @@ -460,6 +460,9 @@ public class CodeGenMojo extends AbstractMojo { @Parameter(defaultValue = "false", property = "openapi.generator.maven.plugin.addTestCompileSourceRoot") private boolean addTestCompileSourceRoot = false; + @Parameter(defaultValue = "false", property = "openapi.generator.maven.plugin.dryRun") + private Boolean dryRun = false; + // TODO: Rename to global properties in version 5.1 @Parameter protected Map environmentVariables = new HashMap<>(); @@ -867,7 +870,7 @@ public class CodeGenMojo extends AbstractMojo { return; } adjustAdditionalProperties(config); - new DefaultGenerator().opts(input).generate(); + new DefaultGenerator(dryRun).opts(input).generate(); if (buildContext != null) { buildContext.refresh(new File(getCompileSourceRoot()));