Skip to content

Commit f3944b1

Browse files
authored
Add treatWarningsAsErrors option to ValidateTask in gradle plugin (#21626)
1 parent 0e97e19 commit f3944b1

File tree

4 files changed

+63
-4
lines changed

4 files changed

+63
-4
lines changed

modules/openapi-generator-gradle-plugin/src/main/kotlin/org/openapitools/generator/gradle/plugin/OpenApiGeneratorPlugin.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ class OpenApiGeneratorPlugin : Plugin<Project> {
8585

8686
inputSpec.set(validate.inputSpec)
8787
recommend.set(validate.recommend)
88+
treatWarningsAsErrors.set(validate.treatWarningsAsErrors)
8889
}
8990

9091
register("openApiGenerate", GenerateTask::class.java).configure {

modules/openapi-generator-gradle-plugin/src/main/kotlin/org/openapitools/generator/gradle/plugin/extensions/OpenApiGeneratorValidateExtension.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,9 @@ open class OpenApiGeneratorValidateExtension(project: Project) {
3434
* Whether to offer recommendations related to the validated specification document.
3535
*/
3636
val recommend = project.objects.property<Boolean>().convention(true)
37+
38+
/**
39+
* Whether to treat warnings as errors and fail the task.
40+
*/
41+
val treatWarningsAsErrors = project.objects.property<Boolean>().convention(false)
3742
}

modules/openapi-generator-gradle-plugin/src/main/kotlin/org/openapitools/generator/gradle/plugin/tasks/ValidateTask.kt

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ open class ValidateTask : DefaultTask() {
6060
@Input
6161
val recommend = project.objects.property<Boolean>().convention(true)
6262

63+
@Optional
64+
@Input
65+
val treatWarningsAsErrors = project.objects.property<Boolean>().convention(false)
66+
6367
@get:Internal
6468
@set:Option(option = "input", description = "The input specification.")
6569
var input: String? = null
@@ -73,6 +77,7 @@ open class ValidateTask : DefaultTask() {
7377

7478
val spec = inputSpec.get()
7579
val recommendations = recommend.get()
80+
val failOnWarnings = treatWarningsAsErrors.get()
7681

7782
logger.quiet("Validating spec $spec")
7883

@@ -117,10 +122,16 @@ open class ValidateTask : DefaultTask() {
117122
}
118123

119124
throw GradleException("Validation failed.")
120-
} else {
121-
out.withStyle(StyledTextOutput.Style.Success)
122-
logger.debug("No error validations from swagger-parser or internal validations.")
123-
out.println("Spec is valid.")
124125
}
126+
127+
if (failOnWarnings && validationResult.warnings.isNotEmpty()) {
128+
out.withStyle(StyledTextOutput.Style.Error)
129+
out.println("\nWarnings found in the spec and 'treatWarningsAsErrors' is enabled.\nFailing validation.\n")
130+
throw GradleException("Validation failed due to warnings (treatWarningsAsErrors = true).")
131+
}
132+
133+
out.withStyle(StyledTextOutput.Style.Success)
134+
logger.debug("No error validations from swagger-parser or internal validations.")
135+
out.println("Spec is valid.")
125136
}
126137
}

modules/openapi-generator-gradle-plugin/src/test/kotlin/ValidateTaskDslTest.kt

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,48 @@ class ValidateTaskDslTest : TestBase() {
308308
)
309309
}
310310

311+
@Test(dataProvider = "gradle_version_provider")
312+
fun `openApiValidate should fail with treatWarningsAsErrors on valid spec with warnings`(gradleVersion: String?) {
313+
// Arrange
314+
val projectFiles = mapOf(
315+
"spec.yaml" to javaClass.classLoader.getResourceAsStream("specs/petstore-v3.0-recommend.yaml")
316+
)
317+
318+
withProject(
319+
"""
320+
| plugins {
321+
| id 'org.openapi.generator'
322+
| }
323+
|
324+
| openApiValidate {
325+
| inputSpec = file("spec.yaml").absolutePath
326+
| treatWarningsAsErrors = true
327+
| }
328+
""".trimMargin(), projectFiles
329+
)
330+
331+
// Act
332+
val result = getGradleRunner(gradleVersion)
333+
.withProjectDir(temp)
334+
.withArguments("openApiValidate")
335+
.withPluginClasspath()
336+
.buildAndFail()
337+
338+
// Assert
339+
assertTrue(
340+
result.output.contains("Spec has issues or recommendations."),
341+
"Unexpected/no message presented to the user for a valid spec."
342+
)
343+
assertTrue(
344+
result.output.contains("Failing validation."),
345+
"Expected validation to fail due to warnings, but no failure message was found."
346+
)
347+
assertEquals(
348+
FAILED, result.task(":openApiValidate")?.outcome,
349+
"Expected a failed run, but found ${result.task(":openApiValidate")?.outcome}"
350+
)
351+
}
352+
311353
@Test(dataProvider = "gradle_version_provider")
312354
fun `openApiValidate should succeed without recommendations on valid spec`(gradleVersion: String?) {
313355
// Arrange

0 commit comments

Comments
 (0)