#13726 Introduce new remoteInputSpec parameter (#13727)

* #13726 Introduce new remoteInputSpec parameter

* #13726 Add documentation and new warning log
This commit is contained in:
Daniel Hoffmann 2022-11-07 16:32:58 +01:00 committed by GitHub
parent 7a17d3dc55
commit 3eec4eb326
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 86 additions and 0 deletions

View File

@ -154,6 +154,11 @@ apply plugin: 'org.openapi.generator'
|None
|The Open API 2.0/3.x specification location.
|remoteInputSpec
|String
|None
|The remote Open API 2.0/3.x specification URL location.
|templateDir
|String
|None

View File

@ -97,6 +97,7 @@ class OpenApiGeneratorPlugin : Plugin<Project> {
generatorName.set(generate.generatorName)
outputDir.set(generate.outputDir)
inputSpec.set(generate.inputSpec)
remoteInputSpec.set(generate.remoteInputSpec)
templateDir.set(generate.templateDir)
auth.set(generate.auth)
globalProperties.set(generate.globalProperties)

View File

@ -52,6 +52,11 @@ open class OpenApiGeneratorGenerateExtension(project: Project) {
*/
val inputSpec = project.objects.property<String>()
/**
* The remote Open API 2.0/3.x specification URL location.
*/
val remoteInputSpec = project.objects.property<String>()
/**
* The template directory holding a custom template.
*/

View File

@ -91,10 +91,18 @@ open class GenerateTask : DefaultTask() {
/**
* The Open API 2.0/3.x specification location.
*/
@Optional
@get:InputFile
@PathSensitive(PathSensitivity.RELATIVE)
val inputSpec = project.objects.property<String>()
/**
* The remote Open API 2.0/3.x specification URL location.
*/
@Input
@Optional
val remoteInputSpec = project.objects.property<String>()
/**
* The template directory holding a custom template.
*/
@ -556,6 +564,10 @@ open class GenerateTask : DefaultTask() {
GlobalSettings.setProperty(CodegenConstants.WITH_XML, withXml.get().toString())
}
if (inputSpec.isPresent && remoteInputSpec.isPresent) {
logger.warn("Both inputSpec and remoteInputSpec is specified. The remoteInputSpec will take priority over inputSpec.")
}
// now override with any specified parameters
verbose.ifNotEmpty { value ->
configurator.setVerbose(value)
@ -573,6 +585,10 @@ open class GenerateTask : DefaultTask() {
configurator.setInputSpec(value)
}
remoteInputSpec.ifNotEmpty { value ->
configurator.setInputSpec(value)
}
generatorName.ifNotEmpty { value ->
configurator.setGeneratorName(value)
}

View File

@ -29,6 +29,65 @@ class GenerateTaskDslTest : TestBase() {
}
""".trimIndent()
@Test
fun `openApiGenerate should create an expected file structure from URL config`() {
val specUrl = "https://raw.githubusercontent.com/OpenAPITools/openapi-generator/b6b8c0db872fb4a418ae496e89c7e656e14be165/modules/openapi-generator-gradle-plugin/src/test/resources/specs/petstore-v3.0.yaml"
// Arrange
val buildContents = """
plugins {
id 'org.openapi.generator'
}
openApiGenerate {
generatorName = "kotlin"
remoteInputSpec = "$specUrl"
outputDir = file("build/kotlin").absolutePath
apiPackage = "org.openapitools.example.api"
invokerPackage = "org.openapitools.example.invoker"
modelPackage = "org.openapitools.example.model"
configOptions = [
dateLibrary: "java8"
]
}
""".trimIndent()
File(temp, "build.gradle").writeText(buildContents)
// Act
val result = GradleRunner.create()
.withProjectDir(temp)
.withArguments("openApiGenerate")
.withPluginClasspath()
.build()
// Assert
assertTrue(
result.output.contains("Successfully generated code to"),
"User friendly generate notice is missing."
)
listOf(
"build/kotlin/.openapi-generator-ignore",
"build/kotlin/docs/PetsApi.md",
"build/kotlin/docs/Error.md",
"build/kotlin/docs/Pet.md",
"build/kotlin/README.md",
"build/kotlin/build.gradle",
"build/kotlin/.openapi-generator/VERSION",
"build/kotlin/settings.gradle",
"build/kotlin/src/main/kotlin/org/openapitools/example/model/Pet.kt",
"build/kotlin/src/main/kotlin/org/openapitools/example/model/Error.kt",
"build/kotlin/src/main/kotlin/org/openapitools/example/api/PetsApi.kt",
"build/kotlin/src/main/kotlin/org/openapitools/client/infrastructure/ApiClient.kt"
).map {
val f = File(temp, it)
assertTrue(f.exists() && f.isFile, "An expected file was not generated when invoking the generation: $f")
}
assertEquals(
TaskOutcome.SUCCESS, result.task(":openApiGenerate")?.outcome,
"Expected a successful run, but found ${result.task(":openApiGenerate")?.outcome}"
)
}
@Test
fun `openApiGenerate should create an expected file structure from DSL config`() {
// Arrange