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 @@ -33,12 +33,5 @@ open class OpenApiGeneratorValidateExtension(project: Project) {
/**
* Whether to offer recommendations related to the validated specification document.
*/
val recommend = project.objects.property<Boolean?>()

init {
applyDefaults()
}

@Suppress("MemberVisibilityCanBePrivate")
fun applyDefaults() = recommend.set(true)
val recommend = project.objects.property<Boolean>().convention(true)
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ open class ValidateTask : DefaultTask() {

@Optional
@Input
val recommend = project.objects.property<Boolean?>()
val recommend = project.objects.property<Boolean>().convention(true)

@get:Internal
@set:Option(option = "input", description = "The input specification.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,161 @@ class ValidateTaskDslTest : TestBase() {
"Expected a failed run, but found ${result.task(":openApiValidate")?.outcome}"
)
}

@Test(dataProvider = "gradle_version_provider")
fun `validateGoodSpec as defined task should succeed on valid spec`(gradleVersion: String?) {
// Arrange
val projectFiles = mapOf(
"spec.yaml" to javaClass.classLoader.getResourceAsStream("specs/petstore-v3.0.yaml")
)

withProject(
"""
| plugins {
| id 'org.openapi.generator'
| }
|
| task validateGoodSpec(type: org.openapitools.generator.gradle.plugin.tasks.ValidateTask) {
| inputSpec.set(file("spec.yaml").absolutePath)
| }
""".trimMargin(), projectFiles
)

// Act
val result = getGradleRunner(gradleVersion)
.withProjectDir(temp)
.withArguments("validateGoodSpec")
.withPluginClasspath()
.build()

// Assert
assertTrue(
result.output.contains("Spec is valid."),
"Unexpected/no message presented to the user for a valid spec."
)
assertEquals(
SUCCESS, result.task(":validateGoodSpec")?.outcome,
"Expected a successful run, but found ${result.task(":validateGoodSpec")?.outcome}"
)
}

@Test(dataProvider = "gradle_version_provider")
fun `validateBadSpec as defined task should fail on invalid spec`(gradleVersion: String?) {
// Arrange
val projectFiles = mapOf(
"spec.yaml" to javaClass.classLoader.getResourceAsStream("specs/petstore-v3.0-invalid.yaml")
)
withProject(
"""
| plugins {
| id 'org.openapi.generator'
| }
|
| task validateBadSpec(type: org.openapitools.generator.gradle.plugin.tasks.ValidateTask) {
| inputSpec.set(file("spec.yaml").absolutePath)
| }
""".trimMargin(), projectFiles
)

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

// Assert
assertTrue(
result.output.contains("Spec is invalid."),
"Unexpected/no message presented to the user for an invalid spec."
)
assertEquals(
FAILED, result.task(":validateBadSpec")?.outcome,
"Expected a failed run, but found ${result.task(":validateBadSpec")?.outcome}"
)
}

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

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

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

// Assert
assertTrue(
result.output.contains("Spec is valid."),
"Unexpected/no message presented to the user for a valid spec."
)
assertTrue(
result.output.contains("Spec has issues or recommendations."),
"Unexpected/no recommendations message presented to the user for a valid spec."
)
assertEquals(
SUCCESS, result.task(":openApiValidate")?.outcome,
"Expected a successful run, but found ${result.task(":openApiValidate")?.outcome}"
)
}

@Test(dataProvider = "gradle_version_provider")
fun `openApiValidate should succeed without recommendations on valid spec`(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
| recommend = false
| }
""".trimMargin(), projectFiles
)

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

// Assert
assertTrue(
result.output.contains("Spec is valid."),
"Unexpected/no message presented to the user for a valid spec."
)
assertTrue(
result.output.contains("Spec has issues or recommendations.").not(),
"Unexpected/recommendations message presented to the user for a valid spec"
)
assertEquals(
SUCCESS, result.task(":openApiValidate")?.outcome,
"Expected a successful run, but found ${result.task(":openApiValidate")?.outcome}"
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
openapi: "3.0.0"
info:
version: 1.0.0
title: Swagger Petstore
license:
name: MIT
servers:
- url: http://petstore.swagger.io/v1
paths:
/pets:
get:
summary: List all pets
operationId: listPets
tags:
- pets
parameters:
- name: limit
in: query
description: How many items to return at one time (max 100)
required: false
schema:
type: integer
format: int32
requestBody: # warning `API GET/HEAD defined with request body`
content:
application/json:
schema:
$ref: "#/components/schemas/Pets"
responses:
'200':
description: A paged array of pets
headers:
x-next:
description: A link to the next page of responses
schema:
type: string
content:
application/json:
schema:
$ref: "#/components/schemas/Pets"
default:
description: unexpected error
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
post:
summary: Create a pet
operationId: createPets
tags:
- pets
responses:
'201':
description: Null response
default:
description: unexpected error
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
/pets/{petId}:
get:
summary: Info for a specific pet
operationId: showPetById
tags:
- pets
parameters:
- name: petId
in: path
required: true
description: The id of the pet to retrieve
schema:
type: string
responses:
'200':
description: Expected response to a valid request
content:
application/json:
schema:
$ref: "#/components/schemas/Pets"
default:
description: unexpected error
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
components:
schemas:
Pet:
required:
- id
- name
properties:
id:
type: integer
format: int64
name:
type: string
tag:
type: string
Pets:
type: array
items:
$ref: "#/components/schemas/Pet"
Error:
required:
- code
- message
properties:
code:
type: integer
format: int32
message:
type: string