mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-10-13 16:03:43 +00:00
Add treatWarningsAsErrors option to ValidateTask in gradle plugin (#21626)
This commit is contained in:
parent
0e97e19bbc
commit
f3944b152d
@ -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 {
|
||||
|
@ -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)
|
||||
}
|
@ -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
|
||||
@ -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")
|
||||
|
||||
@ -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.")
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user