Skip to content

Commit 256a431

Browse files
authored
[gradle] Include engine option for handlebars generation (#5686)
1 parent 625c734 commit 256a431

5 files changed

Lines changed: 58 additions & 0 deletions

File tree

modules/openapi-generator-gradle-plugin/README.adoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,10 @@ apply plugin: 'org.openapi.generator'
330330
|false
331331
|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.
332332

333+
|engine
334+
|String
335+
|mustache
336+
|Templating engine: "mustache" (default) or "handlebars" (beta)
333337
|===
334338

335339
[NOTE]

modules/openapi-generator-gradle-plugin/src/main/kotlin/org/openapitools/generator/gradle/plugin/OpenApiGeneratorPlugin.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ class OpenApiGeneratorPlugin : Plugin<Project> {
138138
enablePostProcessFile.set(generate.enablePostProcessFile)
139139
skipValidateSpec.set(generate.skipValidateSpec)
140140
generateAliasAsModel.set(generate.generateAliasAsModel)
141+
engine.set(generate.engine)
141142
}
142143
}
143144
}

modules/openapi-generator-gradle-plugin/src/main/kotlin/org/openapitools/generator/gradle/plugin/extensions/OpenApiGeneratorGenerateExtension.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,11 @@ open class OpenApiGeneratorGenerateExtension(project: Project) {
307307
*/
308308
val configOptions = project.objects.mapProperty<String, String>()
309309

310+
/**
311+
* Templating engine: "mustache" (default) or "handlebars" (beta)
312+
*/
313+
val engine = project.objects.property<String?>()
314+
310315
init {
311316
applyDefaults()
312317
}

modules/openapi-generator-gradle-plugin/src/main/kotlin/org/openapitools/generator/gradle/plugin/tasks/GenerateTask.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,12 @@ open class GenerateTask : DefaultTask() {
375375
@get:Internal
376376
val configOptions = project.objects.mapProperty<String, String>()
377377

378+
/**
379+
* Templating engine: "mustache" (default) or "handlebars" (beta)
380+
*/
381+
@get:Internal
382+
val engine = project.objects.property<String?>()
383+
378384
private fun <T : Any?> Property<T>.ifNotEmpty(block: Property<T>.(T) -> Unit) {
379385
if (isPresent) {
380386
val item: T? = get()
@@ -561,6 +567,12 @@ open class GenerateTask : DefaultTask() {
561567
configurator.setGenerateAliasAsModel(value)
562568
}
563569

570+
engine.ifNotEmpty { value ->
571+
if ("handlebars".equals(value, ignoreCase = true)) {
572+
configurator.setTemplatingEngineName("handlebars")
573+
}
574+
}
575+
564576
if (systemProperties.isPresent) {
565577
systemProperties.get().forEach { entry ->
566578
configurator.addSystemProperty(entry.key, entry.value)

modules/openapi-generator-gradle-plugin/src/test/kotlin/GenerateTaskDslTest.kt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,40 @@ class GenerateTaskDslTest : TestBase() {
126126
assertEquals(TaskOutcome.SUCCESS, result.task(":openApiGenerate")?.outcome,
127127
"Expected a successful run, but found ${result.task(":openApiGenerate")?.outcome}")
128128
}
129+
130+
@Test
131+
fun `openapiGenerate should attempt to set handlebars when specified as engine`(){
132+
// Arrange
133+
val projectFiles = mapOf(
134+
"spec.yaml" to javaClass.classLoader.getResourceAsStream("specs/petstore-v3.0.yaml")
135+
)
136+
137+
withProject("""
138+
plugins {
139+
id 'org.openapi.generator'
140+
}
141+
openApiGenerate {
142+
generatorName = "kotlin"
143+
inputSpec = file("spec.yaml").absolutePath
144+
outputDir = file("build/kotlin").absolutePath
145+
apiPackage = "org.openapitools.example.api"
146+
invokerPackage = "org.openapitools.example.invoker"
147+
modelPackage = "org.openapitools.example.model"
148+
engine = "handlebars"
149+
}
150+
""".trimIndent(), projectFiles)
151+
152+
// Act
153+
val result = GradleRunner.create()
154+
.withProjectDir(temp)
155+
.withArguments("openApiGenerate")
156+
.withPluginClasspath()
157+
.buildAndFail()
158+
159+
// Assert
160+
// rather than write out full handlebars generator templates, we'll just test that the configurator has set handlebars as the engine.
161+
assertTrue(result.output.contains("kotlin-client/model.handlebars (No such file or directory)"), "Build should have attempted to use handlebars.")
162+
assertEquals(TaskOutcome.FAILED, result.task(":openApiGenerate")?.outcome,
163+
"Expected a failed run, but found ${result.task(":openApiGenerate")?.outcome}")
164+
}
129165
}

0 commit comments

Comments
 (0)