diff --git a/docs/templating.html b/docs/templating.html index 01b7e3152477..b6438c53f8d8 100644 --- a/docs/templating.html +++ b/docs/templating.html @@ -71,7 +71,7 @@
templateDirBuilt-in templates are written in Mustache and processed by jmustache. We plan to eventually support Handlebars and user-defined template engines via plugins.
+Built-in templates are written in Mustache and processed by jmustache. Beginning with version 4.0.0, we support experimental Handlebars and user-defined template engines via plugins.
OpenAPI Generator supports user-defined templates. This approach is often the easiest when creating a custom template. Our generators implement a combination of language and framework features, and it's fully possible to use an existing generator to implement a custom template for a different framework. Suppose you have internal utilities which you'd like to incorporate into generated code (e.g. logging, monitoring, fault-handling)... this is easy to add via custom templates.
Note: You cannot use this approach to create new templates, only override existing ones. If you'd like to create a new generator to contribute back to the project, see
@@ -269,6 +269,133 @@ Download https://jcenter.bintray.com/com/jcabi/jcabi-aspects/0.22.6/jcabi-aspectnew.shin the repository root. If you'd like to create a private generator for more templating control, see the customization docs.Execute
./gradlew buildand thencat target/rolling/rollingtest.log. You should see messages logged for every call in PetApi with a stubbed unit test.Congratulations! You've now modified one of the built-in templates to meet your client code's needs.
Adding/modifying template logic simply requires a little bit of mustache, for which you can use existing templates as a guide.
+Custom Engines
+++Custom template engine support is experimental
+If Mustache or the experimental Handlebars engines don't suit your needs, you can define an adapter to your templating engine of choice. To do this, you'll need to define a new project which consumes the
+openapi-generator-coreartifact, and at a minimum implementTemplatingEngineAdapter.This example:
++
+- creates an adapter providing the fundamental logic to compile Pebble Templates
+- will be implemented in Kotlin to demonstrate ServiceLoader configuration specific to Kotlin (Java will be similar)
+- requires Gradle 5.0+
+- provides project setup instructions for IntelliJ
+To begin, create a new Gradle project with Kotlin support. To do this, go to
+File➞New➞Project, choose "Gradle" and "Kotlin". Specify groupIdorg.openapitools.examplesand artifactIdpebble-template-adapter.Ensure the new project uses Gradle 5.0. Navigate to the newly created directory and execute:
++gradle wrapper --gradle-version 5.0 +In
+build.gradle, we'll add a dependency for OpenAPI Tools core which defines the interface and an abstract helper type for implementing the adapter. We'll also pull in the Pebble artifact. We'll be evaluating this new artifact locally, so we'll also add the Maven plugin for installing to the local maven repository. We'll also create a fatjar using theshadowplugin to simplify our classpath.Modifications to the new project's
+build.gradleshould be made in thepluginsanddependenciesnodes:+plugins { + id 'org.jetbrains.kotlin.jvm' version '1.3.11' + id "com.github.johnrengelman.shadow" version "5.0.0" + } + + dependencies { + compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8" + compile "org.openapitools:openapi-generator-core:4.0.0-SNAPSHOT" + compile "io.pebbletemplates:pebble:3.0.8" + } +The above configuration for the
+shadowplugin is strictly optional. It is not needed, for instance, if you plan to publish your adapter and consume it via the Maven or Gradle plugins.Next, create a new class file called
+PebbleTemplateEngineAdapterundersrc/kotlin. We'll define the template adapter's name aspebbleand we'll also list this as the only supported file extension. We'll implement the adapter by extendingAbstractTemplatingEngineAdapter, which includes reusable logic, such as retrieving a list of all possible template names for our provided template extensions(s).The class in its simplest form looks like this (with inline comments):
++// Allows specifying engine by class name +// e.g. -e org.openapitools.examples.templating.PebbleTemplateAdapter +@file:JvmName("PebbleTemplateAdapter") +package org.openapitools.examples.templating + +// imports + +class PebbleTemplateAdapter : AbstractTemplatingEngineAdapter() { + // initialize the template compilation engine + private val engine: PebbleEngine = PebbleEngine.Builder() + .cacheActive(false) + .loader(DelegatingLoader(listOf(FileLoader(), ClasspathLoader()))) + .build() + + // allows targeting engine by id/name: -e pebble + override fun getIdentifier(): String = "pebble" + + override fun compileTemplate( + generator: TemplatingGenerator?, + bundle: MutableMap<String, Any>?, + templateFile: String? + ): String { + // This will convert, for example, model.mustache to model.pebble + val modifiedTemplate = this.getModifiedFileLocation(templateFile).first() + + // Uses generator built-in template resolution strategy to find the full template file + val filePath = generator?.getFullTemplatePath(modifiedTemplate) + + val writer = StringWriter() + // Conditionally writes out the template if found. + if (filePath != null) { + engine.getTemplate(filePath.toAbsolutePath().toString())?.evaluate(writer, bundle) + } + return writer.toString() + } + + override fun getFileExtensions(): Array<String> = arrayOf("pebble") +} +Lastly, create a file
+resources/META-INF/services/org.openapitools.codegen.api.TemplatingEngineAdapter, containing the full class path to the above class:+org.openapitools.examples.templating.PebbleTemplateAdapter +This allows the adapter to load via ServiceLoader, and to be referenced via the identifier
+pebble. This is optional; if you don't provide the above file and contents, you'll only be able to load the engine via full class name (explained in a bit).Now, build the fatjar for this new adapter:
++./gradlew shadowJar +To test compilation of some templates, we'll need to first create one or more template files. Create a temp directory at
+/tmp/pebble-example/templatesand add the following files.api.pebble
++package {{packageName}} + +import ( + "net/http" +{% for item in imports %} + "{{item.import}}" +{% endfor %} +) + +type Generated{{classname}}Servicer + +// etc +model.pebble
++package {{packageName}} + +{% for item in models %} +{% if item.isEnum %} +// TODO: enum +{% else %} +{% if item.description is not empty %}// {{item.description}}{% endif %} +type {{item.classname}} struct { +{% for var in item.model.vars %} + {% if var.description is not empty %}// {{var.description}}{% endif %} + {{var.name}} {% if var.isNullable %}*{% endif %}{{var.dataType}} `json:"{{var.baseName}}{% if var.required == false %},omitempty{% endif %}"{% if var.withXml == true %} xml:"{{var.baseName}}{% if var.isXmlAttribute %},attr{% endif %}"{% endif %}` +{% endfor %} +} +{% endif %} +{{model.name}} +{% endfor %} +++Find object structures passed to templates later in this document's Structures section.
+Finally, we can compile some code by explicitly defining our classpath and jar entrypoint for CLI (be sure to modify
+/your/pathbelow)+java $JAVA_OPTS -cp /your/path/build/libs/pebble-template-adapter-1.0-SNAPSHOT-all.jar:modules/openapi-generator-cli/target/openapi-generator-cli.jar \ + org.openapitools.codegen.OpenAPIGenerator \ + generate \ + -g go \ + -i https://raw.githubusercontent.com/OAI/OpenAPI-Specification/master/examples/v2.0/json/petstore-minimal.json \ + -e pebble \ + -o /tmp/pebble-example/out \ + -t /tmp/pebble-example/templates \ + -Dmodels -DmodelDocs=false -DmodelTests=false -Dapis -DapiTests=false -DapiDocs=false +Notice how we've targeted our custom template engine adapter via
+-e pebble. If you don't include the SPI file underMETA-INF/services, you'll need to specify the exact classpath:org.openapitools.examples.templating.PebbleTemplateAdapter. Notice that the target class here matches the Kotlin class name. This is because of the@file:JvmNameannotation.Congratulations on creating a custom templating engine adapter!
Structures
Aside from transforming an API document, the implementing class gets to decide how to apply the data structure to templates. We can decide which data structure to apply to which template files. You have the following structures at your disposal.
Examples for the following structures will be presented using the following spec document:
@@ -707,4 +834,4 @@ For more details on Mustache see {{#enums}}{{-index}} {{enum}}{{/enums}} -Last updated on 2019-4-22