Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class OpenApiGeneratorPlugin : Plugin<Project> {

inputSpec.set(validate.inputSpec)
recommend.set(validate.recommend)
treatWarningsAsErrors.set(validate.treatWarningsAsErrors)
}

register("openApiGenerate", GenerateTask::class.java).configure {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,9 @@ open class OpenApiGeneratorValidateExtension(project: Project) {
* Whether to offer recommendations related to the validated specification document.
*/
val recommend = project.objects.property<Boolean>().convention(true)

/**
* Whether to treat warnings as errors and fail the task.
*/
val treatWarningsAsErrors = project.objects.property<Boolean>().convention(false)
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ open class ValidateTask : DefaultTask() {
@Input
val recommend = project.objects.property<Boolean>().convention(true)

@Optional
@Input
val treatWarningsAsErrors = project.objects.property<Boolean>().convention(false)

@get:Internal
@set:Option(option = "input", description = "The input specification.")
var input: String? = null
Expand All @@ -73,6 +77,7 @@ open class ValidateTask : DefaultTask() {

val spec = inputSpec.get()
val recommendations = recommend.get()
val failOnWarnings = treatWarningsAsErrors.get()

logger.quiet("Validating spec $spec")

Expand Down Expand Up @@ -117,10 +122,16 @@ open class ValidateTask : DefaultTask() {
}

throw GradleException("Validation failed.")
} else {
out.withStyle(StyledTextOutput.Style.Success)
logger.debug("No error validations from swagger-parser or internal validations.")
out.println("Spec is valid.")
}

if (failOnWarnings && validationResult.warnings.isNotEmpty()) {
out.withStyle(StyledTextOutput.Style.Error)
out.println("\nWarnings found in the spec and 'treatWarningsAsErrors' is enabled.\nFailing validation.\n")
throw GradleException("Validation failed due to warnings (treatWarningsAsErrors = true).")
}

out.withStyle(StyledTextOutput.Style.Success)
logger.debug("No error validations from swagger-parser or internal validations.")
out.println("Spec is valid.")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,48 @@ class ValidateTaskDslTest : TestBase() {
)
}

@Test(dataProvider = "gradle_version_provider")
fun `openApiValidate should fail with treatWarningsAsErrors on valid spec with warnings`(gradleVersion: String?) {
// Arrange
val projectFiles = mapOf(
"spec.yaml" to javaClass.classLoader.getResourceAsStream("specs/petstore-v3.0-recommend.yaml")
)

withProject(
"""
| plugins {
| id 'org.openapi.generator'
| }
|
| openApiValidate {
| inputSpec = file("spec.yaml").absolutePath
| treatWarningsAsErrors = true
| }
""".trimMargin(), projectFiles
)

// Act
val result = getGradleRunner(gradleVersion)
.withProjectDir(temp)
.withArguments("openApiValidate")
.withPluginClasspath()
.buildAndFail()

// Assert
assertTrue(
result.output.contains("Spec has issues or recommendations."),
"Unexpected/no message presented to the user for a valid spec."
)
assertTrue(
result.output.contains("Failing validation."),
"Expected validation to fail due to warnings, but no failure message was found."
)
assertEquals(
FAILED, result.task(":openApiValidate")?.outcome,
"Expected a failed run, but found ${result.task(":openApiValidate")?.outcome}"
)
}

@Test(dataProvider = "gradle_version_provider")
fun `openApiValidate should succeed without recommendations on valid spec`(gradleVersion: String?) {
// Arrange
Expand Down
Loading