Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Feature: Experimental Handlebars support (rienafairefr) (#2657)
* new module: openapi-generator-core * templating engine adapters to support extension (currently only Handlebars) * new `-e` templating engine CLI option * adapt Generator to process Template with the passed TemplatingEngineAdpater * add a MustacheEngineAdapter to the codegen in the unit tests * force default MustacheEngineAdapter * copy new core module in the root Dockerfile * add processTemplatingEngine to CodegenConfig, to be overriden by Codegen classes if needed * support multiple file extensions per templating engine adapter * Extends handlebars experimental adapter with explicit contextual resolvers (e.g. map processing) * Add new openapi-generator-core/pom.xml to release_version_update.sh * A detailed message will be logged on missing handlebars helper * Adds README documentation around template default and beta options * Moves mustache package under new templating package * Include built-in handlebars helpers which require explicit registration, and custom `startsWith` helper.
- Loading branch information
1 parent
61ed2ee
commit 8bbeb8b
Showing
35 changed files
with
515 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,16 @@ | |||
<?xml version="1.0" encoding="UTF-8"?> | |||
<project xmlns="http://maven.apache.org/POM/4.0.0" | |||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |||
<parent> | |||
<artifactId>openapi-generator-project</artifactId> | |||
<groupId>org.openapitools</groupId> | |||
<version>4.0.0-SNAPSHOT</version> | |||
<relativePath>../..</relativePath> | |||
</parent> | |||
<modelVersion>4.0.0</modelVersion> | |||
|
|||
<artifactId>openapi-generator-core</artifactId> | |||
<name>openapi-generator-core</name> | |||
<url>https://github.com/openapitools/openapi-generator</url> | |||
</project> |
40 changes: 40 additions & 0 deletions
40
...ator-core/src/main/java/org/openapitools/codegen/api/AbstractTemplatingEngineAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,40 @@ | |||
package org.openapitools.codegen.api; | |||
|
|||
import java.util.Locale; | |||
|
|||
/** | |||
* Provides abstractions around the template engine adapter interface, for reuse by implementers. | |||
*/ | |||
public abstract class AbstractTemplatingEngineAdapter implements TemplatingEngineAdapter { | |||
|
|||
/** | |||
* Gets all possible template paths for a given location. | |||
* | |||
* @param location The full location of the template. | |||
* | |||
* @return A new array of locations, modified according to the extensions or other adapter rules. | |||
*/ | |||
protected String[] getModifiedFileLocation(String location) { | |||
String[] extensions = getFileExtensions(); | |||
String[] result = new String[extensions.length]; | |||
for (int i = 0; i < extensions.length; i++) { | |||
String extension = extensions[i]; | |||
result[i] = String.format(Locale.ROOT, "%s.%s", getPathWithoutExtension(location), extension); | |||
} | |||
return result; | |||
} | |||
|
|||
/** | |||
* Returns the path without an extension for an input location. | |||
* | |||
* @param location The location of the file, with original file extension intact. | |||
* | |||
* @return The full path, without extension (e.g. /path/to/file.txt => /path/to/file) | |||
*/ | |||
private String getPathWithoutExtension(String location) { | |||
if (location == null) return null; | |||
int idx = location.lastIndexOf('.'); | |||
if (idx == -1) return location; | |||
return location.substring(0, idx); | |||
} | |||
} |
30 changes: 30 additions & 0 deletions
30
...pi-generator-core/src/main/java/org/openapitools/codegen/api/TemplatingEngineAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,30 @@ | |||
package org.openapitools.codegen.api; | |||
|
|||
import java.io.IOException; | |||
import java.util.Map; | |||
|
|||
/** | |||
* Each templating engine is called by an Adapter, selected at runtime | |||
*/ | |||
public interface TemplatingEngineAdapter{ | |||
|
|||
/** | |||
* Compiles a template into a string | |||
* | |||
* @param generator From where we can fetch the templates content (e.g. an instance of DefaultGenerator) | |||
* @param bundle The map of values to pass to the template | |||
* @param templateFile The name of the template (e.g. model.mustache ) | |||
* @return the processed template result | |||
* @throws IOException an error ocurred in the template processing | |||
*/ | |||
String compileTemplate(TemplatingGenerator generator, Map<String, Object> bundle, | |||
String templateFile) throws IOException; | |||
|
|||
/** | |||
* During generation, if a supporting file has a file extension that is | |||
* inside that array, then it is considered a templated supporting file | |||
* and we use the templating engine adapter to generate it | |||
* @return string array of the valid file extensions for this templating engine | |||
*/ | |||
String[] getFileExtensions(); | |||
} |
17 changes: 17 additions & 0 deletions
17
...penapi-generator-core/src/main/java/org/openapitools/codegen/api/TemplatingGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,17 @@ | |||
package org.openapitools.codegen.api; | |||
|
|||
/** | |||
* interface to the full template content | |||
* implementers might take into account the -t cli option, | |||
* look in the resources for a language specific template, etc | |||
*/ | |||
public interface TemplatingGenerator { | |||
|
|||
/** | |||
* returns the template content by name | |||
* @param name the template name (e.g. model.mustache) | |||
* @return the contents of that template | |||
*/ | |||
String getFullTemplateContents(String name); | |||
|
|||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.