[gradle] Include engine option for handlebars generation (#5686)

This commit is contained in:
Jim Schubert 2020-03-25 12:27:05 -04:00 committed by GitHub
parent 625c734cfe
commit 256a431f03
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 58 additions and 0 deletions

View File

@ -330,6 +330,10 @@ apply plugin: 'org.openapi.generator'
|false
|To generate alias (array, list, map) as model. When false, top-level objects defined as array, list, or map will result in those definitions generated as top-level Array-of-items, List-of-items, Map-of-items definitions. When true, A model representation either containing or extending the array,list,map (depending on specific generator implementation) will be generated.
|engine
|String
|mustache
|Templating engine: "mustache" (default) or "handlebars" (beta)
|===
[NOTE]

View File

@ -138,6 +138,7 @@ class OpenApiGeneratorPlugin : Plugin<Project> {
enablePostProcessFile.set(generate.enablePostProcessFile)
skipValidateSpec.set(generate.skipValidateSpec)
generateAliasAsModel.set(generate.generateAliasAsModel)
engine.set(generate.engine)
}
}
}

View File

@ -307,6 +307,11 @@ open class OpenApiGeneratorGenerateExtension(project: Project) {
*/
val configOptions = project.objects.mapProperty<String, String>()
/**
* Templating engine: "mustache" (default) or "handlebars" (beta)
*/
val engine = project.objects.property<String?>()
init {
applyDefaults()
}

View File

@ -375,6 +375,12 @@ open class GenerateTask : DefaultTask() {
@get:Internal
val configOptions = project.objects.mapProperty<String, String>()
/**
* Templating engine: "mustache" (default) or "handlebars" (beta)
*/
@get:Internal
val engine = project.objects.property<String?>()
private fun <T : Any?> Property<T>.ifNotEmpty(block: Property<T>.(T) -> Unit) {
if (isPresent) {
val item: T? = get()
@ -561,6 +567,12 @@ open class GenerateTask : DefaultTask() {
configurator.setGenerateAliasAsModel(value)
}
engine.ifNotEmpty { value ->
if ("handlebars".equals(value, ignoreCase = true)) {
configurator.setTemplatingEngineName("handlebars")
}
}
if (systemProperties.isPresent) {
systemProperties.get().forEach { entry ->
configurator.addSystemProperty(entry.key, entry.value)

View File

@ -126,4 +126,40 @@ class GenerateTaskDslTest : TestBase() {
assertEquals(TaskOutcome.SUCCESS, result.task(":openApiGenerate")?.outcome,
"Expected a successful run, but found ${result.task(":openApiGenerate")?.outcome}")
}
@Test
fun `openapiGenerate should attempt to set handlebars when specified as engine`(){
// Arrange
val projectFiles = mapOf(
"spec.yaml" to javaClass.classLoader.getResourceAsStream("specs/petstore-v3.0.yaml")
)
withProject("""
plugins {
id 'org.openapi.generator'
}
openApiGenerate {
generatorName = "kotlin"
inputSpec = file("spec.yaml").absolutePath
outputDir = file("build/kotlin").absolutePath
apiPackage = "org.openapitools.example.api"
invokerPackage = "org.openapitools.example.invoker"
modelPackage = "org.openapitools.example.model"
engine = "handlebars"
}
""".trimIndent(), projectFiles)
// Act
val result = GradleRunner.create()
.withProjectDir(temp)
.withArguments("openApiGenerate")
.withPluginClasspath()
.buildAndFail()
// Assert
// rather than write out full handlebars generator templates, we'll just test that the configurator has set handlebars as the engine.
assertTrue(result.output.contains("kotlin-client/model.handlebars (No such file or directory)"), "Build should have attempted to use handlebars.")
assertEquals(TaskOutcome.FAILED, result.task(":openApiGenerate")?.outcome,
"Expected a failed run, but found ${result.task(":openApiGenerate")?.outcome}")
}
}