You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* master:
resize lightbow logo
add lightbow logo
add Lightbow as sponsor (#2822)
[docs] Document new templating engine, adding ability to reference templates by file to comlete the example (#2773)
[JAVA][KOTLIN] fix var Naming for all uppercase with Numbers (#2794)
[Documentation] Add instructions to build the javascript client module (#2806)
use mvn instead of run-in-docker (#2821)
Better handling of form data (#2818)
[haskell-servant] Add some missing types to the generated modules (#2675)
OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an [OpenAPI Spec](https://github.com/OAI/OpenAPI-Specification) (both 2.0 and 3.0 are supported). Currently, the following languages/frameworks are supported:
Copy file name to clipboardExpand all lines: docs/templating.md
+160-1Lines changed: 160 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -18,7 +18,7 @@ The transform logic needs to implement [CodegenConfig.java](https://github.com/o
18
18
> * Maven Plugin: `templateDirectory`
19
19
> * Gradle Plugin: `templateDir`
20
20
21
-
Built-in templates are written in Mustache and processed by [jmustache](https://github.com/samskivert/jmustache). We plan to eventually support Handlebars and user-defined template engines via plugins.
21
+
Built-in templates are written in Mustache and processed by [jmustache](https://github.com/samskivert/jmustache). Beginning with version 4.0.0, we support experimental Handlebars and user-defined template engines via plugins.
22
22
23
23
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.
24
24
@@ -261,6 +261,165 @@ Congratulations! You've now modified one of the built-in templates to meet your
261
261
262
262
Adding/modifying template logic simply requires a little bit of [mustache](https://mustache.github.io/), for which you can use existing templates as a guide.
263
263
264
+
### Custom Engines
265
+
266
+
> Custom template engine support is *experimental*
267
+
268
+
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-core` artifact, and at a minimum implement `TemplatingEngineAdapter`.
269
+
270
+
This example:
271
+
272
+
* creates an adapter providing the fundamental logic to compile [Pebble Templates](https://pebbletemplates.io)
273
+
* will be implemented in Kotlin to demonstrate ServiceLoader configuration specific to Kotlin (Java will be similar)
274
+
* requires Gradle 5.0+
275
+
* provides project setup instructions for IntelliJ
276
+
277
+
To begin, create a [new Gradle project](https://www.jetbrains.com/help/idea/getting-started-with-gradle.html) with Kotlin support. To do this, go to `File` ➞ `New` ➞ `Project`, choose "Gradle" and "Kotlin". Specify groupId `org.openapitools.examples` and artifactId `pebble-template-adapter`.
278
+
279
+
Ensure the new project uses Gradle 5.0. Navigate to the newly created directory and execute:
280
+
281
+
```bash
282
+
gradle wrapper --gradle-version 5.0
283
+
```
284
+
285
+
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 the `shadow` plugin to simplify our classpath.
286
+
287
+
Modifications to the new project's `build.gradle` should be made in the `plugins` and `dependencies` nodes:
288
+
289
+
```diff
290
+
plugins {
291
+
id 'org.jetbrains.kotlin.jvm' version '1.3.11'
292
+
id "com.github.johnrengelman.shadow" version "5.0.0"
The above configuration for the `shadow` plugin 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.
303
+
304
+
Next, create a new class file called `PebbleTemplateEngineAdapter` under `src/kotlin`. We'll define the template adapter's name as `pebble` and we'll also list this as the only supported file extension. We'll implement the adapter by extending `AbstractTemplatingEngineAdapter`, which includes reusable logic, such as retrieving a list of all possible template names for our provided template extensions(s).
305
+
306
+
The class in its simplest form looks like this (with inline comments):
307
+
308
+
```kotlin
309
+
// Allows specifying engine by class name
310
+
// e.g. -e org.openapitools.examples.templating.PebbleTemplateAdapter
Lastly, create a file `resources/META-INF/services/org.openapitools.codegen.api.TemplatingEngineAdapter`, containing the full class path to the above class:
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).
356
+
357
+
Now, build the fatjar for this new adapter:
358
+
359
+
```bash
360
+
./gradlew shadowJar
361
+
```
362
+
363
+
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/templates` and add the following files.
364
+
365
+
*api.pebble*
366
+
367
+
```
368
+
package {{packageName}}
369
+
370
+
import (
371
+
"net/http"
372
+
{% for item in imports %}
373
+
"{{item.import}}"
374
+
{% endfor %}
375
+
)
376
+
377
+
type Generated{{classname}}Servicer
378
+
379
+
// etc
380
+
```
381
+
382
+
*model.pebble*
383
+
384
+
```
385
+
package {{packageName}}
386
+
387
+
{% for item in models %}
388
+
{% if item.isEnum %}
389
+
// TODO: enum
390
+
{% else %}
391
+
{% if item.description is not empty %}// {{item.description}}{% endif %}
392
+
type {{item.classname}} struct {
393
+
{% for var in item.model.vars %}
394
+
{% if var.description is not empty %}// {{var.description}}{% endif %}
395
+
{{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 %}`
396
+
{% endfor %}
397
+
}
398
+
{% endif %}
399
+
{{model.name}}
400
+
{% endfor %}
401
+
```
402
+
403
+
> Find object structures passed to templates later in this document's **Structures** section.
404
+
405
+
Finally, we can compile some code by explicitly defining our classpath and jar entrypoint for CLI (be sure to modify `/your/path` below)
Notice how we've targeted our custom template engine adapter via `-e pebble`. If you don't include the SPI file under `META-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:JvmName` annotation.
420
+
421
+
Congratulations on creating a custom templating engine adapter!
422
+
264
423
## Structures
265
424
266
425
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.
0 commit comments