Skip to content

Commit

Permalink
[gradle] Include engine option for handlebars generation (OpenAPITool…
Browse files Browse the repository at this point in the history
  • Loading branch information
jimschubert authored and michaelpro1 committed May 7, 2020
1 parent e5ee9f5 commit afde6b5
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 0 deletions.
4 changes: 4 additions & 0 deletions modules/openapi-generator-gradle-plugin/README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ class OpenApiGeneratorPlugin : Plugin<Project> {
enablePostProcessFile.set(generate.enablePostProcessFile)
skipValidateSpec.set(generate.skipValidateSpec)
generateAliasAsModel.set(generate.generateAliasAsModel)
engine.set(generate.engine)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
}
}

0 comments on commit afde6b5

Please sign in to comment.